From 1b22200c440e56766cd01f33f4e65350db375bfb Mon Sep 17 00:00:00 2001 From: Thomas Poindessous Date: Thu, 20 Apr 2017 12:13:17 +0200 Subject: [PATCH] Corrected test for snapshot. Simplified tests. Added a new test for source_disk_link --- .../google/resource_compute_snapshot_test.go | 70 +++++-------------- 1 file changed, 17 insertions(+), 53 deletions(-) diff --git a/builtin/providers/google/resource_compute_snapshot_test.go b/builtin/providers/google/resource_compute_snapshot_test.go index cc74dc1fb..2a29f940d 100644 --- a/builtin/providers/google/resource_compute_snapshot_test.go +++ b/builtin/providers/google/resource_compute_snapshot_test.go @@ -34,8 +34,8 @@ func TestAccComputeSnapshot_basic(t *testing.T) { func TestAccComputeSnapshot_encryption(t *testing.T) { snapshotName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - var snapshot compute.Snapshot diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + var snapshot compute.Snapshot resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -47,8 +47,6 @@ func TestAccComputeSnapshot_encryption(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeSnapshotExists( "google_compute_snapshot.foobar", &snapshot), - testAccCheckSnapshotEncryptionKey( - "google_compute_snapshot.foobar", &snapshot), ), }, }, @@ -104,52 +102,38 @@ func testAccCheckComputeSnapshotExists(n string, snapshot *compute.Snapshot) res attr := rs.Primary.Attributes["snapshot_encryption_key_sha256"] if found.SnapshotEncryptionKey != nil && found.SnapshotEncryptionKey.Sha256 != attr { - return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", + return fmt.Errorf("Snapshot %s has mismatched encryption key (Sha256).\nTF State: %+v.\nGCP State: %+v", n, attr, found.SnapshotEncryptionKey.Sha256) } else if found.SnapshotEncryptionKey == nil && attr != "" { return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", n, attr, found.SnapshotEncryptionKey) } - attr = rs.Primary.Attributes["snapshot_encryption_key_raw"] - if found.SnapshotEncryptionKey != nil && found.SnapshotEncryptionKey.RawKey != attr { - return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", - n, attr, found.SnapshotEncryptionKey.RawKey) - } else if found.SnapshotEncryptionKey == nil && attr != "" { - return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", - n, attr, found.SnapshotEncryptionKey) - } - attr = rs.Primary.Attributes["source_disk_encryption_key_sha256"] if found.SourceDiskEncryptionKey != nil && found.SourceDiskEncryptionKey.Sha256 != attr { - return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", + return fmt.Errorf("Snapshot %s has mismatched source disk encryption key (Sha256).\nTF State: %+v.\nGCP State: %+v", n, attr, found.SourceDiskEncryptionKey.Sha256) } else if found.SourceDiskEncryptionKey == nil && attr != "" { return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", n, attr, found.SourceDiskEncryptionKey) } - attr = rs.Primary.Attributes["source_disk_encryption_key_raw"] - if found.SourceDiskEncryptionKey != nil && found.SourceDiskEncryptionKey.RawKey != attr { - return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", - n, attr, found.SourceDiskEncryptionKey.RawKey) - } else if found.SourceDiskEncryptionKey == nil && attr != "" { - return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", - n, attr, found.SourceDiskEncryptionKey) - } - - attr = rs.Primary.Attributes["source_disk_id"] - if found.SourceDiskId != attr { - return fmt.Errorf("Snapshot %s has mismatched source disk id.\nTF State: %+v.\nGCP State: %+v", - n, attr, found.SourceDiskId) - } - - attr = rs.Primary.Attributes["source_disk"] + attr = rs.Primary.Attributes["source_disk_link"] if found.SourceDisk != attr { - return fmt.Errorf("Snapshot %s has mismatched source disk.\nTF State: %+v.\nGCP State: %+v", + return fmt.Errorf("Snapshot %s has mismatched source disk link.\nTF State: %+v.\nGCP State: %+v", n, attr, found.SourceDisk) } + foundDisk, errDisk := config.clientCompute.Disks.Get( + config.Project, rs.Primary.Attributes["zone"], rs.Primary.Attributes["source_disk"]).Do() + if errDisk != nil { + return errDisk + } + if foundDisk.SelfLink != attr { + return fmt.Errorf("Snapshot %s has mismatched source disk\nTF State: %+v.\nGCP State: %+v", + n, attr, foundDisk.SelfLink) + } + attr = rs.Primary.Attributes["self_link"] if found.SelfLink != attr { return fmt.Errorf("Snapshot %s has mismatched self link.\nTF State: %+v.\nGCP State: %+v", @@ -162,26 +146,6 @@ func testAccCheckComputeSnapshotExists(n string, snapshot *compute.Snapshot) res } } -func testAccCheckSnapshotEncryptionKey(n string, snapshot *compute.Snapshot) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - attr := rs.Primary.Attributes["snapshot_encryption_key_sha256"] - if snapshot.SnapshotEncryptionKey == nil && attr != "" { - return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v\nGCP State: ", n, attr) - } - - if attr != snapshot.SnapshotEncryptionKey.Sha256 { - return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", - n, attr, snapshot.SnapshotEncryptionKey.Sha256) - } - return nil - } -} - func testAccComputeSnapshot_basic(snapshotName string, diskName string) string { return fmt.Sprintf(` resource "google_compute_disk" "foobar" { @@ -194,7 +158,7 @@ resource "google_compute_disk" "foobar" { resource "google_compute_snapshot" "foobar" { name = "%s" - disk = "${google_compute_disk.foobar.name}" + source_disk = "${google_compute_disk.foobar.name}" zone = "us-central1-a" }`, diskName, snapshotName) } @@ -211,7 +175,7 @@ resource "google_compute_disk" "foobar" { } resource "google_compute_snapshot" "foobar" { name = "%s" - disk = "${google_compute_disk.foobar.name}" + source_disk = "${google_compute_disk.foobar.name}" zone = "us-central1-a" source_disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0=" snapshot_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="