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

Add k6 new subcommand #3394

Merged
merged 15 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*.syso
.idea
*.DS_Store
# Excluding as it is the file generated by the k6 init command
script.js
andrewslotin marked this conversation as resolved.
Show resolved Hide resolved

/versioninfo.json

Expand Down
169 changes: 169 additions & 0 deletions cmd/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package cmd

import (
"fmt"
"path"
"text/template"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/lib/fsext"
)

const defaultNewScriptName = "script.js"

//nolint:gochecknoglobals
var defaultNewScriptTemplate = template.Must(template.New("new").Parse(`import http from 'k6/http';
import { sleep } from 'k6';

export const options = {
// A number specifying the number of VUs to run concurrently.
vus: 10,
// A string specifying the total duration of the test run.
duration: '30s',

// The following section contains configuration options for execution of this
// test script in Grafana Cloud.
//
// See https://grafana.com/docs/grafana-cloud/k6/get-started/run-cloud-tests-from-the-cli/
// to learn about authoring and running k6 test scripts in Grafana k6 Cloud.
//
// ext: {
// loadimpact: {
// // The ID of the project to which the test is assigned in the k6 Cloud UI.
// // By default tests are executed in default project.
// projectID: "",
// // The name of the test in the k6 Cloud UI.
// // Test runs with the same name will be grouped.
// name: "{{ .ScriptName }}"
// }
// },

// Uncomment this section to enable the use of Browser API in your tests.
//
// See https://grafana.com/docs/k6/latest/using-k6-browser/running-browser-tests/ to learn more
// about using Browser API in your test scripts.
//
// scenarios: {
// // The scenario name appears in the result summary, tags, and so on.
// // You can give the scenario any name, as long as each name in the script is unique.
// ui: {
// // Executor is a mandatory parameter for browser-based tests.
// // Shared iterations in this case tells k6 to reuse VUs to execute iterations.
// //
// // See https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/ for other executor types.
// executor: 'shared-iterations',
// options: {
// browser: {
// // This is a mandatory parameter that instructs k6 to launch and
// // connect to a chromium-based browser, and use it to run UI-based
// // tests.
// type: 'chromium',
// },
// },
// },
// }
};

// The function that defines VU logic.
//
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
// about authoring k6 scripts.
//
export default function() {
http.get('https://test.k6.io');
sleep(1);
}
`))

type initScriptTemplateArgs struct {
ScriptName string
}

// newScriptCmd represents the `k6 new` command
type newScriptCmd struct {
gs *state.GlobalState
overwriteFiles bool
}

func (c *newScriptCmd) flagSet() *pflag.FlagSet {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SortFlags = false
flags.BoolVarP(&c.overwriteFiles, "force", "f", false, "Overwrite existing files")

return flags
}

func (c *newScriptCmd) run(cmd *cobra.Command, args []string) error { //nolint:revive
target := defaultNewScriptName
if len(args) > 0 {
target = args[0]
}

fileExists, err := fsext.Exists(c.gs.FS, target)
if err != nil {
return err
}

if fileExists && !c.overwriteFiles {
return fmt.Errorf("%s already exists, please use the `--force` flag if you want overwrite it", target)
}

fd, err := c.gs.FS.Create(target)
if err != nil {
return err
}
defer func() {
_ = fd.Close() // we may think to check the error and log
}()

if err := defaultNewScriptTemplate.Execute(fd, initScriptTemplateArgs{
ScriptName: path.Base(target),
}); err != nil {
return err
}

valueColor := getColor(c.gs.Flags.NoColor || !c.gs.Stdout.IsTTY, color.Bold)
printToStdout(c.gs, fmt.Sprintf(
"Initialized a new k6 test script in %s. You can now execute it by running `%s run %s`.\n",
valueColor.Sprint(target),
c.gs.BinaryName,
target,
))

return nil
}

func getCmdNewScript(gs *state.GlobalState) *cobra.Command {
c := &newScriptCmd{gs: gs}

exampleText := getExampleText(gs, `
# Create a minimal k6 script in the current directory. By default, k6 creates script.js.
{{.}} new

# Create a minimal k6 script in the current directory and store it in test.js
{{.}} new test.js

# Overwrite existing test.js with a minimal k6 script
{{.}} new -f test.js`[1:])

initCmd := &cobra.Command{
Use: "new",
Short: "Create and initialize a new k6 script",
Long: `Create and initialize a new k6 script.

This command will create a minimal k6 script in the current directory and
store it in the file specified by the first argument. If no argument is
provided, the script will be stored in script.js.

This command will not overwrite existing files.`,
Example: exampleText,
Args: cobra.MaximumNArgs(1),
RunE: c.run,
}
initCmd.Flags().AddFlagSet(c.flagSet())

return initCmd
}
97 changes: 97 additions & 0 deletions cmd/new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/lib/fsext"
)

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

testCases := []struct {
name string
scriptNameArg string
expectedCloudName string
expectedFilePath string
}{
{
name: "default script name",
expectedCloudName: "script.js",
expectedFilePath: defaultNewScriptName,
},
{
name: "user-specified script name",
scriptNameArg: "mytest.js",
expectedCloudName: "mytest.js",
expectedFilePath: "mytest.js",
},
{
name: "script outside of current working directory",
scriptNameArg: "../mytest.js",
expectedCloudName: "mytest.js",
expectedFilePath: "../mytest.js",
},
}

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

ts := tests.NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "new"}
if testCase.scriptNameArg != "" {
ts.CmdArgs = append(ts.CmdArgs, testCase.scriptNameArg)
}

newRootCommand(ts.GlobalState).execute()

data, err := fsext.ReadFile(ts.FS, testCase.expectedFilePath)
require.NoError(t, err)

jsData := string(data)
assert.Contains(t, jsData, "export const options = {")
assert.Contains(t, jsData, fmt.Sprintf(`name: "%s"`, testCase.expectedCloudName))
assert.Contains(t, jsData, "export default function() {")
})
}
}

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

ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, defaultNewScriptName, []byte("untouched"), 0o644))

ts.CmdArgs = []string{"k6", "new"}
ts.ExpectedExitCode = -1

newRootCommand(ts.GlobalState).execute()

data, err := fsext.ReadFile(ts.FS, defaultNewScriptName)
require.NoError(t, err)

assert.Contains(t, string(data), "untouched")
}

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

ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, defaultNewScriptName, []byte("untouched"), 0o644))

ts.CmdArgs = []string{"k6", "new", "-f"}

newRootCommand(ts.GlobalState).execute()

data, err := fsext.ReadFile(ts.FS, defaultNewScriptName)
require.NoError(t, err)

assert.Contains(t, string(data), "export const options = {")
assert.Contains(t, string(data), "export default function() {")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newRootCommand(gs *state.GlobalState) *rootCommand {
rootCmd.SetIn(gs.Stdin)

subCommands := []func(*state.GlobalState) *cobra.Command{
getCmdArchive, getCmdCloud, getCmdInspect,
getCmdArchive, getCmdCloud, getCmdNewScript, getCmdInspect,
getCmdLogin, getCmdPause, getCmdResume, getCmdScale, getCmdRun,
getCmdStats, getCmdStatus, getCmdVersion,
}
Expand Down