Skip to content

Commit

Permalink
Fix Windows integration tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebsoto committed Apr 10, 2020
1 parent edbc1d3 commit 5727d2a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -5,7 +5,7 @@ jobs:
strategy:
matrix:
go-versions: [1.12.x, 1.13.x, 1.14.x]
platform: [ubuntu-latest, macos-latest]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
Expand Down
59 changes: 50 additions & 9 deletions integration_tests/klog_test.go
Expand Up @@ -2,12 +2,15 @@ package integration_tests_test

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
)

Expand Down Expand Up @@ -181,6 +184,11 @@ func TestDestinationsWithDifferentFlags(t *testing.T) {
},
}

binaryFileExtention := ""
if runtime.GOOS == "windows" {
binaryFileExtention = ".exe"
}

for tcName, tc := range tests {
tc := tc
t.Run(tcName, func(t *testing.T) {
Expand Down Expand Up @@ -214,13 +222,19 @@ func TestDestinationsWithDifferentFlags(t *testing.T) {
}

// check files in log_dir
for level, file := range logFileName {
logfile := filepath.Join(logdir, file) // /some/tmp/dir/main.WARNING
for level, levelName := range logFileLevels {
binaryName := "main" + binaryFileExtention
logfile, err := getLogFilePath(logdir, binaryName, levelName)
if tc.expectedLogDir {
if err != nil {
t.Errorf("Unable to find log file: %v", err)
}
content := getFileContent(t, logfile)
checkForLogs(t, tc.expectedInDir[level], tc.notExpectedInDir[level], content, "logfile["+file+"]")
checkForLogs(t, tc.expectedInDir[level], tc.notExpectedInDir[level], content, "logfile["+logfile+"]")
} else {
assertFileIsAbsent(t, logfile)
if err == nil {
t.Errorf("Unexpectedly found log file %s", logfile)
}
}
}
})
Expand Down Expand Up @@ -252,11 +266,11 @@ func klogRun(t *testing.T, flags []string, stderr io.Writer) {
}
}

var logFileName = map[int]string{
0: "main.INFO",
1: "main.WARNING",
2: "main.ERROR",
3: "main.FATAL",
var logFileLevels = map[int]string{
0: "INFO",
1: "WARNING",
2: "ERROR",
3: "FATAL",
}

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

f(tmpDir)
}

// getLogFileFromDir returns the path of either the symbolic link to the logfile, or the the logfile itself. This must
// be done as the creation of a symlink is not guaranteed on any platform. On Windows, only users with administration
// privileges can create a symlink.
func getLogFilePath(dir, binaryName, levelName string) (string, error) {
symlink := filepath.Join(dir, binaryName+"."+levelName)
if _, err := os.Stat(symlink); err == nil {
return symlink, nil
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("could not read directory %s: %v", dir, err)
}
var foundFile string
for _, file := range files {
if strings.HasPrefix(file.Name(), binaryName) && strings.Contains(file.Name(), levelName) {
if foundFile != "" {
return "", fmt.Errorf("found multiple matching files")
}
foundFile = file.Name()
}
}
if foundFile != "" {
return filepath.Join(dir, foundFile), nil
}
return "", fmt.Errorf("file missing from directory")
}

0 comments on commit 5727d2a

Please sign in to comment.