Skip to content

Commit

Permalink
chore(tests): fix some issues with e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 committed Oct 22, 2024
1 parent df4f5b4 commit 625b2ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
36 changes: 26 additions & 10 deletions test/e2e/test_helm_install_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"
testutils "github.com/kong/gateway-operator/pkg/utils/test"
"github.com/kong/gateway-operator/pkg/vars"
"github.com/kong/gateway-operator/test/helpers"
)

func init() {
Expand All @@ -47,13 +48,13 @@ func TestHelmUpgrade(t *testing.T) {
// and dumping diagnostics if the test fails.
e := CreateEnvironment(t, ctx)

// assertion is run after the upgrade to assert the state of the resources in the cluster.
// Assertion is run after the upgrade to assert the state of the resources in the cluster.
type assertion struct {
Name string
Func func(*assert.CollectT, *testutils.K8sClients)
}

testcases := []struct {
testCases := []struct {
name string
fromVersion string
toVersion string
Expand Down Expand Up @@ -113,6 +114,20 @@ func TestHelmUpgrade(t *testing.T) {
Name: "Gateway is programmed",
Func: func(c *assert.CollectT, cl *testutils.K8sClients) {
gatewayAndItsListenersAreProgrammedAssertion("gw-upgrade-120-123=true")(ctx, c, cl.MgrClient)

gw := getGatewayByLabelSelector("gw-upgrade-120-123=true", ctx, c, cl.MgrClient)
if !assert.NotNil(c, gw) {
return
}
deployments, err := listDataPlaneDeploymentsForGateway(c, ctx, cl.MgrClient, gw)
if err != nil {
return
}
d := &deployments[0]

t.Logf(">>> Deployment dump %q:\n%# v",
client.ObjectKeyFromObject(d), pretty.Formatter(d),
)
},
},
},
Expand Down Expand Up @@ -191,10 +206,10 @@ func TestHelmUpgrade(t *testing.T) {
},
},
{
Name: "DataPlane deployment is patched after operator upgrade (due to change in default Kong image version to 3.7)",
Name: fmt.Sprintf("DataPlane deployment is patched after operator upgrade (due to change in default Kong image version to %q)", helpers.GetDefaultDataPlaneImagePrevious()),
Func: func(c *assert.CollectT, cl *testutils.K8sClients) {
gatewayDataPlaneDeploymentIsPatched("gw-upgrade-123-130=true")(ctx, c, cl.MgrClient)
gatewayDataPlaneDeploymentHasImageSetTo("gw-upgrade-123-130=true", "kong:3.7")(ctx, c, cl.MgrClient)
gatewayDataPlaneDeploymentHasImageSetTo("gw-upgrade-123-130=true", helpers.GetDefaultDataPlaneImagePrevious())(ctx, c, cl.MgrClient)
},
},
// NOTE: We do not check managed cluster wide resource labels because the fix for migrating
Expand Down Expand Up @@ -260,10 +275,10 @@ func TestHelmUpgrade(t *testing.T) {
},
},
{
Name: "DataPlane deployment is patched after operator upgrade (due to change in default Kong image version to 3.8)",
Name: fmt.Sprintf("DataPlane deployment is patched after operator upgrade (due to change in default Kong image version to %q)", helpers.GetDefaultDataPlaneImage()),
Func: func(c *assert.CollectT, cl *testutils.K8sClients) {
gatewayDataPlaneDeploymentIsPatched("gw-upgrade-130-current=true")(ctx, c, cl.MgrClient)
gatewayDataPlaneDeploymentHasImageSetTo("gw-upgrade-130-current=true", "kong:3.8")(ctx, c, cl.MgrClient)
gatewayDataPlaneDeploymentHasImageSetTo("gw-upgrade-130-current=true", helpers.GetDefaultDataPlaneImage())(ctx, c, cl.MgrClient)
},
},
{
Expand Down Expand Up @@ -359,7 +374,7 @@ func TestHelmUpgrade(t *testing.T) {
currentRepository, currentTag = splitRepoVersionFromImage(t, imageOverride)
}

for _, tc := range testcases {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var (
tag string
Expand Down Expand Up @@ -499,6 +514,7 @@ func baseGatewayConfigurationSpec() operatorv1beta1.GatewayConfigurationSpec {
},
},
},

ControlPlaneOptions: &operatorv1beta1.ControlPlaneOptions{
Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{
PodTemplateSpec: &corev1.PodTemplateSpec{
Expand Down Expand Up @@ -540,7 +556,7 @@ func getGatewayByLabelSelector(gatewayLabelSelector string, ctx context.Context,
}

if len(gws.Items) != 1 {
c.Errorf("got %d gateways, expected 1", len(gws.Items))
c.Errorf("expected 1 Gateway, got %d", len(gws.Items))
return nil
}

Expand Down Expand Up @@ -573,8 +589,8 @@ func gatewayDataPlaneDeploymentHasImageSetTo(
}

if container[0].Image != image {
return fmt.Errorf("Gateway's DataPlane Deployment %q expected image %s but got %s",
client.ObjectKeyFromObject(d), container[0].Image, image,
return fmt.Errorf("Gateway's DataPlane Deployment %q expected image %s got %s",
client.ObjectKeyFromObject(d), image, container[0].Image,
)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/test_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

operatorv1beta1 "github.com/kong/gateway-operator/api/v1beta1"
"github.com/kong/gateway-operator/pkg/consts"
"github.com/kong/gateway-operator/test/helpers"
)

func init() {
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestDataPlaneValidatingWebhook(t *testing.T) {
Value: "postgres",
},
},
Image: consts.DefaultDataPlaneImage,
Image: helpers.GetDefaultDataPlaneImage(),
},
},
},
Expand Down
24 changes: 24 additions & 0 deletions test/helpers/defaults.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package helpers

import (
"fmt"
"strconv"
"strings"
"sync"

"github.com/kong/gateway-operator/pkg/consts"
Expand Down Expand Up @@ -33,6 +36,27 @@ func GetDefaultDataPlaneImage() string {
return _defaultDataPlaneImage
}

// GetDefaultDataPlaneImagePrevious returns the default data plane image with the previous version.
func GetDefaultDataPlaneImagePrevious() string {
_defaultDataPlaneImageLock.RLock()
defer _defaultDataPlaneImageLock.RUnlock()
s := strings.Split(_defaultDataPlaneImage, ".")
if len(s) != 2 {
panic(fmt.Sprintf("invalid default data plane image (more than one '.' in version), %s", _defaultDataPlaneImage))
}
v, err := strconv.Atoi(s[1])
if err != nil {
panic(fmt.Sprintf("invalid default data plane image (after '.' not a number), %s", _defaultDataPlaneImage))
}
// NOTICE: Kong Gateway 4.0 rather won't happen in foreseeable future, thus for now it's safe to have it hardcoded this way.
previous := v - 1
if previous < 0 {
panic(fmt.Sprintf("invalid default data plane image (previous version is negative), %s", _defaultDataPlaneImage))
}
s[1] = strconv.Itoa(previous)
return strings.Join(s, ".")
}

// SetDefaultDataPlaneImage sets the default data plane image.
func SetDefaultDataPlaneImage(image string) {
_defaultDataPlaneImageLock.Lock()
Expand Down

0 comments on commit 625b2ef

Please sign in to comment.