Skip to content

Commit

Permalink
WIP: e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Jul 18, 2024
1 parent 0054f9d commit 2eb7522
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 1,436 deletions.
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)
}
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

0 comments on commit 2eb7522

Please sign in to comment.