-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
441581b
commit 0d90b76
Showing
4 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2019 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package setters2 | ||
|
||
import ( | ||
"strings" | ||
|
||
"sigs.k8s.io/kustomize/kyaml/errors" | ||
"sigs.k8s.io/kustomize/kyaml/fieldmeta" | ||
"sigs.k8s.io/kustomize/kyaml/openapi" | ||
) | ||
|
||
// CheckRequiredSettersSet iterates through all the setter definitions in openAPI | ||
// schema and returns error if any of the setter has required field true and isSet false | ||
func CheckRequiredSettersSet() error { | ||
for key := range openapi.Schema().Definitions { | ||
if strings.Contains(key, fieldmeta.SetterDefinitionPrefix) { | ||
val := openapi.Schema().Definitions[key] | ||
defExt, err := GetExtFromSchema(&val) // parse the extension out of the openAPI | ||
if err != nil { | ||
return errors.Wrap(err) | ||
} | ||
if defExt.Setter.Required && !defExt.Setter.IsSet { | ||
return errors.Errorf("setter %s is required but not set, "+ | ||
"please set it to new value and try again", strings.TrimPrefix(key, fieldmeta.SetterDefinitionPrefix)) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright 2019 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package setters2 | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"sigs.k8s.io/kustomize/kyaml/openapi" | ||
) | ||
|
||
func TestCheckRequiredSettersSet(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
inputOpenAPIfile string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "no required, no isSet", | ||
inputOpenAPIfile: ` | ||
apiVersion: v1alpha1 | ||
kind: OpenAPIfile | ||
openAPI: | ||
definitions: | ||
io.k8s.cli.setters.gcloud.project.projectNumber: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: gcloud.project.projectNumber | ||
value: "123" | ||
setBy: me | ||
io.k8s.cli.setters.replicas: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: replicas | ||
value: "3" | ||
setBy: me | ||
`, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "required true, no isSet", | ||
inputOpenAPIfile: ` | ||
apiVersion: v1alpha1 | ||
kind: Example | ||
openAPI: | ||
definitions: | ||
io.k8s.cli.setters.replicas: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: replicas | ||
value: "3" | ||
setBy: me | ||
required: true | ||
`, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "required true, isSet true", | ||
inputOpenAPIfile: ` | ||
apiVersion: v1alpha1 | ||
kind: Example | ||
openAPI: | ||
definitions: | ||
io.k8s.cli.setters.replicas: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: replicas | ||
value: "3" | ||
setBy: me | ||
required: true | ||
isSet: true | ||
`, | ||
expectedError: false, | ||
}, | ||
|
||
{ | ||
name: "required false, isSet true", | ||
inputOpenAPIfile: ` | ||
apiVersion: v1alpha1 | ||
kind: OpenAPIfile | ||
openAPI: | ||
definitions: | ||
io.k8s.cli.setters.gcloud.project.projectNumber: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: gcloud.project.projectNumber | ||
value: "123" | ||
setBy: me | ||
io.k8s.cli.setters.replicas: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: replicas | ||
value: "3" | ||
setBy: me | ||
required: false | ||
isSet: true | ||
`, | ||
expectedError: false, | ||
}, | ||
|
||
{ | ||
name: "required true, isSet false", | ||
inputOpenAPIfile: ` | ||
apiVersion: v1alpha1 | ||
kind: OpenAPIfile | ||
openAPI: | ||
definitions: | ||
io.k8s.cli.setters.gcloud.project.projectNumber: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: gcloud.project.projectNumber | ||
value: "123" | ||
setBy: me | ||
io.k8s.cli.setters.replicas: | ||
description: hello world | ||
x-k8s-cli: | ||
setter: | ||
name: replicas | ||
value: "3" | ||
setBy: me | ||
required: true | ||
isSet: false | ||
`, | ||
expectedError: true, | ||
}, | ||
|
||
{ | ||
name: "no openAPI", | ||
inputOpenAPIfile: ``, | ||
expectedError: false, | ||
}, | ||
} | ||
for i := range tests { | ||
test := tests[i] | ||
t.Run(test.name, func(t *testing.T) { | ||
openapi.ResetOpenAPI() | ||
defer openapi.ResetOpenAPI() | ||
dir, err := ioutil.TempDir("", "") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
err = ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(test.inputOpenAPIfile), 0600) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
err = openapi.AddSchemaFromFile(filepath.Join(dir, "Krmfile")) | ||
if err != nil { | ||
// do nothing if openAPI file or schema doesn't exist, CheckRequiredSettersSet() | ||
// should not throw any error | ||
fmt.Println("Unable to load schema from file, continuing...") | ||
} | ||
err = CheckRequiredSettersSet() | ||
if test.expectedError && !assert.Error(t, err) { | ||
t.FailNow() | ||
} | ||
if !test.expectedError && !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.