From 90cbd960cd5f311e1f5ba46c70d53bbd1ca9eb37 Mon Sep 17 00:00:00 2001 From: Pavel Simovec Date: Tue, 4 Aug 2020 15:50:36 +0200 Subject: [PATCH 1/2] test file extensions --- test/integration/bugs_test.go | 33 +++++++++++++++++++++++++++++++++ test/integration/main_test.go | 8 ++++++++ 2 files changed, 41 insertions(+) diff --git a/test/integration/bugs_test.go b/test/integration/bugs_test.go index fa7e86146..df7f41bb3 100644 --- a/test/integration/bugs_test.go +++ b/test/integration/bugs_test.go @@ -1,6 +1,9 @@ package integration import ( + "fmt" + "regexp" + "strings" "testing" "time" @@ -155,12 +158,42 @@ func latestArchiveContainsEvent(t *testing.T) { } } +//https://bugzilla.redhat.com/show_bug.cgi?id=1840012 +func latestArchiveFilesContainExtensions(t *testing.T) { + suffixes := []string{ + // known file extensions + `\.crt`, + `\.json`, + `\.log`, + // exceptions - files without extension + `/config`, + `/id`, + `/invoker`, + `/metrics`, + `/version`, + } + pattern := fmt.Sprintf(`(%s)$`, strings.Join(suffixes, "|")) + regex, err := regexp.Compile(pattern) + e(t, err, "failed to compile pattern") + archiveFiles := latestArchiveFiles(t) + t.Log(strings.Join(archiveFiles, "\n")) + if len(archiveFiles) == 0 { + t.Fatal("No files in archive to check") + } + for _, fileName := range archiveFiles { + if !regex.MatchString(fileName) { + t.Errorf(`file "%s" does not match pattern "%s"`, fileName, pattern) + } + } +} + func TestCollectingAfterDegradingOperator(t *testing.T) { defer ChangeReportTimeInterval(t, 1)() defer degradeOperatorMonitoring(t)() checkPodsLogs(t, clientset, `Wrote \d+ records to disk in \d+`, true) t.Run("Logs", latestArchiveContainsPodLogs) t.Run("Event", latestArchiveContainsEvent) + t.Run("FileExtensions", latestArchiveFilesContainExtensions) } // https://bugzilla.redhat.com/show_bug.cgi?id=1782151 diff --git a/test/integration/main_test.go b/test/integration/main_test.go index 7dc5695c6..7fb50b00a 100644 --- a/test/integration/main_test.go +++ b/test/integration/main_test.go @@ -354,6 +354,14 @@ func ChangeReportTimeInterval(t *testing.T, minutes time.Duration) func(){ return func(){ changeReportTimeInterval(t, previousInterval) } } +func latestArchiveFiles(t *testing.T) []string { + insightsPod := findPod(t, clientset, "openshift-insights", "insights-operator") + archiveLogFiles := `tar tf $(ls -dtr /var/lib/insights-operator/* | tail -1)` + stdout, _, _ := ExecCmd(t, clientset, insightsPod.Name, "openshift-insights", archiveLogFiles, nil) + stdout = strings.TrimSpace(stdout) + return strings.Split(stdout, "\n") +} + func latestArchiveContainsFiles(t *testing.T, pattern string) (int, error) { insightsPod := findPod(t, clientset, "openshift-insights", "insights-operator") hasLatestArchiveLogs := fmt.Sprintf(`tar tf $(ls -dtr /var/lib/insights-operator/* | tail -1)|grep -c "%s"`, pattern) From b9d27d2ad10fa698dd78dba087a5c411144b895e Mon Sep 17 00:00:00 2001 From: Pavel Simovec Date: Tue, 4 Aug 2020 19:39:58 +0200 Subject: [PATCH 2/2] moved regex out of the function for easier changes in future --- test/integration/bugs_test.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/test/integration/bugs_test.go b/test/integration/bugs_test.go index df7f41bb3..fd8b744f8 100644 --- a/test/integration/bugs_test.go +++ b/test/integration/bugs_test.go @@ -1,7 +1,6 @@ package integration import ( - "fmt" "regexp" "strings" "testing" @@ -12,6 +11,14 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ) +const knownFileSuffixesInsideArchiveRegex string = `(`+ + // known file extensions + `\.(crt|json|log)` + + `|` + + // exceptions - file names without extension # + `(\/|^)(config|id|invoker|metrics|version)` + +`)$` + // https://bugzilla.redhat.com/show_bug.cgi?id=1750665 // https://bugzilla.redhat.com/show_bug.cgi?id=1753755 func TestDefaultUploadFrequency(t *testing.T) { @@ -160,20 +167,7 @@ func latestArchiveContainsEvent(t *testing.T) { //https://bugzilla.redhat.com/show_bug.cgi?id=1840012 func latestArchiveFilesContainExtensions(t *testing.T) { - suffixes := []string{ - // known file extensions - `\.crt`, - `\.json`, - `\.log`, - // exceptions - files without extension - `/config`, - `/id`, - `/invoker`, - `/metrics`, - `/version`, - } - pattern := fmt.Sprintf(`(%s)$`, strings.Join(suffixes, "|")) - regex, err := regexp.Compile(pattern) + regex, err := regexp.Compile(knownFileSuffixesInsideArchiveRegex) e(t, err, "failed to compile pattern") archiveFiles := latestArchiveFiles(t) t.Log(strings.Join(archiveFiles, "\n")) @@ -182,7 +176,7 @@ func latestArchiveFilesContainExtensions(t *testing.T) { } for _, fileName := range archiveFiles { if !regex.MatchString(fileName) { - t.Errorf(`file "%s" does not match pattern "%s"`, fileName, pattern) + t.Errorf(`file "%s" does not match pattern "%s"`, fileName, knownFileSuffixesInsideArchiveRegex) } } }