Skip to content

Commit 6da2582

Browse files
Merge pull request #152 from psimovec/registry
Tests e2e: Automate 2 BZ tests & generalize TestArchiveContainsFiles
2 parents 90d1d35 + 557ebb2 commit 6da2582

File tree

2 files changed

+82
-43
lines changed

2 files changed

+82
-43
lines changed

test/integration/bugs_test.go

+44-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package integration
22

33
import (
4+
"fmt"
45
"regexp"
5-
"strings"
66
"testing"
77
"time"
88

@@ -145,49 +145,60 @@ func TestUnreachableHost(t *testing.T) {
145145
t.Log(errDegraded)
146146
}
147147

148-
//https://bugzilla.redhat.com/show_bug.cgi?id=1838973
149-
func latestArchiveContainsPodLogs(t *testing.T) {
150-
logCount, err := latestArchiveContainsFiles(t, `^config/pod/openshift-monitoring/logs/.*\.log$`)
151-
e(t, err, "Checking for log files failed")
152-
t.Log(logCount, "log files found")
153-
if logCount == 0 {
154-
t.Error("no log files in archive")
148+
func genLatestArchiveCheckPattern(prettyName string, check func(*testing.T, string, []string, *regexp.Regexp) error, pattern string) func(t *testing.T) {
149+
return func(t * testing.T){
150+
err := latestArchiveCheckFiles(t, prettyName, check, pattern)
151+
if err!=nil{
152+
t.Fatal(err)
153+
}
155154
}
156155
}
157156

158-
//https://bugzilla.redhat.com/show_bug.cgi?id=1767719
159-
func latestArchiveContainsEvent(t *testing.T) {
160-
logCount, err := latestArchiveContainsFiles(t, `^events/openshift-monitoring.json$`)
161-
e(t, err, "Checking for event failed")
162-
t.Log(logCount, "event files found")
163-
if logCount == 0 {
164-
t.Error("no event file in archive")
157+
func latestArchiveContainsConfigMaps(t *testing.T) {
158+
configMaps, _ := clientset.CoreV1().ConfigMaps("openshift-config").List(metav1.ListOptions{})
159+
if len(configMaps.Items) == 0 {
160+
t.Fatal("Nothing to test: no config maps in openshift-config namespace")
165161
}
166-
}
167-
168-
//https://bugzilla.redhat.com/show_bug.cgi?id=1840012
169-
func latestArchiveFilesContainExtensions(t *testing.T) {
170-
regex, err := regexp.Compile(knownFileSuffixesInsideArchiveRegex)
171-
e(t, err, "failed to compile pattern")
172-
archiveFiles := latestArchiveFiles(t)
173-
t.Log(strings.Join(archiveFiles, "\n"))
174-
if len(archiveFiles) == 0 {
175-
t.Fatal("No files in archive to check")
176-
}
177-
for _, fileName := range archiveFiles {
178-
if !regex.MatchString(fileName) {
179-
t.Errorf(`file "%s" does not match pattern "%s"`, fileName, knownFileSuffixesInsideArchiveRegex)
162+
for _, configMap := range configMaps.Items {
163+
configMapPath := fmt.Sprintf("^config/configmaps/%s/.*$", configMap.Name)
164+
err := latestArchiveCheckFiles(t, "config map", matchingFileExists, configMapPath)
165+
if err != nil {
166+
t.Error(err)
180167
}
181168
}
182169
}
183170

184-
func TestCollectingAfterDegradingOperator(t *testing.T) {
171+
func TestArchiveContains(t *testing.T) {
172+
//https://bugzilla.redhat.com/show_bug.cgi?id=1825756
173+
t.Run("ConfigMaps", latestArchiveContainsConfigMaps)
174+
175+
//https://bugzilla.redhat.com/show_bug.cgi?id=1834677
176+
t.Run("ImageRegistry",
177+
genLatestArchiveCheckPattern(
178+
"image registry", matchingFileExists,
179+
`^config/imageregistry.json$`))
180+
185181
defer ChangeReportTimeInterval(t, 1)()
186182
defer degradeOperatorMonitoring(t)()
187183
checkPodsLogs(t, clientset, `Wrote \d+ records to disk in \d+`, true)
188-
t.Run("Logs", latestArchiveContainsPodLogs)
189-
t.Run("Event", latestArchiveContainsEvent)
190-
t.Run("FileExtensions", latestArchiveFilesContainExtensions)
184+
185+
//https://bugzilla.redhat.com/show_bug.cgi?id=1838973
186+
t.Run("Logs",
187+
genLatestArchiveCheckPattern(
188+
"log", matchingFileExists,
189+
`^config/pod/openshift-monitoring/logs/.*\.log$`))
190+
191+
//https://bugzilla.redhat.com/show_bug.cgi?id=1767719
192+
t.Run("Event",
193+
genLatestArchiveCheckPattern(
194+
"event", matchingFileExists,
195+
`^events/openshift-monitoring.json$`))
196+
197+
//https://bugzilla.redhat.com/show_bug.cgi?id=1840012
198+
t.Run("FileExtensions",
199+
genLatestArchiveCheckPattern(
200+
"extension of", allFilesMatch,
201+
knownFileSuffixesInsideArchiveRegex))
191202
}
192203

193204
// https://bugzilla.redhat.com/show_bug.cgi?id=1782151

test/integration/main_test.go

+38-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"k8s.io/client-go/tools/remotecommand"
99
"os"
1010
"regexp"
11-
"strconv"
1211
"strings"
1312
"testing"
1413
"time"
@@ -362,16 +361,45 @@ func latestArchiveFiles(t *testing.T) []string {
362361
return strings.Split(stdout, "\n")
363362
}
364363

365-
func latestArchiveContainsFiles(t *testing.T, pattern string) (int, error) {
366-
insightsPod := findPod(t, clientset, "openshift-insights", "insights-operator")
367-
hasLatestArchiveLogs := fmt.Sprintf(`tar tf $(ls -dtr /var/lib/insights-operator/* | tail -1)|grep -c "%s"`, pattern)
368-
stdout, _, _ := ExecCmd(t, clientset, insightsPod.Name, "openshift-insights", hasLatestArchiveLogs, nil)
369-
t.Log(stdout)
370-
logCount, err := strconv.Atoi(strings.TrimSpace(stdout))
371-
if err != nil && logCount !=0{
372-
return -1, fmt.Errorf("command returned non-integer: %s", stdout)
364+
func allFilesMatch(t *testing.T, prettyName string, files []string, regex *regexp.Regexp) error {
365+
for _, file := range files {
366+
if !regex.MatchString(file) {
367+
t.Errorf(`%s file "%s" does not match pattern "%s"`, prettyName, file, regex.String())
368+
}
369+
}
370+
return nil
371+
}
372+
373+
func matchingFileExists(t *testing.T, prettyName string, files []string, regex *regexp.Regexp) error {
374+
count := 0
375+
for _, file := range files {
376+
if regex.MatchString(file){
377+
count++
378+
}
379+
}
380+
381+
word := "files"
382+
suffix := ""
383+
if count == 1 {
384+
word = "file"
385+
suffix = "es"
386+
}
387+
t.Logf("%d %s %s match%s pattern `%s`", count, prettyName, word, suffix, regex.String())
388+
389+
if count != 0 {
390+
return nil
391+
}
392+
return fmt.Errorf("did not find any (%s)file matching %s",prettyName, regex.String())
393+
}
394+
395+
func latestArchiveCheckFiles(t *testing.T, prettyName string, check func(*testing.T, string, []string, *regexp.Regexp) error, pattern string) error {
396+
latestFiles := latestArchiveFiles(t)
397+
if len(latestFiles) == 0 {
398+
t.Fatal("No files in archive to check")
373399
}
374-
return logCount, nil
400+
regex, err := regexp.Compile(pattern)
401+
e(t, err, "failed to compile pattern")
402+
return check(t, prettyName,latestFiles, regex)
375403
}
376404

377405
func TestMain(m *testing.M) {

0 commit comments

Comments
 (0)