@@ -2,7 +2,13 @@ package cvo
22
33import (
44 "context"
5+ "fmt"
6+ "io"
7+ "os"
8+ "path/filepath"
9+ "strings"
510
11+ yamlv3 "gopkg.in/yaml.v3"
612 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
713 "k8s.io/client-go/kubernetes"
814 "k8s.io/client-go/rest"
@@ -80,4 +86,79 @@ var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator`,
8086 sccAnnotation := cvoPod .Annotations ["openshift.io/scc" ]
8187 o .Expect (sccAnnotation ).To (o .Equal ("hostaccess" ), "Expected the annotation 'openshift.io/scc annotation' on pod %s to have the value 'hostaccess', but got %s" , cvoPod .Name , sccAnnotation )
8288 })
89+
90+ g .It (`should not install resources annotated with release.openshift.io/delete=true` , g .Label ("Conformance" , "High" , "42543" ), func () {
91+ // Initialize the ocapi.OC instance
92+ g .By ("Setup ocapi.OC" )
93+ err := os .Setenv ("OC_CLI_TIMEOUT" , "90s" )
94+ o .Expect (err ).NotTo (o .HaveOccurred (), "Setup environment variable OC_CLI_TIMEOUT failed" )
95+ ocClient , err := oc .NewOC (logger )
96+ o .Expect (err ).NotTo (o .HaveOccurred ())
97+ o .Expect (ocClient ).NotTo (o .BeNil ())
98+ defer func () {
99+ err = os .Unsetenv ("OC_CLI_TIMEOUT" )
100+ o .Expect (err ).NotTo (o .HaveOccurred (), "Unset environment variable OC_CLI_TIMEOUT failed" )
101+ }()
102+
103+ g .By ("Extract manifests" )
104+ annotation := "release.openshift.io/delete"
105+ manifestDir := ocapi.ReleaseExtractOptions {To : "/tmp/OTA-42543-manifest" }
106+ logger .Info (fmt .Sprintf ("Extract manifests to: %s" , manifestDir .To ))
107+ defer func () { _ = os .RemoveAll (manifestDir .To ) }()
108+ err = ocClient .AdmReleaseExtract (manifestDir )
109+ o .Expect (err ).NotTo (o .HaveOccurred (), "extract manifests failed" )
110+
111+ entries , err := os .ReadDir (manifestDir .To )
112+ o .Expect (err ).NotTo (o .HaveOccurred ())
113+ g .By ("Start to iterate all manifests" )
114+ var closeFilePass = true
115+ for _ , entry := range entries {
116+ nameLower := strings .ToLower (entry .Name ())
117+ if strings .Contains (nameLower , "cleanup" ) {
118+ logger .Info (fmt .Sprintf ("Skipping file %s because it matches cleanup filter" , entry .Name ()))
119+ continue
120+ }
121+ filePath := filepath .Join (manifestDir .To , entry .Name ())
122+ file , err := os .Open (filePath )
123+ o .Expect (err ).NotTo (o .HaveOccurred ())
124+ defer func () {
125+ if ! closeFilePass {
126+ // Close the file again
127+ if err = file .Close (); err != nil {
128+ o .Expect (err ).NotTo (o .HaveOccurred (), "close file failed" )
129+ }
130+ }
131+ }()
132+ decoder := yamlv3 .NewDecoder (file )
133+ for {
134+ var doc map [string ]interface {}
135+ if err := decoder .Decode (& doc ); err != nil {
136+ if err == io .EOF {
137+ break
138+ }
139+ continue
140+ }
141+ meta , _ := doc ["metadata" ].(map [string ]interface {})
142+ ann , _ := meta ["annotations" ].(map [string ]interface {})
143+ if ann == nil || ann [annotation ] != "true" {
144+ continue
145+ }
146+ kind , _ := doc ["kind" ].(string )
147+ name , _ := meta ["name" ].(string )
148+ namespace , _ := meta ["namespace" ].(string )
149+ args := []string {"get" , kind , name }
150+ if namespace != "" {
151+ args = append (args , "-n" , namespace )
152+ }
153+ _ , err := ocClient .Run (args ... )
154+ o .Expect (err ).To (o .HaveOccurred (), "The deleted manifest should not be installed, but actually installed" )
155+ }
156+ // close each file
157+ err = file .Close ()
158+ if err != nil {
159+ closeFilePass = false
160+ o .Expect (err ).NotTo (o .HaveOccurred (), "close file failed" )
161+ }
162+ }
163+ })
83164})
0 commit comments