88 "fmt"
99 "strconv"
1010 "testing"
11+ "time"
1112
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1314 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -24,6 +25,10 @@ import (
2425)
2526
2627var (
28+ // We need a few resources such as KMS vault and container image pre-created
29+ // Therefore, before running the signature tests below, please first set the following env var:
30+ // TF_VAR_container_image_ocid, TF_VAR_kms_vault_ocid, TF_VAR_management_endpoint, and TF_VAR_container_image_signing_signature
31+
2732 message = utils .GetEnvSettingWithBlankDefault ("container_image_signing_signature" )
2833 signingAlgorithm = "SHA_224_RSA_PKCS_PSS"
2934 signingAlgorithmStr = fmt .Sprintf ("variable \" signingAlgorithm\" { default = \" %s\" }\n " , signingAlgorithm )
@@ -55,12 +60,19 @@ var (
5560 "message" : acctest.Representation {RepType : acctest .Required , Create : message },
5661 "signature" : acctest.Representation {RepType : acctest .Required , Create : `${oci_kms_sign.test_container_image_signature_kms_sign.signature}` },
5762 "signing_algorithm" : acctest.Representation {RepType : acctest .Required , Create : signingAlgorithm },
63+ "defined_tags" : acctest.Representation {RepType : acctest .Optional , Create : `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "value")}` , Update : `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "updatedValue")}` },
64+ "freeform_tags" : acctest.Representation {RepType : acctest .Optional , Create : map [string ]string {"Department" : "Finance" }, Update : map [string ]string {"Department" : "Accounting" }},
5865 }
5966
6067 ArtifactsArtifactscontainerContainerImageSignatureSingularDataSourceRepresentation = map [string ]interface {}{
6168 "image_signature_id" : acctest.Representation {RepType : acctest .Required , Create : `${oci_artifacts_container_image_signature.test_container_image_signature.id}` },
6269 }
6370
71+ ArtifactsArtifactscontainerContainerImageSignatureResourceDependencies = descriptionStr + metadataStr + signingAlgorithmStr +
72+ DefinedTagsDependencies +
73+ ArtifactsArtifactscontainerImageResourceConfig +
74+ ArtifactsArtifactscontainerContainerImageSignatureKmsSignResourceDependencies
75+
6476 ArtifactsArtifactscontainerContainerImageSignatureDataSourceRepresentation = map [string ]interface {}{
6577 "compartment_id" : acctest.Representation {RepType : acctest .Required , Create : `${data.oci_artifacts_container_image.test_container_image.compartment_id}` },
6678 "compartment_id_in_subtree" : acctest.Representation {RepType : acctest .Optional , Create : `false` },
@@ -88,18 +100,18 @@ func TestArtifactsContainerImageSignatureResource_basic(t *testing.T) {
88100
89101 config := acctest .ProviderTestConfig ()
90102
103+ compartmentId := utils .GetEnvSettingWithBlankDefault ("compartment_ocid" )
104+
91105 resourceName := "oci_artifacts_container_image_signature.test_container_image_signature"
92106 datasourceName := "data.oci_artifacts_container_image_signatures.test_container_image_signatures"
93107 singularDatasourceName := "data.oci_artifacts_container_image_signature.test_container_image_signature"
94108
95- var resId string
109+ var resId , resId2 string
96110
97111 acctest .ResourceTest (t , testAccCheckArtifactsContainerImageSignatureDestroy , []resource.TestStep {
98112 // verify Create
99113 {
100- Config : config + descriptionStr + metadataStr + signingAlgorithmStr +
101- ArtifactsArtifactscontainerImageResourceConfig +
102- ArtifactsArtifactscontainerContainerImageSignatureKmsSignResourceDependencies +
114+ Config : config + ArtifactsArtifactscontainerContainerImageSignatureResourceDependencies +
103115 acctest .GenerateResourceFromRepresentationMap ("oci_artifacts_container_image_signature" , "test_container_image_signature" , acctest .Required , acctest .Create , ArtifactsArtifactscontainerContainerImageSignatureRepresentation ),
104116 Check : acctest .ComposeAggregateTestCheckFuncWrapper (
105117 resource .TestCheckResourceAttr (resourceName , "compartment_id" , compartmentId ),
@@ -113,6 +125,37 @@ func TestArtifactsContainerImageSignatureResource_basic(t *testing.T) {
113125 resource .TestCheckResourceAttrSet (resourceName , "time_created" ),
114126 resource .TestCheckResourceAttr (resourceName , "signing_algorithm" , signingAlgorithm ),
115127
128+ func (s * terraform.State ) (err error ) {
129+ resId , err = acctest .FromInstanceState (s , resourceName , "id" )
130+ return err
131+ },
132+ ),
133+ },
134+
135+ // delete before next Create
136+ {
137+ Config : config + ArtifactsArtifactscontainerImageResourceConfig ,
138+ },
139+ // verify Create with optionals
140+ {
141+ Config : config + ArtifactsArtifactscontainerContainerImageSignatureResourceDependencies +
142+ acctest .GenerateResourceFromRepresentationMap ("oci_artifacts_container_image_signature" , "test_container_image_signature" , acctest .Optional , acctest .Create , ArtifactsArtifactscontainerContainerImageSignatureRepresentation ),
143+ Check : acctest .ComposeAggregateTestCheckFuncWrapper (
144+ resource .TestCheckResourceAttr (resourceName , "compartment_id" , compartmentId ),
145+ resource .TestCheckResourceAttrSet (resourceName , "created_by" ),
146+ resource .TestCheckResourceAttrSet (resourceName , "display_name" ),
147+ resource .TestCheckResourceAttr (resourceName , "freeform_tags.%" , "1" ),
148+ resource .TestCheckResourceAttr (resourceName , "defined_tags.%" , "1" ),
149+ resource .TestCheckResourceAttrSet (resourceName , "id" ),
150+ resource .TestCheckResourceAttrSet (resourceName , "image_id" ),
151+ resource .TestCheckResourceAttrSet (resourceName , "kms_key_id" ),
152+ resource .TestCheckResourceAttrSet (resourceName , "kms_key_version_id" ),
153+ resource .TestCheckResourceAttr (resourceName , "message" , message ),
154+ resource .TestCheckResourceAttrPair (resourceName , "signature" , "oci_kms_sign.test_container_image_signature_kms_sign" , "signature" ),
155+ resource .TestCheckResourceAttr (resourceName , "signing_algorithm" , signingAlgorithm ),
156+ resource .TestCheckResourceAttrSet (resourceName , "state" ),
157+ resource .TestCheckResourceAttrSet (resourceName , "time_created" ),
158+
116159 func (s * terraform.State ) (err error ) {
117160 resId , err = acctest .FromInstanceState (s , resourceName , "id" )
118161 if isEnableExportCompartment , _ := strconv .ParseBool (utils .GetEnvSettingWithDefault ("enable_export_compartment" , "true" )); isEnableExportCompartment {
@@ -125,11 +168,38 @@ func TestArtifactsContainerImageSignatureResource_basic(t *testing.T) {
125168 ),
126169 },
127170
171+ // verify updates to updatable parameters
172+ {
173+ Config : config + ArtifactsArtifactscontainerContainerImageSignatureResourceDependencies +
174+ acctest .GenerateResourceFromRepresentationMap ("oci_artifacts_container_image_signature" , "test_container_image_signature" , acctest .Optional , acctest .Update , ArtifactsArtifactscontainerContainerImageSignatureRepresentation ),
175+ Check : acctest .ComposeAggregateTestCheckFuncWrapper (
176+ resource .TestCheckResourceAttr (resourceName , "compartment_id" , compartmentId ),
177+ resource .TestCheckResourceAttrSet (resourceName , "created_by" ),
178+ resource .TestCheckResourceAttrSet (resourceName , "display_name" ),
179+ resource .TestCheckResourceAttr (resourceName , "freeform_tags.%" , "1" ),
180+ resource .TestCheckResourceAttr (resourceName , "defined_tags.%" , "1" ),
181+ resource .TestCheckResourceAttrSet (resourceName , "id" ),
182+ resource .TestCheckResourceAttrSet (resourceName , "image_id" ),
183+ resource .TestCheckResourceAttrSet (resourceName , "kms_key_id" ),
184+ resource .TestCheckResourceAttrSet (resourceName , "kms_key_version_id" ),
185+ resource .TestCheckResourceAttr (resourceName , "message" , message ),
186+ resource .TestCheckResourceAttrPair (resourceName , "signature" , "oci_kms_sign.test_container_image_signature_kms_sign" , "signature" ),
187+ resource .TestCheckResourceAttr (resourceName , "signing_algorithm" , signingAlgorithm ),
188+ resource .TestCheckResourceAttrSet (resourceName , "state" ),
189+ resource .TestCheckResourceAttrSet (resourceName , "time_created" ),
190+
191+ func (s * terraform.State ) (err error ) {
192+ resId2 , err = acctest .FromInstanceState (s , resourceName , "id" )
193+ if resId != resId2 {
194+ return fmt .Errorf ("Resource recreated when it was supposed to be updated." )
195+ }
196+ return err
197+ },
198+ ),
199+ },
128200 // verify datasource
129201 {
130- Config : config + descriptionStr + metadataStr + signingAlgorithmStr +
131- ArtifactsArtifactscontainerImageResourceConfig +
132- ArtifactsArtifactscontainerContainerImageSignatureKmsSignResourceDependencies +
202+ Config : config + ArtifactsArtifactscontainerContainerImageSignatureResourceDependencies +
133203 acctest .GenerateResourceFromRepresentationMap ("oci_artifacts_container_image_signature" , "test_container_image_signature" , acctest .Optional , acctest .Update , ArtifactsArtifactscontainerContainerImageSignatureRepresentation ) +
134204 acctest .GenerateDataSourceFromRepresentationMap ("oci_artifacts_container_image_signatures" , "test_container_image_signatures" , acctest .Optional , acctest .Update , ArtifactsArtifactscontainerContainerImageSignatureDataSourceRepresentation ),
135205 Check : acctest .ComposeAggregateTestCheckFuncWrapper (
@@ -146,25 +216,28 @@ func TestArtifactsContainerImageSignatureResource_basic(t *testing.T) {
146216
147217 resource .TestCheckResourceAttr (datasourceName , "container_image_signature_collection.#" , "1" ),
148218 resource .TestCheckResourceAttr (datasourceName , "container_image_signature_collection.0.items.#" , "1" ),
219+ resource .TestCheckResourceAttr (datasourceName , "container_image_signature_collection.0.items.0.defined_tags.%" , "1" ),
220+ resource .TestCheckResourceAttr (datasourceName , "container_image_signature_collection.0.items.0.freeform_tags.%" , "1" ),
149221 ),
150222 },
151223
152224 // verify singular datasource
153225 {
154- Config : config + descriptionStr + metadataStr + signingAlgorithmStr +
155- ArtifactsArtifactscontainerImageResourceConfig +
156- ArtifactsArtifactscontainerContainerImageSignatureKmsSignResourceDependencies +
226+ Config : config + ArtifactsArtifactscontainerContainerImageSignatureResourceDependencies +
157227 acctest .GenerateResourceFromRepresentationMap ("oci_artifacts_container_image_signature" , "test_container_image_signature" , acctest .Optional , acctest .Update , ArtifactsArtifactscontainerContainerImageSignatureRepresentation ) +
158228 acctest .GenerateDataSourceFromRepresentationMap ("oci_artifacts_container_image_signature" , "test_container_image_signature" , acctest .Required , acctest .Create , ArtifactsArtifactscontainerContainerImageSignatureSingularDataSourceRepresentation ),
159229 Check : acctest .ComposeAggregateTestCheckFuncWrapper (
160230 resource .TestCheckResourceAttrSet (singularDatasourceName , "image_signature_id" ),
161231 resource .TestCheckResourceAttr (singularDatasourceName , "compartment_id" , compartmentId ),
162232 resource .TestCheckResourceAttrSet (singularDatasourceName , "created_by" ),
163233 resource .TestCheckResourceAttrSet (singularDatasourceName , "display_name" ),
234+ resource .TestCheckResourceAttr (singularDatasourceName , "freeform_tags.%" , "1" ),
235+ resource .TestCheckResourceAttr (singularDatasourceName , "defined_tags.%" , "1" ),
164236 resource .TestCheckResourceAttrSet (singularDatasourceName , "id" ),
165237 //resource.TestCheckResourceAttr(singularDatasourceName, "message", encodedMessage),
166238 resource .TestCheckResourceAttrSet (singularDatasourceName , "signature" ),
167239 resource .TestCheckResourceAttr (singularDatasourceName , "signing_algorithm" , signingAlgorithm ),
240+ resource .TestCheckResourceAttrSet (singularDatasourceName , "state" ),
168241 resource .TestCheckResourceAttrSet (singularDatasourceName , "time_created" ),
169242 ),
170243 },
@@ -193,10 +266,18 @@ func testAccCheckArtifactsContainerImageSignatureDestroy(s *terraform.State) err
193266
194267 request .RequestMetadata .RetryPolicy = tfresource .GetRetryPolicy (true , "artifacts" )
195268
196- _ , err := client .GetContainerImageSignature (context .Background (), request )
269+ response , err := client .GetContainerImageSignature (context .Background (), request )
197270
198271 if err == nil {
199- return fmt .Errorf ("resource still exists" )
272+ deletedLifecycleStates := map [string ]bool {
273+ string (oci_artifacts .ContainerImageSignatureLifecycleStateDeleted ): true ,
274+ }
275+ if _ , ok := deletedLifecycleStates [string (response .LifecycleState )]; ! ok {
276+ //resource lifecycle state is not in expected deleted lifecycle states.
277+ return fmt .Errorf ("resource lifecycle state: %s is not in expected deleted lifecycle states" , response .LifecycleState )
278+ }
279+ //resource lifecycle state is in expected deleted lifecycle states. continue with next one.
280+ continue
200281 }
201282
202283 //Verify that exception is for '404 not found'.
@@ -241,6 +322,8 @@ func sweepArtifactsContainerImageSignatureResource(compartment string) error {
241322 fmt .Printf ("Error deleting ContainerImageSignature %s %s, It is possible that the resource is already deleted. Please verify manually \n " , containerImageSignatureId , error )
242323 continue
243324 }
325+ acctest .WaitTillCondition (acctest .TestAccProvider , & containerImageSignatureId , ArtifactsContainerImageSignatureSweepWaitCondition , time .Duration (3 * time .Minute ),
326+ ArtifactsContainerImageSignatureSweepResponseFetchOperation , "artifacts" , true )
244327 }
245328 }
246329 return nil
@@ -269,3 +352,19 @@ func getContainerImageSignatureIds(compartment string) ([]string, error) {
269352 }
270353 return resourceIds , nil
271354}
355+
356+ func ArtifactsContainerImageSignatureSweepWaitCondition (response common.OCIOperationResponse ) bool {
357+ // Only stop if the resource is available beyond 3 mins. As there could be an issue for the sweeper to delete the resource and manual intervention required.
358+ if containerImageSignatureResponse , ok := response .Response .(oci_artifacts.GetContainerImageSignatureResponse ); ok {
359+ return containerImageSignatureResponse .LifecycleState != oci_artifacts .ContainerImageSignatureLifecycleStateDeleted
360+ }
361+ return false
362+ }
363+
364+ func ArtifactsContainerImageSignatureSweepResponseFetchOperation (client * tf_client.OracleClients , resourceId * string , retryPolicy * common.RetryPolicy ) error {
365+ _ , err := client .ArtifactsClient ().GetContainerImageSignature (context .Background (), oci_artifacts.GetContainerImageSignatureRequest {RequestMetadata : common.RequestMetadata {
366+ RetryPolicy : retryPolicy ,
367+ },
368+ })
369+ return err
370+ }
0 commit comments