github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_disk_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  )
    12  
    13  func TestAccComputeDisk_basic(t *testing.T) {
    14  	diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    15  	var disk compute.Disk
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckComputeDiskDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccComputeDisk_basic(diskName),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckComputeDiskExists(
    26  						"google_compute_disk.foobar", &disk),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccComputeDisk_encryption(t *testing.T) {
    34  	diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    35  	var disk compute.Disk
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckComputeDiskDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccComputeDisk_encryption(diskName),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckComputeDiskExists(
    46  						"google_compute_disk.foobar", &disk),
    47  					testAccCheckEncryptionKey(
    48  						"google_compute_disk.foobar", &disk),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckComputeDiskDestroy(s *terraform.State) error {
    56  	config := testAccProvider.Meta().(*Config)
    57  
    58  	for _, rs := range s.RootModule().Resources {
    59  		if rs.Type != "google_compute_disk" {
    60  			continue
    61  		}
    62  
    63  		_, err := config.clientCompute.Disks.Get(
    64  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
    65  		if err == nil {
    66  			return fmt.Errorf("Disk still exists")
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No ID is set")
    82  		}
    83  
    84  		config := testAccProvider.Meta().(*Config)
    85  
    86  		found, err := config.clientCompute.Disks.Get(
    87  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
    88  		if err != nil {
    89  			return err
    90  		}
    91  
    92  		if found.Name != rs.Primary.ID {
    93  			return fmt.Errorf("Disk not found")
    94  		}
    95  
    96  		*disk = *found
    97  
    98  		return nil
    99  	}
   100  }
   101  
   102  func testAccCheckEncryptionKey(n string, disk *compute.Disk) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not found: %s", n)
   107  		}
   108  
   109  		attr := rs.Primary.Attributes["disk_encryption_key_sha256"]
   110  		if disk.DiskEncryptionKey == nil && attr != "" {
   111  			return fmt.Errorf("Disk %s has mismatched encryption key.\nTF State: %+v\nGCP State: <empty>", n, attr)
   112  		}
   113  
   114  		if attr != disk.DiskEncryptionKey.Sha256 {
   115  			return fmt.Errorf("Disk %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v",
   116  				n, attr, disk.DiskEncryptionKey.Sha256)
   117  		}
   118  		return nil
   119  	}
   120  }
   121  
   122  func testAccComputeDisk_basic(diskName string) string {
   123  	return fmt.Sprintf(`
   124  resource "google_compute_disk" "foobar" {
   125  	name = "%s"
   126  	image = "debian-8-jessie-v20160803"
   127  	size = 50
   128  	type = "pd-ssd"
   129  	zone = "us-central1-a"
   130  }`, diskName)
   131  }
   132  
   133  func testAccComputeDisk_encryption(diskName string) string {
   134  	return fmt.Sprintf(`
   135  resource "google_compute_disk" "foobar" {
   136  	name = "%s"
   137  	image = "debian-8-jessie-v20160803"
   138  	size = 50
   139  	type = "pd-ssd"
   140  	zone = "us-central1-a"
   141  	disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
   142  }`, diskName)
   143  }