Skip to content

Commit

Permalink
cnf ran: update skipped talm test case (#210)
Browse files Browse the repository at this point in the history
There is currently a test case in the TALM suite that is always skipped due to the inputs being wrong. This PR switches it to use a regex that will work in newer versions.
  • Loading branch information
klaskosk authored Sep 20, 2024
1 parent 86b0e5e commit be66204
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tests/cnf/ran/internal/ranconfig/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bmcTimeout: "15s"
ocpUpgradeUpstreamUrl: "https://api.openshift.com/api/upgrades_info/v1/graph"
ptpOperatorNamespace: "openshift-ptp"
talmPreCachePolicies:
- "common-config-policy"
- "common-subscriptions-policy"
- "^common(-v4\\.\\d\\d)?-config-policy"
- "^common(-v4\\.\\d\\d)?-subscriptions-policy"
ztpSiteGenerateImage: "registry-proxy.engineering.redhat.com/rh-osbs/openshift4-ztp-site-generate"
...
53 changes: 40 additions & 13 deletions tests/cnf/ran/talm/tests/talm-precache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"context"
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -38,7 +39,10 @@ import (
var _ = Describe("TALM precache", Label(tsparams.LabelPreCacheTestCases), func() {
When("there is a single spoke", func() {
Context("precache operator", func() {
var suffixes []string
var (
policies []string
suffixes []string
)

BeforeEach(func() {
By("verifying TalmPrecachePolicies from config are available on hub")
Expand All @@ -48,7 +52,11 @@ var _ = Describe("TALM precache", Label(tsparams.LabelPreCacheTestCases), func()
Skip("could not find all policies in TalmPreCachePolicies in config on hub")
}

suffixes = copyPoliciesWithSubscription(preCachePolicies)
policies, suffixes = copyPoliciesWithSubscription(preCachePolicies)

for _, suffix := range suffixes {
policies = append(policies, tsparams.PolicyName+suffix)
}
})

AfterEach(func() {
Expand All @@ -60,11 +68,6 @@ var _ = Describe("TALM precache", Label(tsparams.LabelPreCacheTestCases), func()

// 48902 Tests image precaching - operators
It("tests for precache operator with multiple sources", reportxml.ID("48902"), func() {
var policies []string
for _, suffix := range suffixes {
policies = append(policies, tsparams.PolicyName+suffix)
}

By("creating CGU with created operator upgrade policy")
cguBuilder := getPrecacheCGU(policies, []string{RANConfig.Spoke1Name})
_, err := cguBuilder.Create()
Expand Down Expand Up @@ -607,16 +610,26 @@ func checkPrecachePodLog(client *clients.Settings) error {
}

// checkPoliciesExist returns the PolicyBuilder for all the provided policyNames, regardless of namespace, and whether
// all policyNames could be found on the hub.
// all policyNames could be found on the hub. It takes the policyNames as valid regular expressions and uses a match to
// determine if a policy exists.
func checkPoliciesExist(client *clients.Settings, policyNames []string) ([]*ocm.PolicyBuilder, bool) {
var policyRegexps []*regexp.Regexp

for _, policyName := range policyNames {
policyRegexp, err := regexp.Compile(policyName)
Expect(err).ToNot(HaveOccurred(), "Failed to compile policy name regex %s", policyName)

policyRegexps = append(policyRegexps, policyRegexp)
}

allPolicies, err := ocm.ListPoliciesInAllNamespaces(client)
Expect(err).ToNot(HaveOccurred(), "Failed to list policies in all namespaces")

var expectedPolicies []*ocm.PolicyBuilder

for _, policyName := range policyNames {
for _, policyRegexp := range policyRegexps {
for _, policy := range allPolicies {
if policy.Object.Name == policyName {
if policyRegexp.MatchString(policy.Object.Name) {
expectedPolicies = append(expectedPolicies, policy)

break
Expand All @@ -627,8 +640,14 @@ func checkPoliciesExist(client *clients.Settings, policyNames []string) ([]*ocm.
return expectedPolicies, len(expectedPolicies) == len(policyNames)
}

func copyPoliciesWithSubscription(policies []*ocm.PolicyBuilder) []string {
var suffixes []string
// copyPoliciesWithSubscription copies the policies that have a subscription and makes them NonCompliant. Policies
// without a subscription have their names returned first and then the second return is the suffixes for policies that
// were copied.
func copyPoliciesWithSubscription(policies []*ocm.PolicyBuilder) ([]string, []string) {
var (
originals []string
suffixes []string
)

for index, policy := range policies {
glog.V(tsparams.LogLevel).Infof(
Expand All @@ -638,6 +657,8 @@ func copyPoliciesWithSubscription(policies []*ocm.PolicyBuilder) []string {
configPolicy, err := ranhelper.UnmarshalRaw[configurationPolicyv1.ConfigurationPolicy](template.ObjectDefinition.Raw)
Expect(err).ToNot(HaveOccurred(), "Failed to unmarshal config policy")

hadSubscription := false

for _, objectTemplate := range configPolicy.Spec.ObjectTemplates {
untyped := &unstructured.Unstructured{}
err := untyped.UnmarshalJSON(objectTemplate.ObjectDefinition.Raw)
Expand All @@ -648,6 +669,8 @@ func copyPoliciesWithSubscription(policies []*ocm.PolicyBuilder) []string {
continue
}

hadSubscription = true

// if the current policy has a subscription then copy the policy and force it to be non-compliant
suffix := fmt.Sprintf("-with-subscription-%d", index)
suffixes = append(suffixes, suffix)
Expand Down Expand Up @@ -692,7 +715,11 @@ func copyPoliciesWithSubscription(policies []*ocm.PolicyBuilder) []string {

break
}

if !hadSubscription {
originals = append(originals, policy.Definition.Name)
}
}

return suffixes
return originals, suffixes
}

0 comments on commit be66204

Please sign in to comment.