Skip to content

Commit 5727d2a

Browse files
committedApr 10, 2020
Fix Windows integration tests
This commit fixes issues with Windows integration tests and enables the tests to run through github. The problem was that the tests using the log_dir option expected symlinks to the log files to exist, those symlinks being (main.INFO, main.WARN, etc). This was a problem for two reasons: 1) On Windows the symlinks would start with main.exe, not just main 2) The creation of symlinks is not guarenteed on any platform, and on Windows creating a symlink requires Administrator permissions. This was fixed by changing the tests to search for either the symlinks (with a fixed filepath), or the log files themselves. Future work can be done to deal with the lack of symlinks on Windows, but this commit's purpose is to get tests in a passing state.
1 parent edbc1d3 commit 5727d2a

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed
 

‎.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
strategy:
66
matrix:
77
go-versions: [1.12.x, 1.13.x, 1.14.x]
8-
platform: [ubuntu-latest, macos-latest]
8+
platform: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:
1111
- name: Install Go

‎integration_tests/klog_test.go

+50-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package integration_tests_test
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"io/ioutil"
78
"os"
89
"os/exec"
910
"path/filepath"
1011
"regexp"
12+
"runtime"
13+
"strings"
1114
"testing"
1215
)
1316

@@ -181,6 +184,11 @@ func TestDestinationsWithDifferentFlags(t *testing.T) {
181184
},
182185
}
183186

187+
binaryFileExtention := ""
188+
if runtime.GOOS == "windows" {
189+
binaryFileExtention = ".exe"
190+
}
191+
184192
for tcName, tc := range tests {
185193
tc := tc
186194
t.Run(tcName, func(t *testing.T) {
@@ -214,13 +222,19 @@ func TestDestinationsWithDifferentFlags(t *testing.T) {
214222
}
215223

216224
// check files in log_dir
217-
for level, file := range logFileName {
218-
logfile := filepath.Join(logdir, file) // /some/tmp/dir/main.WARNING
225+
for level, levelName := range logFileLevels {
226+
binaryName := "main" + binaryFileExtention
227+
logfile, err := getLogFilePath(logdir, binaryName, levelName)
219228
if tc.expectedLogDir {
229+
if err != nil {
230+
t.Errorf("Unable to find log file: %v", err)
231+
}
220232
content := getFileContent(t, logfile)
221-
checkForLogs(t, tc.expectedInDir[level], tc.notExpectedInDir[level], content, "logfile["+file+"]")
233+
checkForLogs(t, tc.expectedInDir[level], tc.notExpectedInDir[level], content, "logfile["+logfile+"]")
222234
} else {
223-
assertFileIsAbsent(t, logfile)
235+
if err == nil {
236+
t.Errorf("Unexpectedly found log file %s", logfile)
237+
}
224238
}
225239
}
226240
})
@@ -252,11 +266,11 @@ func klogRun(t *testing.T, flags []string, stderr io.Writer) {
252266
}
253267
}
254268

255-
var logFileName = map[int]string{
256-
0: "main.INFO",
257-
1: "main.WARNING",
258-
2: "main.ERROR",
259-
3: "main.FATAL",
269+
var logFileLevels = map[int]string{
270+
0: "INFO",
271+
1: "WARNING",
272+
2: "ERROR",
273+
3: "FATAL",
260274
}
261275

262276
func getFileContent(t *testing.T, filePath string) string {
@@ -306,3 +320,30 @@ func withTmpDir(t *testing.T, f func(string)) {
306320

307321
f(tmpDir)
308322
}
323+
324+
// getLogFileFromDir returns the path of either the symbolic link to the logfile, or the the logfile itself. This must
325+
// be done as the creation of a symlink is not guaranteed on any platform. On Windows, only users with administration
326+
// privileges can create a symlink.
327+
func getLogFilePath(dir, binaryName, levelName string) (string, error) {
328+
symlink := filepath.Join(dir, binaryName+"."+levelName)
329+
if _, err := os.Stat(symlink); err == nil {
330+
return symlink, nil
331+
}
332+
files, err := ioutil.ReadDir(dir)
333+
if err != nil {
334+
return "", fmt.Errorf("could not read directory %s: %v", dir, err)
335+
}
336+
var foundFile string
337+
for _, file := range files {
338+
if strings.HasPrefix(file.Name(), binaryName) && strings.Contains(file.Name(), levelName) {
339+
if foundFile != "" {
340+
return "", fmt.Errorf("found multiple matching files")
341+
}
342+
foundFile = file.Name()
343+
}
344+
}
345+
if foundFile != "" {
346+
return filepath.Join(dir, foundFile), nil
347+
}
348+
return "", fmt.Errorf("file missing from directory")
349+
}

0 commit comments

Comments
 (0)
Please sign in to comment.