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

Move cloud run and login functionalities under cloud subcommands #3813

Merged
merged 11 commits into from
Jul 24, 2024
Prev Previous commit
Next Next commit
Apply Pull-Request suggestions
Co-authored-by: Joan López de la Franca Beltran <[email protected]>
  • Loading branch information
oleiade and joanlopez committed Jul 23, 2024
commit 231f50e35eb566308a3ffc9bd9460ac08d8c6d1e
49 changes: 19 additions & 30 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type cmdCloud struct {
uploadOnly bool
}

//nolint:dupl // remove this statement once the migration from the `k6 cloud` to the `k6 cloud run` is complete.
//nolint:dupl // function duplicated from the deprecated `k6 cloud` command, stmt can go when the command is remove
func (c *cmdCloud) preRun(cmd *cobra.Command, _ []string) error {
// TODO: refactor (https://github.com/loadimpact/k6/issues/883)
//
Expand Down Expand Up @@ -344,33 +344,34 @@ func getCmdCloud(gs *state.GlobalState) *cobra.Command {
}

exampleText := getExampleText(gs, `
# Authenticate with Grafana k6 Cloud
# Authenticate with Grafana Cloud k6
$ {{.}} cloud login

# Run a k6 script in the Grafana k6 cloud
# Run a k6 script in the Grafana Cloud k6
$ {{.}} cloud run script.js

# Run a k6 archive in the Grafana k6 cloud
# Run a k6 archive in the Grafana Cloud k6
$ {{.}} cloud run archive.tar

# [deprecated] Run a k6 script in the Grafana k6 cloud
# [deprecated] Run a k6 script in the Grafana Cloud k6
$ {{.}} cloud script.js

# [deprecated] Run a k6 archive in the Grafana k6 cloud
# [deprecated] Run a k6 archive in the Grafana Cloud k6
$ {{.}} cloud archive.tar`[1:])

cloudCmd := &cobra.Command{
Use: "cloud",
Short: "Run a test on the cloud",
Long: `[deprecation notice]
The k6 team is in the process of modifying and deprecating the cloud command behavior. In the future, the "cloud"
command will only display a help text, instead of running tests in the cloud.
To run tests in the cloud, users are now invited to migrate to the "k6 cloud run" command instead.
Long: `Run a test archive in the Grafana Cloud k6.
oleiade marked this conversation as resolved.
Show resolved Hide resolved

Run a test on the cloud.
This will execute the test in the Grafana Cloud k6 service. Be sure to run the "k6 cloud login" command prior to
oleiade marked this conversation as resolved.
Show resolved Hide resolved
authenticate with Grafana Cloud k6.`,
Args: exactCloudArgs(),
Deprecated: `the k6 team is in the process of modifying and deprecating the "k6 cloud" command behavior. In the future, the "cloud"
command will only display a help text, instead of running tests in the Grafana Cloud k6.

This will execute the test on the k6 cloud service. Use "k6 login cloud" to authenticate.`,
Args: exactCloudArgs(),
To run tests in the cloud, users are now invited to migrate to the "k6 cloud run" command instead.
`,
PreRunE: c.preRun,
RunE: c.run,
Example: exampleText,
Expand All @@ -388,23 +389,11 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth

func exactCloudArgs() cobra.PositionalArgs {
return func(_ *cobra.Command, args []string) error {
if len(args) < 1 || len(args) > 2 {
return fmt.Errorf("accepts 1 or 2 arg(s), received %d", len(args))
}

var (
isRunSubcommand = args[0] == "run"
isLoginSubcommand = args[0] == "login"
isScript = filepath.Ext(args[0]) == ".js"
isArchive = filepath.Ext(args[0]) == ".tar"
)

if len(args) == 1 && !isScript && !isArchive {
return fmt.Errorf("unexpected argument: %s", args[0])
}

if len(args) == 2 && !isRunSubcommand && !isLoginSubcommand {
return fmt.Errorf("unexpected argument: %s", args[0])
if len(args) == 0 {
return fmt.Errorf(
"the k6 cloud command accepts 1 argument consisting in either in "+
"a subcommand such as `run` or `cloud`, or the path to a script/archive, or "+
"the `-` symbol, received: %d arguments instead", len(args))
oleiade marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
Expand Down
15 changes: 9 additions & 6 deletions cmd/cloud_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ func getCmdCloudLogin(gs *state.GlobalState) *cobra.Command {
{{.}} cloud login -t <YOUR_TOKEN>

# Display the stored token.
{{.}} cloud login -s`[1:])
{{.}} cloud login -s

# Reset the stored token.
{{.}} cloud login -r`[1:])

loginCloudCommand := &cobra.Command{
Use: cloudLoginCommandName,
Short: "Authenticate with Grafana k6 Cloud",
Short: "Authenticate with Grafana Cloud k6",
Long: `Authenticate with Grafana Cloud k6.

This command will authenticate you with Grafana Cloud k6. Once authenticated,
Once authenticated you can start running tests in the cloud by using the "k6 cloud"
This command will authenticate you with Grafana Cloud k6.
Once authenticated you can start running tests in the cloud by using the "k6 cloud run"
command, or by executing a test locally and outputting samples to the cloud using
the "k6 run -o cloud" command.
`,
Expand All @@ -56,7 +59,7 @@ the "k6 run -o cloud" command.

loginCloudCommand.Flags().StringP("token", "t", "", "specify `token` to use")
loginCloudCommand.Flags().BoolP("show", "s", false, "display saved token and exit")
loginCloudCommand.Flags().BoolP("reset", "r", false, "reset token")
loginCloudCommand.Flags().BoolP("reset", "r", false, "reset stored token")

return loginCloudCommand
}
Expand Down Expand Up @@ -144,7 +147,7 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {

if res.Token == "" {
return errors.New("your account does not appear to have an active API token, please consult the " +
"Grafana k6 cloud documentation for instructions on how to generate " +
"Grafana Cloud k6 documentation for instructions on how to generate " +
"one: https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication")
}

Expand Down
29 changes: 17 additions & 12 deletions cmd/cloud_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"go.k6.io/k6/ui/pb"
)

// cmdCloudRun handles the `k6 cloud` sub-command
// cmdCloudRun handles the `k6 cloud run` sub-command
type cmdCloudRun struct {
gs *state.GlobalState

Expand All @@ -42,22 +42,27 @@ func getCmdCloudRun(gs *state.GlobalState) *cobra.Command {
}

exampleText := getExampleText(gs, `
# Run a test in the Grafana k6 cloud
# Run a test script in Grafana Cloud k6
$ {{.}} cloud run script.js

# Run a test in the Grafana k6 cloud with a specific token
$ {{.}} cloud run --token <YOUR_API_TOKEN> script.js`[1:])
# Run a test archive in Grafana Cloud k6
$ {{.}} cloud run archive.tar

# Read a test script or archive from stdin and run it in Grafana Cloud k6
$ {{.}} cloud run - < script.js`[1:])

// FIXME: when the command is "k6 cloud run" without an script/archive, we should display an error and the help
cloudRunCmd := &cobra.Command{
Use: cloudRunCommandName,
Short: "Run a test in the Grafana k6 cloud",
Long: `Run a test in the Grafana k6 cloud.
Short: "Run a test in Grafana Cloud k6",
Long: `Run a test in Grafana Cloud k6.

This will execute the test in the Grafana k6 cloud service. Using this command requires to be authenticated
against the Grafana k6 cloud. Use the "k6 cloud login" command to authenticate.`,
This will execute the test in the Grafana Cloud k6 service. Using this command requires to be authenticated
oleiade marked this conversation as resolved.
Show resolved Hide resolved
against Grafana Cloud k6. Use the "k6 cloud login" command to authenticate.`,
Example: exampleText,
Args: exactArgsWithMsg(1, "arg should either be \"-\", if reading script from stdin, or a path to a script file"),
Args: exactArgsWithMsg(1,
"the k6 cloud run command expects a single argument consisting in either a path to a script or "+
"archive file, or the \"-\" symbol indicating the script or archive should be read from stdin",
),
PreRunE: c.preRun,
RunE: c.run,
}
Expand All @@ -68,7 +73,7 @@ against the Grafana k6 cloud. Use the "k6 cloud login" command to authenticate.`
return cloudRunCmd
}

//nolint:dupl // remove this statement once the migration from the `k6 cloud` to the `k6 cloud run` is complete.
//nolint:dupl // function duplicated from the deprecated `k6 cloud` command, stmt can go when the command is remove
func (c *cmdCloudRun) preRun(cmd *cobra.Command, _ []string) error {
// TODO: refactor (https://github.com/loadimpact/k6/issues/883)
//
Expand Down Expand Up @@ -154,7 +159,7 @@ func (c *cmdCloudRun) run(cmd *cobra.Command, args []string) error {
}
if !cloudConfig.Token.Valid {
return errors.New( //nolint:golint
"not logged in, please login to the Grafana k6 Cloud " +
"not logged in, please login to the Grafana Cloud k6 " +
"using the `k6 cloud login` command",
)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Authenticate with a service.
Logging into a service changes the default when just "-o [type]" is passed with
no parameters, you can always override the stored credentials by passing some
on the commandline.`,
Deprecated: `This command is deprecated and will be removed in a future release.
Please use the "k6 cloud login" command instead.
`,
oleiade marked this conversation as resolved.
Show resolved Hide resolved
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Usage()
},
Expand Down
14 changes: 0 additions & 14 deletions cmd/tests/cmd_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,3 @@ func TestCloudWithArchive(t *testing.T) {
assert.Contains(t, stdout, `output: https://app.k6.io/runs/123`)
assert.Contains(t, stdout, `test status: Finished`)
}

// FIXME: This test fails because our test setup leaves stdout empty
//func TestCloudArgs(t *testing.T) {
// t.Parallel()
//
// // ts := NewGlobalTestState(t)
// ts := getSimpleCloudTestState(t, nil, nil, nil, nil)
// ts.CmdArgs = []string{"k6", "cloud", "run"}
// ts.ExpectedExitCode = -1
// cmd.ExecuteWithGlobalState(ts.GlobalState)
//
// stdout := ts.Stdout.String()
// assert.Contains(t, stdout, `accepts 1 or 2 arg(s), received 0`)
//}