Skip to content

Commit

Permalink
Merge pull request #116384 from liggitt/fixup-after-roundtrip
Browse files Browse the repository at this point in the history
Detect and clean up unneeded  after_roundtrip fixtures

Kubernetes-commit: 61050182b543c7b30d61edbd7951249c24e11280
  • Loading branch information
k8s-publishing-bot committed Mar 10, 2023
2 parents ca95f42 + 027c209 commit 8fccf3d
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkg/api/apitesting/roundtrip/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ func writeFile(t *testing.T, dir string, gvk schema.GroupVersionKind, suffix, ex
}
}

func deleteFile(t *testing.T, dir string, gvk schema.GroupVersionKind, suffix, extension string) {
if err := os.Remove(filepath.Join(dir, makeName(gvk)+suffix+"."+extension)); err != nil {
t.Fatalf("error removing %s: %v", extension, err)
}
}

func (c *CompatibilityTestOptions) runPreviousVersionTest(t *testing.T, gvk schema.GroupVersionKind, previousVersionDir string, usedFiles sets.String) {
jsonBeforeRoundTrip, yamlBeforeRoundTrip, protoBeforeRoundTrip, err := read(previousVersionDir, gvk, "", usedFiles)
if os.IsNotExist(err) || (len(jsonBeforeRoundTrip) == 0 && len(yamlBeforeRoundTrip) == 0 && len(protoBeforeRoundTrip) == 0) {
Expand Down Expand Up @@ -422,15 +428,28 @@ func (c *CompatibilityTestOptions) runPreviousVersionTest(t *testing.T, gvk sche
}
protoAfterRoundTrip := protoBytes.Bytes()

jsonNeedsRemove := false
yamlNeedsRemove := false
protoNeedsRemove := false

expectedJSONAfterRoundTrip, expectedYAMLAfterRoundTrip, expectedProtoAfterRoundTrip, _ := read(previousVersionDir, gvk, ".after_roundtrip", usedFiles)
if len(expectedJSONAfterRoundTrip) == 0 {
expectedJSONAfterRoundTrip = jsonBeforeRoundTrip
} else if bytes.Equal(jsonBeforeRoundTrip, expectedJSONAfterRoundTrip) {
t.Errorf("JSON after_roundtrip file is identical and should be removed")
jsonNeedsRemove = true
}
if len(expectedYAMLAfterRoundTrip) == 0 {
expectedYAMLAfterRoundTrip = yamlBeforeRoundTrip
} else if bytes.Equal(yamlBeforeRoundTrip, expectedYAMLAfterRoundTrip) {
t.Errorf("YAML after_roundtrip file is identical and should be removed")
yamlNeedsRemove = true
}
if len(expectedProtoAfterRoundTrip) == 0 {
expectedProtoAfterRoundTrip = protoBeforeRoundTrip
} else if bytes.Equal(protoBeforeRoundTrip, expectedProtoAfterRoundTrip) {
t.Errorf("Proto after_roundtrip file is identical and should be removed")
protoNeedsRemove = true
}

jsonNeedsUpdate := false
Expand All @@ -456,17 +475,25 @@ func (c *CompatibilityTestOptions) runPreviousVersionTest(t *testing.T, gvk sche
// t.Logf("json (for locating the offending field based on surrounding data): %s", string(expectedJSON))
}

if jsonNeedsUpdate || yamlNeedsUpdate || protoNeedsUpdate {
if jsonNeedsUpdate || yamlNeedsUpdate || protoNeedsUpdate || jsonNeedsRemove || yamlNeedsRemove || protoNeedsRemove {
const updateEnvVar = "UPDATE_COMPATIBILITY_FIXTURE_DATA"
if os.Getenv(updateEnvVar) == "true" {
if jsonNeedsUpdate {
writeFile(t, previousVersionDir, gvk, ".after_roundtrip", "json", jsonAfterRoundTrip)
} else if jsonNeedsRemove {
deleteFile(t, previousVersionDir, gvk, ".after_roundtrip", "json")
}

if yamlNeedsUpdate {
writeFile(t, previousVersionDir, gvk, ".after_roundtrip", "yaml", yamlAfterRoundTrip)
} else if yamlNeedsRemove {
deleteFile(t, previousVersionDir, gvk, ".after_roundtrip", "yaml")
}

if protoNeedsUpdate {
writeFile(t, previousVersionDir, gvk, ".after_roundtrip", "pb", protoAfterRoundTrip)
} else if protoNeedsRemove {
deleteFile(t, previousVersionDir, gvk, ".after_roundtrip", "pb")
}
t.Logf("wrote expected compatibility data... verify, commit, and rerun tests")
} else {
Expand Down

0 comments on commit 8fccf3d

Please sign in to comment.