Skip to content

Commit

Permalink
fix: simplify api test dependencies (#117)
Browse files Browse the repository at this point in the history
* fix: simplify api test dependencies

* Cleanup commented code
  • Loading branch information
brandtkeller authored Nov 17, 2023
1 parent d834a24 commit 7e683e8
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 917 deletions.
3 changes: 2 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"platformAutomerge": true,
"platformCommit": true,
"postUpdateOptions": ["gomodTidy"],
"commitBodyTable": true
"commitBodyTable": true,
"commitMessagePrefix": "deps"
}
147 changes: 21 additions & 126 deletions src/test/e2e/api_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package test

import (
"context"
"testing"
"time"
"github.com/defenseunicorns/lula/src/cmd/validate"
"github.com/defenseunicorns/lula/src/test/util"
"github.com/defenseunicorns/lula/src/types"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"sigs.k8s.io/e2e-framework/klient/k8s"
// netv1 "k8s.io/api/networking/v1"
// "sigs.k8s.io/e2e-framework/klient/k8s"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
"testing"
"time"
)

func TestApiValidation(t *testing.T) {
featureTrueValidation := features.New("Check API Validation - Success").
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
// Create the configmap
configMap, err := util.GetConfigMap("./scenarios/api-field/configmap.pass.yaml")
if err != nil {
t.Fatal(err)
Expand All @@ -28,6 +29,7 @@ func TestApiValidation(t *testing.T) {
}
ctx = context.WithValue(ctx, "api-field-configmap", configMap)

// Create the pod
pod, err := util.GetPod("./scenarios/api-field/pod.yaml")
if err != nil {
t.Fatal(err)
Expand All @@ -37,42 +39,13 @@ func TestApiValidation(t *testing.T) {
}
err = wait.
For(conditions.New(config.Client().Resources()).
PodConditionMatch(pod, corev1.PodReady, corev1.ConditionTrue),
wait.WithTimeout(time.Minute*5))
PodConditionMatch(pod, corev1.PodReady, corev1.ConditionTrue),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-pod", pod)

service, err := util.GetService("./scenarios/api-field/service.yaml")
if err != nil {
t.Fatal(err)
}
if err = config.Client().Resources().Create(ctx, service); err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-service", service)

ingress, err := util.GetIngress("./scenarios/api-field/ingress.yaml")
if err != nil {
t.Fatal(err)
}
if err = config.Client().Resources().Create(ctx, ingress); err != nil {
t.Fatal(err)
}
err = wait.
For(conditions.New(config.Client().Resources()).
ResourceMatch(ingress, func(object k8s.Object) bool {
ing, _ := object.(*netv1.Ingress)
if len(ing.Status.LoadBalancer.Ingress) < 1 { return false }
return ing.Status.LoadBalancer.Ingress[0].Hostname == "localhost"
}),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-ingress", ingress)

return ctx
}).
Assess("Validate API response field", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
Expand Down Expand Up @@ -102,38 +75,14 @@ func TestApiValidation(t *testing.T) {
return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
ingress := ctx.Value("api-field-ingress").(*netv1.Ingress)
if err := config.Client().Resources().Delete(ctx, ingress); err != nil {
t.Fatal(err)
}
err := wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(ingress),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}

service := ctx.Value("api-field-service").(*corev1.Service)
if err := config.Client().Resources().Delete(ctx, service); err != nil {
t.Fatal(err)
}
err = wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(service),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}

pod := ctx.Value("api-field-pod").(*corev1.Pod)
if err := config.Client().Resources().Delete(ctx, pod); err != nil {
t.Fatal(err)
}
err = wait.
err := wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(pod),
wait.WithTimeout(time.Minute*5))
ResourceDeleted(pod),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}
Expand All @@ -144,8 +93,8 @@ func TestApiValidation(t *testing.T) {
}
err = wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(configMap),
wait.WithTimeout(time.Minute*5))
ResourceDeleted(configMap),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -173,42 +122,12 @@ func TestApiValidation(t *testing.T) {
}
err = wait.
For(conditions.New(config.Client().Resources()).
PodConditionMatch(pod, corev1.PodReady, corev1.ConditionTrue),
wait.WithTimeout(time.Minute*5))
PodConditionMatch(pod, corev1.PodReady, corev1.ConditionTrue),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-pod", pod)

