Skip to content

Commit 005dc31

Browse files
authored
Validate generated schemas in generator script (GoogleContainerTools#6385)
* fix build --push=false for missing kubeconfig * Fixed log ordering per PR comment * Added validation to schema generation * Added validation to schema generation * Updated validate function * Refactor validate method * Updated error logic * Fix linter error
1 parent 1734f56 commit 005dc31

File tree

7 files changed

+233
-7
lines changed

7 files changed

+233
-7
lines changed

docs/content/en/schemas/v2beta19.json

+23
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,29 @@
914914
"description": "describes a dependency on another skaffold configuration.",
915915
"x-intellij-html-description": "describes a dependency on another skaffold configuration."
916916
},
917+
"ContainerHook": {
918+
"required": [
919+
"command"
920+
],
921+
"properties": {
922+
"command": {
923+
"items": {
924+
"type": "string"
925+
},
926+
"type": "array",
927+
"description": "command to execute.",
928+
"x-intellij-html-description": "command to execute.",
929+
"default": "[]"
930+
}
931+
},
932+
"preferredOrder": [
933+
"command"
934+
],
935+
"additionalProperties": false,
936+
"type": "object",
937+
"description": "describes a lifecycle hook definition to execute on a container. The container name is inferred from the scope in which this hook is defined.",
938+
"x-intellij-html-description": "describes a lifecycle hook definition to execute on a container. The container name is inferred from the scope in which this hook is defined."
939+
},
917940
"CustomArtifact": {
918941
"properties": {
919942
"buildCommand": {

hack/schemas/main.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"sync"
3333

3434
blackfriday "github.com/russross/blackfriday/v2"
35+
"github.com/xeipuuv/gojsonschema"
3536

3637
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema"
3738
)
@@ -474,7 +475,23 @@ func (g *schemaGenerator) Apply(inputPath string) ([]byte, error) {
474475
Definitions: definitions,
475476
}
476477

477-
return toJSON(schema)
478+
buf, err := toJSON(schema)
479+
if err != nil {
480+
return nil, err
481+
}
482+
483+
if err := validate(buf); err != nil {
484+
return nil, fmt.Errorf("invalid schema generated: %v", err.Error())
485+
}
486+
487+
return buf, nil
488+
}
489+
490+
// Validate generated schema
491+
func validate(data []byte) error {
492+
schemaLoader := gojsonschema.NewBytesLoader(data)
493+
_, err := gojsonschema.NewSchema(schemaLoader)
494+
return err
478495
}
479496

480497
// Make sure HTML description are not encoded

hack/schemas/main_test.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"testing"
2424

2525
"github.com/google/go-cmp/cmp"
26-
"github.com/xeipuuv/gojsonschema"
2726

2827
"github.com/GoogleContainerTools/skaffold/testutil"
2928
)
@@ -46,6 +45,7 @@ func TestGenerators(t *testing.T) {
4645
{name: "inline"},
4746
{name: "inline-anyof"},
4847
{name: "inline-hybrid"},
48+
{name: "inline-skiptrim"},
4949
{name: "integer"},
5050
}
5151
for _, test := range tests {
@@ -65,14 +65,35 @@ func TestGenerators(t *testing.T) {
6565

6666
expected = bytes.ReplaceAll(expected, []byte("\r\n"), []byte("\n"))
6767

68-
schemaLoader := gojsonschema.NewBytesLoader(actual)
69-
_, err = gojsonschema.NewSchema(schemaLoader)
70-
t.CheckNoError(err)
71-
7268
if diff := cmp.Diff(string(actual), string(expected)); diff != "" {
7369
t.Errorf("%T differ (-got, +want): %s\n actual:\n%s", string(expected), diff, string(actual))
7470
return
7571
}
7672
})
7773
}
7874
}
75+
76+
func TestGeneratorErrors(t *testing.T) {
77+
tests := []struct {
78+
name string
79+
shouldErr bool
80+
expectedError string
81+
}{
82+
{name: "invalid-schema", shouldErr: true, expectedError: "Object has no key 'InlineStruct'"},
83+
}
84+
for _, test := range tests {
85+
testutil.Run(t, test.name, func(t *testutil.T) {
86+
input := filepath.Join("testdata", test.name, "input.go")
87+
88+
generator := schemaGenerator{
89+
strict: false,
90+
}
91+
92+
_, err := generator.Apply(input)
93+
t.CheckError(test.shouldErr, err)
94+
if test.expectedError != "" {
95+
t.CheckErrorContains(test.expectedError, err)
96+
}
97+
})
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2021 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package latest
18+
19+
// TestStruct for testing the schema generator.
20+
type TestStruct struct {
21+
22+
// RequiredField should be required
23+
RequiredField string `yaml:"reqField" yamltags:"required"`
24+
25+
// AnotherField has reference to InlineStruct
26+
AnotherField *InlineStruct `yaml:"anotherField"`
27+
}
28+
29+
// AnotherTestStruct for testing the schema s generator.
30+
type AnotherTestStruct struct {
31+
InlineStruct `yaml:"inline" yamltags:"skipTrim"`
32+
}
33+
34+
// InlineStruct is embedded inline into TestStruct
35+
type InlineStruct struct {
36+
37+
// Field1 should be the first choice
38+
Field1 string `yaml:"f1"`
39+
40+
// Field2 should be the second choice
41+
Field2 string `yaml:"f2"`
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"type": "object",
3+
"anyOf": [
4+
{
5+
"$ref": "#/definitions/TestStruct"
6+
}
7+
],
8+
"$schema": "http://json-schema-org/draft-07/schema#",
9+
"definitions": {
10+
"AnotherTestStruct": {
11+
"properties": {
12+
"f1": {
13+
"type": "string",
14+
"description": "should be the first choice",
15+
"x-intellij-html-description": "should be the first choice"
16+
},
17+
"f2": {
18+
"type": "string",
19+
"description": "should be the second choice",
20+
"x-intellij-html-description": "should be the second choice"
21+
}
22+
},
23+
"preferredOrder": [
24+
"f1",
25+
"f2"
26+
],
27+
"type": "object",
28+
"description": "for testing the schema s generator.",
29+
"x-intellij-html-description": "for testing the schema s generator."
30+
},
31+
"InlineStruct": {
32+
"properties": {
33+
"f1": {
34+
"type": "string",
35+
"description": "should be the first choice",
36+
"x-intellij-html-description": "should be the first choice"
37+
},
38+
"f2": {
39+
"type": "string",
40+
"description": "should be the second choice",
41+
"x-intellij-html-description": "should be the second choice"
42+
}
43+
},
44+
"preferredOrder": [
45+
"f1",
46+
"f2"
47+
],
48+
"additionalProperties": false,
49+
"type": "object",
50+
"description": "embedded inline into TestStruct",
51+
"x-intellij-html-description": "embedded inline into TestStruct"
52+
},
53+
"TestStruct": {
54+
"required": [
55+
"reqField"
56+
],
57+
"properties": {
58+
"anotherField": {
59+
"$ref": "#/definitions/InlineStruct",
60+
"description": "has reference to InlineStruct",
61+
"x-intellij-html-description": "has reference to InlineStruct"
62+
},
63+
"reqField": {
64+
"type": "string",
65+
"description": "should be required",
66+
"x-intellij-html-description": "should be required"
67+
}
68+
},
69+
"preferredOrder": [
70+
"reqField",
71+
"anotherField"
72+
],
73+
"additionalProperties": false,
74+
"type": "object",
75+
"description": "for testing the schema generator.",
76+
"x-intellij-html-description": "for testing the schema generator."
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2021 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package latest
18+
19+
// TestStruct for testing the schema generator.
20+
type TestStruct struct {
21+
22+
// RequiredField should be required
23+
RequiredField string `yaml:"reqField" yamltags:"required"`
24+
25+
// AnotherField has reference to InlineStruct
26+
AnotherField *InlineStruct `yaml:"anotherField"`
27+
}
28+
29+
// AnotherTestStruct for testing the schema s generator.
30+
type AnotherTestStruct struct {
31+
// Not adding yamltags:"skipTrim" causes InlineStruct to be removed from definitions
32+
// This breaks the TestStruct reference above
33+
InlineStruct `yaml:"inline"`
34+
}
35+
36+
// InlineStruct is embedded inline into TestStruct
37+
type InlineStruct struct {
38+
39+
// Field1 should be the first choice
40+
Field1 string `yaml:"f1"`
41+
42+
// Field2 should be the second choice
43+
Field2 string `yaml:"f2"`
44+
}

pkg/skaffold/schema/v2beta19/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ type ContainerHook struct {
13811381
// NamedContainerHook describes a lifecycle hook definition to execute on a named container.
13821382
type NamedContainerHook struct {
13831383
// ContainerHook describes a lifecycle hook definition to execute on a container.
1384-
ContainerHook `yaml:",inline"`
1384+
ContainerHook `yaml:",inline" yamltags:"skipTrim"`
13851385
// PodName is the name of the pod to execute the command in.
13861386
PodName string `yaml:"podName" yamltags:"required"`
13871387
// ContainerName is the name of the container to execute the command in.

0 commit comments

Comments
 (0)