Skip to content

Commit

Permalink
use k6provider (#90)
Browse files Browse the repository at this point in the history
* use k6provider
* restructure tests to reuse k6build service
* enable analysis for cloud command

Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin authored Jan 29, 2025
1 parent e210570 commit 32d696c
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 285 deletions.
25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ k6exec is a launcher library for k6 with seamless use of extensions. The launche

k6exec is primarily used as a go library. In addition, it also contains a command-line tool, which is suitable for listing the dependencies of k6 test scripts.

The command line tool can be integrated into other command line tools as a subcommand. For this purpose, the library also contains the functionality of the command line tool as a factrory function that returns [cobra.Command](https://pkg.go.dev/github.com/spf13/cobra#Command).
The command line tool can be integrated into other command line tools as a subcommand. For this purpose, the library also contains the functionality of the command line tool as a factory function that returns [cobra.Command](https://pkg.go.dev/github.com/spf13/cobra#Command).

## Install

Expand Down Expand Up @@ -54,7 +54,7 @@ Since k6exec tries to emulate the `k6` command line, the `help` command or the `

### Prerequisites

k6exec tries to provide the appropriate k6 executable after detecting the extension dependencies. This can be done using a build service or a native builder.
k6exec tries to provide the appropriate k6 executable after detecting the extension dependencies using a build service.

#### Build Service

Expand All @@ -64,12 +64,6 @@ The build service URL can be specified in the `K6_BUILD_SERVICE_URL` environment

There is no default URL for the build service, otherwise k6exec will automatically provide k6 with the native builder.

#### Native Builder

To use the native builder, you only need to install the [Go language toolkit](https://go.dev/doc/install).

The native builder uses a k6 extension catalog to resolve extension URLs and versions. The extension catalog URL has a default value. A different extension catalog URL can be specified in the `K6_EXTENSION_CATALOG_URL` environment variable or by using the `--extension-catalog-url` flag.

### Dependencies

Dependencies can come from three sources: k6 test script, manifest file, `K6_DEPENDENCIES` environment variable. Instead of these three sources, a k6 archive can also be specified, which can contain all three sources.
Expand Down Expand Up @@ -137,14 +131,13 @@ k6exec [flags] [command]
### Flags

```
--build-service-url string URL of the k6 build service to be used
--extension-catalog-url string URL of the k6 extension catalog to be used
-h, --help help for k6
--no-color disable colored output
-q, --quiet disable progress updates
--usage print launcher usage
-v, --verbose enable verbose logging
--version version for k6
--build-service-url string URL of the k6 build service to be used
-h, --help help for k6
--no-color disable colored output
-q, --quiet disable progress updates
--usage print launcher usage
-v, --verbose enable verbose logging
--version version for k6
```

<!-- #endregion cli -->
Expand Down
2 changes: 1 addition & 1 deletion analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func scriptArg(args []string) (string, bool) {
}

cmd := args[0]
if cmd != "run" && cmd != "archive" && cmd != "inspect" {
if cmd != "run" && cmd != "archive" && cmd != "inspect" && cmd != "cloud" {
return "", false
}

Expand Down
9 changes: 0 additions & 9 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ func New(levelVar *slog.LevelVar) *cobra.Command {

flags := root.PersistentFlags()

flags.StringVar(
&state.extensionCatalogURL,
"extension-catalog-url",
state.extensionCatalogURL,
"URL of the k6 extension catalog to be used",
)
flags.StringVar(
&state.buildServiceURL,
"build-service-url",
Expand All @@ -67,13 +61,10 @@ func New(levelVar *slog.LevelVar) *cobra.Command {
flags.BoolVarP(&state.quiet, "quiet", "q", false, "disable progress updates")
flags.BoolVar(&state.nocolor, "no-color", false, "disable colored output")
flags.BoolVar(&state.usage, "usage", false, "print launcher usage")

root.InitDefaultHelpFlag()
root.Flags().Lookup("help").Usage = "help for k6"
root.Flags().BoolVar(&state.version, "version", false, "version for k6")

root.MarkFlagsMutuallyExclusive("extension-catalog-url", "build-service-url")

return root
}

Expand Down
8 changes: 1 addition & 7 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"
"log/slog"
"os"
"strings"
"testing"

"github.com/grafana/k6exec/cmd"
Expand All @@ -25,18 +24,13 @@ func TestNew(t *testing.T) { //nolint:paralleltest

flags := c.PersistentFlags()

require.NotNil(t, flags.Lookup("extension-catalog-url"))
require.NotNil(t, flags.Lookup("build-service-url"))
require.NotNil(t, flags.Lookup("verbose"))
require.NotNil(t, flags.Lookup("quiet"))
require.NotNil(t, flags.Lookup("no-color"))

out := captureStdout(t, func() { require.NoError(t, c.Execute()) })

require.True(t, strings.Contains(out, " k6"))
}

//nolint:forbidigo
//nolint:forbidigo,unused
func captureStdout(t *testing.T, fn func()) string {
t.Helper()

Expand Down
6 changes: 0 additions & 6 deletions cmd/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ The build service URL can be specified in the `K6_BUILD_SERVICE_URL` environment

There is no default URL for the build service, otherwise k6exec will automatically provide k6 with the native builder.

#### Native Builder

To use the native builder, you only need to install the [Go language toolkit](https://go.dev/doc/install).

The native builder uses a k6 extension catalog to resolve extension URLs and versions. The extension catalog URL has a default value. A different extension catalog URL can be specified in the `K6_EXTENSION_CATALOG_URL` environment variable or by using the `--extension-catalog-url` flag.

### Dependencies

Dependencies can come from three sources: k6 test script, manifest file, `K6_DEPENDENCIES` environment variable. Instead of these three sources, a k6 archive can also be specified, which can contain all three sources.
Expand Down
4 changes: 2 additions & 2 deletions cmd/k6exec/main_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func Test_newCmd(t *testing.T) { //nolint:paralleltest

lvar := new(slog.LevelVar)

// TODO: add more assertions and more test cases
cmd := newCmd([]string{"run", abs}, lvar)

require.NoError(t, cmd.Execute())
require.Equal(t, "k6exec", cmd.Name())
}

func Test_initLogging(t *testing.T) { //nolint:paralleltest
Expand Down
51 changes: 14 additions & 37 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"log/slog"
"net/url"
"os"
"os/exec"

Expand All @@ -13,54 +12,28 @@ import (

type state struct {
k6exec.Options
buildServiceURL string
extensionCatalogURL string
verbose bool
quiet bool
nocolor bool
version bool
usage bool
levelVar *slog.LevelVar

cmd *exec.Cmd

cleanup func() error
buildServiceURL string
verbose bool
quiet bool
nocolor bool
version bool
usage bool
levelVar *slog.LevelVar
cmd *exec.Cmd
cleanup func() error
}

//nolint:forbidigo
func newState(levelVar *slog.LevelVar) *state {
s := new(state)

s.levelVar = levelVar

if value, found := os.LookupEnv("K6_BUILD_SERVICE_URL"); found {
s.buildServiceURL = value
}

if value, found := os.LookupEnv("K6_EXTENSION_CATALOG_URL"); found {
s.extensionCatalogURL = value
}

return s
}

func (s *state) persistentPreRunE(_ *cobra.Command, _ []string) error {
if len(s.buildServiceURL) > 0 {
val, err := url.Parse(s.buildServiceURL)
if err != nil {
return err
}

s.Options.BuildServiceURL = val
}

if len(s.extensionCatalogURL) > 0 {
val, err := url.Parse(s.extensionCatalogURL)
if err != nil {
return err
}

s.Options.ExtensionCatalogURL = val
s.Options.BuildServiceURL = s.buildServiceURL
}

if s.verbose && s.levelVar != nil {
Expand Down Expand Up @@ -122,6 +95,7 @@ func (s *state) preRunE(sub *cobra.Command, args []string) error {
func (s *state) runE(_ *cobra.Command, _ []string) error {
var err error

// FIXME: I think this code is not setting the error to the cleanup function (pablochacin)
defer func() {
e := s.cleanup()
if err == nil {
Expand All @@ -138,6 +112,9 @@ func (s *state) helpFunc(cmd *cobra.Command, args []string) {
err := s.preRunE(cmd, append(args, "-h"))
if err != nil {
cmd.PrintErr(err)
// FIXME: added this return because in case of error provisioning the binary,
// it doesn't make sense to continue (pablochacin)
return
}

err = s.runE(cmd, args)
Expand Down
Loading

0 comments on commit 32d696c

Please sign in to comment.