Skip to content

Commit

Permalink
chore(tests): add logs in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Apr 29, 2024
1 parent f148e27 commit ed369ed
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
48 changes: 41 additions & 7 deletions test/e2e/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,

Check failure on line 319 in test/e2e/environment.go

View workflow job for this annotation

GitHub Actions / lint

`waitForOperatorDeployment` - `waitTime` always receives `waitTime` (`60000000000`) (unparam)
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:
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions test/e2e/test_helm_install_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/helm"
"github.com/gruntwork-io/terratest/modules/k8s"
Expand All @@ -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())
Expand Down Expand Up @@ -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()...),
),
)
})
}
}
Expand Down
9 changes: 7 additions & 2 deletions test/e2e/test_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/kong/kubernetes-testing-framework/pkg/clusters"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ed369ed

Please sign in to comment.