Add check for unused API compatibility fixture files
This commit is contained in:
parent
05bfa26791
commit
bd405cd8b0
@ -26,6 +26,9 @@ kube::golang::setup_env
|
|||||||
# run in module mode to match test command in readme.md
|
# run in module mode to match test command in readme.md
|
||||||
export GO111MODULE=on
|
export GO111MODULE=on
|
||||||
|
|
||||||
|
# Nuke old files so we don't accidentally carry stuff forward.
|
||||||
|
rm -f staging/src/k8s.io/api/testdata/HEAD/*.{yaml,json,pb}
|
||||||
|
|
||||||
# UPDATE_COMPATIBILITY_FIXTURE_DATA=true regenerates fixture data if needed.
|
# UPDATE_COMPATIBILITY_FIXTURE_DATA=true regenerates fixture data if needed.
|
||||||
# -run //HEAD only runs the test cases comparing against testdata for HEAD.
|
# -run //HEAD only runs the test cases comparing against testdata for HEAD.
|
||||||
# We suppress the output because we are expecting to have changes.
|
# We suppress the output because we are expecting to have changes.
|
||||||
|
@ -268,28 +268,46 @@ func CompatibilityTestFuzzer(scheme *runtime.Scheme, fuzzFuncs []interface{}) *f
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CompatibilityTestOptions) Run(t *testing.T) {
|
func (c *CompatibilityTestOptions) Run(t *testing.T) {
|
||||||
|
usedHEADFixtures := sets.NewString()
|
||||||
|
|
||||||
for _, gvk := range c.Kinds {
|
for _, gvk := range c.Kinds {
|
||||||
t.Run(makeName(gvk), func(t *testing.T) {
|
t.Run(makeName(gvk), func(t *testing.T) {
|
||||||
|
|
||||||
t.Run("HEAD", func(t *testing.T) {
|
t.Run("HEAD", func(t *testing.T) {
|
||||||
c.runCurrentVersionTest(t, gvk)
|
c.runCurrentVersionTest(t, gvk, usedHEADFixtures)
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, previousVersionDir := range c.TestDataDirsPreviousVersions {
|
for _, previousVersionDir := range c.TestDataDirsPreviousVersions {
|
||||||
t.Run(filepath.Base(previousVersionDir), func(t *testing.T) {
|
t.Run(filepath.Base(previousVersionDir), func(t *testing.T) {
|
||||||
c.runPreviousVersionTest(t, gvk, previousVersionDir)
|
c.runPreviousVersionTest(t, gvk, previousVersionDir, nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for unused HEAD fixtures
|
||||||
|
t.Run("unused_fixtures", func(t *testing.T) {
|
||||||
|
files, err := os.ReadDir(c.TestDataDirCurrentVersion)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
allFixtures := sets.NewString()
|
||||||
|
for _, file := range files {
|
||||||
|
allFixtures.Insert(file.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
if unused := allFixtures.Difference(usedHEADFixtures); len(unused) > 0 {
|
||||||
|
t.Fatalf("remove unused fixtures from %s:\n%s", c.TestDataDirCurrentVersion, strings.Join(unused.List(), "\n"))
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CompatibilityTestOptions) runCurrentVersionTest(t *testing.T, gvk schema.GroupVersionKind) {
|
func (c *CompatibilityTestOptions) runCurrentVersionTest(t *testing.T, gvk schema.GroupVersionKind, usedFiles sets.String) {
|
||||||
expectedObject := c.FuzzedObjects[gvk]
|
expectedObject := c.FuzzedObjects[gvk]
|
||||||
expectedJSON, expectedYAML, expectedProto := c.encode(t, expectedObject)
|
expectedJSON, expectedYAML, expectedProto := c.encode(t, expectedObject)
|
||||||
|
|
||||||
actualJSON, actualYAML, actualProto, err := read(c.TestDataDirCurrentVersion, gvk, "")
|
actualJSON, actualYAML, actualProto, err := read(c.TestDataDirCurrentVersion, gvk, "", usedFiles)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -387,10 +405,18 @@ func (c *CompatibilityTestOptions) encode(t *testing.T, obj runtime.Object) (jso
|
|||||||
return jsonBytes.Bytes(), yamlBytes.Bytes(), protoBytes.Bytes()
|
return jsonBytes.Bytes(), yamlBytes.Bytes(), protoBytes.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func read(dir string, gvk schema.GroupVersionKind, suffix string) (json, yaml, proto []byte, err error) {
|
func read(dir string, gvk schema.GroupVersionKind, suffix string, usedFiles sets.String) (json, yaml, proto []byte, err error) {
|
||||||
actualJSON, jsonErr := ioutil.ReadFile(filepath.Join(dir, makeName(gvk)+suffix+".json"))
|
jsonFilename := makeName(gvk) + suffix + ".json"
|
||||||
actualYAML, yamlErr := ioutil.ReadFile(filepath.Join(dir, makeName(gvk)+suffix+".yaml"))
|
actualJSON, jsonErr := ioutil.ReadFile(filepath.Join(dir, jsonFilename))
|
||||||
actualProto, protoErr := ioutil.ReadFile(filepath.Join(dir, makeName(gvk)+suffix+".pb"))
|
yamlFilename := makeName(gvk) + suffix + ".yaml"
|
||||||
|
actualYAML, yamlErr := ioutil.ReadFile(filepath.Join(dir, yamlFilename))
|
||||||
|
protoFilename := makeName(gvk) + suffix + ".pb"
|
||||||
|
actualProto, protoErr := ioutil.ReadFile(filepath.Join(dir, protoFilename))
|
||||||
|
if usedFiles != nil {
|
||||||
|
usedFiles.Insert(jsonFilename)
|
||||||
|
usedFiles.Insert(yamlFilename)
|
||||||
|
usedFiles.Insert(protoFilename)
|
||||||
|
}
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
return actualJSON, actualYAML, actualProto, jsonErr
|
return actualJSON, actualYAML, actualProto, jsonErr
|
||||||
}
|
}
|
||||||
@ -412,8 +438,8 @@ func writeFile(t *testing.T, dir string, gvk schema.GroupVersionKind, suffix, ex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CompatibilityTestOptions) runPreviousVersionTest(t *testing.T, gvk schema.GroupVersionKind, previousVersionDir string) {
|
func (c *CompatibilityTestOptions) runPreviousVersionTest(t *testing.T, gvk schema.GroupVersionKind, previousVersionDir string, usedFiles sets.String) {
|
||||||
jsonBeforeRoundTrip, yamlBeforeRoundTrip, protoBeforeRoundTrip, err := read(previousVersionDir, gvk, "")
|
jsonBeforeRoundTrip, yamlBeforeRoundTrip, protoBeforeRoundTrip, err := read(previousVersionDir, gvk, "", usedFiles)
|
||||||
if os.IsNotExist(err) || (len(jsonBeforeRoundTrip) == 0 && len(yamlBeforeRoundTrip) == 0 && len(protoBeforeRoundTrip) == 0) {
|
if os.IsNotExist(err) || (len(jsonBeforeRoundTrip) == 0 && len(yamlBeforeRoundTrip) == 0 && len(protoBeforeRoundTrip) == 0) {
|
||||||
t.SkipNow()
|
t.SkipNow()
|
||||||
return
|
return
|
||||||
@ -470,7 +496,7 @@ func (c *CompatibilityTestOptions) runPreviousVersionTest(t *testing.T, gvk sche
|
|||||||
}
|
}
|
||||||
protoAfterRoundTrip := protoBytes.Bytes()
|
protoAfterRoundTrip := protoBytes.Bytes()
|
||||||
|
|
||||||
expectedJSONAfterRoundTrip, expectedYAMLAfterRoundTrip, expectedProtoAfterRoundTrip, _ := read(previousVersionDir, gvk, ".after_roundtrip")
|
expectedJSONAfterRoundTrip, expectedYAMLAfterRoundTrip, expectedProtoAfterRoundTrip, _ := read(previousVersionDir, gvk, ".after_roundtrip", usedFiles)
|
||||||
if len(expectedJSONAfterRoundTrip) == 0 {
|
if len(expectedJSONAfterRoundTrip) == 0 {
|
||||||
expectedJSONAfterRoundTrip = jsonBeforeRoundTrip
|
expectedJSONAfterRoundTrip = jsonBeforeRoundTrip
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user