github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/google/resource_compute_snapshot_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"google.golang.org/api/compute/v1"
    11  	"google.golang.org/api/googleapi"
    12  )
    13  
    14  func TestAccComputeSnapshot_basic(t *testing.T) {
    15  	snapshotName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    16  	var snapshot compute.Snapshot
    17  	diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckComputeSnapshotDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccComputeSnapshot_basic(snapshotName, diskName),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckComputeSnapshotExists(
    28  						"google_compute_snapshot.foobar", &snapshot),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccComputeSnapshot_encryption(t *testing.T) {
    36  	snapshotName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    37  	diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    38  	var snapshot compute.Snapshot
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckComputeSnapshotDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccComputeSnapshot_encryption(snapshotName, diskName),
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckComputeSnapshotExists(
    49  						"google_compute_snapshot.foobar", &snapshot),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func testAccCheckComputeSnapshotDestroy(s *terraform.State) error {
    57  	config := testAccProvider.Meta().(*Config)
    58  
    59  	for _, rs := range s.RootModule().Resources {
    60  		if rs.Type != "google_compute_snapshot" {
    61  			continue
    62  		}
    63  
    64  		_, err := config.clientCompute.Snapshots.Get(
    65  			config.Project, rs.Primary.ID).Do()
    66  		if err != nil {
    67  			if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
    68  				return nil
    69  			} else if ok {
    70  				return fmt.Errorf("Error while requesting Google Cloud Plateform: http code error : %d, http message error: %s", gerr.Code, gerr.Message)
    71  			}
    72  			return fmt.Errorf("Error while requesting Google Cloud Plateform")
    73  		}
    74  		return fmt.Errorf("Snapshot still exists")
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func testAccCheckComputeSnapshotExists(n string, snapshot *compute.Snapshot) resource.TestCheckFunc {
    81  	return func(s *terraform.State) error {
    82  		rs, ok := s.RootModule().Resources[n]
    83  		if !ok {
    84  			return fmt.Errorf("Not found: %s", n)
    85  		}
    86  
    87  		if rs.Primary.ID == "" {
    88  			return fmt.Errorf("No ID is set")
    89  		}
    90  
    91  		config := testAccProvider.Meta().(*Config)
    92  
    93  		found, err := config.clientCompute.Snapshots.Get(
    94  			config.Project, rs.Primary.ID).Do()
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		if found.Name != rs.Primary.ID {
   100  			return fmt.Errorf("Snapshot %s not found", n)
   101  		}
   102  
   103  		attr := rs.Primary.Attributes["snapshot_encryption_key_sha256"]
   104  		if found.SnapshotEncryptionKey != nil && found.SnapshotEncryptionKey.Sha256 != attr {
   105  			return fmt.Errorf("Snapshot %s has mismatched encryption key (Sha256).\nTF State: %+v.\nGCP State: %+v",
   106  				n, attr, found.SnapshotEncryptionKey.Sha256)
   107  		} else if found.SnapshotEncryptionKey == nil && attr != "" {
   108  			return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v",
   109  				n, attr, found.SnapshotEncryptionKey)
   110  		}
   111  
   112  		attr = rs.Primary.Attributes["source_disk_encryption_key_sha256"]
   113  		if found.SourceDiskEncryptionKey != nil && found.SourceDiskEncryptionKey.Sha256 != attr {
   114  			return fmt.Errorf("Snapshot %s has mismatched source disk encryption key (Sha256).\nTF State: %+v.\nGCP State: %+v",
   115  				n, attr, found.SourceDiskEncryptionKey.Sha256)
   116  		} else if found.SourceDiskEncryptionKey == nil && attr != "" {
   117  			return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v",
   118  				n, attr, found.SourceDiskEncryptionKey)
   119  		}
   120  
   121  		attr = rs.Primary.Attributes["source_disk_link"]
   122  		if found.SourceDisk != attr {
   123  			return fmt.Errorf("Snapshot %s has mismatched source disk link.\nTF State: %+v.\nGCP State: %+v",
   124  				n, attr, found.SourceDisk)
   125  		}
   126  
   127  		foundDisk, errDisk := config.clientCompute.Disks.Get(
   128  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.Attributes["source_disk"]).Do()
   129  		if errDisk != nil {
   130  			return errDisk
   131  		}
   132  		if foundDisk.SelfLink != attr {
   133  			return fmt.Errorf("Snapshot %s has mismatched source disk\nTF State: %+v.\nGCP State: %+v",
   134  				n, attr, foundDisk.SelfLink)
   135  		}
   136  
   137  		attr = rs.Primary.Attributes["self_link"]
   138  		if found.SelfLink != attr {
   139  			return fmt.Errorf("Snapshot %s has mismatched self link.\nTF State: %+v.\nGCP State: %+v",
   140  				n, attr, found.SelfLink)
   141  		}
   142  
   143  		*snapshot = *found
   144  
   145  		return nil
   146  	}
   147  }
   148  
   149  func testAccComputeSnapshot_basic(snapshotName string, diskName string) string {
   150  	return fmt.Sprintf(`
   151  resource "google_compute_disk" "foobar" {
   152  	name = "%s"
   153  	image = "debian-8-jessie-v20160921"
   154  	size = 10
   155  	type = "pd-ssd"
   156  	zone = "us-central1-a"
   157  }
   158  
   159  resource "google_compute_snapshot" "foobar" {
   160  	name = "%s"
   161  	source_disk = "${google_compute_disk.foobar.name}"
   162  	zone = "us-central1-a"
   163  }`, diskName, snapshotName)
   164  }
   165  
   166  func testAccComputeSnapshot_encryption(snapshotName string, diskName string) string {
   167  	return fmt.Sprintf(`
   168  resource "google_compute_disk" "foobar" {
   169  	name = "%s"
   170  	image = "debian-8-jessie-v20160921"
   171  	size = 10
   172  	type = "pd-ssd"
   173  	zone = "us-central1-a"
   174  	disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
   175  }
   176  resource "google_compute_snapshot" "foobar" {
   177  	name = "%s"
   178  	source_disk = "${google_compute_disk.foobar.name}"
   179  	zone = "us-central1-a"
   180  	source_disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
   181  	snapshot_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
   182  }`, diskName, snapshotName)
   183  }