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 e2e tests #113

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const podSpec = {
export default function () {
const kubernetes = new Kubernetes();

kubernetes.create(pod)
kubernetes.create(podSpec)

const pods = kubernetes.list("Pod", "testns");

Expand Down Expand Up @@ -206,7 +206,7 @@ export default function () {
const kubernetes = new Kubernetes();

// create pod
kubernetes.create(pod)
kubernetes.create(podSpec)

// get helpers for test namespace
const helpers = kubernetes.helpers()
Expand Down
130 changes: 130 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package kubernetes

import (
"context"
"io"
"testing"

"github.com/grafana/sobek"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/modules/k3s"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/metrics"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

// setupE2eTestEnv should be called from each test to build the execution environment for e2e tests
func setupE2eTestEnv(t *testing.T) *sobek.Runtime {
rt := sobek.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})

testLog := logrus.New()
testLog.AddHook(&testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.WarnLevel},
})
testLog.SetOutput(io.Discard)

state := &lib.State{
Options: lib.Options{
SystemTags: metrics.NewSystemTagSet(metrics.TagVU),
},
Logger: testLog,
Tags: lib.NewVUStateTags(metrics.NewRegistry().RootTagSet()),
}

root := &RootModule{}
m, ok := root.NewModuleInstance(
&modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: context.Background(),
StateField: state,
},
).(*ModuleInstance)
require.True(t, ok)
require.NoError(t, rt.Set("Kubernetes", m.Exports().Named["Kubernetes"]))

ctx := context.Background()

// start a k3s container
k3sContainer, err := k3s.RunContainer(ctx)

Check failure on line 55 in e2e_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: k3s.RunContainer is deprecated: use Run instead RunContainer creates an instance of the K3s container type (staticcheck)

Check failure on line 55 in e2e_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: k3s.RunContainer is deprecated: use Run instead RunContainer creates an instance of the K3s container type (staticcheck)
if err != nil {
t.Fatal(err)
}

// Clean up the container
defer func() {
if err := k3sContainer.Terminate(ctx); err != nil {

Check failure on line 62 in e2e_test.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 55 (govet)

Check failure on line 62 in e2e_test.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 55 (govet)
t.Fatal(err)
}
}()

Check failure on line 66 in e2e_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)

Check failure on line 66 in e2e_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
kubeConfigYaml, err := k3sContainer.GetKubeConfig(context.TODO())
if err != nil {
t.Fatalf("failed to get kubeconfig: %s", err)
}

restcfg, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigYaml)
if err != nil {
t.Fatalf("failed to create rest config: %s", err)
}

clientset, err := kubernetes.NewForConfig(restcfg)
if err != nil {
t.Fatalf("failed to create k8s client: %s", err)
}

m.clientset = clientset
m.config = restcfg

return rt
}

// E2eTest runs helpers
func TestE2e(t *testing.T) {
t.Parallel()

rt := setupE2eTestEnv(t)

_, err := rt.RunString(`
const k8s = new Kubernetes()

let pod = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "default"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
},
status: {
phase: "Running"
}
}

// create pod in test namespace
k8s.create(pod)

// get helpers for test namespace
const helpers = k8s.helpers()

// wait for pod to be running
if (!helpers.waitPodRunning(pod.metadata.name, 5)) {
throw new Error("should not timeout")
}
`)
require.NoError(t, err)
}
32 changes: 32 additions & 0 deletions examples/create_with_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Kubernetes } from 'k6/x/kubernetes';

const podSpec = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "testns"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
}
}

export default function () {
const kubernetes = new Kubernetes();

kubernetes.create(podSpec)

const pods = kubernetes.list("Pod", "testns");

console.log(`${pods.length} Pods found:`);
pods.map(function(pod) {
console.log(` ${pod.metadata.name}`)
});
}
30 changes: 30 additions & 0 deletions examples/create_with_yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Kubernetes } from 'k6/x/kubernetes';

const manifest = `
apiVersion: batch/v1
kind: Job
metadata:
name: busybox
namespace: testns
spec:
template:
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "300"]
restartPolicy: Never
`

export default function () {
const kubernetes = new Kubernetes();

kubernetes.apply(manifest)

const jobs = kubernetes.list("Job", "testns");

console.log(`${jobs.length} Jobs found:`);
pods.map(function(job) {
console.log(` ${job.metadata.name}`)
});
}
35 changes: 35 additions & 0 deletions examples/wait_pod_ready.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Kubernetes } from 'k6/x/kubernetes';

let podSpec = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "busybox",
namespace: "default"
},
spec: {
containers: [
{
name: "busybox",
image: "busybox",
command: ["sh", "-c", "sleep 30"]
}
]
}
}

export default function () {
const kubernetes = new Kubernetes();

// create pod
kubernetes.create(podSpec)

// get helpers for test namespace
const helpers = kubernetes.helpers()

// wait for pod to be running
const timeout = 10
if (!helpers.waitPodRunning(pod.metadata.name, timeout)) {
console.log(`"pod ${pod.metadata.name} not ready after ${timeout} seconds`)
}
}
48 changes: 43 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,61 +1,98 @@
module github.com/grafana/xk6-kubernetes

go 1.20
go 1.21

toolchain go1.22.2

require (
go.k6.io/k6 v0.51.1-0.20240610082146-1f01a9bc2365
k8s.io/api v0.27.8
k8s.io/apimachinery v0.27.8
k8s.io/client-go v0.27.8
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
k8s.io/client-go v0.29.2
)

require (
github.com/grafana/sobek v0.0.0-20240607083612-4f0cd64f4e78
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go/modules/k3s v0.32.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Microsoft/hcsshim v0.11.5 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/docker/docker v27.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanw/esbuild v0.21.2 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.7.0 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd // indirect
github.com/mstoykov/k6-taskqueue-lib v0.1.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/testcontainers/testcontainers-go v0.32.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect
Expand All @@ -64,6 +101,7 @@ require (
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sys v0.21.0 // indirect
Expand Down
Loading
Loading