Skip to content

Commit c275389

Browse files
Merge pull request #1144 from Xia-Zhao-rh/fix-opm-cases
OCPQE-30680: Fix OPM test cases failure: Ensure container policy.json exists
2 parents dfc1cfc + f2b4126 commit c275389

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

tests-extension/test/qe/specs/olmv0_opm.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
7070
})
7171

7272
g.It("PolarionID:43171-[OTP][Skipped:Disconnected] opm render blob from bundle db based index dc based index db file and directory", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:43171-[Skipped:Disconnected] opm render blob from bundle db based index dc based index db file and directory"), func() {
73+
err := opmcli.EnsureContainerPolicy()
74+
o.Expect(err).NotTo(o.HaveOccurred())
75+
7376
g.By("render db-based index image")
7477
output, err := opmCLI.Run("render").Args("quay.io/olmqe/olm-index:OLM-2199").Output()
7578
o.Expect(err).NotTo(o.HaveOccurred())
@@ -236,6 +239,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
236239
})
237240

238241
g.It("PolarionID:45402-[OTP][Skipped:Disconnected] opm render should automatically pulling in the images used in the deployments", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:45402-[Skipped:Disconnected] opm render should automatically pulling in the images used in the deployments"), func() {
242+
err := opmcli.EnsureContainerPolicy()
243+
o.Expect(err).NotTo(o.HaveOccurred())
244+
239245
g.By("render bundle image")
240246
output, err := opmCLI.Run("render").Args("quay.io/olmqe/mta-operator:v0.0.4-45402", "quay.io/olmqe/eclipse-che:7.32.2-45402", "-oyaml").Output()
241247
o.Expect(err).NotTo(o.HaveOccurred())
@@ -261,6 +267,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
261267
})
262268

263269
g.It("PolarionID:48438-[OTP][Skipped:Disconnected] opm render should support olm.constraint which is defined in dependencies", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:48438-[Skipped:Disconnected] opm render should support olm.constraint which is defined in dependencies"), func() {
270+
err := opmcli.EnsureContainerPolicy()
271+
o.Expect(err).NotTo(o.HaveOccurred())
272+
264273
g.By("render bundle image")
265274
output, err := opmCLI.Run("render").Args("quay.io/olmqe/etcd-bundle:v0.9.2-48438", "-oyaml").Output()
266275
o.Expect(err).NotTo(o.HaveOccurred())
@@ -272,6 +281,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
272281
})
273282

274283
g.It("PolarionID:70013-[OTP][Skipped:Disconnected] opm support deprecated channel", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:70013-[Skipped:Disconnected] opm support deprecated channel"), func() {
284+
err := opmcli.EnsureContainerPolicy()
285+
o.Expect(err).NotTo(o.HaveOccurred())
286+
275287
opmBaseDir := exutil.FixturePath("testdata", "opm", "70013")
276288
opmCLI.ExecCommandPath = opmBaseDir
277289

@@ -302,6 +314,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
302314
})
303315

304316
g.It("PolarionID:54168-[OTP][Skipped:Disconnected] opm support '--use-http' global flag", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:54168-[Skipped:Disconnected] opm support '--use-http' global flag"), func() {
317+
err := opmcli.EnsureContainerPolicy()
318+
o.Expect(err).NotTo(o.HaveOccurred())
319+
305320
if os.Getenv("HTTP_PROXY") != "" || os.Getenv("http_proxy") != "" {
306321
g.Skip("HTTP_PROXY is not empty - skipping test ...")
307322
}
@@ -345,6 +360,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
345360
})
346361

347362
g.It("PolarionID:43409-[OTP][Skipped:Disconnected] opm can list catalog contents", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:43409-[Skipped:Disconnected] opm can list catalog contents"), func() {
363+
err := opmcli.EnsureContainerPolicy()
364+
o.Expect(err).NotTo(o.HaveOccurred())
365+
348366
dcimagetag := "quay.io/olmqe/nginxolm-operator-index:v1"
349367
g.By("1, testing with dc format index image")
350368
g.By("1.1 list packages")
@@ -415,6 +433,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
415433
})
416434

417435
g.It("PolarionID:53869-[OTP][Skipped:Disconnected] opm supports creating a catalog using basic veneer", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:53869-[Skipped:Disconnected] opm supports creating a catalog using basic veneer"), func() {
436+
err := opmcli.EnsureContainerPolicy()
437+
o.Expect(err).NotTo(o.HaveOccurred())
438+
418439
if os.Getenv("HTTP_PROXY") != "" || os.Getenv("http_proxy") != "" {
419440
g.Skip("HTTP_PROXY is not empty - skipping test ...")
420441
}
@@ -423,7 +444,7 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
423444

424445
g.By("step: create dir catalog")
425446
catsrcPathYaml := filepath.Join(opmBaseDir, "catalog-yaml")
426-
err := os.MkdirAll(catsrcPathYaml, 0755)
447+
err = os.MkdirAll(catsrcPathYaml, 0755)
427448
o.Expect(err).NotTo(o.HaveOccurred())
428449

429450
g.By("step: create a catalog using basic veneer with yaml format")
@@ -646,6 +667,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
646667
})
647668

