Skip to content

Commit 95bf4e9

Browse files
committed
add tests to TestErrorSpam, switch to using log
1 parent 330cf55 commit 95bf4e9

File tree

2 files changed

+95
-125
lines changed

2 files changed

+95
-125
lines changed

test/integration/error_log_test.go

-125
This file was deleted.

test/integration/error_spam_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ package integration
2020

2121
import (
2222
"context"
23+
"fmt"
24+
"os"
2325
"os/exec"
26+
"path/filepath"
2427
"regexp"
2528
"strings"
2629
"testing"
@@ -104,4 +107,96 @@ func TestErrorSpam(t *testing.T) {
104107
}
105108
}
106109
})
110+
111+
logTests := []struct {
112+
command string
113+
args []string
114+
runCount int // number of times to run command
115+
expectedLogFiles int // number of logfiles expected after running command runCount times
116+
}{
117+
{
118+
command: "logs",
119+
runCount: 15, // calling this 15 times should create 2 files with 1 greater than 1M
120+
expectedLogFiles: 2,
121+
},
122+
{
123+
command: "status",
124+
runCount: 100,
125+
expectedLogFiles: 1,
126+
}, {
127+
command: "pause",
128+
runCount: 5,
129+
expectedLogFiles: 1,
130+
}, {
131+
command: "unpause",
132+
runCount: 1,
133+
expectedLogFiles: 1,
134+
}, {
135+
command: "stop",
136+
runCount: 1,
137+
expectedLogFiles: 1,
138+
},
139+
}
140+
141+
for _, test := range logTests {
142+
t.Run(test.command, func(t *testing.T) {
143+
args := []string{test.command, "-p", profile}
144+
args = append(args, test.args...)
145+
// run command runCount times
146+
for i := 0; i < test.runCount; i++ {
147+
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
148+
if err != nil {
149+
t.Fatalf("%q failed: %v", rr.Command(), err)
150+
}
151+
}
152+
// get log files generated above
153+
logFiles, err := getLogFiles(test.command)
154+
if err != nil {
155+
t.Errorf("failed to find tmp log files: command %s : %v", test.command, err)
156+
}
157+
// cleanup generated logfiles
158+
defer cleanupLogFiles(t, logFiles)
159+
// if not the expected number of files, throw err
160+
if len(logFiles) != test.expectedLogFiles {
161+
t.Errorf("failed to find expected number of log files: cmd %s: expected: %d got %d", test.command, test.expectedLogFiles, len(logFiles))
162+
}
163+
// if more than 1 logfile is expected, only one file should be less than 1M
164+
if test.expectedLogFiles > 1 {
165+
foundSmall := false
166+
maxSize := 1024 * 1024 // 1M
167+
for _, logFile := range logFiles {
168+
isSmall := int(logFile.Size()) < maxSize
169+
if isSmall && !foundSmall {
170+
foundSmall = true
171+
} else if isSmall && foundSmall {
172+
t.Errorf("expected to find only one file less than 1M: cmd %s:", test.command)
173+
}
174+
}
175+
}
176+
})
177+
178+
}
179+
}
180+
181+
// getLogFiles returns logfiles corresponding to cmd
182+
func getLogFiles(cmdName string) ([]os.FileInfo, error) {
183+
var logFiles []os.FileInfo
184+
err := filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, err error) error {
185+
if strings.Contains(info.Name(), fmt.Sprintf("minikube_%s", cmdName)) {
186+
logFiles = append(logFiles, info)
187+
}
188+
return nil
189+
})
190+
return logFiles, err
191+
}
192+
193+
// cleanupLogFiles removes logfiles generated during testing
194+
func cleanupLogFiles(t *testing.T, logFiles []os.FileInfo) {
195+
for _, logFile := range logFiles {
196+
logFilePath := filepath.Join(os.TempDir(), logFile.Name())
197+
t.Logf("Cleaning up logfile %s ...", logFilePath)
198+
if err := os.Remove(logFilePath); err != nil {
199+
t.Errorf("failed to cleanup log file: %s : %v", logFilePath, err)
200+
}
201+
}
107202
}

0 commit comments

Comments
 (0)