@@ -525,6 +525,93 @@ func TestResourceStorageBucketObjectUpdate_ContentChange(t *testing.T) {
525525 })
526526}
527527
528+ func TestAccStorageObject_sourceMd5Hash (t * testing.T ) {
529+ t .Parallel ()
530+
531+ bucketName := acctest .TestBucketName (t )
532+
533+ data := []byte ("data data data" )
534+
535+ writeMd5 := func (data []byte ) string {
536+ h := md5 .New ()
537+ if _ , err := h .Write (data ); err != nil {
538+ t .Errorf ("error calculating md5: %v" , err )
539+ }
540+ dataMd5 := base64 .StdEncoding .EncodeToString (h .Sum (nil ))
541+ return dataMd5
542+ }
543+
544+ dataMd5 := writeMd5 (data )
545+
546+ updatedata := []byte ("datum" )
547+ updatedDataMd5 := writeMd5 (updatedata )
548+
549+ testFile := getNewTmpTestFile (t , "tf-test" )
550+ if err := ioutil .WriteFile (testFile .Name (), data , 0644 ); err != nil {
551+ t .Errorf ("error writing file: %v" , err )
552+ }
553+
554+ updateMd5 := []byte ("sample" )
555+ newMd5 := writeMd5 (updateMd5 )
556+
557+ acctest .VcrTest (t , resource.TestCase {
558+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
559+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
560+ CheckDestroy : testAccStorageObjectDestroyProducer (t ),
561+ Steps : []resource.TestStep {
562+ {
563+ Config : testGoogleStorageBucketsObjectBasic (bucketName , testFile .Name ()),
564+ Check : testAccCheckGoogleStorageObject (t , bucketName , objectName , dataMd5 ),
565+ },
566+ {
567+ PreConfig : func () {
568+ if err := ioutil .WriteFile (testFile .Name (), updatedata , 0644 ); err != nil {
569+ t .Errorf ("error writing file: %v" , err )
570+ }
571+ },
572+ Config : testGoogleStorageBucketsObjectFileMd5 (bucketName , testFile .Name (), updatedDataMd5 ),
573+ Check : testAccCheckGoogleStorageObject (t , bucketName , objectName , updatedDataMd5 ),
574+ },
575+ {
576+ Config : testGoogleStorageBucketsObjectFileMd5 (bucketName , testFile .Name (), newMd5 ),
577+ Check : testAccCheckGoogleStorageObject (t , bucketName , objectName , updatedDataMd5 ),
578+ },
579+ },
580+ })
581+ }
582+
583+ func TestAccStorageObject_knownAfterApply (t * testing.T ) {
584+ t .Parallel ()
585+
586+ bucketName := acctest .TestBucketName (t )
587+ destinationFilePath := getNewTmpTestFile (t , "tf-test-apply-" )
588+
589+ acctest .VcrTest (t , resource.TestCase {
590+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
591+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
592+ CheckDestroy : testAccStorageObjectDestroyProducer (t ),
593+ ExternalProviders : map [string ]resource.ExternalProvider {
594+ "local" : resource.ExternalProvider {
595+ VersionConstraint : "> 2.5.0" ,
596+ },
597+ },
598+ Steps : []resource.TestStep {
599+ {
600+ Config : testGoogleStorageBucketObject (bucketName , "first" , destinationFilePath .Name ()),
601+ Check : resource .ComposeTestCheckFunc (
602+ testAccCheckGoogleStorageValidOutput (t ),
603+ ),
604+ },
605+ {
606+ Config : testGoogleStorageBucketObjectKnownAfterApply (bucketName , "second" , destinationFilePath .Name ()),
607+ Check : resource .ComposeTestCheckFunc (
608+ testAccCheckGoogleStorageValidOutput (t ),
609+ ),
610+ },
611+ },
612+ })
613+ }
614+
528615func testAccCheckGoogleStorageObject (t * testing.T , bucket , object , md5 string ) resource.TestCheckFunc {
529616 return testAccCheckGoogleStorageObjectWithEncryption (t , bucket , object , md5 , "" )
530617}
@@ -863,3 +950,112 @@ func getNewTmpTestFile(t *testing.T, prefix string) *os.File {
863950 }
864951 return testFile
865952}
953+
954+ func testGoogleStorageBucketsObjectFileMd5 (bucketName , sourceFilename , md5hash string ) string {
955+ return fmt .Sprintf (`
956+ resource "google_storage_bucket" "bucket" {
957+ name = "%s"
958+ location = "US"
959+ }
960+
961+ resource "google_storage_bucket_object" "bo_1861894" {
962+ name = "%s"
963+ source_md5hash = "%s"
964+ bucket = google_storage_bucket.bucket.name
965+ source = "%s"
966+ }
967+ ` , bucketName , objectName , md5hash , sourceFilename )
968+ }
969+
970+ func testAccCheckGoogleStorageValidOutput (t * testing.T ) resource.TestCheckFunc {
971+ return func (s * terraform.State ) error {
972+ var root = s .Modules [0 ]
973+ var outputs , ok = root .Outputs ["valid" ]
974+
975+ if ! ok {
976+ return fmt .Errorf ("Error: `valid` output missing" )
977+ }
978+
979+ if outputs == nil {
980+ return fmt .Errorf ("Terraform output `valid` does not exists" )
981+ }
982+
983+ if outputs .Value == false {
984+ return fmt .Errorf ("File content is not valid" )
985+ }
986+ return nil
987+ }
988+ }
989+
990+ func testGoogleStorageBucketObject (bucketName , content , filename string ) string {
991+ return fmt .Sprintf (`
992+ resource "google_storage_bucket" "bucket" {
993+ name = "%s"
994+ location = "US"
995+ }
996+
997+ resource "google_storage_bucket_object" "changing" {
998+ bucket = google_storage_bucket.bucket.name
999+ name = "dynamic"
1000+ content = "%s"
1001+ }
1002+
1003+ resource "local_file" "test" {
1004+ content = jsonencode(google_storage_bucket_object.changing.content)
1005+ filename = "%s"
1006+ }
1007+
1008+ resource "google_storage_bucket_object" "bo" {
1009+ source = local_file.test.filename
1010+ bucket = google_storage_bucket.bucket.name
1011+ name = "test-file-bucket"
1012+ }
1013+
1014+ data "google_storage_bucket_object_content" "bo" {
1015+ bucket = google_storage_bucket_object.bo.bucket
1016+ name = google_storage_bucket_object.bo.name
1017+ depends_on = [google_storage_bucket_object.bo]
1018+ }
1019+
1020+ output "valid" {
1021+ value = nonsensitive(local_file.test.content) == data.google_storage_bucket_object_content.bo.content
1022+ }
1023+ ` , bucketName , content , filename )
1024+ }
1025+
1026+ func testGoogleStorageBucketObjectKnownAfterApply (bucketName , content , filename string ) string {
1027+ return fmt .Sprintf (`
1028+ resource "google_storage_bucket" "bucket" {
1029+ name = "%s"
1030+ location = "US"
1031+ }
1032+
1033+ resource "google_storage_bucket_object" "changing" {
1034+ bucket = google_storage_bucket.bucket.name
1035+ name = "dynamic"
1036+ content = "%s"
1037+ }
1038+
1039+ resource "local_file" "test" {
1040+ content = jsonencode(google_storage_bucket_object.changing.content)
1041+ filename = "%s"
1042+ }
1043+
1044+ resource "google_storage_bucket_object" "bo" {
1045+ source = local_file.test.filename
1046+ source_md5hash = local_file.test.content_md5
1047+ bucket = google_storage_bucket.bucket.name
1048+ name = "test-file-bucket"
1049+ }
1050+
1051+ data "google_storage_bucket_object_content" "bo" {
1052+ bucket = google_storage_bucket_object.bo.bucket
1053+ name = google_storage_bucket_object.bo.name
1054+ depends_on = [google_storage_bucket_object.bo]
1055+ }
1056+
1057+ output "valid" {
1058+ value = nonsensitive(local_file.test.content) == data.google_storage_bucket_object_content.bo.content
1059+ }
1060+ ` , bucketName , content , filename )
1061+ }
0 commit comments