648669
g.It("PolarionID:53917-[OTP][Skipped:Disconnected] opm can visualize the update graph for a given Operator from an arbitrary version", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:53917-[Skipped:Disconnected] opm can visualize the update graph for a given Operator from an arbitrary version"), func() {
670+
err := opmcli.EnsureContainerPolicy()
671+
o.Expect(err).NotTo(o.HaveOccurred())
672+
649673
if os.Getenv("HTTP_PROXY") != "" || os.Getenv("http_proxy") != "" {
650674
g.Skip("HTTP_PROXY is not empty - skipping test ...")
651675
}
@@ -702,6 +726,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
702726
})
703727

704728
g.It("PolarionID:60573-[OTP][Skipped:Disconnected] opm exclude bundles with olm.deprecated property when rendering", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:60573-[Skipped:Disconnected] opm exclude bundles with olm.deprecated property when rendering"), func() {
729+
err := opmcli.EnsureContainerPolicy()
730+
o.Expect(err).NotTo(o.HaveOccurred())
731+
705732
g.By("opm render the sqlite index image message")
706733
msg, err := opmCLI.Run("render").Args("quay.io/olmqe/catalogtest-index:v4.12depre", "-oyaml").Output()
707734
o.Expect(err).NotTo(o.HaveOccurred())
@@ -720,6 +747,9 @@ var _ = g.Describe("[sig-operator][Jira:OLM] OLMv0 opm should", g.Label("NonHype
720747
})
721748

722749
g.It("PolarionID:73218-[OTP][Skipped:Disconnected] opm alpha render-graph indicate deprecated graph content", g.Label("original-name:[sig-operator][Jira:OLM] OLMv0 opm should PolarionID:73218-[Skipped:Disconnected] opm alpha render-graph indicate deprecated graph content"), func() {
750+
err := opmcli.EnsureContainerPolicy()
751+
o.Expect(err).NotTo(o.HaveOccurred())
752+
723753
if os.Getenv("HTTP_PROXY") != "" || os.Getenv("http_proxy") != "" {
724754
g.Skip("HTTP_PROXY is not empty - skipping test ...")
725755
}

tests-extension/test/qe/util/opmcli/opm_client.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"runtime/debug"
1414
"strings"
15+
"sync"
1516

1617
g "github.com/onsi/ginkgo/v2"
1718
e2e "k8s.io/kubernetes/test/e2e/framework"
@@ -232,3 +233,62 @@ func DeleteDir(filePathStr string, filePre string) bool {
232233
return true
233234
}
234235
}
236+
237+
var (
238+
// policyMutex ensures thread-safe access to container policy file creation
239+
policyMutex sync.Mutex
240+
// policyCreated tracks whether policy.json has been created to avoid redundant operations
241+
policyCreated bool
242+
)
243+
244+
// EnsureContainerPolicy ensures that the containers policy.json file exists.
245+
// Creates ${HOME}/.config/containers/policy.json with insecureAcceptAnything policy if it doesn't exist.
246+
// This is required for OPM commands that interact with container images.
247+
// This function is thread-safe and can be called concurrently from parallel test cases.
248+
// Returns error if HOME is not set or if file creation fails.
249+
func EnsureContainerPolicy() error {
250+
// Use mutex to ensure only one goroutine creates the file at a time
251+
policyMutex.Lock()
252+
defer policyMutex.Unlock()
253+
254+
// If already created in a previous call, just verify it exists
255+
if policyCreated {
256+
return nil
257+
}
258+
259+
homeDir := os.Getenv("HOME")
260+
if homeDir == "" {
261+
return fmt.Errorf("HOME environment variable is not set")
262+
}
263+
policyDir := filepath.Join(homeDir, ".config", "containers")
264+
policyFile := filepath.Join(policyDir, "policy.json")
265+
266+
// Check if policy.json exists
267+
if _, err := os.Stat(policyFile); os.IsNotExist(err) {
268+
// Create directory if it doesn't exist
269+
if err := os.MkdirAll(policyDir, 0755); err != nil {
270+
return fmt.Errorf("failed to create directory %s: %w", policyDir, err)
271+
}
272+
273+
// Create policy.json with insecure accept anything policy
274+
policyContent := `{
275+
"default": [
276+
{
277+
"type": "insecureAcceptAnything"
278+
}
279+
]
280+
}
281+
`
282+
if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil {
283+
return fmt.Errorf("failed to create policy.json at %s: %w", policyFile, err)
284+
}
285+
e2e.Logf("Created containers policy.json at: %s", policyFile)
286+
policyCreated = true
287+
} else if err != nil {
288+
return fmt.Errorf("failed to check policy.json at %s: %w", policyFile, err)
289+
} else {
290+
e2e.Logf("Containers policy.json already exists at: %s", policyFile)
291+
policyCreated = true
292+
}
293+
return nil
294+
}

0 commit comments

Comments
 (0)