Skip to content

Commit

Permalink
Fix tests on Windows (grafana#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbafilho authored and mstoykov committed Jan 21, 2019
1 parent 4aab6b9 commit abe50b7
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 96 deletions.
7 changes: 6 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install:
# specific to go
- set PATH=%GOPATH%\bin;%PATH%


# build msi artifacts
build_script:
# In case of non tag build, mock a version
Expand All @@ -36,6 +37,10 @@ build_script:
- candle.exe -arch x64 -dVERSION=%VERSION% k6.wxs
- light.exe -ext WixUIExtension k6.wixobj

test_script:
- cd %APPVEYOR_BUILD_FOLDER%
- go test ./...

deploy_script:
# Checking if the build has started by pushed tag
- ps: |
Expand All @@ -54,4 +59,4 @@ deploy_script:
(Get-Content '.\k6.portable.nuspec' -Raw).Replace("<version>__REPLACE__</version>", "<version>$($env:APPVEYOR_REPO_TAG_NAME.substring(1))</version>") | Out-File '.\k6.portable.nuspec'
choco pack
# Publising the chocolatey package
- 'curl -H "X-GPG-PASSPHRASE: %GPG_PASSPHRASE%" -T .\k6.portable.%VERSION%.nupkg "https://%BINTRAY_USER%:%BINTRAY_KEY%@api.bintray.com/content/loadimpact/choco/k6.portable/%VERSION%/k6.portable.%VERSION%.nupkg?publish=1&override=1"'
- 'curl -H "X-GPG-PASSPHRASE: %GPG_PASSPHRASE%" -T .\k6.portable.%VERSION%.nupkg "https://%BINTRAY_USER%:%BINTRAY_KEY%@api.bintray.com/content/loadimpact/choco/k6.portable/%VERSION%/k6.portable.%VERSION%.nupkg?publish=1&override=1"'
34 changes: 25 additions & 9 deletions cmd/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package cmd

import (
"bytes"
"os"
"regexp"
"testing"

"io/ioutil"
Expand Down Expand Up @@ -120,6 +122,13 @@ export default function() {
`

func TestIntegrationConvertCmd(t *testing.T) {
var tmpFile, err = ioutil.TempFile("", "")
if err != nil {
t.Fatalf("Couldn't create temporary file: %s", err)
}
harFile := tmpFile.Name()
defer os.Remove(harFile)
tmpFile.Close()
t.Run("Correlate", func(t *testing.T) {
har, err := ioutil.ReadFile("testdata/example.har")
assert.NoError(t, err)
Expand All @@ -128,7 +137,8 @@ func TestIntegrationConvertCmd(t *testing.T) {
assert.NoError(t, err)

defaultFs = afero.NewMemMapFs()
err = afero.WriteFile(defaultFs, "/input.har", []byte(har), 0644)

err = afero.WriteFile(defaultFs, harFile, []byte(har), 0644)
assert.NoError(t, err)

buf := &bytes.Buffer{}
Expand All @@ -139,48 +149,54 @@ func TestIntegrationConvertCmd(t *testing.T) {
assert.NoError(t, convertCmd.Flags().Set("enable-status-code-checks", "true"))
assert.NoError(t, convertCmd.Flags().Set("return-on-failed-check", "true"))

err = convertCmd.RunE(convertCmd, []string{"/input.har"})
err = convertCmd.RunE(convertCmd, []string{harFile})

// reset the convertCmd to default flags. There must be a nicer and less error prone way to do this...
assert.NoError(t, convertCmd.Flags().Set("correlate", "false"))
assert.NoError(t, convertCmd.Flags().Set("no-batch", "false"))
assert.NoError(t, convertCmd.Flags().Set("enable-status-code-checks", "false"))
assert.NoError(t, convertCmd.Flags().Set("return-on-failed-check", "false"))

//Sanitizing to avoid windows problems with carriage returns
re := regexp.MustCompile(`\r`)
expected := re.ReplaceAllString(string(expectedTestPlan), ``)
result := re.ReplaceAllString(buf.String(), ``)

if assert.NoError(t, err) {
// assert.Equal suppresses the diff it is too big, so we add it as the test error message manually as well.
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(string(expectedTestPlan)),
B: difflib.SplitLines(buf.String()),
A: difflib.SplitLines(expected),
B: difflib.SplitLines(result),
FromFile: "Expected",
FromDate: "",
ToFile: "Actual",
ToDate: "",
Context: 1,
})
assert.Equal(t, string(expectedTestPlan), buf.String(), diff)

assert.Equal(t, expected, result, diff)
}
})
t.Run("Stdout", func(t *testing.T) {
defaultFs = afero.NewMemMapFs()
err := afero.WriteFile(defaultFs, "/input.har", []byte(testHAR), 0644)
err := afero.WriteFile(defaultFs, harFile, []byte(testHAR), 0644)
assert.NoError(t, err)

buf := &bytes.Buffer{}
defaultWriter = buf

err = convertCmd.RunE(convertCmd, []string{"/input.har"})
err = convertCmd.RunE(convertCmd, []string{harFile})
assert.NoError(t, err)
assert.Equal(t, testHARConvertResult, buf.String())
})
t.Run("Output file", func(t *testing.T) {
defaultFs = afero.NewMemMapFs()
err := afero.WriteFile(defaultFs, "/input.har", []byte(testHAR), 0644)
err := afero.WriteFile(defaultFs, harFile, []byte(testHAR), 0644)
assert.NoError(t, err)

err = convertCmd.Flags().Set("output", "/output.js")
assert.NoError(t, err)
err = convertCmd.RunE(convertCmd, []string{"/input.har"})
err = convertCmd.RunE(convertCmd, []string{harFile})
assert.NoError(t, err)

output, err := afero.ReadFile(defaultFs, "/output.js")
Expand Down
16 changes: 16 additions & 0 deletions cmd/runtime_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"bytes"
"fmt"
"os"
"runtime"
"strings"
"testing"

"github.com/loadimpact/k6/lib"
Expand All @@ -32,6 +34,12 @@ import (
"github.com/stretchr/testify/require"
)

var envVars []string

func init() {
envVars = os.Environ()
}

type EnvVarTest struct {
name string
useSysEnv bool // Whether to include the system env vars by default (run) or not (cloud/archive/inspect)
Expand Down Expand Up @@ -202,6 +210,14 @@ func TestEnvVars(t *testing.T) {
}
jsCode += "}"

// windows requires the environment variables to be loaded to gerenate the rand source
if runtime.GOOS == "windows" {
for _, e := range envVars {
parts := strings.Split(e, "=")
os.Setenv(parts[0], parts[1])
}
}

runner, err := newRunner(
&lib.SourceData{
Data: []byte(jsCode),
Expand Down
177 changes: 106 additions & 71 deletions js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"os"
"testing"

"gopkg.in/guregu/null.v3"
null "gopkg.in/guregu/null.v3"

"github.com/dop251/goja"
"github.com/loadimpact/k6/js/common"
Expand Down Expand Up @@ -128,81 +128,116 @@ func TestConsole(t *testing.T) {
}

func TestFileConsole(t *testing.T) {
logFile := "/tmp/loadtest.log"
levels := map[string]log.Level{
"log": log.InfoLevel,
"debug": log.DebugLevel,
"info": log.InfoLevel,
"warn": log.WarnLevel,
"error": log.ErrorLevel,
}
argsets := map[string]struct {
Message string
Data log.Fields
}{
`"string"`: {Message: "string"},
`"string","a","b"`: {Message: "string", Data: log.Fields{"0": "a", "1": "b"}},
`"string",1,2`: {Message: "string", Data: log.Fields{"0": "1", "1": "2"}},
`{}`: {Message: "[object Object]"},
}
var (
levels = map[string]log.Level{
"log": log.InfoLevel,
"debug": log.DebugLevel,
"info": log.InfoLevel,
"warn": log.WarnLevel,
"error": log.ErrorLevel,
}
argsets = map[string]struct {
Message string
Data log.Fields
}{
`"string"`: {Message: "string"},
`"string","a","b"`: {Message: "string", Data: log.Fields{"0": "a", "1": "b"}},
`"string",1,2`: {Message: "string", Data: log.Fields{"0": "1", "1": "2"}},
`{}`: {Message: "[object Object]"},
}
preExisting = map[string]bool{
"log exists": false,
"log doesn't exist": true,
}
preExistingText = "Prexisting file\n"
)
for name, level := range levels {
t.Run(name, func(t *testing.T) {
for args, result := range argsets {
t.Run(args, func(t *testing.T) {
r, err := New(&lib.SourceData{
Filename: "/script",
Data: []byte(fmt.Sprintf(
`export default function() { console.%s(%s); }`,
name, args,
)),
}, afero.NewMemMapFs(), lib.RuntimeOptions{})
assert.NoError(t, err)

err = r.SetOptions(lib.Options{
ConsoleOutput: null.StringFrom(logFile),
})
assert.NoError(t, err)

samples := make(chan stats.SampleContainer, 100)
vu, err := r.newVU(samples)
assert.NoError(t, err)

vu.Console.Logger.Level = log.DebugLevel
hook := logtest.NewLocal(vu.Console.Logger)

err = vu.RunOnce(context.Background())
assert.NoError(t, err)

// Test if the file was created.
_, err = os.Stat(logFile)
assert.NoError(t, err)

entry := hook.LastEntry()
if assert.NotNil(t, entry, "nothing logged") {
assert.Equal(t, level, entry.Level)
assert.Equal(t, result.Message, entry.Message)

data := result.Data
if data == nil {
data = make(log.Fields)
}
assert.Equal(t, data, entry.Data)

// Test if what we logged to the hook is the same as what we logged
// to the file.
entryStr, err := entry.String()
assert.NoError(t, err)

f, err := os.Open(logFile)
assert.NoError(t, err)

fileContent, err := ioutil.ReadAll(f)
assert.NoError(t, err)

assert.Equal(t, entryStr, string(fileContent))
// whether the file is existed before logging
for msg, deleteFile := range preExisting {
t.Run(msg, func(t *testing.T) {
var f, err = ioutil.TempFile("", "")
if err != nil {
t.Fatalf("Couldn't create temporary file for testing: %s", err)
}
var logFilename = f.Name()
defer os.Remove(logFilename)
// close it as we will want to reopen it and maybe remove it
if deleteFile {
f.Close()
if err := os.Remove(logFilename); err != nil {
t.Fatalf("Couldn't remove tempfile: %s", err)
}
} else {
// TODO: handle case where the string was no written in full ?
_, err := f.WriteString(preExistingText)
f.Close()
if err != nil {
t.Fatalf("Error while writing text to preexisting logfile: %s", err)
}

}
r, err := New(&lib.SourceData{
Filename: "/script",
Data: []byte(fmt.Sprintf(
`export default function() { console.%s(%s); }`,
name, args,
)),
}, afero.NewMemMapFs(), lib.RuntimeOptions{})
assert.NoError(t, err)

err = r.SetOptions(lib.Options{
ConsoleOutput: null.StringFrom(logFilename),
})
assert.NoError(t, err)

samples := make(chan stats.SampleContainer, 100)
vu, err := r.newVU(samples)
assert.NoError(t, err)

vu.Console.Logger.Level = log.DebugLevel
hook := logtest.NewLocal(vu.Console.Logger)

err = vu.RunOnce(context.Background())
assert.NoError(t, err)

// Test if the file was created.
_, err = os.Stat(logFilename)
assert.NoError(t, err)

entry := hook.LastEntry()
if assert.NotNil(t, entry, "nothing logged") {
assert.Equal(t, level, entry.Level)
assert.Equal(t, result.Message, entry.Message)

data := result.Data
if data == nil {
data = make(log.Fields)
}
assert.Equal(t, data, entry.Data)

// Test if what we logged to the hook is the same as what we logged
// to the file.
entryStr, err := entry.String()
assert.NoError(t, err)

f, err := os.Open(logFilename)
assert.NoError(t, err)

fileContent, err := ioutil.ReadAll(f)
assert.NoError(t, err)

var expectedStr = entryStr
if !deleteFile {
expectedStr = preExistingText + expectedStr
}
assert.Equal(t, expectedStr, string(fileContent))
}

})
}

os.Remove(logFile)
})
}
})
Expand Down
3 changes: 2 additions & 1 deletion js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package js

import (
"context"
"path/filepath"
"strings"

"github.com/dop251/goja"
Expand Down Expand Up @@ -62,7 +63,7 @@ func NewInitContext(rt *goja.Runtime, compiler *compiler.Compiler, ctxPtr *conte
compiler: compiler,
ctxPtr: ctxPtr,
fs: fs,
pwd: pwd,
pwd: filepath.ToSlash(pwd),

programs: make(map[string]programWithSource),
files: make(map[string][]byte),
Expand Down
7 changes: 4 additions & 3 deletions js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ func TestInitContextRequire(t *testing.T) {

t.Run("Files", func(t *testing.T) {
t.Run("Nonexistent", func(t *testing.T) {
path := filepath.FromSlash("/nonexistent.js")
_, err := getSimpleBundle("/script.js", `import "/nonexistent.js"; export default function() {}`)
assert.EqualError(t, err, "GoError: open /nonexistent.js: file does not exist")
assert.EqualError(t, err, fmt.Sprintf("GoError: open %s: file does not exist", path))
})
t.Run("Invalid", func(t *testing.T) {
fs := afero.NewMemMapFs()
Expand Down Expand Up @@ -322,12 +323,12 @@ func TestInitContextOpen(t *testing.T) {

t.Run("Nonexistent", func(t *testing.T) {
fs := afero.NewMemMapFs()

path := filepath.FromSlash("/nonexistent.txt")
_, err := NewBundle(&lib.SourceData{
Filename: "/script.js",
Data: []byte(`open("/nonexistent.txt"); export default function() {}`),
}, fs, lib.RuntimeOptions{})
assert.EqualError(t, err, "GoError: open /nonexistent.txt: file does not exist")
assert.EqualError(t, err, fmt.Sprintf("GoError: open %s: file does not exist", path))
})

}
Expand Down
Loading

0 comments on commit abe50b7

Please sign in to comment.