diff --git a/cmd/kops/integration_test.go b/cmd/kops/integration_test.go index 1cbcc5a5ff..fafb0eb574 100644 --- a/cmd/kops/integration_test.go +++ b/cmd/kops/integration_test.go @@ -29,6 +29,7 @@ import ( "io" "os" "path" + "path/filepath" "reflect" "sort" "strings" @@ -910,15 +911,40 @@ func (i *integrationTest) runTest(t *testing.T, h *testutils.IntegrationTestHarn // Compare data files if they are provided if len(expectedDataFilenames) > 0 { - actualDataPath := path.Join(h.TempDir, "out", "data") - files, err := os.ReadDir(actualDataPath) + actualDataDir := filepath.Join(h.TempDir, "out", "data") + + expectedDataDir := filepath.Join(i.srcDir, "data") + for _, filename := range expectedDataFilenames { + expectedPath := filepath.Join(expectedDataDir, filename) + actualPath := filepath.Join(actualDataDir, filename) + actualDataContent, err := os.ReadFile(actualPath) + if err != nil { + t.Errorf("failed to read actual data file %q: %v", actualPath, err) + continue + } + golden.AssertMatchesFile(t, string(actualDataContent), expectedPath) + } + + actualFiles, err := os.ReadDir(actualDataDir) if err != nil { - t.Fatalf("failed to read data dir: %v", err) + t.Fatalf("failed to read data dir %q: %v", actualDataDir, err) } var actualDataFilenames []string - for _, f := range files { + for _, f := range actualFiles { actualDataFilenames = append(actualDataFilenames, f.Name()) + + if golden.UpdateExpectedOutput() { + filename := f.Name() + expectedPath := filepath.Join(expectedDataDir, filename) + actualPath := filepath.Join(actualDataDir, filename) + actualDataContent, err := os.ReadFile(actualPath) + if err != nil { + t.Errorf("failed to read actual data file %q: %v", actualPath, err) + continue + } + golden.AssertMatchesFile(t, string(actualDataContent), expectedPath) + } } sort.Strings(expectedDataFilenames) @@ -933,26 +959,12 @@ func (i *integrationTest) runTest(t *testing.T, h *testutils.IntegrationTestHarn expected := strings.Join(expectedDataFilenames, "\n") diff := diff.FormatDiff(actual, expected) t.Log(diff) - t.Fatal("unexpected data files.") + t.Error("unexpected data files.") } - // Some tests might provide _some_ tf data files (not necessarily all that - // are actually produced), validate that the provided expected data file - // contents match actual data file content - expectedDataPath := path.Join(i.srcDir, "data") - { - for _, dataFileName := range expectedDataFilenames { - actualDataContent, err := os.ReadFile(path.Join(actualDataPath, dataFileName)) - if err != nil { - t.Fatalf("failed to read actual data file: %v", err) - } - golden.AssertMatchesFile(t, string(actualDataContent), path.Join(expectedDataPath, dataFileName)) - } - } - - existingExpectedFiles, err := os.ReadDir(expectedDataPath) + existingExpectedFiles, err := os.ReadDir(expectedDataDir) if err != nil { - t.Fatalf("failed to read data dir: %v", err) + t.Fatalf("failed to read data dir %q: %v", expectedDataDir, err) } existingExpectedFilenames := make([]string, len(existingExpectedFiles)) for i, f := range existingExpectedFiles { diff --git a/pkg/testutils/golden/compare.go b/pkg/testutils/golden/compare.go index 08a2e0a285..aa86406048 100644 --- a/pkg/testutils/golden/compare.go +++ b/pkg/testutils/golden/compare.go @@ -26,6 +26,11 @@ import ( "k8s.io/kops/pkg/diff" ) +// UpdateExpectedOutput returns true if we should be writing/updating the expected output. +func UpdateExpectedOutput() bool { + return os.Getenv("HACK_UPDATE_EXPECTED_IN_PLACE") != "" +} + // AssertMatchesFile matches the actual value to a with expected file. // If HACK_UPDATE_EXPECTED_IN_PLACE is set, it will write the actual value to the expected file, // which is very handy when updating our tests. @@ -41,7 +46,7 @@ func AssertMatchesFile(t *testing.T, actual string, p string) { expectedBytes, err := os.ReadFile(p) if err != nil { - if !os.IsNotExist(err) || os.Getenv("HACK_UPDATE_EXPECTED_IN_PLACE") == "" { + if !os.IsNotExist(err) && !UpdateExpectedOutput() { t.Fatalf("error reading file %q: %v", p, err) } } @@ -55,7 +60,7 @@ func AssertMatchesFile(t *testing.T, actual string, p string) { return } - if os.Getenv("HACK_UPDATE_EXPECTED_IN_PLACE") != "" { + if UpdateExpectedOutput() { t.Logf("HACK_UPDATE_EXPECTED_IN_PLACE: writing expected output %s", p) // Keep git happy with a trailing newline