@@ -20,7 +20,10 @@ package integration
20
20
21
21
import (
22
22
"context"
23
+ "fmt"
24
+ "os"
23
25
"os/exec"
26
+ "path/filepath"
24
27
"regexp"
25
28
"strings"
26
29
"testing"
@@ -104,4 +107,96 @@ func TestErrorSpam(t *testing.T) {
104
107
}
105
108
}
106
109
})
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
+ }
107
202
}
0 commit comments