fix tests

This commit is contained in:
yugo kobayashi
2022-09-03 00:17:11 +00:00
parent a54226e0a9
commit e49bf52928

View File

@@ -58,7 +58,7 @@ func TestFunctionFilter_Filter(t *testing.T) {
// verify that resources emitted from the function have a file path defaulted // verify that resources emitted from the function have a file path defaulted
// if none already exists // if none already exists
{ {
name: "default_file_path_annotation", name: "default file path annotation",
run: testRun{ run: testRun{
output: ` output: `
apiVersion: config.kubernetes.io/v1 apiVersion: config.kubernetes.io/v1
@@ -99,7 +99,7 @@ metadata:
// verify that resources emitted from the function do not have a file path defaulted // verify that resources emitted from the function do not have a file path defaulted
// if one already exists // if one already exists
{ {
name: "no_default_file_path_annotation", name: "no default file path annotation",
run: testRun{ run: testRun{
output: ` output: `
apiVersion: config.kubernetes.io/v1 apiVersion: config.kubernetes.io/v1
@@ -142,7 +142,7 @@ metadata:
// verify the FunctionFilter correctly writes the inputs and reads the outputs // verify the FunctionFilter correctly writes the inputs and reads the outputs
// of Run // of Run
{ {
name: "write_read", name: "write read",
run: testRun{ run: testRun{
output: ` output: `
apiVersion: config.kubernetes.io/v1 apiVersion: config.kubernetes.io/v1
@@ -200,7 +200,7 @@ metadata:
// verify that the results file is written // verify that the results file is written
// //
{ {
name: "write_results_file", name: "write results file",
run: testRun{ run: testRun{
output: `apiVersion: config.kubernetes.io/v1 output: `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
@@ -277,7 +277,7 @@ metadata:
// verify that the results file is written for functions that exist non-0 // verify that the results file is written for functions that exist non-0
// and the FunctionFilter returns the error // and the FunctionFilter returns the error
{ {
name: "write_results_file_function_exit_non_0", name: "write results file function exit non 0",
expectedError: "failed", expectedError: "failed",
run: testRun{ run: testRun{
err: fmt.Errorf("failed"), err: fmt.Errorf("failed"),
@@ -356,7 +356,7 @@ metadata:
// verify that if deferFailure is set, the results file is written and the // verify that if deferFailure is set, the results file is written and the
// exit error is saved, but the FunctionFilter does not return an error. // exit error is saved, but the FunctionFilter does not return an error.
{ {
name: "write_results_defer_failure", name: "write results defer failure",
instance: FunctionFilter{DeferFailure: true}, instance: FunctionFilter{DeferFailure: true},
expectedSavedError: "failed", expectedSavedError: "failed",
run: testRun{ run: testRun{
@@ -434,7 +434,7 @@ metadata:
}, },
{ {
name: "write_results_bad_results_file", name: "write results bad results file",
expectedError: "open /not/real/file:", expectedError: "open /not/real/file:",
noMakeResultsFile: true, noMakeResultsFile: true,
run: testRun{ run: testRun{
@@ -484,7 +484,7 @@ metadata:
// resources not provided to the function should still appear in the FunctionFilter // resources not provided to the function should still appear in the FunctionFilter
// output // output
{ {
name: "scope_resources_by_directory", name: "scope resources by directory",
run: testRun{ run: testRun{
expectedInput: `apiVersion: config.kubernetes.io/v1 expectedInput: `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
@@ -575,7 +575,7 @@ metadata:
// verify functions without file path annotation are not scoped to functions // verify functions without file path annotation are not scoped to functions
{ {
name: "scope_resources_by_directory_resources_missing_path", name: "scope resources by directory resources missing path",
run: testRun{ run: testRun{
expectedInput: `apiVersion: config.kubernetes.io/v1 expectedInput: `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
@@ -662,7 +662,7 @@ metadata:
// verify the functions can see all resources if global scope is set // verify the functions can see all resources if global scope is set
{ {
name: "scope_resources_global", name: "scope resources global",
instance: FunctionFilter{GlobalScope: true}, instance: FunctionFilter{GlobalScope: true},
run: testRun{ run: testRun{
expectedInput: `apiVersion: config.kubernetes.io/v1 expectedInput: `apiVersion: config.kubernetes.io/v1
@@ -767,7 +767,7 @@ metadata:
}, },
{ {
name: "scope_no_resources", name: "scope no resources",
run: testRun{ run: testRun{
expectedInput: `apiVersion: config.kubernetes.io/v1 expectedInput: `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
@@ -837,7 +837,7 @@ metadata:
}, },
{ {
name: "scope_functions_dir", name: "scope functions dir",
run: testRun{ run: testRun{
expectedInput: `apiVersion: config.kubernetes.io/v1 expectedInput: `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
@@ -928,7 +928,7 @@ metadata:
}, },
{ {
name: "copy_comments", name: "copy comments",
run: testRun{ run: testRun{
expectedInput: `apiVersion: config.kubernetes.io/v1 expectedInput: `apiVersion: config.kubernetes.io/v1
kind: ResourceList kind: ResourceList
@@ -1242,7 +1242,7 @@ container:
}, },
{ {
name: "path_with_uncorrect_position", name: "path with uncorrect position",
resource: ` resource: `
apiVersion: v1beta1 apiVersion: v1beta1
kind: Example kind: Example
@@ -1257,7 +1257,7 @@ metadata:
}, },
{ {
name: "network_with_uncorrect_position", name: "network with uncorrect position",
resource: ` resource: `
apiVersion: v1beta1 apiVersion: v1beta1
kind: Example kind: Example
@@ -1271,7 +1271,7 @@ metadata:
missingFn: true, missingFn: true,
}, },
{ {
name: "fn missing spell", name: "typo in function config",
resource: ` resource: `
apiVersion: v1beta1 apiVersion: v1beta1
kind: Example kind: Example
@@ -1320,12 +1320,13 @@ metadata:
tt := tests[i] tt := tests[i]
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
resource := yaml.MustParse(tt.resource) resource := yaml.MustParse(tt.resource)
fn, _ := GetFunctionSpec(resource) fn, err := GetFunctionSpec(resource)
if tt.missingFn { if tt.missingFn {
if !assert.Nil(t, fn) { if err == nil && !assert.Nil(t, fn) {
t.FailNow() t.FailNow()
} }
} else { return
}
b, err := yaml.Marshal(fn) b, err := yaml.Marshal(fn)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.FailNow() t.FailNow()
@@ -1335,7 +1336,6 @@ metadata:
strings.TrimSpace(string(b))) { strings.TrimSpace(string(b))) {
t.FailNow() t.FailNow()
} }
}
}) })
} }
} }
@@ -1399,9 +1399,12 @@ metadata:
for _, tc := range tests { for _, tc := range tests {
cfg, err := yaml.Parse(tc.input) cfg, err := yaml.Parse(tc.input)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return t.FailNow()
}
fn, err := GetFunctionSpec(cfg)
if !assert.NoError(t, err) {
t.FailNow()
} }
fn, _ := GetFunctionSpec(cfg)
assert.Equal(t, tc.required, fn.Container.Network) assert.Equal(t, tc.required, fn.Container.Network)
} }
} }
@@ -1537,9 +1540,12 @@ metadata:
for _, tc := range tests { for _, tc := range tests {
cfg, err := yaml.Parse(tc.input) cfg, err := yaml.Parse(tc.input)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return t.FailNow()
}
fn, err := GetFunctionSpec(cfg)
if !assert.NoError(t, err) {
t.FailNow()
} }
fn, _ := GetFunctionSpec(cfg)
assert.Equal(t, tc.expected, *NewContainerEnvFromStringSlice(fn.Container.Env)) assert.Equal(t, tc.expected, *NewContainerEnvFromStringSlice(fn.Container.Env))
} }
} }