k6provision

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2025 License: AGPL-3.0 Imports: 19 Imported by: 1

README

GitHub Release Go Reference Go Report Card GitHub Actions codecov GitHub Downloads

k6provision

Provision k6 with extensions

The purpose of k6provision is to provision k6 executables with extensions based on dependencies.

k6provision is primarily used as a go library. In addition, it also contains a command-line tool, which is suitable for provisioning k6 executable based on 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.

Install

Precompiled binaries can be downloaded and installed from the Releases page.

If you have a go development environment, the installation can also be done with the following command:

go install github.com/grafana/k6provision/cmd/k6provision@latest

Usage

Using provisioning is extremely easy. You simply need to pass in the dependencies (name, version constraints pairs) and the filename where you want to save the k6 executable. That's it.

package k6provision_test

import (
	"bytes"
	"context"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/grafana/k6deps"
	"github.com/grafana/k6provision"
)

const depsStr = `k6=0.56.0;k6/x/faker=0.4.1;k6/x/sql=1.0.1;k6/x/sql/driver/ramsql=0.1.0`

func ExampleProvision() {
	deps := k6deps.Dependencies{}
	_ = deps.UnmarshalText([]byte(depsStr))

	exe, _ := filepath.Abs(k6provision.ExeName) // k6 or k6.exe

	_ = k6provision.Provision(context.TODO(), deps, exe, nil)

	cmd := exec.Command(exe, "version") //nolint

	out, _ := cmd.CombinedOutput()

	_, _ = os.Stdout.Write(out[bytes.Index(out, []byte("Extensions:")):]) //nolint

	// Output:
	// Extensions:
	//   github.com/grafana/xk6-faker v0.4.1, k6/x/faker [js]
	//   github.com/grafana/xk6-sql v1.0.1, k6/x/sql [js]
	//   github.com/grafana/xk6-sql v1.0.1, k6/x/sql/driver/ramsql [js]
}

CLI

k6provision

Provision k6 with extensions.

Synopsis

Analyze the k6 test script and provision k6 with extensions based on dependencies.

Sources

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.

Output

By default, the k6 executable is created in the current directory with the name k6 (k6.exe on Windows). This can be overridden by using the --o/-output flag.

k6provision [flags] [script-file]

Flags

  -o, --output string                  output file (default "k6")
  -e, --env string                     environment variable to analyze (default "K6_DEPENDENCIES")
      --manifest string                manifest file to analyze (default the 'package.json' nearest to the script-file)
      --ingnore-env                    ignore K6_DEPENDENCIES environment variable processing
      --ignore-manifest                disable package.json detection and processing
      --extension-catalog-url string   URL of the k6 extension catalog to be used (default "https://registry.k6.io/catalog.json")
      --build-service-url string       URL of the k6 build service to be used
  -h, --help                           help for k6provision

Contribute

If you want to contribute or help with the development of k6provision, start by reading CONTRIBUTING.md.

Documentation

Overview

Package k6provision contains a k6 binary provisioning library. The purpose of the k6provision library is to provision k6 executables with extensions based on dependencies.

Index

Examples

Constants

View Source
const DefaultExtensionCatalogURL = "https://registry.k6.io/catalog.json"

DefaultExtensionCatalogURL contains the address of the default k6 extension catalog.

View Source
const ExeName = k6Exe

ExeName contains the name of the k6 executable. Its value is "k6.exe" for Windows, otherwise "k6".

Variables

View Source
var (
	// ErrDownload is returned if an error occurs during download.
	ErrDownload = errors.New("download error")
	// ErrBuild is returned if an error occurs during build.
	ErrBuild = errors.New("build error")
	// ErrCache is returned if an error occurs during cache handling.
	ErrCache = errors.New("cache error")
)

Functions

func Provision

func Provision(ctx context.Context, deps k6deps.Dependencies, dest string, opts *Options) error

Provision provisions a k6 executable with extensions based on dependencies.

Example
package main

import (
	"bytes"
	"context"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/grafana/k6deps"
	"github.com/grafana/k6provision"
)

const depsStr = `k6=0.56.0;k6/x/faker=0.4.1;k6/x/sql=1.0.1;k6/x/sql/driver/ramsql=0.1.0`

func main() {
	deps := k6deps.Dependencies{}
	_ = deps.UnmarshalText([]byte(depsStr))

	exe, _ := filepath.Abs(k6provision.ExeName) // k6 or k6.exe

	_ = k6provision.Provision(context.TODO(), deps, exe, nil)

	cmd := exec.Command(exe, "version") //nolint

	out, _ := cmd.CombinedOutput()

	_, _ = os.Stdout.Write(out[bytes.Index(out, []byte("Extensions:")):]) //nolint

}
Output:
Extensions:
  github.com/grafana/xk6-faker v0.4.1, k6/x/faker [js]
  github.com/grafana/xk6-sql v1.0.1, k6/x/sql [js]
  github.com/grafana/xk6-sql v1.0.1, k6/x/sql/driver/ramsql [js]

Types

type Options

type Options struct {
	// AppName contains the name of the application. It is used to define the default value of CacheDir.
	// If empty, it defaults to os.Args[0].
	AppName string
	// CacheDir specifies the name of the directory where the cacheable files can be cached.
	// Its default is determined based on the XDG Base Directory Specification.
	// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
	CacheDir string
	// Client is used during HTTP communication with the build service.
	// If absent, http.DefaultClient will be used.
	Client *http.Client
	// ExtensionCatalogURL contains the URL of the k6 extension catalog to be used.
	// If absent, DefaultExtensionCatalogURL will be used.
	ExtensionCatalogURL *url.URL
	// BuildServiceURL contains the URL of the k6 build service to be used.
	// If the value is not nil, the k6 binary is built using the build service instead of the local build.
	BuildServiceURL *url.URL
}

Options contains the optional parameters of the Command function.

Directories

Path Synopsis
cmd
Package cmd contains k6provision cobra command factory function.
Package cmd contains k6provision cobra command factory function.
k6provision command
Package main contains the main function for k6deps CLI tool.
Package main contains the main function for k6deps CLI tool.
tools
gendoc command
Package main contains CLI documentation generator tool.
Package main contains CLI documentation generator tool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL