From ed369ed72e255bcc1ae2c55df17805081238bcca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Mon, 29 Apr 2024 13:39:13 +0200 Subject: [PATCH] chore(tests): add logs in e2e tests --- test/e2e/environment.go | 48 +++++++++++++++++++++++---- test/e2e/test_helm_install_upgrade.go | 14 +++++--- test/e2e/test_upgrade.go | 9 +++-- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/test/e2e/environment.go b/test/e2e/environment.go index 63e99422c..2f29e2883 100644 --- a/test/e2e/environment.go +++ b/test/e2e/environment.go @@ -116,6 +116,11 @@ var loggerOnce sync.Once // When running with helm caller is responsible for cleaning up the environment. func CreateEnvironment(t *testing.T, ctx context.Context, opts ...TestEnvOption) TestEnvironment { t.Helper() + + const ( + waitTime = 1 * time.Minute + ) + var opt testEnvOptions for _, o := range opts { o(&opt) @@ -230,7 +235,7 @@ func CreateEnvironment(t *testing.T, ctx context.Context, opts ...TestEnvOption) require.NoError(t, clusters.KustomizeDeployForCluster(ctx, env.Cluster(), kustomizeDir.Tests(), "--server-side")) t.Log("waiting for operator deployment to complete") - require.NoError(t, waitForOperatorDeployment(ctx, "kong-system", clients.K8sClient)) + require.NoError(t, waitForOperatorDeployment(t, ctx, "kong-system", clients.K8sClient, waitTime)) t.Log("waiting for operator webhook service to be connective") require.Eventually(t, waitForOperatorWebhookEventually(t, ctx, clients.K8sClient), @@ -280,22 +285,48 @@ func cleanupEnvironment(t *testing.T, ctx context.Context, env environments.Envi type deploymentAssertOptions func(*appsv1.Deployment) bool -func deploymentAssertConditions(conds ...appsv1.DeploymentCondition) deploymentAssertOptions { +type TestingT interface { + assert.TestingT + Log(args ...interface{}) + Logf(format string, args ...interface{}) + Helper() +} + +func deploymentAssertConditions(t TestingT, conds ...appsv1.DeploymentCondition) deploymentAssertOptions { + t.Helper() + return func(deployment *appsv1.Deployment) bool { return lo.EveryBy(conds, func(cond appsv1.DeploymentCondition) bool { - return lo.ContainsBy(deployment.Status.Conditions, func(c appsv1.DeploymentCondition) bool { + if !lo.ContainsBy(deployment.Status.Conditions, func(c appsv1.DeploymentCondition) bool { return c.Type == cond.Type && c.Status == cond.Status && c.Reason == cond.Reason - }) + }) { + t.Logf("Deployment %s/%s does not have condition %#v", deployment.Namespace, deployment.Name, cond) + t.Logf("Deployment %s/%s current status: %#v", deployment.Namespace, deployment.Name, deployment.Status) + return false + } + return true }) } } -func waitForOperatorDeployment(ctx context.Context, ns string, k8sClient *kubernetes.Clientset, opts ...deploymentAssertOptions) error { -outer: +func waitForOperatorDeployment( + t TestingT, + ctx context.Context, + ns string, + k8sClient *kubernetes.Clientset, + waitTime time.Duration, + opts ...deploymentAssertOptions, +) error { + t.Helper() + + timer := time.NewTimer(waitTime) + for { select { + case <-timer.C: + return fmt.Errorf("timed out waiting for operator deployment in namespace %s", ns) case <-ctx.Done(): return ctx.Err() default: @@ -307,18 +338,21 @@ outer: return err } if len(deploymentList.Items) == 0 { + t.Logf("No operator deployment found in namespace %s", ns) continue } deployment := &deploymentList.Items[0] if deployment.Status.AvailableReplicas <= 0 { + t.Logf("Deployment %s/%s has no AvailableReplicas", ns, deployment.Name) + t.Logf("Deployment %s/%s status: %#v", ns, deployment.Name, deployment.Status) continue } for _, opt := range opts { if !opt(deployment) { - continue outer + continue } } return nil diff --git a/test/e2e/test_helm_install_upgrade.go b/test/e2e/test_helm_install_upgrade.go index 8fa13b85e..6af66294c 100644 --- a/test/e2e/test_helm_install_upgrade.go +++ b/test/e2e/test_helm_install_upgrade.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" "testing" + "time" "github.com/gruntwork-io/terratest/modules/helm" "github.com/gruntwork-io/terratest/modules/k8s" @@ -24,6 +25,8 @@ func TestUpgrade(t *testing.T) { // Rel: https://github.com/Kong/charts/tree/main/charts/gateway-operator chart = "kong/gateway-operator" image = "docker.io/kong/gateway-operator-oss" + + waitTime = 1 * time.Minute ) ctx, cancel := context.WithCancel(context.Background()) @@ -108,16 +111,17 @@ func TestUpgrade(t *testing.T) { } }) - require.NoError(t, waitForOperatorDeployment(ctx, e.Namespace.Name, e.Clients.K8sClient, - deploymentAssertConditions(deploymentReadyConditions()...), + require.NoError(t, waitForOperatorDeployment(t, ctx, e.Namespace.Name, e.Clients.K8sClient, waitTime, + deploymentAssertConditions(t, deploymentReadyConditions()...), )) opts.SetValues["image.tag"] = tag require.NoError(t, helm.UpgradeE(t, opts, chart, releaseName)) - require.NoError(t, waitForOperatorDeployment(ctx, e.Namespace.Name, e.Clients.K8sClient, - deploymentAssertConditions(deploymentReadyConditions()...), - )) + require.NoError(t, waitForOperatorDeployment(t, ctx, e.Namespace.Name, e.Clients.K8sClient, waitTime, + deploymentAssertConditions(t, deploymentReadyConditions()...), + ), + ) }) } } diff --git a/test/e2e/test_upgrade.go b/test/e2e/test_upgrade.go index b6129871d..32576d52a 100644 --- a/test/e2e/test_upgrade.go +++ b/test/e2e/test_upgrade.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "testing" + "time" "github.com/kong/kubernetes-testing-framework/pkg/clusters" "github.com/stretchr/testify/require" @@ -76,14 +77,18 @@ func testManifestsUpgrade( ctx context.Context, testParams upgradeTestParams, ) { + const ( + waitTime = 1 * time.Minute + ) + e := CreateEnvironment(t, ctx, WithOperatorImage(testParams.fromImage), WithInstallViaKustomize()) kustomizationDir := PrepareKustomizeDir(t, testParams.toImage) t.Logf("deploying operator %q to test cluster %q via kustomize", testParams.toImage, e.Environment.Name()) require.NoError(t, clusters.KustomizeDeployForCluster(ctx, e.Environment.Cluster(), kustomizationDir.Tests(), "--server-side", "-v5")) t.Log("waiting for operator deployment to complete") - require.NoError(t, waitForOperatorDeployment(ctx, "kong-system", e.Clients.K8sClient, - deploymentAssertConditions( + require.NoError(t, waitForOperatorDeployment(t, ctx, "kong-system", e.Clients.K8sClient, waitTime, + deploymentAssertConditions(t, appsv1.DeploymentCondition{ Reason: "NewReplicaSetAvailable", Status: "True",