Skip to content

Refactoring to use the cobra library. #167

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

Merged
merged 13 commits into from
Apr 8, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
53 changes: 53 additions & 0 deletions .github/helpers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
_common_setup() {
BASEDIR=$(dirname $BATS_TEST_DIRNAME)

IT_MOD=github.com/grafana/xk6-it
IT_VER=$(_latest_it_version)

EXT_MOD=github.com/grafana/xk6-it/ext
EXT_VER=${IT_VER}

local arch=$(_get_arch)

XK6=${XK6:-$(echo ${BASEDIR}/it/xk6)}
if [ ! -x "$XK6" ]; then
echo " - building snapshot" >&3
cd $BASEDIR
goreleaser build --clean --snapshot --single-target --id xk6
fi

XK6_IMAGE=grafana/xk6:latest-${arch}

IFS=', ' read -r -a K6_VERSIONS <<<"${K6_VERSIONS:-$(_latest_k6_version)}"

K6_LATEST_VERSION=$(_latest_k6_version)

export K6=${BATS_TEST_TMPDIR}/k6
}

_latest_k6_version() {
_get_latest_version "grafana/k6"
}

_latest_it_version() {
_get_latest_version "grafana/xk6-it"
}

_get_latest_version() {
local url=$(curl -s -I "https://github.com/$1/releases/latest" | grep -i location)
local version="${url##*v}"
version=${version//[[:space:]]/}
echo -n "v${version}"
}

_get_arch() {
local arch="$(docker info -f '{{.Architecture}}')"
case $arch in
x86_64)
echo -n "amd64"
;;
arm64)
echo -n "arm64"
;;
esac
}
32 changes: 13 additions & 19 deletions .github/release.bats
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
#!/usr/bin/env bats

setup() {
BASEDIR="$(git rev-parse --show-toplevel)"
cd $BASEDIR
load helpers
_common_setup

EXE="$(ls ${BASEDIR}/dist/xk6_linux_$(dpkg --print-architecture)_v*/xk6)"
IMAGE=grafana/xk6:latest-$(dpkg --print-architecture)
IFS=', ' read -r -a K6_VERSIONS <<<"${K6_VERSIONS:-latest}"
K6_EXTENSION_MODULE=github.com/grafana/xk6-faker
K6_EXTENSION_VERSION="v0.4.3"

if [ -z "$(docker images $IMAGE --format json)" ]; then
echo " - building release" >&3
goreleaser release --clean --snapshot
fi
cd $BATS_TEST_TMPDIR
}

@test 'build (k6 versions: ${K6_VERSIONS[@]:-latest})' {
for K6_VERSION in "${K6_VERSIONS[@]}"; do
[ -f ./k6 ] && rm ./k6 </dev/null
run $EXE build $K6_VERSION --with "${K6_EXTENSION_MODULE}@${K6_EXTENSION_VERSION}"
run $XK6 build $K6_VERSION --with "${IT_MOD}@${IT_VER}"
[ $status -eq 0 ]
echo "$output" | grep -q "xk6 has now produced a new k6 binary"
./k6 version | grep -q "${K6_EXTENSION_MODULE} ${K6_EXTENSION_VERSION}"
./k6 version | grep -q "${IT_MOD} ${ID_VER}"
done
}

@test 'build using docker (k6 versions: ${K6_VERSIONS[@]:-latest})' {
for K6_VERSION in "${K6_VERSIONS[@]}"; do
[ -f ./k6 ] && rm ./k6 </dev/null
run docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" $IMAGE build $K6_VERSION --with "${K6_EXTENSION_MODULE}@${K6_EXTENSION_VERSION}"
if [ -z "$(docker images $XK6_IMAGE --format json)" ]; then
echo " - building release" >&3
cd $BASEDIR
goreleaser release --clean --snapshot
fi

for K6_VERSION in "${K6_VERSIONS[@]}"; do
run docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" $XK6_IMAGE build $K6_VERSION --with "${IT_MOD}@${IT_VER}"
[ $status -eq 0 ]
echo "$output" | grep -q "xk6 has now produced a new k6 binary"
./k6 version | grep -q "${K6_EXTENSION_MODULE} ${K6_EXTENSION_VERSION}"
./k6 version | grep -q "${IT_MOD} ${IT_VER}"
done
}
19 changes: 5 additions & 14 deletions .github/validate.bats
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
#!/usr/bin/env bats

setup() {
BASEDIR="$(git rev-parse --show-toplevel)"
cd $BASEDIR
load helpers
_common_setup

EXE="$(ls ${BASEDIR}/dist/xk6_linux_$(dpkg --print-architecture)_v*/xk6)"
IFS=', ' read -r -a K6_VERSIONS <<<"${K6_VERSIONS:-latest}"
K6_EXTENSION_MODULE=github.com/grafana/xk6-faker
K6_EXTENSION_VERSION="v0.4.3"

if [ ! -x "$EXE" ]; then
echo " - building snapshot" >&3
goreleaser build --clean --snapshot --single-target
fi
cd $BATS_TEST_TMPDIR
}

@test 'build (k6 versions: ${K6_VERSIONS[@]:-latest})' {
for K6_VERSION in "${K6_VERSIONS[@]}"; do
[ -f ./k6 ] && rm ./k6 </dev/null
run $EXE build $K6_VERSION --with "${K6_EXTENSION_MODULE}@${K6_EXTENSION_VERSION}"
run $XK6 build $K6_VERSION --with "${IT_MOD}@${IT_VER}"
[ $status -eq 0 ]
echo "$output" | grep -q "xk6 has now produced a new k6 binary"
./k6 version | grep -q "${K6_EXTENSION_MODULE} ${K6_EXTENSION_VERSION}"
./k6 version | grep -q "${IT_MOD} ${IT_VER}"
done
}
42 changes: 42 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Integration

