Skip to content

Commit 6de09d7

Browse files
authored
Implement some unit tests for the katibconfig package (#1690)
* resolve conflict * implement unit tests for GetEarlyStoppingConfigData and GetMetricsCollectorConfigData in katib-config * fix envtest for suggestion-controller * remove debug code * fix invalidCollectorKind value * refactor tests struct * remove unnecessary empty line * add tests for custom resource requirements * fix variable name
1 parent 30e47df commit 6de09d7

File tree

8 files changed

+514
-48
lines changed

8 files changed

+514
-48
lines changed

cmd/metricscollector/v1beta1/file-metricscollector/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func watchMetricsFile(mFile string, stopRules stopRulesFlag, filters []string) {
159159
// Check that metric file exists.
160160
checkMetricFile(mFile)
161161

162-
// Get Main proccess.
162+
// Get Main process.
163163
_, mainProcPid, err := common.GetMainProcesses(mFile)
164164
if err != nil {
165165
klog.Fatalf("GetMainProcesses failed: %v", err)
@@ -271,7 +271,7 @@ func watchMetricsFile(mFile string, stopRules stopRulesFlag, filters []string) {
271271
klog.Fatalf("Write to file %v error: %v", markFile, err)
272272
}
273273

274-
// Get child proccess from main PID.
274+
// Get child process from main PID.
275275
childProc, err := mainProc.Children()
276276
if err != nil {
277277
klog.Fatalf("Get children proceses for main PID: %v failed: %v", mainProcPid, err)
@@ -291,7 +291,7 @@ func watchMetricsFile(mFile string, stopRules stopRulesFlag, filters []string) {
291291
// Report metrics to DB.
292292
reportMetrics(filters)
293293

294-
// Wait until main proccess is completed.
294+
// Wait until main process is completed.
295295
timeout := 60 * time.Second
296296
endTime := time.Now().Add(timeout)
297297
isProcRunning := true

pkg/controller.v1beta1/experiment/manifest/generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestGetRunSpecWithHP(t *testing.T) {
126126
Err: true,
127127
testDescription: "Number of parameter assignments is not equal to number of Trial parameters",
128128
},
129-
// Parameter from assignments not found in Trial paramters
129+
// Parameter from assignments not found in Trial parameters
130130
{
131131
Instance: newFakeInstance(),
132132
ParameterAssignments: func() []commonapiv1beta1.ParameterAssignment {

pkg/controller.v1beta1/suggestion/suggestion_controller_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,12 @@ func TestReconcile(t *testing.T) {
144144
suggestionDeploy := &appsv1.Deployment{}
145145

146146
// Expect that deployment with appropriate name and image is created
147-
g.Eventually(func() bool {
147+
g.Eventually(func() string {
148148
if err = c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: resourceName}, suggestionDeploy); err != nil {
149-
return false
149+
return ""
150150
}
151-
return len(suggestionDeploy.Spec.Template.Spec.Containers) > 0 &&
152-
suggestionDeploy.Spec.Template.Spec.Containers[0].Image == suggestionImage
153-
}, timeout).Should(gomega.BeTrue())
151+
return suggestionDeploy.Spec.Template.Spec.Containers[0].Image
152+
}, timeout).Should(gomega.Equal(suggestionImage))
154153

155154
// Expect that service with appropriate name is created
156155
g.Eventually(func() error {
@@ -300,8 +299,10 @@ func newFakeInstance() *suggestionsv1beta1.Suggestion {
300299

301300
func newKatibConfigMapInstance() *corev1.ConfigMap {
302301
// Create suggestion config
303-
suggestionConfig := map[string]map[string]string{
304-
"random": {"image": suggestionImage},
302+
suggestionConfig := map[string]katibconfig.SuggestionConfig{
303+
"random": {
304+
Image: suggestionImage,
305+
},
305306
}
306307
bSuggestionConfig, _ := json.Marshal(suggestionConfig)
307308

pkg/new-ui/v1beta1/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func generateNNImage(architecture string, decoder string) string {
255255
Embeding is a map: int to Parameter
256256
Parameter has id, type, Option
257257
258-
Beforehand substite all ' to " and wrap the string in `
258+
Beforehand substitute all ' to " and wrap the string in `
259259
*/
260260

261261
replacedDecoder := strings.Replace(decoder, `'`, `"`, -1)

pkg/ui/v1beta1/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func generateNNImage(architecture string, decoder string) string {
242242
Embeding is a map: int to Parameter
243243
Parameter has id, type, Option
244244
245-
Beforehand substite all ' to " and wrap the string in `
245+
Beforehand substitute all ' to " and wrap the string in `
246246
*/
247247

248248
replacedDecoder := strings.Replace(decoder, `'`, `"`, -1)

pkg/util/v1beta1/katibconfig/config.go

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package katibconfig
1919
import (
2020
"context"
2121
"encoding/json"
22-
"errors"
22+
"fmt"
2323
"strings"
2424

2525
corev1 "k8s.io/api/core/v1"
@@ -44,6 +44,12 @@ type SuggestionConfig struct {
4444
PersistentVolumeLabels map[string]string `json:"persistentVolumeLabels,omitempty"`
4545
}
4646

47+
// EarlyStoppingConfig is the JSON early stopping structure in Katib config.
48+
type EarlyStoppingConfig struct {
49+
Image string `json:"image"`
50+
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
51+
}
52+
4753
// MetricsCollectorConfig is the JSON metrics collector structure in Katib config.
4854
type MetricsCollectorConfig struct {
4955
Image string `json:"image"`
@@ -52,12 +58,6 @@ type MetricsCollectorConfig struct {
5258
WaitAllProcesses *bool `json:"waitAllProcesses,omitempty"`
5359
}
5460

55-
// EarlyStoppingConfig is the JSON early stopping structure in Katib config.
56-
type EarlyStoppingConfig struct {
57-
Image string `json:"image"`
58-
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
59-
}
60-
6161
// GetSuggestionConfigData gets the config data for the given suggestion algorithm name.
6262
func GetSuggestionConfigData(algorithmName string, client client.Client) (SuggestionConfig, error) {
6363
configMap := &corev1.ConfigMap{}
@@ -73,7 +73,7 @@ func GetSuggestionConfigData(algorithmName string, client client.Client) (Sugges
7373
// Try to find suggestion data in config map
7474
config, ok := configMap.Data[consts.LabelSuggestionTag]
7575
if !ok {
76-
return SuggestionConfig{}, errors.New("failed to find suggestions config in ConfigMap: " + consts.KatibConfigMapName)
76+
return SuggestionConfig{}, fmt.Errorf("failed to find suggestions config in ConfigMap: %s", consts.KatibConfigMapName)
7777
}
7878

7979
// Parse suggestion data to map where key = algorithm name, value = SuggestionConfig
@@ -85,20 +85,17 @@ func GetSuggestionConfigData(algorithmName string, client client.Client) (Sugges
8585
// Try to find SuggestionConfig for the algorithm
8686
suggestionConfigData, ok = suggestionsConfig[algorithmName]
8787
if !ok {
88-
return SuggestionConfig{}, errors.New("failed to find suggestion config for algorithm: " + algorithmName + " in ConfigMap: " + consts.KatibConfigMapName)
88+
return SuggestionConfig{}, fmt.Errorf("failed to find suggestion config for algorithm: %s in ConfigMap: %s", algorithmName, consts.KatibConfigMapName)
8989
}
9090

9191
// Get image from config
9292
image := suggestionConfigData.Image
9393
if strings.TrimSpace(image) == "" {
94-
return SuggestionConfig{}, errors.New("required value for image configuration of algorithm name: " + algorithmName)
94+
return SuggestionConfig{}, fmt.Errorf("required value for image configuration of algorithm name: %s", algorithmName)
9595
}
9696

97-
// Get Image Pull Policy
98-
imagePullPolicy := suggestionConfigData.ImagePullPolicy
99-
if imagePullPolicy != corev1.PullAlways && imagePullPolicy != corev1.PullIfNotPresent && imagePullPolicy != corev1.PullNever {
100-
suggestionConfigData.ImagePullPolicy = consts.DefaultImagePullPolicy
101-
}
97+
// Set Image Pull Policy
98+
suggestionConfigData.ImagePullPolicy = setImagePullPolicy(suggestionConfigData.ImagePullPolicy)
10299

103100
// Set resource requirements for suggestion
104101
suggestionConfigData.Resource = setResourceRequirements(suggestionConfigData.Resource)
@@ -119,9 +116,8 @@ func GetSuggestionConfigData(algorithmName string, client client.Client) (Sugges
119116
}
120117

121118
// Set default resources
122-
defaultVolumeStorage, _ := resource.ParseQuantity(consts.DefaultSuggestionVolumeStorage)
123119
if len(pvcSpec.Resources.Requests) == 0 {
124-
120+
defaultVolumeStorage, _ := resource.ParseQuantity(consts.DefaultSuggestionVolumeStorage)
125121
pvcSpec.Resources.Requests = make(map[corev1.ResourceName]resource.Quantity)
126122
pvcSpec.Resources.Requests[corev1.ResourceStorage] = defaultVolumeStorage
127123
}
@@ -157,7 +153,7 @@ func GetEarlyStoppingConfigData(algorithmName string, client client.Client) (Ear
157153
// Try to find early stopping data in config map.
158154
config, ok := configMap.Data[consts.LabelEarlyStoppingTag]
159155
if !ok {
160-
return EarlyStoppingConfig{}, errors.New("failed to find early stopping config in ConfigMap: " + consts.KatibConfigMapName)
156+
return EarlyStoppingConfig{}, fmt.Errorf("failed to find early stopping config in ConfigMap: %s", consts.KatibConfigMapName)
161157
}
162158

163159
// Parse early stopping data to map where key = algorithm name, value = EarlyStoppingConfig.
@@ -169,20 +165,17 @@ func GetEarlyStoppingConfigData(algorithmName string, client client.Client) (Ear
169165
// Try to find EarlyStoppingConfig for the algorithm.
170166
earlyStoppingConfigData, ok = earlyStoppingsConfig[algorithmName]
171167
if !ok {
172-
return EarlyStoppingConfig{}, errors.New("failed to find early stopping config for algorithm: " + algorithmName + " in ConfigMap: " + consts.KatibConfigMapName)
168+
return EarlyStoppingConfig{}, fmt.Errorf("failed to find early stopping config for algorithm: %s in ConfigMap: %s", algorithmName, consts.KatibConfigMapName)
173169
}
174170

175171
// Get image from config.
176172
image := earlyStoppingConfigData.Image
177173
if strings.TrimSpace(image) == "" {
178-
return EarlyStoppingConfig{}, errors.New("required value for image configuration of algorithm name: " + algorithmName)
174+
return EarlyStoppingConfig{}, fmt.Errorf("required value for image configuration of algorithm name: %s", algorithmName)
179175
}
180176

181-
// Get Image Pull Policy.
182-
imagePullPolicy := earlyStoppingConfigData.ImagePullPolicy
183-
if imagePullPolicy != corev1.PullAlways && imagePullPolicy != corev1.PullIfNotPresent && imagePullPolicy != corev1.PullNever {
184-
earlyStoppingConfigData.ImagePullPolicy = consts.DefaultImagePullPolicy
185-
}
177+
// Set Image Pull Policy.
178+
earlyStoppingConfigData.ImagePullPolicy = setImagePullPolicy(earlyStoppingConfigData.ImagePullPolicy)
186179

187180
return earlyStoppingConfigData, nil
188181
}
@@ -202,7 +195,7 @@ func GetMetricsCollectorConfigData(cKind common.CollectorKind, client client.Cli
202195
// Try to find metrics collector data in config map
203196
config, ok := configMap.Data[consts.LabelMetricsCollectorSidecar]
204197
if !ok {
205-
return MetricsCollectorConfig{}, errors.New("failed to find metrics collector config in ConfigMap: " + consts.KatibConfigMapName)
198+
return MetricsCollectorConfig{}, fmt.Errorf("failed to find metrics collector config in ConfigMap: %s", consts.KatibConfigMapName)
206199
}
207200
// Parse metrics collector data to map where key = collector kind, value = MetricsCollectorConfig
208201
kind := string(cKind)
@@ -214,27 +207,31 @@ func GetMetricsCollectorConfigData(cKind common.CollectorKind, client client.Cli
214207
// Try to find MetricsCollectorConfig for the collector kind
215208
metricsCollectorConfigData, ok = mcsConfig[kind]
216209
if !ok {
217-
return MetricsCollectorConfig{}, errors.New("failed to find metrics collector config for kind: " + kind + " in ConfigMap: " + consts.KatibConfigMapName)
210+
return MetricsCollectorConfig{}, fmt.Errorf("failed to find metrics collector config for kind: %s in ConfigMap: %s", kind, consts.KatibConfigMapName)
218211
}
219212

220213
// Get image from config
221214
image := metricsCollectorConfigData.Image
222215
if strings.TrimSpace(image) == "" {
223-
return MetricsCollectorConfig{}, errors.New("required value for image configuration of metrics collector kind: " + kind)
216+
return MetricsCollectorConfig{}, fmt.Errorf("required value for image configuration of metrics collector kind: %s", kind)
224217
}
225218

226-
// Get Image Pull Policy
227-
imagePullPolicy := metricsCollectorConfigData.ImagePullPolicy
228-
if imagePullPolicy != corev1.PullAlways && imagePullPolicy != corev1.PullIfNotPresent && imagePullPolicy != corev1.PullNever {
229-
metricsCollectorConfigData.ImagePullPolicy = consts.DefaultImagePullPolicy
230-
}
219+
// Set Image Pull Policy
220+
metricsCollectorConfigData.ImagePullPolicy = setImagePullPolicy(metricsCollectorConfigData.ImagePullPolicy)
231221

232222
// Set resource requirements for metrics collector
233223
metricsCollectorConfigData.Resource = setResourceRequirements(metricsCollectorConfigData.Resource)
234224

235225
return metricsCollectorConfigData, nil
236226
}
237227

228+
func setImagePullPolicy(imagePullPolicy corev1.PullPolicy) corev1.PullPolicy {
229+
if imagePullPolicy != corev1.PullAlways && imagePullPolicy != corev1.PullIfNotPresent && imagePullPolicy != corev1.PullNever {
230+
return consts.DefaultImagePullPolicy
231+
}
232+
return imagePullPolicy
233+
}
234+
238235
func setResourceRequirements(configResource corev1.ResourceRequirements) corev1.ResourceRequirements {
239236

240237
// If requests are empty create new map
@@ -298,7 +295,7 @@ func setResourceRequirements(configResource corev1.ResourceRequirements) corev1.
298295
}
299296

300297
// If user explicitly sets ephemeral-storage value to something negative, nuke it.
301-
// This enables compability with the GKE nodepool autoscalers, which cannot scale
298+
// This enables compatibility with the GKE nodepool autoscalers, which cannot scale
302299
// pods which define ephemeral-storage resource constraints.
303300
if diskLimit.Sign() == -1 && diskRequest.Sign() == -1 {
304301
delete(configResource.Limits, corev1.ResourceEphemeralStorage)

0 commit comments

Comments
 (0)