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 13, 2023
1 parent 510e41d commit 2248fff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ integration-agent: agent-image
integration-kubernetes:
go test -tags integration ./pkg/kubernetes/...

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

integration: integration-agent integration-kubernetes integration-kubectl


# Running with -buildvcs=false works around the issue of `go list all` failing when git, which runs as root inside
# the container, refuses to operate on the disruptor source tree as it is not owned by the same user (root).
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 2248fff

Please sign in to comment.