Skip to content

Commit

Permalink
fix(tests): skip upgrade tests if not upgrading between two consecuti…
Browse files Browse the repository at this point in the history
…ve Kong versions (#5327) (#5365)

(cherry picked from commit 614c6c0)

Co-authored-by: Grzegorz Burzyński <[email protected]>
  • Loading branch information
tao12345666333 and czeslavo authored Dec 19, 2023
1 parent 622c990 commit 4f025c4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ require (
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
Expand Down
88 changes: 87 additions & 1 deletion test/e2e/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ package e2e
import (
"context"
"fmt"
"os"
"regexp"
"strings"
"testing"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/kong/go-kong/kong"
"github.com/kong/kubernetes-testing-framework/pkg/environments"
"github.com/samber/lo"
Expand All @@ -15,6 +21,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kong/kubernetes-ingress-controller/v2/test/internal/helpers"
"github.com/kong/kubernetes-ingress-controller/v2/test/internal/testenv"
)

const (
Expand Down Expand Up @@ -103,12 +110,14 @@ func testManifestsUpgrade(
oldManifest, err := httpClient.Get(testParams.fromManifestURL)
require.NoError(t, err)
defer oldManifest.Body.Close()
oldManifestPath := dumpToTempFile(t, oldManifest.Body)

skipIfNotTwoConsecutiveKongMinorVersions(t, oldManifestPath, testParams.toManifestPath)

t.Log("configuring upgrade manifests test")
ctx, env := setupE2ETest(t)

t.Logf("deploying previous kong manifests: %s", testParams.fromManifestURL)
oldManifestPath := dumpToTempFile(t, oldManifest.Body)
ManifestDeploy{
Path: oldManifestPath,
SkipTestPatches: true,
Expand Down Expand Up @@ -154,3 +163,80 @@ func testManifestsUpgrade(

verifyIngressWithEchoBackendsPath(ctx, t, env, numberOfEchoBackends, newPath)
}

// skipIfNotTwoConsecutiveKongMinorVersions skips the test if the old and new Kong versions are not two consecutive
// minor versions. This is necessary because Kong in DB-mode doesn't support skipping minor versions when upgrading.
// See the Gateway upgrade guide for details: https://docs.konghq.com/gateway/latest/upgrade/.
func skipIfNotTwoConsecutiveKongMinorVersions(
t *testing.T,
oldManifestPath string,
newManifestPath string,
) {
oldKongVersion := extractKongVersionFromManifest(t, oldManifestPath)

var newKongVersion kong.Version
if targetKongImage := testenv.KongImageTag(); targetKongImage != "" {
// If the target Kong image is specified via environment variable, use it...
newKongVersion = extractKongVersionFromDockerImage(t, targetKongImage)
} else {
// ...otherwise, use the version used in the new manifest.
newKongVersion = extractKongVersionFromManifest(t, newManifestPath)
}

if oldKongVersion.Major() != newKongVersion.Major() ||
oldKongVersion.Minor()+1 != newKongVersion.Minor() {
t.Skipf("skipping upgrade test because the old and new Kong versions are not two consecutive minor versions: %s and %s",
oldKongVersion, newKongVersion)
}
}

var kongVersionRegex = regexp.MustCompile(`image: (kong:.*)`)

// extractKongVersionFromManifest extracts the Kong version from the manifest.
func extractKongVersionFromManifest(t *testing.T, manifestPath string) kong.Version {
manifest, err := os.ReadFile(manifestPath)
require.NoError(t, err)

res := kongVersionRegex.FindStringSubmatch(string(manifest))
require.NotEmpty(t, res)

version := res[1]
return extractKongVersionFromDockerImage(t, version)
}

// extractKongVersionFromDockerImage extracts the Kong version from the docker image by inspecting the image's env vars
// for the KONG_VERSION env var.
func extractKongVersionFromDockerImage(t *testing.T, image string) kong.Version {
dockerc, err := client.NewClientWithOpts(client.FromEnv)
require.NoError(t, err)

ctx := context.Background()

t.Logf("pulling docker image %s to inspect it", image)
_, err = dockerc.ImagePull(ctx, image, types.ImagePullOptions{})
require.NoError(t, err)

t.Logf("inspecting docker image %s", image)
var imageDetails types.ImageInspect
// Retry because the image may not be available immediately after pulling it.
require.Eventually(t, func() bool {
var err error
imageDetails, _, err = dockerc.ImageInspectWithRaw(ctx, image)
if err != nil {
t.Logf("failed to inspect docker image %s: %s", image, err)
return false
}
return true
}, time.Minute, time.Second)

kongVersionEnv, ok := lo.Find(imageDetails.Config.Env, func(s string) bool {
return strings.HasPrefix(s, "KONG_VERSION=")
})
require.True(t, ok, "KONG_VERSION env var not found in image %s", image)

version, err := kong.ParseSemanticVersion(strings.TrimPrefix(kongVersionEnv, "KONG_VERSION="))
require.NoError(t, err)
t.Logf("parsed Kong version %s from docker image %s", version, image)

return version
}
9 changes: 9 additions & 0 deletions test/internal/testenv/testenv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testenv

import (
"fmt"
"os"
"time"
)
Expand Down Expand Up @@ -40,6 +41,14 @@ func KongTag() string {
return os.Getenv("TEST_KONG_TAG")
}

// KongImageTag is the combined Kong image and tag if both are set, or empty string if not.
func KongImageTag() string {
if KongImage() != "" && KongTag() != "" {
return fmt.Sprintf("%s:%s", KongImage(), KongTag())
}
return ""
}

// KongEffectiveVersion is the effective semver of kong gateway.
// When testing against "nightly" image of kong gateway, we need to set the effective version for parsing semver in chart templates.
func KongEffectiveVersion() string {
Expand Down

1 comment on commit 4f025c4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Go Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 4f025c4 Previous: 6ee74d4 Ratio
BenchmarkDefaultContentToDBLessConfigConverter_Convert - ns/op 128.4 ns/op 71.24 ns/op 1.80

This comment was automatically generated by workflow using github-action-benchmark.

CC: @Kong/k8s-maintainers

Please sign in to comment.