Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and handleSummaryResult() tests #1803

Merged
merged 6 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ a commandline interface for interacting with it.`,
TestRunDuration: executionState.GetCurrentTestRunDuration(),
})
if err == nil {
err = handleSummaryResult(afero.NewOsFs(), os.Stdout, os.Stderr, summaryResult)
err = handleSummaryResult(afero.NewOsFs(), stdout, stderr, summaryResult)
}
if err != nil {
logger.WithError(err).Error("failed to handle the end-of-test summary")
Expand Down
124 changes: 124 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2020 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package cmd

import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/loadimpact/k6/lib/fsext"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type mockWriter struct {
err error
errAfter int
}

func (fw mockWriter) Write(p []byte) (n int, err error) {
if fw.err != nil {
return fw.errAfter, fw.err
}
return len(p), nil
}

var _ io.Writer = mockWriter{}

func getFiles(t *testing.T, fs afero.Fs) map[string]*bytes.Buffer {
result := map[string]*bytes.Buffer{}
walkFn := func(filePath string, info os.FileInfo, err error) error {
if filePath == "/" {
return nil
}
require.NoError(t, err)
contents, err := afero.ReadFile(fs, filePath)
require.NoError(t, err)
result[filePath] = bytes.NewBuffer(contents)
return nil
}

err := fsext.Walk(fs, afero.FilePathSeparator, filepath.WalkFunc(walkFn))
require.NoError(t, err)

return result
}

func assertEqual(t *testing.T, exp string, actual io.Reader) {
act, err := ioutil.ReadAll(actual)
require.NoError(t, err)
assert.Equal(t, []byte(exp), act)
}

func initVars() (
content map[string]io.Reader, stdout *bytes.Buffer, stderr *bytes.Buffer, fs afero.Fs,
) {
fs = afero.NewMemMapFs()
if runtime.GOOS == "windows" {
fs = fsext.NewTrimFilePathSeparatorFs(fs)
}

return map[string]io.Reader{}, bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{}), fs
}

func TestHandleSummaryResultSimple(t *testing.T) {
t.Parallel()
content, stdout, stderr, fs := initVars()

// Test noop
assert.NoError(t, handleSummaryResult(fs, stdout, stderr, content))
require.Empty(t, getFiles(t, fs))
require.Empty(t, stdout.Bytes())
require.Empty(t, stderr.Bytes())

// Test stdout only
content["stdout"] = bytes.NewBufferString("some stdout summary")
assert.NoError(t, handleSummaryResult(fs, stdout, stderr, content))
require.Empty(t, getFiles(t, fs))
assertEqual(t, "some stdout summary", stdout)
require.Empty(t, stderr.Bytes())
}

func TestHandleSummaryResultError(t *testing.T) {
t.Parallel()
content, _, stderr, fs := initVars()

expErr := errors.New("test error")
stdout := mockWriter{err: expErr, errAfter: 10}

content["stdout"] = bytes.NewBufferString("some stdout summary")
content["stderr"] = bytes.NewBufferString("some stderr summary")
content["/path/file1"] = bytes.NewBufferString("file summary 1")
content["/path/file2"] = bytes.NewBufferString("file summary 2")
err := handleSummaryResult(fs, stdout, stderr, content)
assert.Error(t, err)
assert.Contains(t, err.Error(), expErr.Error())
files := getFiles(t, fs)
assertEqual(t, "file summary 1", files["/path/file1"])
assertEqual(t, "file summary 2", files["/path/file2"])
}
16 changes: 7 additions & 9 deletions js/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ import (
// TODO: move this to a separate JS file and use go.rice to embed it
const summaryWrapperLambdaCode = `
(function() {
var forEach = function(obj, callback) {
var forEach = function (obj, callback) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
callback(key, obj[key]);
if (callback(key, obj[key])) {
break;
}
na-- marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -88,9 +90,7 @@ const summaryWrapperLambdaCode = `
return JSON.stringify(results, null, 4);
};

var oldTextSummary = function(data) {
// TODO: implement something like the current end of test summary
};
// TODO: bundle the text summary generation from jslib and get rid of oldCallback

return function(exportedSummaryCallback, jsonSummaryPath, data, oldCallback) {
var result = {};
Expand All @@ -99,12 +99,10 @@ const summaryWrapperLambdaCode = `
result = exportedSummaryCallback(data, oldCallback);
} catch (e) {
console.error('handleSummary() failed with error "' + e + '", falling back to the default summary');
//result["stdout"] = oldTextSummary(data);
result["stdout"] = oldCallback(); // TODO: delete
result["stdout"] = oldCallback(); // TODO: replace with JS function
}
} else {
// result["stdout"] = oldTextSummary(data);
result["stdout"] = oldCallback(); // TODO: delete
result["stdout"] = oldCallback(); // TODO: replace with JS function
}

// TODO: ensure we're returning a map of strings or null/undefined...
Expand Down
2 changes: 2 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ var unitMap = map[string][]interface{}{
"us": {"µs", time.Microsecond},
}

// HumanizeValue makes the value human-readable
// TODO: get rid of this after we remove the Go-based summary
func (m *Metric) HumanizeValue(v float64, timeUnit string) string {
switch m.Type {
case Rate:
Expand Down