service, err := util.GetService("./scenarios/api-field/service.yaml")
if err != nil {
t.Fatal(err)
}
if err = config.Client().Resources().Create(ctx, service); err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-service", service)

ingress, err := util.GetIngress("./scenarios/api-field/ingress.yaml")
if err != nil {
t.Fatal(err)
}
if err = config.Client().Resources().Create(ctx, ingress); err != nil {
t.Fatal(err)
}
err = wait.
For(conditions.New(config.Client().Resources()).
ResourceMatch(ingress, func(object k8s.Object) bool {
ing, _ := object.(*netv1.Ingress)
if len(ing.Status.LoadBalancer.Ingress) < 1 { return false }
return ing.Status.LoadBalancer.Ingress[0].Hostname == "localhost"
}),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-ingress", ingress)

return ctx
}).
Assess("Validate API response field", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
Expand Down Expand Up @@ -236,38 +155,14 @@ func TestApiValidation(t *testing.T) {
return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
ingress := ctx.Value("api-field-ingress").(*netv1.Ingress)
if err := config.Client().Resources().Delete(ctx, ingress); err != nil {
t.Fatal(err)
}
err := wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(ingress),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}

service := ctx.Value("api-field-service").(*corev1.Service)
if err := config.Client().Resources().Delete(ctx, service); err != nil {
t.Fatal(err)
}
err = wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(service),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}

pod := ctx.Value("api-field-pod").(*corev1.Pod)
if err := config.Client().Resources().Delete(ctx, pod); err != nil {
t.Fatal(err)
}
err = wait.
err := wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(pod),
wait.WithTimeout(time.Minute*5))
ResourceDeleted(pod),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}
Expand All @@ -278,14 +173,14 @@ func TestApiValidation(t *testing.T) {
}
err = wait.
For(conditions.New(config.Client().Resources()).
ResourceDeleted(configMap),
wait.WithTimeout(time.Minute*5))
ResourceDeleted(configMap),
wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal(err)
}

return ctx
}).Feature()

testEnv.Test(t, featureTrueValidation, featureFalseValidation )
testEnv.Test(t, featureTrueValidation, featureFalseValidation)
}
9 changes: 0 additions & 9 deletions src/test/e2e/kind-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
57 changes: 0 additions & 57 deletions src/test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@ package test
import (
"os"
"testing"
"log"
"context"
"strings"
"time"

"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
"sigs.k8s.io/e2e-framework/support/kind"
"sigs.k8s.io/e2e-framework/klient/k8s/resources"
"sigs.k8s.io/e2e-framework/klient/decoder"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/klient/wait"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
Expand All @@ -40,52 +29,6 @@ func TestMain(m *testing.M) {
"kind-config.yaml"),

envfuncs.CreateNamespace(namespace),

func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
// load stream of nginx-ingress resources
ingressBytes, err := os.ReadFile("nginx-ingress.yaml")
if err != nil {
log.Fatal(err)
}
ingressYAML := string(ingressBytes)
resource, err := resources.New(cfg.Client().RESTConfig())
if err != nil {
return ctx, err
}
decoder.DecodeEach(ctx, strings.NewReader(ingressYAML), decoder.CreateHandler(resource))

// wait for ingress controller deployment object to be ready
deployment := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "ingress-nginx-controller",
Namespace: "ingress-nginx",
},
}
err = wait.For(conditions.New(cfg.Client().Resources()).DeploymentConditionMatch(&deployment, appsv1.DeploymentAvailable, corev1.ConditionTrue), wait.WithTimeout(time.Minute*5))
if err != nil {
log.Fatal(err)
}

// find nginx ingress controller pod
var pods corev1.PodList
err = cfg.Client().Resources().WithNamespace("ingress-nginx").List(
ctx, &pods, resources.WithLabelSelector(
"app.kubernetes.io/component=controller," +
"app.kubernetes.io/instance=ingress-nginx," +
"app.kubernetes.io/name=ingress-nginx"))
if err != nil {
log.Fatal(err)
}
pod := &pods.Items[0]

// wait for ingress controller to be ready
err = wait.For(conditions.New(cfg.Client().Resources()).PodConditionMatch(pod, corev1.PodReady, corev1.ConditionTrue), wait.WithTimeout(time.Minute*5))
if err != nil {
log.Fatal(err)
}

return ctx, nil
},
)

testEnv.Finish(
Expand Down
Loading

0 comments on commit 7e683e8

Please sign in to comment.