Skip to content

Commit

Permalink
Merge pull request #136 from sebsoto/fix_windows_tests
Browse files Browse the repository at this point in the history
Fix Windows integration tests
  • Loading branch information
k8s-ci-robot committed Apr 10, 2020
2 parents 4cae117 + 5727d2a commit 99c45d9
Show file tree
Hide file tree
Showing 3 changed files with 58 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")
}
7 changes: 7 additions & 0 deletions klog_test.go
Expand Up @@ -28,6 +28,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -317,13 +318,16 @@ func TestVmoduleOff(t *testing.T) {
func TestSetOutputDataRace(t *testing.T) {
setFlags()
defer logging.swap(logging.newBuffers())
var wg sync.WaitGroup
for i := 1; i <= 50; i++ {
go func() {
logging.flushDaemon()
}()
}
for i := 1; i <= 50; i++ {
wg.Add(1)
go func() {
defer wg.Done()
SetOutput(ioutil.Discard)
}()
}
Expand All @@ -333,7 +337,9 @@ func TestSetOutputDataRace(t *testing.T) {
}()
}
for i := 1; i <= 50; i++ {
wg.Add(1)
go func() {
defer wg.Done()
SetOutputBySeverity("INFO", ioutil.Discard)
}()
}
Expand All @@ -342,6 +348,7 @@ func TestSetOutputDataRace(t *testing.T) {
logging.flushDaemon()
}()
}
wg.Wait()
}

// vGlobs are patterns that match/don't match this file at V=2.
Expand Down

0 comments on commit 99c45d9

Please sign in to comment.