github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_sql_database_instance_test.go (about) 1 package google 2 3 /** 4 * Note! You must run these tests once at a time. Google Cloud SQL does 5 * not allow you to reuse a database for a short time after you reserved it, 6 * and for this reason the tests will fail if the same config is used serveral 7 * times in short succession. 8 */ 9 10 import ( 11 "fmt" 12 "strconv" 13 "testing" 14 15 "github.com/hashicorp/terraform/helper/acctest" 16 "github.com/hashicorp/terraform/helper/resource" 17 "github.com/hashicorp/terraform/terraform" 18 19 "google.golang.org/api/sqladmin/v1beta4" 20 ) 21 22 func TestAccGoogleSqlDatabaseInstance_basic(t *testing.T) { 23 var instance sqladmin.DatabaseInstance 24 databaseID := acctest.RandInt() 25 26 resource.Test(t, resource.TestCase{ 27 PreCheck: func() { testAccPreCheck(t) }, 28 Providers: testAccProviders, 29 CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy, 30 Steps: []resource.TestStep{ 31 resource.TestStep{ 32 Config: fmt.Sprintf( 33 testGoogleSqlDatabaseInstance_basic, databaseID), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckGoogleSqlDatabaseInstanceExists( 36 "google_sql_database_instance.instance", &instance), 37 testAccCheckGoogleSqlDatabaseInstanceEquals( 38 "google_sql_database_instance.instance", &instance), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func TestAccGoogleSqlDatabaseInstance_basic2(t *testing.T) { 46 var instance sqladmin.DatabaseInstance 47 48 resource.Test(t, resource.TestCase{ 49 PreCheck: func() { testAccPreCheck(t) }, 50 Providers: testAccProviders, 51 CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy, 52 Steps: []resource.TestStep{ 53 resource.TestStep{ 54 Config: testGoogleSqlDatabaseInstance_basic2, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckGoogleSqlDatabaseInstanceExists( 57 "google_sql_database_instance.instance", &instance), 58 testAccCheckGoogleSqlDatabaseInstanceEquals( 59 "google_sql_database_instance.instance", &instance), 60 ), 61 }, 62 }, 63 }) 64 } 65 66 func TestAccGoogleSqlDatabaseInstance_settings_basic(t *testing.T) { 67 var instance sqladmin.DatabaseInstance 68 databaseID := acctest.RandInt() 69 70 resource.Test(t, resource.TestCase{ 71 PreCheck: func() { testAccPreCheck(t) }, 72 Providers: testAccProviders, 73 CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy, 74 Steps: []resource.TestStep{ 75 resource.TestStep{ 76 Config: fmt.Sprintf( 77 testGoogleSqlDatabaseInstance_settings, databaseID), 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckGoogleSqlDatabaseInstanceExists( 80 "google_sql_database_instance.instance", &instance), 81 testAccCheckGoogleSqlDatabaseInstanceEquals( 82 "google_sql_database_instance.instance", &instance), 83 ), 84 }, 85 }, 86 }) 87 } 88 89 func TestAccGoogleSqlDatabaseInstance_settings_upgrade(t *testing.T) { 90 var instance sqladmin.DatabaseInstance 91 databaseID := acctest.RandInt() 92 93 resource.Test(t, resource.TestCase{ 94 PreCheck: func() { testAccPreCheck(t) }, 95 Providers: testAccProviders, 96 CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy, 97 Steps: []resource.TestStep{ 98 resource.TestStep{ 99 Config: fmt.Sprintf( 100 testGoogleSqlDatabaseInstance_basic, databaseID), 101 Check: resource.ComposeTestCheckFunc( 102 testAccCheckGoogleSqlDatabaseInstanceExists( 103 "google_sql_database_instance.instance", &instance), 104 testAccCheckGoogleSqlDatabaseInstanceEquals( 105 "google_sql_database_instance.instance", &instance), 106 ), 107 }, 108 resource.TestStep{ 109 Config: fmt.Sprintf( 110 testGoogleSqlDatabaseInstance_settings, databaseID), 111 Check: resource.ComposeTestCheckFunc( 112 testAccCheckGoogleSqlDatabaseInstanceExists( 113 "google_sql_database_instance.instance", &instance), 114 testAccCheckGoogleSqlDatabaseInstanceEquals( 115 "google_sql_database_instance.instance", &instance), 116 ), 117 }, 118 }, 119 }) 120 } 121 122 func TestAccGoogleSqlDatabaseInstance_settings_downgrade(t *testing.T) { 123 var instance sqladmin.DatabaseInstance 124 databaseID := acctest.RandInt() 125 126 resource.Test(t, resource.TestCase{ 127 PreCheck: func() { testAccPreCheck(t) }, 128 Providers: testAccProviders, 129 CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy, 130 Steps: []resource.TestStep{ 131 resource.TestStep{ 132 Config: fmt.Sprintf( 133 testGoogleSqlDatabaseInstance_settings, databaseID), 134 Check: resource.ComposeTestCheckFunc( 135 testAccCheckGoogleSqlDatabaseInstanceExists( 136 "google_sql_database_instance.instance", &instance), 137 testAccCheckGoogleSqlDatabaseInstanceEquals( 138 "google_sql_database_instance.instance", &instance), 139 ), 140 }, 141 resource.TestStep{ 142 Config: fmt.Sprintf( 143 testGoogleSqlDatabaseInstance_basic, databaseID), 144 Check: resource.ComposeTestCheckFunc( 145 testAccCheckGoogleSqlDatabaseInstanceExists( 146 "google_sql_database_instance.instance", &instance), 147 testAccCheckGoogleSqlDatabaseInstanceEquals( 148 "google_sql_database_instance.instance", &instance), 149 ), 150 }, 151 }, 152 }) 153 } 154 155 // GH-4222 156 func TestAccGoogleSqlDatabaseInstance_authNets(t *testing.T) { 157 // var instance sqladmin.DatabaseInstance 158 databaseID := acctest.RandInt() 159 160 resource.Test(t, resource.TestCase{ 161 PreCheck: func() { testAccPreCheck(t) }, 162 Providers: testAccProviders, 163 CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy, 164 Steps: []resource.TestStep{ 165 resource.TestStep{ 166 Config: fmt.Sprintf( 167 testGoogleSqlDatabaseInstance_authNets_step1, databaseID), 168 }, 169 resource.TestStep{ 170 Config: fmt.Sprintf( 171 testGoogleSqlDatabaseInstance_authNets_step2, databaseID), 172 }, 173 resource.TestStep{ 174 Config: fmt.Sprintf( 175 testGoogleSqlDatabaseInstance_authNets_step1, databaseID), 176 }, 177 }, 178 }) 179 } 180 181 func testAccCheckGoogleSqlDatabaseInstanceEquals(n string, 182 instance *sqladmin.DatabaseInstance) resource.TestCheckFunc { 183 return func(s *terraform.State) error { 184 rs, ok := s.RootModule().Resources[n] 185 if !ok { 186 return fmt.Errorf("Not found: %s", n) 187 } 188 attributes := rs.Primary.Attributes 189 190 server := instance.Name 191 local := attributes["name"] 192 if server != local { 193 return fmt.Errorf("Error name mismatch, (%s, %s)", server, local) 194 } 195 196 server = instance.Settings.Tier 197 local = attributes["settings.0.tier"] 198 if server != local { 199 return fmt.Errorf("Error settings.tier mismatch, (%s, %s)", server, local) 200 } 201 202 server = instance.MasterInstanceName 203 local = attributes["master_instance_name"] 204 if server != local && len(server) > 0 && len(local) > 0 { 205 return fmt.Errorf("Error master_instance_name mismatch, (%s, %s)", server, local) 206 } 207 208 server = instance.Settings.ActivationPolicy 209 local = attributes["settings.0.activation_policy"] 210 if server != local && len(server) > 0 && len(local) > 0 { 211 return fmt.Errorf("Error settings.activation_policy mismatch, (%s, %s)", server, local) 212 } 213 214 if instance.Settings.BackupConfiguration != nil { 215 server = strconv.FormatBool(instance.Settings.BackupConfiguration.BinaryLogEnabled) 216 local = attributes["settings.0.backup_configuration.0.binary_log_enabled"] 217 if server != local && len(server) > 0 && len(local) > 0 { 218 return fmt.Errorf("Error settings.backup_configuration.binary_log_enabled mismatch, (%s, %s)", server, local) 219 } 220 221 server = strconv.FormatBool(instance.Settings.BackupConfiguration.Enabled) 222 local = attributes["settings.0.backup_configuration.0.enabled"] 223 if server != local && len(server) > 0 && len(local) > 0 { 224 return fmt.Errorf("Error settings.backup_configuration.enabled mismatch, (%s, %s)", server, local) 225 } 226 227 server = instance.Settings.BackupConfiguration.StartTime 228 local = attributes["settings.0.backup_configuration.0.start_time"] 229 if server != local && len(server) > 0 && len(local) > 0 { 230 return fmt.Errorf("Error settings.backup_configuration.start_time mismatch, (%s, %s)", server, local) 231 } 232 } 233 234 server = strconv.FormatBool(instance.Settings.CrashSafeReplicationEnabled) 235 local = attributes["settings.0.crash_safe_replication"] 236 if server != local && len(server) > 0 && len(local) > 0 { 237 return fmt.Errorf("Error settings.crash_safe_replication mismatch, (%s, %s)", server, local) 238 } 239 240 if instance.Settings.IpConfiguration != nil { 241 server = strconv.FormatBool(instance.Settings.IpConfiguration.Ipv4Enabled) 242 local = attributes["settings.0.ip_configuration.0.ipv4_enabled"] 243 if server != local && len(server) > 0 && len(local) > 0 { 244 return fmt.Errorf("Error settings.ip_configuration.ipv4_enabled mismatch, (%s, %s)", server, local) 245 } 246 247 server = strconv.FormatBool(instance.Settings.IpConfiguration.RequireSsl) 248 local = attributes["settings.0.ip_configuration.0.require_ssl"] 249 if server != local && len(server) > 0 && len(local) > 0 { 250 return fmt.Errorf("Error settings.ip_configuration.require_ssl mismatch, (%s, %s)", server, local) 251 } 252 } 253 254 if instance.Settings.LocationPreference != nil { 255 server = instance.Settings.LocationPreference.FollowGaeApplication 256 local = attributes["settings.0.location_preference.0.follow_gae_application"] 257 if server != local && len(server) > 0 && len(local) > 0 { 258 return fmt.Errorf("Error settings.location_preference.follow_gae_application mismatch, (%s, %s)", server, local) 259 } 260 261 server = instance.Settings.LocationPreference.Zone 262 local = attributes["settings.0.location_preference.0.zone"] 263 if server != local && len(server) > 0 && len(local) > 0 { 264 return fmt.Errorf("Error settings.location_preference.zone mismatch, (%s, %s)", server, local) 265 } 266 } 267 268 server = instance.Settings.PricingPlan 269 local = attributes["settings.0.pricing_plan"] 270 if server != local && len(server) > 0 && len(local) > 0 { 271 return fmt.Errorf("Error settings.pricing_plan mismatch, (%s, %s)", server, local) 272 } 273 274 if instance.ReplicaConfiguration != nil && 275 instance.ReplicaConfiguration.MysqlReplicaConfiguration != nil { 276 server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.CaCertificate 277 local = attributes["replica_configuration.0.ca_certificate"] 278 if server != local && len(server) > 0 && len(local) > 0 { 279 return fmt.Errorf("Error replica_configuration.ca_certificate mismatch, (%s, %s)", server, local) 280 } 281 282 server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.ClientCertificate 283 local = attributes["replica_configuration.0.client_certificate"] 284 if server != local && len(server) > 0 && len(local) > 0 { 285 return fmt.Errorf("Error replica_configuration.client_certificate mismatch, (%s, %s)", server, local) 286 } 287 288 server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.ClientKey 289 local = attributes["replica_configuration.0.client_key"] 290 if server != local && len(server) > 0 && len(local) > 0 { 291 return fmt.Errorf("Error replica_configuration.client_key mismatch, (%s, %s)", server, local) 292 } 293 294 server = strconv.FormatInt(instance.ReplicaConfiguration.MysqlReplicaConfiguration.ConnectRetryInterval, 10) 295 local = attributes["replica_configuration.0.connect_retry_interval"] 296 if server != local && len(server) > 0 && len(local) > 0 { 297 return fmt.Errorf("Error replica_configuration.connect_retry_interval mismatch, (%s, %s)", server, local) 298 } 299 300 server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.DumpFilePath 301 local = attributes["replica_configuration.0.dump_file_path"] 302 if server != local && len(server) > 0 && len(local) > 0 { 303 return fmt.Errorf("Error replica_configuration.dump_file_path mismatch, (%s, %s)", server, local) 304 } 305 306 server = strconv.FormatInt(instance.ReplicaConfiguration.MysqlReplicaConfiguration.MasterHeartbeatPeriod, 10) 307 local = attributes["replica_configuration.0.master_heartbeat_period"] 308 if server != local && len(server) > 0 && len(local) > 0 { 309 return fmt.Errorf("Error replica_configuration.master_heartbeat_period mismatch, (%s, %s)", server, local) 310 } 311 312 server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.Password 313 local = attributes["replica_configuration.0.password"] 314 if server != local && len(server) > 0 && len(local) > 0 { 315 return fmt.Errorf("Error replica_configuration.password mismatch, (%s, %s)", server, local) 316 } 317 318 server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.SslCipher 319 local = attributes["replica_configuration.0.ssl_cipher"] 320 if server != local && len(server) > 0 && len(local) > 0 { 321 return fmt.Errorf("Error replica_configuration.ssl_cipher mismatch, (%s, %s)", server, local) 322 } 323 324 server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.Username 325 local = attributes["replica_configuration.0.username"] 326 if server != local && len(server) > 0 && len(local) > 0 { 327 return fmt.Errorf("Error replica_configuration.username mismatch, (%s, %s)", server, local) 328 } 329 330 server = strconv.FormatBool(instance.ReplicaConfiguration.MysqlReplicaConfiguration.VerifyServerCertificate) 331 local = attributes["replica_configuration.0.verify_server_certificate"] 332 if server != local && len(server) > 0 && len(local) > 0 { 333 return fmt.Errorf("Error replica_configuration.verify_server_certificate mismatch, (%s, %s)", server, local) 334 } 335 } 336 337 return nil 338 } 339 } 340 341 func testAccCheckGoogleSqlDatabaseInstanceExists(n string, 342 instance *sqladmin.DatabaseInstance) resource.TestCheckFunc { 343 return func(s *terraform.State) error { 344 config := testAccProvider.Meta().(*Config) 345 rs, ok := s.RootModule().Resources[n] 346 if !ok { 347 return fmt.Errorf("Not found: %s", n) 348 } 349 350 found, err := config.clientSqlAdmin.Instances.Get(config.Project, 351 rs.Primary.Attributes["name"]).Do() 352 353 *instance = *found 354 355 if err != nil { 356 return fmt.Errorf("Not found: %s", n) 357 } 358 359 return nil 360 } 361 } 362 363 func testAccGoogleSqlDatabaseInstanceDestroy(s *terraform.State) error { 364 for _, rs := range s.RootModule().Resources { 365 config := testAccProvider.Meta().(*Config) 366 if rs.Type != "google_sql_database_instance" { 367 continue 368 } 369 370 _, err := config.clientSqlAdmin.Instances.Get(config.Project, 371 rs.Primary.Attributes["name"]).Do() 372 if err == nil { 373 return fmt.Errorf("Database Instance still exists") 374 } 375 } 376 377 return nil 378 } 379 380 var testGoogleSqlDatabaseInstance_basic = ` 381 resource "google_sql_database_instance" "instance" { 382 name = "tf-lw-%d" 383 region = "us-central" 384 settings { 385 tier = "D0" 386 crash_safe_replication = false 387 } 388 } 389 ` 390 391 var testGoogleSqlDatabaseInstance_basic2 = ` 392 resource "google_sql_database_instance" "instance" { 393 region = "us-central" 394 settings { 395 tier = "D0" 396 crash_safe_replication = false 397 } 398 } 399 ` 400 401 var testGoogleSqlDatabaseInstance_settings = ` 402 resource "google_sql_database_instance" "instance" { 403 name = "tf-lw-%d" 404 region = "us-central" 405 settings { 406 tier = "D0" 407 crash_safe_replication = false 408 replication_type = "ASYNCHRONOUS" 409 location_preference { 410 zone = "us-central1-f" 411 } 412 413 ip_configuration { 414 ipv4_enabled = "true" 415 authorized_networks { 416 value = "108.12.12.12" 417 name = "misc" 418 expiration_time = "2017-11-15T16:19:00.094Z" 419 } 420 } 421 422 backup_configuration { 423 enabled = "true" 424 start_time = "19:19" 425 } 426 427 activation_policy = "ON_DEMAND" 428 } 429 } 430 ` 431 432 // Note - this test is not feasible to run unless we generate 433 // backups first. 434 var testGoogleSqlDatabaseInstance_replica = ` 435 resource "google_sql_database_instance" "instance_master" { 436 name = "tf-lw-%d" 437 database_version = "MYSQL_5_6" 438 region = "us-east1" 439 440 settings { 441 tier = "D0" 442 crash_safe_replication = true 443 444 backup_configuration { 445 enabled = true 446 start_time = "00:00" 447 binary_log_enabled = true 448 } 449 } 450 } 451 452 resource "google_sql_database_instance" "instance" { 453 name = "tf-lw-%d" 454 database_version = "MYSQL_5_6" 455 region = "us-central" 456 457 settings { 458 tier = "D0" 459 } 460 461 master_instance_name = "${google_sql_database_instance.instance_master.name}" 462 463 replica_configuration { 464 ca_certificate = "${file("~/tmp/fake.pem")}" 465 client_certificate = "${file("~/tmp/fake.pem")}" 466 client_key = "${file("~/tmp/fake.pem")}" 467 connect_retry_interval = 100 468 master_heartbeat_period = 10000 469 password = "password" 470 username = "username" 471 ssl_cipher = "ALL" 472 verify_server_certificate = false 473 } 474 } 475 ` 476 477 var testGoogleSqlDatabaseInstance_authNets_step1 = ` 478 resource "google_sql_database_instance" "instance" { 479 name = "tf-lw-%d" 480 region = "us-central" 481 settings { 482 tier = "D0" 483 crash_safe_replication = false 484 485 ip_configuration { 486 ipv4_enabled = "true" 487 authorized_networks { 488 value = "108.12.12.12" 489 name = "misc" 490 expiration_time = "2017-11-15T16:19:00.094Z" 491 } 492 } 493 } 494 } 495 ` 496 497 var testGoogleSqlDatabaseInstance_authNets_step2 = ` 498 resource "google_sql_database_instance" "instance" { 499 name = "tf-lw-%d" 500 region = "us-central" 501 settings { 502 tier = "D0" 503 crash_safe_replication = false 504 505 ip_configuration { 506 ipv4_enabled = "true" 507 } 508 } 509 } 510 `