77 "crypto/md5"
88 "encoding/hex"
99 "fmt"
10+ "io/ioutil"
1011 "regexp"
1112 "testing"
1213
@@ -316,11 +317,15 @@ func testAccCheckObjectStorageObjectDestroy(s *terraform.State) error {
316317 noResourceFound := true
317318 client := testAccProvider .Meta ().(* OracleClients ).objectStorageClient
318319
319- if _ , err := os .Stat (pathToSinglePartFile ); err == nil {
320- os .Remove (pathToSinglePartFile )
320+ if singlePartFile != nil {
321+ if _ , err := os .Stat (singlePartFile .Name ()); err == nil {
322+ os .Remove (singlePartFile .Name ())
323+ }
321324 }
322- if _ , err := os .Stat (pathToMultiPartFile ); err == nil {
323- os .Remove (pathToMultiPartFile )
325+ if multiPartFile != nil {
326+ if _ , err := os .Stat (multiPartFile .Name ()); err == nil {
327+ os .Remove (multiPartFile .Name ())
328+ }
324329 }
325330
326331 for _ , rs := range s .RootModule ().Resources {
@@ -377,32 +382,39 @@ resource "oci_objectstorage_object" "test_object" {
377382`
378383
379384 //the object size is less than default part value, single part upload
380- pathToSinglePartFile = "small.tmp "
385+ singlePartFilePrefix = "small- "
381386 singlePartFileSize = 42e6
382387 opcSingleMd5 = "iMBtc3kGpfXuMgOX9sVm0Q=="
383388
384389 //the object will be split on 3 parts
385- pathToMultiPartFile = "large.tmp "
390+ multiPartFilePrefix = "large- "
386391 multiPartFileSize = 300e6
387392 opcMultipartMd5 = "leCtKnqvcLbdMUeTbjnKnA==-3"
388393)
389394
390- func createTmpFile (t * testing.T ) {
391- tmpfile , err := os .Create (pathToSinglePartFile )
395+ var (
396+ singlePartFile * os.File
397+ multiPartFile * os.File
398+ )
399+
400+ func createTmpFiles () (string , string , error ) {
401+ singlePartFile , err := ioutil .TempFile (os .TempDir (), singlePartFilePrefix )
392402 if err != nil {
393- t . Fatalf ( "Unable to create %s file to upload. Error: %q" , pathToSinglePartFile , err )
403+ return "" , "" , err
394404 }
395- if err := tmpfile .Truncate (singlePartFileSize ); err != nil {
396- t . Fatalf ( "unable to truncate %s file. Error: %q" , pathToSinglePartFile , err )
405+ if err := singlePartFile .Truncate (singlePartFileSize ); err != nil {
406+ return "" , "" , err
397407 }
398408
399- tmpfile , err = os .Create ( pathToMultiPartFile )
409+ multiPartFile , err = ioutil . TempFile ( os .TempDir (), multiPartFilePrefix )
400410 if err != nil {
401- t . Fatalf ( "Unable to create %s file to upload. Error: %q" , pathToMultiPartFile , err )
411+ return "" , "" , err
402412 }
403- if err := tmpfile .Truncate (multiPartFileSize ); err != nil {
404- t . Fatalf ( "Unable to truncate %s file. Error: %q" , pathToMultiPartFile , err )
413+ if err := multiPartFile .Truncate (multiPartFileSize ); err != nil {
414+ return "" , "" , err
405415 }
416+
417+ return singlePartFile .Name (), multiPartFile .Name (), nil
406418}
407419
408420func TestObjectStorageObjectResource_multipartUpload (t * testing.T ) {
@@ -417,8 +429,15 @@ func TestObjectStorageObjectResource_multipartUpload(t *testing.T) {
417429
418430 var resId , resId2 string
419431
432+ singlePartFilePath , multiPartFilePath , err := createTmpFiles ()
433+ if err != nil {
434+ t .Fatalf ("Unable to create files to upload. Error: %q" , err )
435+ }
436+ singlePartFileVariable := fmt .Sprintf ("variable \" object_source\" { default = \" %s\" }\n " , singlePartFilePath )
437+ multiPartFileVariable := fmt .Sprintf ("variable \" object_source\" { default = \" %s\" }\n " , multiPartFilePath )
438+
420439 resource .Test (t , resource.TestCase {
421- PreCheck : func () { testAccPreCheck (t ); createTmpFile ( t ) },
440+ PreCheck : func () { testAccPreCheck (t ) },
422441 Providers : map [string ]terraform.ResourceProvider {
423442 "oci" : provider ,
424443 },
@@ -452,9 +471,7 @@ func TestObjectStorageObjectResource_multipartUpload(t *testing.T) {
452471 },
453472 // verify create singlepart with optionals
454473 {
455- Config : config + ObjectPropertyVariables + `
456- variable "object_source" { default = "` + pathToSinglePartFile + `" }
457- ` + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
474+ Config : config + ObjectPropertyVariables + singlePartFileVariable + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
458475 Check : resource .ComposeAggregateTestCheckFunc (
459476 resource .TestCheckResourceAttr (resourceName , "content_encoding" , "identity" ),
460477 resource .TestCheckResourceAttr (resourceName , "content_language" , "en-US" ),
@@ -481,9 +498,7 @@ func TestObjectStorageObjectResource_multipartUpload(t *testing.T) {
481498 },
482499 // verify create with optionals
483500 {
484- Config : config + ObjectPropertyVariables + `
485- variable "object_source" { default = "` + pathToMultiPartFile + `" }
486- ` + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
501+ Config : config + ObjectPropertyVariables + multiPartFileVariable + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
487502 Check : resource .ComposeAggregateTestCheckFunc (
488503 resource .TestCheckResourceAttr (resourceName , "content_encoding" , "identity" ),
489504 resource .TestCheckResourceAttr (resourceName , "content_language" , "en-US" ),
@@ -511,9 +526,8 @@ variable "object_content_language" { default = "en-CA" }
511526variable "object_content_type" { default = "text/xml" }
512527variable "object_metadata" { default = {"content-type" = "text/xml"} }
513528variable "object_object" { default = "my-test-object-3" }
514- variable "object_source" { default = "` + pathToMultiPartFile + `" }
515529
516- ` + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
530+ ` + multiPartFileVariable + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
517531 Check : resource .ComposeAggregateTestCheckFunc (
518532 resource .TestCheckResourceAttr (resourceName , "content_encoding" , "identity" ),
519533 resource .TestCheckResourceAttr (resourceName , "content_language" , "en-CA" ),
@@ -545,7 +559,6 @@ variable "object_content_language" { default = "en-CA" }
545559variable "object_content_type" { default = "text/xml" }
546560variable "object_metadata" { default = {"content-type" = "text/xml"} }
547561variable "object_object" { default = "my-test-object-1" }
548- variable "object_source" { default = "` + pathToMultiPartFile + `" }
549562
550563data "oci_objectstorage_objects" "test_objects" {
551564 #Required
@@ -557,7 +570,7 @@ data "oci_objectstorage_objects" "test_objects" {
557570 values = ["${oci_objectstorage_object.test_object.object}"]
558571 }
559572}
560- ` + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
573+ ` + multiPartFileVariable + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
561574 Check : resource .ComposeAggregateTestCheckFunc (
562575 resource .TestCheckResourceAttr (datasourceName , "bucket" , "my-test-1" ),
563576 resource .TestCheckResourceAttrSet (datasourceName , "namespace" ),
@@ -571,7 +584,6 @@ data "oci_objectstorage_objects" "test_objects" {
571584variable "object_content_encoding" { default = "identity" }
572585variable "object_content_language" { default = "en-CA" }
573586variable "object_content_type" { default = "text/xml" }
574- variable "object_source" { default = "` + pathToMultiPartFile + `" }
575587variable "object_metadata" { default = {"content-type" = "text/xml"} }
576588variable "object_object" { default = "my-test/object-1" }
577589
@@ -588,7 +600,7 @@ data "oci_objectstorage_objects" "test_objects" {
588600 values = ["${oci_objectstorage_object.test_object.object}"]
589601 }
590602}
591- ` + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
603+ ` + multiPartFileVariable + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
592604 Check : resource .ComposeAggregateTestCheckFunc (
593605 resource .TestCheckResourceAttr (datasourceName , "bucket" , "my-test-1" ),
594606 resource .TestCheckResourceAttrSet (datasourceName , "namespace" ),
@@ -689,14 +701,17 @@ resource "oci_objectstorage_object" "test_object_copy" {
689701`
690702)
691703
692- func createTmpObjectInOtherRegion (t * testing.T ) {
693- tmpfile , err := os .Create (pathToSinglePartFile )
704+ func createTmpObjectInOtherRegion () (string , error ) {
705+ // now running tests in one region
706+ singlePartFile , err := ioutil .TempFile (os .TempDir (), singlePartFilePrefix )
694707 if err != nil {
695- t . Fatalf ( "Unable to create %s file to upload. Error: %q" , pathToSinglePartFile , err )
708+ return "" , err
696709 }
697- if err := tmpfile .Truncate (singlePartFileSize ); err != nil {
698- t . Fatalf ( "unable to truncate %s file. Error: %q" , pathToSinglePartFile , err )
710+ if err := singlePartFile .Truncate (singlePartFileSize ); err != nil {
711+ return "" , err
699712 }
713+
714+ return singlePartFile .Name (), nil
700715}
701716
702717func TestObjectStorageObjectResource_crossRegionCopy (t * testing.T ) {
@@ -706,6 +721,12 @@ func TestObjectStorageObjectResource_crossRegionCopy(t *testing.T) {
706721 compartmentId := getEnvSettingWithBlankDefault ("compartment_ocid" )
707722 compartmentIdVariableStr := fmt .Sprintf ("variable \" compartment_id\" { default = \" %s\" }\n " , compartmentId )
708723
724+ singlePartFilePath , err := createTmpObjectInOtherRegion ()
725+ if err != nil {
726+ t .Fatalf ("Unable to create file to upload. Error: %q" , err )
727+ }
728+ singlePartFileVariable := fmt .Sprintf ("variable \" object_source\" { default = \" %s\" }\n " , singlePartFilePath )
729+
709730 resourceName := "oci_objectstorage_object.test_object"
710731 resourceNameCopy := "oci_objectstorage_object.test_object_copy"
711732
@@ -714,17 +735,15 @@ func TestObjectStorageObjectResource_crossRegionCopy(t *testing.T) {
714735 md5sum := hex .EncodeToString (hexSum [:])
715736
716737 resource .Test (t , resource.TestCase {
717- PreCheck : func () { testAccPreCheck (t ); createTmpObjectInOtherRegion ( t ) },
738+ PreCheck : func () { testAccPreCheck (t ) },
718739 Providers : map [string ]terraform.ResourceProvider {
719740 "oci" : provider ,
720741 },
721742 CheckDestroy : testAccCheckObjectStorageObjectDestroy ,
722743 Steps : []resource.TestStep {
723744 // create from source with options to copy
724745 {
725- Config : config + ObjectPropertyVariables + `
726- variable "object_source" { default = "` + pathToSinglePartFile + `" }
727- ` + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
746+ Config : config + ObjectPropertyVariables + singlePartFileVariable + compartmentIdVariableStr + ObjectResourceConfigWithSource ,
728747 Check : resource .ComposeAggregateTestCheckFunc (
729748 resource .TestCheckResourceAttr (resourceName , "content_encoding" , "identity" ),
730749 resource .TestCheckResourceAttr (resourceName , "content_language" , "en-US" ),
@@ -743,9 +762,8 @@ func TestObjectStorageObjectResource_crossRegionCopy(t *testing.T) {
743762 {
744763 Config : config + ObjectPropertyVariables + `
745764 variable "object_copy2_metadata" { default = {"content-type" = "text/plain-copy"} }
746- variable "object_object_copy" { default = "my-test-object-1-copy" }
747- variable "object_source" { default = "` + pathToSinglePartFile + `" }
748- ` + compartmentIdVariableStr + ObjectResourceConfigWithSourceURIFromContentObjectWithoutSourceEtag + ObjectResourceConfigWithSource ,
765+ variable "object_object_copy" { default = "my-test-object-1-copy" }` +
766+ singlePartFileVariable + compartmentIdVariableStr + ObjectResourceConfigWithSourceURIFromContentObjectWithoutSourceEtag + ObjectResourceConfigWithSource ,
749767 Check : resource .ComposeAggregateTestCheckFunc (
750768 resource .TestCheckResourceAttrSet (resourceNameCopy , "namespace" ),
751769 resource .TestCheckResourceAttr (resourceNameCopy , "bucket" , "my-test-1" ),
@@ -854,11 +872,9 @@ func TestObjectStorageObjectResource_crossRegionCopy(t *testing.T) {
854872 // recreate copy of copy of content object by singlepart with optionals
855873 {
856874 Config : config + ObjectPropertyVariables + `
857- variable "object_source" { default = "` + pathToSinglePartFile + `" }
858875 variable "object_object_copy" { default = "my-test-object-1-copy" }
859876 variable "object_copy2_metadata" { default = {"content-type" = "text/plain-copy"} }
860-
861- ` + ObjectResourceConfigWithSourceURIFromContentObjectWithoutSourceEtag +
877+ ` + singlePartFileVariable + ObjectResourceConfigWithSourceURIFromContentObjectWithoutSourceEtag +
862878 compartmentIdVariableStr + ObjectResourceConfigWithSource ,
863879 Check : resource .ComposeAggregateTestCheckFunc (
864880 resource .TestCheckResourceAttr (resourceName , "content_encoding" , "identity" ),
0 commit comments