Tweak integration tests to be simpler

We require that all generated files are in the data directory, so we
can simplify the logic here.
This commit is contained in:
justinsb 2022-05-13 12:33:20 -04:00
parent f5afc80a1e
commit f47d7b2e43
2 changed files with 40 additions and 23 deletions

View File

@ -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 {

View File

@ -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