on:
workflow_dispatch:
pull_request:
branches: ["main", "master"]

jobs:
integration:
name: Integration
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: true

- name: Setup Go ${{ vars.GO_VERSION }}
uses: actions/setup-go@v5
with:
go-version: ${{ vars.GO_VERSION }}

- name: GoReleaser build
uses: goreleaser/goreleaser-action@v6
with:
version: "${{ vars.GORELEASER_VERSION }}"
args: build --clean --snapshot --single-target --id xk6

- name: Setup Bats
uses: bats-core/bats-action@3.0.0
with:
bats-version: "1.11.1"

- name: Run tests
run: |
cat >> $GITHUB_STEP_SUMMARY <<END

### Integration Test

END

bats it/test | tee -a $GITHUB_STEP_SUMMARY
4 changes: 3 additions & 1 deletion .github/workflows/tooling-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ jobs:

- name: Setup Bats
if: ${{ inputs.bats != '' && runner.os == 'Linux' }}
run: sudo apt-get install -y bats
uses: bats-core/bats-action@3.0.0
with:
bats-version: "1.11.1"

- name: GoReleaser snapshot
uses: goreleaser/goreleaser-action@v6
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/tooling-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ jobs:

- name: Setup Bats
if: ${{ inputs.bats != '' && runner.os == 'Linux' }}
run: sudo apt-get install -y bats
uses: bats-core/bats-action@3.0.0
with:
bats-version: "1.11.1"

- name: GoReleaser build
uses: goreleaser/goreleaser-action@v6
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ k6.exe

/coverage.txt

dist/
/dist/
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "it"]
path = it
url = git@github.com:grafana/xk6-it.git
branch = v0.1.6
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ linters:
prevent_accidental_imports:
allow:
- $gostd
- golang.org/x/mod
- github.com/stretchr/testify/require
- github.com/spf13/cobra
- github.com/spf13/pflag
- github.com/lmittmann/tint
- github.com/mattn/go-isatty
- github.com/mattn/go-colorable
- github.com/szkiba/docsme
- github.com/szkiba/efa
- github.com/grafana/k6foundry
- go.k6.io/xk6
exclusions:
Expand Down
29 changes: 26 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,35 @@ before:
- go mod tidy

builds:
- id: xk6
- id: xk6legacy
binary: xk6
main: ./cmd/xk6
goos: ["linux", "windows", "darwin"]
goarch: ["amd64", "arm64"]
flags: ["-trimpath"]
env: ["CGO_ENABLED=0"]
ldflags: ["-s -w"]
ldflags:
- "-s -w"
- "-X {{.ModulePath}}/internal/cmd.version={{.Version}}"
- "-X {{.ModulePath}}/internal/cmd.appname={{.Binary}}"
hooks:
post:
- cp "{{ .Path }}" it/xk6

- id: xk6
binary: xk6ea
main: .
goos: ["linux", "windows", "darwin"]
goarch: ["amd64", "arm64"]
flags: ["-trimpath"]
env: ["CGO_ENABLED=0"]
ldflags:
- "-s -w"
- "-X {{.ModulePath}}/internal/cmd.version={{.Version}}"
- "-X {{.ModulePath}}/internal/cmd.appname={{.Binary}}"
hooks:
post:
- cp "{{ .Path }}" it/xk6

- id: fixids
binary: fixids
Expand All @@ -33,11 +54,13 @@ source:

archives:
- id: bundle
builds: ["xk6"]
builds: ["xk6", "xk6legacy"]
formats: ["tar.gz"]
format_overrides:
- goos: windows
formats: ["zip"]
files:
- READMEea.md

checksum:
name_template: "{{ .ProjectName }}_{{ .Version }}_checksums.txt"
Expand Down
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ __help__:
@echo ' it Run the integration tests'
@echo ' lint Run the linter'
@echo ' makefile Generate the Makefile'
@echo ' security Run security and vulnerability checks'
@echo ' test Run the tests'

# Clean build
.PHONY: all
all: clean format test build it doc
all: clean format security lint test build it doc makefile

# Build custom k6 with extension
.PHONY: build
build:
@(\
goreleaser build --clean --snapshot --single-target;\
goreleaser build --clean --snapshot --single-target --id xk6;\
)

# Clean the working directory
Expand All @@ -49,6 +50,7 @@ coverage: test
doc:
@(\
mdcode update docs/workflows/README.md;\
go run ./tools/docsme --heading 1 -o READMEea.md;\
)

# Format the go source codes
Expand All @@ -62,7 +64,7 @@ format:
.PHONY: it
it:
@(\
bats .github/validate.bats .github/release.bats;\
bats -r .;\
)

# Run the linter
Expand All @@ -79,6 +81,14 @@ makefile:
cdo --makefile Makefile;\
)

# Run security and vulnerability checks
.PHONY: security
security:
@(\
gosec ./...;\
govulncheck ./...;\
)

# Run the tests
.PHONY: test
test:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
`xk6` - Custom k6 Builder
===============================

>[!IMPORTANT]
> **Upcoming changes**
>
> The xk6 tool is currently under refactoring. As of `v0.17.0`, an early access executable (`xk6ea`) is available in the downloadable release archives alongside `xk6`. Check [READMEea.md](READMEea.md) for usage.

This command line tool and associated Go package makes it easy to make custom builds of [k6](https://github.com/grafana/k6).

It is used heavily by k6 extension developers as well as anyone who wishes to make custom `k6` binaries (with or without extensions).
Expand Down
Loading
Loading