Skip to content

Commit

Permalink
Move integration tests from cmd to cmd/tests
Browse files Browse the repository at this point in the history
See #2459
  • Loading branch information
Ivan Mirić committed Jan 17, 2023
1 parent 0f93267 commit 2378549
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 244 deletions.
8 changes: 4 additions & 4 deletions cmd/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/errext/exitcodes"
)

Expand Down Expand Up @@ -79,7 +79,7 @@ func TestArchiveThresholds(t *testing.T) {
testScript, err := ioutil.ReadFile(testCase.testFilename)
require.NoError(t, err)

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, testCase.testFilename), testScript, 0o644))
ts.CmdArgs = []string{"k6", "archive", testCase.testFilename}
if testCase.noThresholds {
Expand All @@ -100,7 +100,7 @@ func TestArchiveContainsEnv(t *testing.T) {
// given some script that will be archived
fileName := "script.js"
testScript := []byte(`export default function () {}`)
ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644))

// when we do archiving and passing the `--env` flags
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestArchiveNotContainsEnv(t *testing.T) {
// given some script that will be archived
fileName := "script.js"
testScript := []byte(`export default function () {}`)
ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644))

// when we do archiving and passing the `--env` flags altogether with `--exclude-env-vars` flag
Expand Down
3 changes: 2 additions & 1 deletion cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/cmd/state"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/executor"
"go.k6.io/k6/lib/types"
Expand Down Expand Up @@ -486,7 +487,7 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
func runTestCase(t *testing.T, testCase configConsolidationTestCase, subCmd string) {
t.Logf("Test for `k6 %s` with opts=%#v and exp=%#v\n", subCmd, testCase.options, testCase.expected)

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
ts.CmdArgs = append([]string{"k6", subCmd}, testCase.options.cli...)
ts.Env = state.BuildEnvMap(testCase.options.env)
if testCase.options.fs != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/cmd/tests"
)

const testHAR = `
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestConvertCmdCorrelate(t *testing.T) {
expectedTestPlan, err := ioutil.ReadFile("testdata/example.js")
require.NoError(t, err)

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, "correlate.har", har, 0o644))
ts.CmdArgs = []string{
"k6", "convert", "--output=result.js", "--correlate=true", "--no-batch=true",
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestConvertCmdCorrelate(t *testing.T) {

func TestConvertCmdStdout(t *testing.T) {
t.Parallel()
ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, "stdout.har", []byte(testHAR), 0o644))
ts.CmdArgs = []string{"k6", "convert", "stdout.har"}

Expand All @@ -154,7 +154,7 @@ func TestConvertCmdStdout(t *testing.T) {
func TestConvertCmdOutputFile(t *testing.T) {
t.Parallel()

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, "output.har", []byte(testHAR), 0o644))
ts.CmdArgs = []string{"k6", "convert", "--output", "result.js", "output.har"}

Expand Down
4 changes: 2 additions & 2 deletions cmd/panic_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib/testutils"
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestRunScriptPanicsErrorsAndAbort(t *testing.T) {
t.Parallel()

testFilename := "script.js"
ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, testFilename), []byte(tc.testScript), 0o644))
ts.CmdArgs = []string{"k6", "run", testFilename}

Expand Down
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func Execute() {
newRootCommand(gs).execute()
}

// ExecuteWithGlobalState runs the root command with an existing GlobalState.
// This is needed by integration tests, and we don't want to modify the
// Execute() signature to avoid breaking k6 extensions.
func ExecuteWithGlobalState(gs *state.GlobalState) {
newRootCommand(gs).execute()
}

func (c *rootCommand) waitRemoteLogger() {
if c.loggerIsRemote {
select {
Expand Down
58 changes: 3 additions & 55 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,22 @@ package cmd

import (
"bytes"
"fmt"
"net/http"
"os"
"sync/atomic"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/lib/testutils"
"go.uber.org/goleak"
)

type blockingTransport struct {
fallback http.RoundTripper
forbiddenHosts map[string]bool
counter uint32
}

func (bt *blockingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
host := req.URL.Hostname()
if bt.forbiddenHosts[host] {
atomic.AddUint32(&bt.counter, 1)
panic(fmt.Errorf("trying to make forbidden request to %s during test", host))
}
return bt.fallback.RoundTrip(req)
}

func TestMain(m *testing.M) {
exitCode := 1 // error out by default
defer func() {
os.Exit(exitCode)
}()

bt := &blockingTransport{
fallback: http.DefaultTransport,
forbiddenHosts: map[string]bool{
"ingest.k6.io": true,
"cloudlogs.k6.io": true,
"app.k6.io": true,
"reports.k6.io": true,
},
}
http.DefaultTransport = bt
defer func() {
if bt.counter > 0 {
fmt.Printf("Expected blocking transport count to be 0 but was %d\n", bt.counter) //nolint:forbidigo
exitCode = 2
}
}()

defer func() {
// TODO: figure out why logrus' `Entry.WriterLevel` goroutine sticks
// around and remove this exception.
opt := goleak.IgnoreTopFunction("io.(*pipe).read")
if err := goleak.Find(opt); err != nil {
fmt.Println(err) //nolint:forbidigo
exitCode = 3
}
}()

exitCode = m.Run()
tests.Main(m)
}

func TestDeprecatedOptionWarning(t *testing.T) {
t.Parallel()

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "--logformat", "json", "run", "-"}
ts.Stdin = bytes.NewBuffer([]byte(`
console.log('foo');
Expand Down
8 changes: 4 additions & 4 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.k6.io/k6/cmd/state"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/fsext"
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestRunScriptErrorsAndAbort(t *testing.T) {
testScript, err := ioutil.ReadFile(path.Join("testdata", tc.testFilename))
require.NoError(t, err)

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
ts.CmdArgs = append([]string{"k6", "run", tc.testFilename}, tc.extraArgs...)

Expand Down Expand Up @@ -256,7 +256,7 @@ func TestInvalidOptionsThresholdErrExitCode(t *testing.T) {
testScript, err := ioutil.ReadFile(path.Join("testdata", tc.testFilename))
require.NoError(t, err)

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
ts.CmdArgs = append([]string{"k6", "run", tc.testFilename}, tc.extraArgs...)

Expand Down Expand Up @@ -306,7 +306,7 @@ func TestThresholdsRuntimeBehavior(t *testing.T) {
testScript, err := ioutil.ReadFile(path.Join("testdata", tc.testFilename))
require.NoError(t, err)

ts := state.NewGlobalTestState(t)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))

ts.CmdArgs = []string{"k6", "run", tc.testFilename}
Expand Down
4 changes: 2 additions & 2 deletions cmd/runtime_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/cmd/state"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/lib"
"go.k6.io/k6/loader"
"go.k6.io/k6/metrics"
Expand Down Expand Up @@ -59,7 +59,7 @@ func testRuntimeOptionsCase(t *testing.T, tc runtimeOptionsTestCase) {
fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "/script.js", jsCode.Bytes(), 0o644))

ts := state.NewGlobalTestState(t) // TODO: move upwards, make this into an almost full integration test
ts := tests.NewGlobalTestState(t) // TODO: move upwards, make this into an almost full integration test
registry := metrics.NewRegistry()
test := &loadedTest{
sourceRootPath: "script.js",
Expand Down
Loading

0 comments on commit 2378549

Please sign in to comment.