github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_project_metadata_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "google.golang.org/api/compute/v1" 10 ) 11 12 // Add two key value pairs 13 func TestAccComputeProjectMetadata_basic(t *testing.T) { 14 var project compute.Project 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckComputeProjectMetadataDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccComputeProject_basic0_metadata, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckComputeProjectExists( 25 "google_compute_project_metadata.fizzbuzz", &project), 26 testAccCheckComputeProjectMetadataContains(&project, "banana", "orange"), 27 testAccCheckComputeProjectMetadataContains(&project, "sofa", "darwinism"), 28 testAccCheckComputeProjectMetadataSize(&project, 2), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 // Add three key value pairs, then replace one and modify a second 36 func TestAccComputeProjectMetadata_modify_1(t *testing.T) { 37 var project compute.Project 38 39 resource.Test(t, resource.TestCase{ 40 PreCheck: func() { testAccPreCheck(t) }, 41 Providers: testAccProviders, 42 CheckDestroy: testAccCheckComputeProjectMetadataDestroy, 43 Steps: []resource.TestStep{ 44 resource.TestStep{ 45 Config: testAccComputeProject_modify0_metadata, 46 Check: resource.ComposeTestCheckFunc( 47 testAccCheckComputeProjectExists( 48 "google_compute_project_metadata.fizzbuzz", &project), 49 testAccCheckComputeProjectMetadataContains(&project, "paper", "pen"), 50 testAccCheckComputeProjectMetadataContains(&project, "genghis_khan", "french bread"), 51 testAccCheckComputeProjectMetadataContains(&project, "happy", "smiling"), 52 testAccCheckComputeProjectMetadataSize(&project, 3), 53 ), 54 }, 55 56 resource.TestStep{ 57 Config: testAccComputeProject_modify1_metadata, 58 Check: resource.ComposeTestCheckFunc( 59 testAccCheckComputeProjectExists( 60 "google_compute_project_metadata.fizzbuzz", &project), 61 testAccCheckComputeProjectMetadataContains(&project, "paper", "pen"), 62 testAccCheckComputeProjectMetadataContains(&project, "paris", "french bread"), 63 testAccCheckComputeProjectMetadataContains(&project, "happy", "laughing"), 64 testAccCheckComputeProjectMetadataSize(&project, 3), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 // Add two key value pairs, and replace both 72 func TestAccComputeProjectMetadata_modify_2(t *testing.T) { 73 var project compute.Project 74 75 resource.Test(t, resource.TestCase{ 76 PreCheck: func() { testAccPreCheck(t) }, 77 Providers: testAccProviders, 78 CheckDestroy: testAccCheckComputeProjectMetadataDestroy, 79 Steps: []resource.TestStep{ 80 resource.TestStep{ 81 Config: testAccComputeProject_basic0_metadata, 82 Check: resource.ComposeTestCheckFunc( 83 testAccCheckComputeProjectExists( 84 "google_compute_project_metadata.fizzbuzz", &project), 85 testAccCheckComputeProjectMetadataContains(&project, "banana", "orange"), 86 testAccCheckComputeProjectMetadataContains(&project, "sofa", "darwinism"), 87 testAccCheckComputeProjectMetadataSize(&project, 2), 88 ), 89 }, 90 91 resource.TestStep{ 92 Config: testAccComputeProject_basic1_metadata, 93 Check: resource.ComposeTestCheckFunc( 94 testAccCheckComputeProjectExists( 95 "google_compute_project_metadata.fizzbuzz", &project), 96 testAccCheckComputeProjectMetadataContains(&project, "kiwi", "papaya"), 97 testAccCheckComputeProjectMetadataContains(&project, "finches", "darwinism"), 98 testAccCheckComputeProjectMetadataSize(&project, 2), 99 ), 100 }, 101 }, 102 }) 103 } 104 105 func testAccCheckComputeProjectMetadataDestroy(s *terraform.State) error { 106 config := testAccProvider.Meta().(*Config) 107 108 project, err := config.clientCompute.Projects.Get(config.Project).Do() 109 if err == nil && len(project.CommonInstanceMetadata.Items) > 0 { 110 return fmt.Errorf("Error, metadata items still exist") 111 } 112 113 return nil 114 } 115 116 func testAccCheckComputeProjectExists(n string, project *compute.Project) resource.TestCheckFunc { 117 return func(s *terraform.State) error { 118 rs, ok := s.RootModule().Resources[n] 119 if !ok { 120 return fmt.Errorf("Not found: %s", n) 121 } 122 123 if rs.Primary.ID == "" { 124 return fmt.Errorf("No ID is set") 125 } 126 127 config := testAccProvider.Meta().(*Config) 128 129 found, err := config.clientCompute.Projects.Get( 130 config.Project).Do() 131 if err != nil { 132 return err 133 } 134 135 if "common_metadata" != rs.Primary.ID { 136 return fmt.Errorf("Common metadata not found, found %s", rs.Primary.ID) 137 } 138 139 *project = *found 140 141 return nil 142 } 143 } 144 145 func testAccCheckComputeProjectMetadataContains(project *compute.Project, key string, value string) resource.TestCheckFunc { 146 return func(s *terraform.State) error { 147 config := testAccProvider.Meta().(*Config) 148 project, err := config.clientCompute.Projects.Get(config.Project).Do() 149 if err != nil { 150 return fmt.Errorf("Error, failed to load project service for %s: %s", config.Project, err) 151 } 152 153 for _, kv := range project.CommonInstanceMetadata.Items { 154 if kv.Key == key { 155 if kv.Value != nil && *kv.Value == value { 156 return nil 157 } else { 158 return fmt.Errorf("Error, key value mismatch, wanted (%s, %s), got (%s, %s)", 159 key, value, kv.Key, *kv.Value) 160 } 161 } 162 } 163 164 return fmt.Errorf("Error, key %s not present", key) 165 } 166 } 167 168 func testAccCheckComputeProjectMetadataSize(project *compute.Project, size int) resource.TestCheckFunc { 169 return func(s *terraform.State) error { 170 config := testAccProvider.Meta().(*Config) 171 project, err := config.clientCompute.Projects.Get(config.Project).Do() 172 if err != nil { 173 return fmt.Errorf("Error, failed to load project service for %s: %s", config.Project, err) 174 } 175 176 if size > len(project.CommonInstanceMetadata.Items) { 177 return fmt.Errorf("Error, expected at least %d metadata items, got %d", size, 178 len(project.CommonInstanceMetadata.Items)) 179 } 180 181 return nil 182 } 183 } 184 185 const testAccComputeProject_basic0_metadata = ` 186 resource "google_compute_project_metadata" "fizzbuzz" { 187 metadata { 188 banana = "orange" 189 sofa = "darwinism" 190 } 191 }` 192 193 const testAccComputeProject_basic1_metadata = ` 194 resource "google_compute_project_metadata" "fizzbuzz" { 195 metadata { 196 kiwi = "papaya" 197 finches = "darwinism" 198 } 199 }` 200 201 const testAccComputeProject_modify0_metadata = ` 202 resource "google_compute_project_metadata" "fizzbuzz" { 203 metadata { 204 paper = "pen" 205 genghis_khan = "french bread" 206 happy = "smiling" 207 } 208 }` 209 210 const testAccComputeProject_modify1_metadata = ` 211 resource "google_compute_project_metadata" "fizzbuzz" { 212 metadata { 213 paper = "pen" 214 paris = "french bread" 215 happy = "laughing" 216 } 217 }`