Skip to content

Commit

Permalink
[OSSM-6739] Add test case for CNI resources prune (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
unsortedhashsets authored Jun 28, 2024
1 parent 6558d91 commit 3674da7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/tests/ossm/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})

})
Expand Down Expand Up @@ -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"),
)
}
}
}
39 changes: 39 additions & 0 deletions pkg/util/cni/cni.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions pkg/util/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3674da7

Please sign in to comment.