Skip to content

Commit

Permalink
Implement kubectl integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Sep 12, 2023
1 parent 32a9f92 commit 227595a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ format:
integration-agent: agent-image
go test -tags integration ./pkg/agent/...

integration-kubectl:
go test -tags integration ./pkg/testutils/e2e/kubectl/

integration: integration-agent

# Running with -buildvcs=false works around the issue of `go list all` failing when git, which runs as root inside
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build e2e
// +build e2e
//go:build integration
// +build integration

package e2e
package kubectl

import (
"bytes"
Expand All @@ -12,35 +12,59 @@ import (
"time"

"github.com/grafana/xk6-disruptor/pkg/kubernetes"
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/cluster"
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/deploy"
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/kubectl"
"github.com/grafana/xk6-disruptor/pkg/testutils/e2e/kubernetes/namespace"
"github.com/grafana/xk6-disruptor/pkg/testutils/k3sutils"
"github.com/grafana/xk6-disruptor/pkg/testutils/kubernetes/builders"


"github.com/testcontainers/testcontainers-go/modules/k3s"

"k8s.io/client-go/tools/clientcmd"
)

func Test_Kubectl(t *testing.T) {
t.Parallel()

cluster, err := cluster.BuildE2eCluster(
cluster.DefaultE2eClusterConfig(),
)
ctx := context.Background()

container, err := k3s.RunContainer(ctx)
if err != nil {
t.Errorf("failed to create cluster: %v", err)
return
t.Fatal(err)
}

// wait for the api server to complete initialization.
// see this issue for more details:
// https://github.com/testcontainers/testcontainers-go/issues/1547
timeout := time.Second * 30
err = k3sutils.WaitForRegex(ctx, container, ".*Node controller sync successful.*", timeout)
if err != nil {
t.Fatalf("failed waiting for cluster ready: %s", err)
}

// Clean up the container after the test is complete
t.Cleanup(func() {
_ = cluster.Cleanup()
if err = container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

k8s, err := kubernetes.NewFromKubeconfig(cluster.Kubeconfig())
kubeConfigYaml, err := container.GetKubeConfig(ctx)
if err != nil {
t.Fatalf("failed to get kube-config : %s", err)
}

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

k8s, err := kubernetes.NewFromConfig(restcfg)
if err != nil {
t.Errorf("error creating kubernetes client: %v", err)
return
t.Fatalf("error creating kubernetes client: %v", err)
}

// Test Wait Pod Running
t.Run("Test local random port", func(t *testing.T) {
t.Run("Test port forwarding", func(t *testing.T) {
namespace, err := namespace.CreateTestNamespace(context.TODO(), t, k8s.Client())
if err != nil {
t.Errorf("failed to create test namespace: %v", err)
Expand All @@ -63,7 +87,7 @@ func Test_Kubectl(t *testing.T) {
return
}

client, err := kubectl.NewFromKubeconfig(context.TODO(), cluster.Kubeconfig())
client, err := NewForConfig(context.TODO(), restcfg)
if err != nil {
t.Errorf("failed to create kubectl client: %v", err)
return
Expand Down

0 comments on commit 227595a

Please sign in to comment.