From 3674da70e9e21f50f4f10ed1455cba3289f39d5f Mon Sep 17 00:00:00 2001 From: Mikhail Abramov Date: Fri, 28 Jun 2024 18:08:25 +0200 Subject: [PATCH] [OSSM-6739] Add test case for CNI resources prune (#700) --- pkg/tests/ossm/smoke_test.go | 50 ++++++++++++++++++++++++++++++++++++ pkg/util/cni/cni.go | 39 ++++++++++++++++++++++++++++ pkg/util/compare.go | 10 ++++++++ 3 files changed, 99 insertions(+) create mode 100644 pkg/util/cni/cni.go diff --git a/pkg/tests/ossm/smoke_test.go b/pkg/tests/ossm/smoke_test.go index 1ce05351..4311c1cc 100644 --- a/pkg/tests/ossm/smoke_test.go +++ b/pkg/tests/ossm/smoke_test.go @@ -21,7 +21,9 @@ import ( "time" "github.com/maistra/maistra-test-tool/pkg/app" + "github.com/maistra/maistra-test-tool/pkg/util" "github.com/maistra/maistra-test-tool/pkg/util/check/assert" + "github.com/maistra/maistra-test-tool/pkg/util/cni" "github.com/maistra/maistra-test-tool/pkg/util/env" "github.com/maistra/maistra-test-tool/pkg/util/oc" "github.com/maistra/maistra-test-tool/pkg/util/retry" @@ -82,6 +84,10 @@ func TestSmoke(t *testing.T) { oc.RestartAllPodsAndWaitReady(t, ns) checkSMCP(t, ns) + + t.LogStep("Check that previous version CNI resources were pruned and needed resources were preserved") + t.Log("Related issue: https://issues.redhat.com/browse/OSSM-2101") + assertResourcesPruneUpgrade(t, fromVersion, toVersion) }) t.NewSubTest(fmt.Sprintf("install smcp %s", toVersion)).Run(func(t TestHelper) { @@ -117,6 +123,10 @@ func TestSmoke(t *testing.T) { "SMCP resources are deleted", "Still waiting for resources to be deleted from namespace")) }) + + t.LogStep("Check that CNI resources were pruned") + t.Log("Related issue: https://issues.redhat.com/browse/OSSM-2101") + assertResourcePruneDelete(t, toVersion) }) }) @@ -235,3 +245,43 @@ func getPreviousVersion(ver version.Version) version.Version { } panic(fmt.Sprintf("version %s not found in VERSIONS", ver)) } + +func assertResourcesPruneUpgrade(t TestHelper, fromVersion version.Version, toVersion version.Version) { + for _, res := range cni.CniResources { + if util.Contains(res.UsedInVersions, toVersion) { + oc.Get(t, + "openshift-operators", + res.Obj, + res.Name, + assert.OutputContains(res.Name, + "Resource "+res.Obj+"/"+res.Name+" was preserved", + "Resource "+res.Obj+"/"+res.Name+" was not preserved"), + ) + } else if util.Contains(res.UsedInVersions, fromVersion) { + oc.Get(t, + "openshift-operators", + res.Obj, + res.Name+" --ignore-not-found", + assert.OutputDoesNotContain(res.Name, + "Resource "+res.Obj+"/"+res.Name+" was pruned", + "Resource "+res.Obj+"/"+res.Name+" was not pruned"), + ) + } + + } +} + +func assertResourcePruneDelete(t TestHelper, ver version.Version) { + for _, res := range cni.CniResources { + if util.Contains(res.UsedInVersions, ver) { + oc.Get(t, + "", + res.Obj, + res.Name+" --ignore-not-found", + assert.OutputDoesNotContain(res.Name, + "Resource "+res.Obj+"/"+res.Name+" was pruned", + "Resource "+res.Obj+"/"+res.Name+" was not pruned"), + ) + } + } +} diff --git a/pkg/util/cni/cni.go b/pkg/util/cni/cni.go new file mode 100644 index 00000000..f94d59a4 --- /dev/null +++ b/pkg/util/cni/cni.go @@ -0,0 +1,39 @@ +package cni + +import "github.com/maistra/maistra-test-tool/pkg/util/version" + +type Resource struct { + Obj string + Name string + UsedInVersions []version.Version +} + +// Resources source: https://github.com/maistra/istio-operator/blob/maistra-2.6/pkg/controller/servicemesh/controlplane/cni_pruner.go +var CniResources = []Resource{ + toResource("ClusterRole", "istio-cni", version.SMCP_2_0, version.SMCP_2_1, version.SMCP_2_2, version.SMCP_2_3), + toResource("ClusterRole", "ossm-cni", version.SMCP_2_4, version.SMCP_2_5, version.SMCP_2_6), + toResource("ClusterRoleBinding", "istio-cni", version.SMCP_2_0, version.SMCP_2_1, version.SMCP_2_2, version.SMCP_2_3), + toResource("ClusterRoleBinding", "ossm-cni", version.SMCP_2_4, version.SMCP_2_5, version.SMCP_2_6), + toResource("ConfigMap", "istio-cni-config", version.SMCP_2_0, version.SMCP_2_1, version.SMCP_2_2), + toResource("ConfigMap", "istio-cni-config-v2-3", version.SMCP_2_3), + toResource("ConfigMap", "ossm-cni-config-v2-4", version.SMCP_2_4), + toResource("ConfigMap", "ossm-cni-config-v2-5", version.SMCP_2_5), + toResource("ConfigMap", "ossm-cni-config-v2-6", version.SMCP_2_6), + toResource("DaemonSet", "istio-cni-node", version.SMCP_2_0, version.SMCP_2_1, version.SMCP_2_2), + toResource("DaemonSet", "istio-cni-node-v2-3", version.SMCP_2_3), + toResource("DaemonSet", "istio-cni-node-v2-4", version.SMCP_2_4), + toResource("DaemonSet", "istio-cni-node-v2-5", version.SMCP_2_5), + toResource("DaemonSet", "istio-cni-node-v2-6", version.SMCP_2_6), + toResource("ServiceAccount", "istio-cni", version.SMCP_2_0, version.SMCP_2_1, version.SMCP_2_2, version.SMCP_2_3), + toResource("ServiceAccount", "ossm-cni", version.SMCP_2_4, version.SMCP_2_5, version.SMCP_2_6), +} + +func toResource(obj string, name string, versions ...version.Version) Resource { + r := Resource{ + Obj: obj, + Name: name, + } + r.UsedInVersions = append(r.UsedInVersions, versions...) + + return r +} diff --git a/pkg/util/compare.go b/pkg/util/compare.go index 752e2632..8bfe2416 100644 --- a/pkg/util/compare.go +++ b/pkg/util/compare.go @@ -69,3 +69,13 @@ func CompareToFile(out []byte, modelFile string) error { } return Compare(out, model) } + +// Contains checks if a slice contains an element +func Contains[T comparable](s []T, e T) bool { + for _, v := range s { + if v == e { + return true + } + } + return false +}