From 0f0c64a04e8bed4de357adc287de3807fe38ce1f Mon Sep 17 00:00:00 2001 From: Joyce Ma Date: Fri, 22 Nov 2024 08:27:32 +0000 Subject: [PATCH 1/2] Support env var to only compare http log events with given URL prefix --- pkg/test/utils.go | 69 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/pkg/test/utils.go b/pkg/test/utils.go index 750e8e04fb..9ad7dac4ce 100644 --- a/pkg/test/utils.go +++ b/pkg/test/utils.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "k8s.io/klog/v2" "sigs.k8s.io/yaml" ) @@ -65,15 +66,35 @@ func MustReadFile(t *testing.T, p string) []byte { return b } +func extractEventsWithURLPrefix(allEvents, urlPrefix string) string { + eventStrings := make([]string, 0) + events := strings.Split(allEvents, "---") + for _, event := range events { + processed := strings.TrimSpace(event) + for _, m := range []string{"GET", "POST", "PUT", "PATCH", "DELETE"} { + processed = strings.TrimPrefix(processed, m) + processed = strings.TrimSpace(processed) + } + if strings.HasPrefix(processed, urlPrefix) { + if len(eventStrings) == 0 { + event = strings.TrimPrefix(event, "\n\n") + } + eventStrings = append(eventStrings, event) + } + } + return strings.Join(eventStrings, "---") +} + // CompareGoldenFile performs a file comparison for a golden test. -func CompareGoldenFile(t *testing.T, p string, got string, normalizers ...func(s string) string) { +func CompareGoldenFile(t *testing.T, path, got string, normalizers ...func(s string) string) { writeGoldenOutput := os.Getenv("WRITE_GOLDEN_OUTPUT") != "" for _, normalizer := range normalizers { got = normalizer(got) } + gotForComp := got - wantBytes, err := os.ReadFile(p) + wantBytes, err := os.ReadFile(path) if err != nil { if writeGoldenOutput && os.IsNotExist(err) { // Expected when creating output for the first time; @@ -83,40 +104,58 @@ func CompareGoldenFile(t *testing.T, p string, got string, normalizers ...func(s // Golden file won't be generated if the result is empty. return } else { - t.Fatalf("FAIL: failed to read golden file %q: %v", p, err) + t.Fatalf("FAIL: failed to read golden file %q: %v", path, err) } } - want := string(wantBytes) + wantForComp := string(wantBytes) for _, normalizer := range normalizers { - want = normalizer(want) + wantForComp = normalizer(wantForComp) + } + // If urlPrefix is not an empty string, then we should only compare the http + // log events that has given URL prefix. + urlPrefix := os.Getenv("ONLY_COMPARE_URL_PREFIX") + if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "mock" && urlPrefix != "" { + klog.Infof("only comparing events with URL prefix %q in the http log", urlPrefix) + + wantForComp = extractEventsWithURLPrefix(wantForComp, urlPrefix) + gotForComp = extractEventsWithURLPrefix(gotForComp, urlPrefix) } - if want == got { + if wantForComp == gotForComp { + if urlPrefix != "" && writeGoldenOutput { + // Write the entire http log to the golden file before the current + // comparison only covers events with given URL prefix. + if err := os.WriteFile(path, []byte(got), 0644); err != nil { + t.Fatalf("FAIL: failed to write golden output %s: %v", path, err) + } + t.Logf("wrote updated golden output to %s", path) + } return } - if diff := cmp.Diff(want, got); diff != "" { + if diff := cmp.Diff(wantForComp, gotForComp); diff != "" { onlyWarn := false for _, f := range strings.Split(os.Getenv("ONLY_WARN_ON_GOLDEN_DIFFS"), ",") { - if f == filepath.Base(p) { + if f == filepath.Base(path) { onlyWarn = true } } if onlyWarn { - t.Logf("found diff in golden output %s, but ONLY_WARN_ON_GOLDEN_DIFFS=%s so will treat as a warning", p, os.Getenv("ONLY_WARN_ON_GOLDEN_DIFFS")) - t.Logf("unexpected diff in %s: %s", p, diff) + t.Logf("found diff in golden output %s, but ONLY_WARN_ON_GOLDEN_DIFFS=%s so will treat as a warning", path, os.Getenv("ONLY_WARN_ON_GOLDEN_DIFFS")) + t.Logf("unexpected diff in %s: %s", path, diff) } else { - t.Errorf("FAIL: unexpected diff in %s: %s", p, diff) + t.Errorf("FAIL: unexpected diff in %s: %s", path, diff) } } if writeGoldenOutput { - // Write the output to the golden file - if err := os.WriteFile(p, []byte(got), 0644); err != nil { - t.Fatalf("FAIL: failed to write golden output %s: %v", p, err) + // No matter how we compare the golden files, we should write the entire + // http log to the golden file. + if err := os.WriteFile(path, []byte(got), 0644); err != nil { + t.Fatalf("FAIL: failed to write golden output %s: %v", path, err) } - t.Logf("wrote updated golden output to %s", p) + t.Logf("wrote updated golden output to %s", path) } } From 9a674c0af423496967df19b4e6c145cb9e66f88b Mon Sep 17 00:00:00 2001 From: Joyce Ma Date: Sat, 23 Nov 2024 04:00:07 +0000 Subject: [PATCH 2/2] Address comments --- pkg/test/utils.go | 49 ++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/pkg/test/utils.go b/pkg/test/utils.go index 9ad7dac4ce..566aa7ae7e 100644 --- a/pkg/test/utils.go +++ b/pkg/test/utils.go @@ -86,30 +86,30 @@ func extractEventsWithURLPrefix(allEvents, urlPrefix string) string { } // CompareGoldenFile performs a file comparison for a golden test. -func CompareGoldenFile(t *testing.T, path, got string, normalizers ...func(s string) string) { +func CompareGoldenFile(t *testing.T, p, fullGot string, normalizers ...func(s string) string) { writeGoldenOutput := os.Getenv("WRITE_GOLDEN_OUTPUT") != "" for _, normalizer := range normalizers { - got = normalizer(got) + fullGot = normalizer(fullGot) } - gotForComp := got + got := fullGot - wantBytes, err := os.ReadFile(path) + wantBytes, err := os.ReadFile(p) if err != nil { if writeGoldenOutput && os.IsNotExist(err) { // Expected when creating output for the first time; // treat as empty wantBytes = []byte{} // Not strictly needed, but clearer - } else if got == "" && os.IsNotExist(err) { + } else if fullGot == "" && os.IsNotExist(err) { // Golden file won't be generated if the result is empty. return } else { - t.Fatalf("FAIL: failed to read golden file %q: %v", path, err) + t.Fatalf("FAIL: failed to read golden file %q: %v", p, err) } } - wantForComp := string(wantBytes) + want := string(wantBytes) for _, normalizer := range normalizers { - wantForComp = normalizer(wantForComp) + want = normalizer(want) } // If urlPrefix is not an empty string, then we should only compare the http // log events that has given URL prefix. @@ -117,45 +117,46 @@ func CompareGoldenFile(t *testing.T, path, got string, normalizers ...func(s str if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "mock" && urlPrefix != "" { klog.Infof("only comparing events with URL prefix %q in the http log", urlPrefix) - wantForComp = extractEventsWithURLPrefix(wantForComp, urlPrefix) - gotForComp = extractEventsWithURLPrefix(gotForComp, urlPrefix) + want = extractEventsWithURLPrefix(want, urlPrefix) + got = extractEventsWithURLPrefix(got, urlPrefix) } - if wantForComp == gotForComp { + if want == got { if urlPrefix != "" && writeGoldenOutput { - // Write the entire http log to the golden file before the current - // comparison only covers events with given URL prefix. - if err := os.WriteFile(path, []byte(got), 0644); err != nil { - t.Fatalf("FAIL: failed to write golden output %s: %v", path, err) + // Write the full http log to the golden file. The current + // comparison only covers events with given URL prefix, and the full + // http log may have diffs. + if err := os.WriteFile(p, []byte(fullGot), 0644); err != nil { + t.Fatalf("FAIL: failed to write golden output %s: %v", p, err) } - t.Logf("wrote updated golden output to %s", path) + t.Logf("wrote updated golden output to %s", p) } return } - if diff := cmp.Diff(wantForComp, gotForComp); diff != "" { + if diff := cmp.Diff(want, got); diff != "" { onlyWarn := false for _, f := range strings.Split(os.Getenv("ONLY_WARN_ON_GOLDEN_DIFFS"), ",") { - if f == filepath.Base(path) { + if f == filepath.Base(p) { onlyWarn = true } } if onlyWarn { - t.Logf("found diff in golden output %s, but ONLY_WARN_ON_GOLDEN_DIFFS=%s so will treat as a warning", path, os.Getenv("ONLY_WARN_ON_GOLDEN_DIFFS")) - t.Logf("unexpected diff in %s: %s", path, diff) + t.Logf("found diff in golden output %s, but ONLY_WARN_ON_GOLDEN_DIFFS=%s so will treat as a warning", p, os.Getenv("ONLY_WARN_ON_GOLDEN_DIFFS")) + t.Logf("unexpected diff in %s: %s", p, diff) } else { - t.Errorf("FAIL: unexpected diff in %s: %s", path, diff) + t.Errorf("FAIL: unexpected diff in %s: %s", p, diff) } } if writeGoldenOutput { // No matter how we compare the golden files, we should write the entire // http log to the golden file. - if err := os.WriteFile(path, []byte(got), 0644); err != nil { - t.Fatalf("FAIL: failed to write golden output %s: %v", path, err) + if err := os.WriteFile(p, []byte(fullGot), 0644); err != nil { + t.Fatalf("FAIL: failed to write golden output %s: %v", p, err) } - t.Logf("wrote updated golden output to %s", path) + t.Logf("wrote updated golden output to %s", p) } }