github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/resource"
    16  	"github.com/hashicorp/terraform/terraform"
    17  
    18  	"google.golang.org/api/sqladmin/v1beta4"
    19  )
    20  
    21  func TestAccGoogleSqlDatabaseInstance_basic(t *testing.T) {
    22  	var instance sqladmin.DatabaseInstance
    23  
    24  	resource.Test(t, resource.TestCase{
    25  		PreCheck:     func() { testAccPreCheck(t) },
    26  		Providers:    testAccProviders,
    27  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    28  		Steps: []resource.TestStep{
    29  			resource.TestStep{
    30  				Config: testGoogleSqlDatabaseInstance_basic,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckGoogleSqlDatabaseInstanceExists(
    33  						"google_sql_database_instance.instance", &instance),
    34  					testAccCheckGoogleSqlDatabaseInstanceEquals(
    35  						"google_sql_database_instance.instance", &instance),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccGoogleSqlDatabaseInstance_settings_basic(t *testing.T) {
    43  	var instance sqladmin.DatabaseInstance
    44  
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:     func() { testAccPreCheck(t) },
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    49  		Steps: []resource.TestStep{
    50  			resource.TestStep{
    51  				Config: testGoogleSqlDatabaseInstance_settings,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckGoogleSqlDatabaseInstanceExists(
    54  						"google_sql_database_instance.instance", &instance),
    55  					testAccCheckGoogleSqlDatabaseInstanceEquals(
    56  						"google_sql_database_instance.instance", &instance),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccGoogleSqlDatabaseInstance_settings_upgrade(t *testing.T) {
    64  	var instance sqladmin.DatabaseInstance
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:     func() { testAccPreCheck(t) },
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: testGoogleSqlDatabaseInstance_basic,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckGoogleSqlDatabaseInstanceExists(
    75  						"google_sql_database_instance.instance", &instance),
    76  					testAccCheckGoogleSqlDatabaseInstanceEquals(
    77  						"google_sql_database_instance.instance", &instance),
    78  				),
    79  			},
    80  			resource.TestStep{
    81  				Config: testGoogleSqlDatabaseInstance_settings,
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckGoogleSqlDatabaseInstanceExists(
    84  						"google_sql_database_instance.instance", &instance),
    85  					testAccCheckGoogleSqlDatabaseInstanceEquals(
    86  						"google_sql_database_instance.instance", &instance),
    87  				),
    88  			},
    89  		},
    90  	})
    91  }
    92  
    93  func TestAccGoogleSqlDatabaseInstance_settings_downgrade(t *testing.T) {
    94  	var instance sqladmin.DatabaseInstance
    95  
    96  	resource.Test(t, resource.TestCase{
    97  		PreCheck:     func() { testAccPreCheck(t) },
    98  		Providers:    testAccProviders,
    99  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   100  		Steps: []resource.TestStep{
   101  			resource.TestStep{
   102  				Config: testGoogleSqlDatabaseInstance_settings,
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testAccCheckGoogleSqlDatabaseInstanceExists(
   105  						"google_sql_database_instance.instance", &instance),
   106  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   107  						"google_sql_database_instance.instance", &instance),
   108  				),
   109  			},
   110  			resource.TestStep{
   111  				Config: testGoogleSqlDatabaseInstance_basic,
   112  				Check: resource.ComposeTestCheckFunc(
   113  					testAccCheckGoogleSqlDatabaseInstanceExists(
   114  						"google_sql_database_instance.instance", &instance),
   115  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   116  						"google_sql_database_instance.instance", &instance),
   117  				),
   118  			},
   119  		},
   120  	})
   121  }
   122  
   123  func testAccCheckGoogleSqlDatabaseInstanceEquals(n string,
   124  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  		rs, ok := s.RootModule().Resources[n]
   127  		if !ok {
   128  			return fmt.Errorf("Not found: %s", n)
   129  		}
   130  		attributes := rs.Primary.Attributes
   131  
   132  		server := instance.Name
   133  		local := attributes["name"]
   134  		if server != local {
   135  			return fmt.Errorf("Error name mismatch, (%s, %s)", server, local)
   136  		}
   137  
   138  		server = instance.Settings.Tier
   139  		local = attributes["settings.0.tier"]
   140  		if server != local {
   141  			return fmt.Errorf("Error settings.tier mismatch, (%s, %s)", server, local)
   142  		}
   143  
   144  		server = instance.MasterInstanceName
   145  		local = attributes["master_instance_name"]
   146  		if server != local && len(server) > 0 && len(local) > 0 {
   147  			return fmt.Errorf("Error master_instance_name mismatch, (%s, %s)", server, local)
   148  		}
   149  
   150  		server = instance.Settings.ActivationPolicy
   151  		local = attributes["settings.0.activation_policy"]
   152  		if server != local && len(server) > 0 && len(local) > 0 {
   153  			return fmt.Errorf("Error settings.activation_policy mismatch, (%s, %s)", server, local)
   154  		}
   155  
   156  		if instance.Settings.BackupConfiguration != nil {
   157  			server = strconv.FormatBool(instance.Settings.BackupConfiguration.BinaryLogEnabled)
   158  			local = attributes["settings.0.backup_configuration.0.binary_log_enabled"]
   159  			if server != local && len(server) > 0 && len(local) > 0 {
   160  				return fmt.Errorf("Error settings.backup_configuration.binary_log_enabled mismatch, (%s, %s)", server, local)
   161  			}
   162  
   163  			server = strconv.FormatBool(instance.Settings.BackupConfiguration.Enabled)
   164  			local = attributes["settings.0.backup_configuration.0.enabled"]
   165  			if server != local && len(server) > 0 && len(local) > 0 {
   166  				return fmt.Errorf("Error settings.backup_configuration.enabled mismatch, (%s, %s)", server, local)
   167  			}
   168  
   169  			server = instance.Settings.BackupConfiguration.StartTime
   170  			local = attributes["settings.0.backup_configuration.0.start_time"]
   171  			if server != local && len(server) > 0 && len(local) > 0 {
   172  				return fmt.Errorf("Error settings.backup_configuration.start_time mismatch, (%s, %s)", server, local)
   173  			}
   174  		}
   175  
   176  		server = strconv.FormatBool(instance.Settings.CrashSafeReplicationEnabled)
   177  		local = attributes["settings.0.crash_safe_replication"]
   178  		if server != local && len(server) > 0 && len(local) > 0 {
   179  			return fmt.Errorf("Error settings.crash_safe_replication mismatch, (%s, %s)", server, local)
   180  		}
   181  
   182  		if instance.Settings.IpConfiguration != nil {
   183  			server = strconv.FormatBool(instance.Settings.IpConfiguration.Ipv4Enabled)
   184  			local = attributes["settings.0.ip_configuration.0.ipv4_enabled"]
   185  			if server != local && len(server) > 0 && len(local) > 0 {
   186  				return fmt.Errorf("Error settings.ip_configuration.ipv4_enabled mismatch, (%s, %s)", server, local)
   187  			}
   188  
   189  			server = strconv.FormatBool(instance.Settings.IpConfiguration.RequireSsl)
   190  			local = attributes["settings.0.ip_configuration.0.require_ssl"]
   191  			if server != local && len(server) > 0 && len(local) > 0 {
   192  				return fmt.Errorf("Error settings.ip_configuration.require_ssl mismatch, (%s, %s)", server, local)
   193  			}
   194  		}
   195  
   196  		if instance.Settings.LocationPreference != nil {
   197  			server = instance.Settings.LocationPreference.FollowGaeApplication
   198  			local = attributes["settings.0.location_preference.0.follow_gae_application"]
   199  			if server != local && len(server) > 0 && len(local) > 0 {
   200  				return fmt.Errorf("Error settings.location_preference.follow_gae_application mismatch, (%s, %s)", server, local)
   201  			}
   202  
   203  			server = instance.Settings.LocationPreference.Zone
   204  			local = attributes["settings.0.location_preference.0.zone"]
   205  			if server != local && len(server) > 0 && len(local) > 0 {
   206  				return fmt.Errorf("Error settings.location_preference.zone mismatch, (%s, %s)", server, local)
   207  			}
   208  		}
   209  
   210  		server = instance.Settings.PricingPlan
   211  		local = attributes["settings.0.pricing_plan"]
   212  		if server != local && len(server) > 0 && len(local) > 0 {
   213  			return fmt.Errorf("Error settings.pricing_plan mismatch, (%s, %s)", server, local)
   214  		}
   215  
   216  		if instance.ReplicaConfiguration != nil &&
   217  			instance.ReplicaConfiguration.MysqlReplicaConfiguration != nil {
   218  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.CaCertificate
   219  			local = attributes["replica_configuration.0.ca_certificate"]
   220  			if server != local && len(server) > 0 && len(local) > 0 {
   221  				return fmt.Errorf("Error replica_configuration.ca_certificate mismatch, (%s, %s)", server, local)
   222  			}
   223  
   224  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.ClientCertificate
   225  			local = attributes["replica_configuration.0.client_certificate"]
   226  			if server != local && len(server) > 0 && len(local) > 0 {
   227  				return fmt.Errorf("Error replica_configuration.client_certificate mismatch, (%s, %s)", server, local)
   228  			}
   229  
   230  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.ClientKey
   231  			local = attributes["replica_configuration.0.client_key"]
   232  			if server != local && len(server) > 0 && len(local) > 0 {
   233  				return fmt.Errorf("Error replica_configuration.client_key mismatch, (%s, %s)", server, local)
   234  			}
   235  
   236  			server = strconv.FormatInt(instance.ReplicaConfiguration.MysqlReplicaConfiguration.ConnectRetryInterval, 10)
   237  			local = attributes["replica_configuration.0.connect_retry_interval"]
   238  			if server != local && len(server) > 0 && len(local) > 0 {
   239  				return fmt.Errorf("Error replica_configuration.connect_retry_interval mismatch, (%s, %s)", server, local)
   240  			}
   241  
   242  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.DumpFilePath
   243  			local = attributes["replica_configuration.0.dump_file_path"]
   244  			if server != local && len(server) > 0 && len(local) > 0 {
   245  				return fmt.Errorf("Error replica_configuration.dump_file_path mismatch, (%s, %s)", server, local)
   246  			}
   247  
   248  			server = strconv.FormatInt(instance.ReplicaConfiguration.MysqlReplicaConfiguration.MasterHeartbeatPeriod, 10)
   249  			local = attributes["replica_configuration.0.master_heartbeat_period"]
   250  			if server != local && len(server) > 0 && len(local) > 0 {
   251  				return fmt.Errorf("Error replica_configuration.master_heartbeat_period mismatch, (%s, %s)", server, local)
   252  			}
   253  
   254  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.Password
   255  			local = attributes["replica_configuration.0.password"]
   256  			if server != local && len(server) > 0 && len(local) > 0 {
   257  				return fmt.Errorf("Error replica_configuration.password mismatch, (%s, %s)", server, local)
   258  			}
   259  
   260  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.SslCipher
   261  			local = attributes["replica_configuration.0.ssl_cipher"]
   262  			if server != local && len(server) > 0 && len(local) > 0 {
   263  				return fmt.Errorf("Error replica_configuration.ssl_cipher mismatch, (%s, %s)", server, local)
   264  			}
   265  
   266  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.Username
   267  			local = attributes["replica_configuration.0.username"]
   268  			if server != local && len(server) > 0 && len(local) > 0 {
   269  				return fmt.Errorf("Error replica_configuration.username mismatch, (%s, %s)", server, local)
   270  			}
   271  
   272  			server = strconv.FormatBool(instance.ReplicaConfiguration.MysqlReplicaConfiguration.VerifyServerCertificate)
   273  			local = attributes["replica_configuration.0.verify_server_certificate"]
   274  			if server != local && len(server) > 0 && len(local) > 0 {
   275  				return fmt.Errorf("Error replica_configuration.verify_server_certificate mismatch, (%s, %s)", server, local)
   276  			}
   277  		}
   278  
   279  		return nil
   280  	}
   281  }
   282  
   283  func testAccCheckGoogleSqlDatabaseInstanceExists(n string,
   284  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   285  	return func(s *terraform.State) error {
   286  		config := testAccProvider.Meta().(*Config)
   287  		rs, ok := s.RootModule().Resources[n]
   288  		if !ok {
   289  			return fmt.Errorf("Not found: %s", n)
   290  		}
   291  
   292  		found, err := config.clientSqlAdmin.Instances.Get(config.Project,
   293  			rs.Primary.Attributes["name"]).Do()
   294  
   295  		*instance = *found
   296  
   297  		if err != nil {
   298  			return fmt.Errorf("Not found: %s", n)
   299  		}
   300  
   301  		return nil
   302  	}
   303  }
   304  
   305  func testAccGoogleSqlDatabaseInstanceDestroy(s *terraform.State) error {
   306  	for _, rs := range s.RootModule().Resources {
   307  		config := testAccProvider.Meta().(*Config)
   308  		if rs.Type != "google_sql_database_instance" {
   309  			continue
   310  		}
   311  
   312  		_, err := config.clientSqlAdmin.Instances.Get(config.Project,
   313  			rs.Primary.Attributes["name"]).Do()
   314  		if err == nil {
   315  			return fmt.Errorf("Database Instance still exists")
   316  		}
   317  	}
   318  
   319  	return nil
   320  }
   321  
   322  var databaseId = genRandInt()
   323  
   324  var testGoogleSqlDatabaseInstance_basic = fmt.Sprintf(`
   325  resource "google_sql_database_instance" "instance" {
   326  	name = "tf-lw-%d"
   327  	region = "us-central"
   328  	settings {
   329  		tier = "D0"
   330  		crash_safe_replication = false
   331  	}
   332  }
   333  `, databaseId)
   334  
   335  var testGoogleSqlDatabaseInstance_settings = fmt.Sprintf(`
   336  resource "google_sql_database_instance" "instance" {
   337  	name = "tf-lw-%d"
   338  	region = "us-central"
   339  	settings {
   340  		tier = "D0"
   341  		crash_safe_replication = false
   342  		replication_type = "ASYNCHRONOUS"
   343  		location_preference {
   344  			zone = "us-central1-f"
   345  		}
   346  
   347  		ip_configuration {
   348  			ipv4_enabled = "true"
   349  			authorized_networks {
   350  				value = "108.12.12.12"
   351  				name = "misc"
   352  				expiration_time = "2017-11-15T16:19:00.094Z"
   353  			}
   354  		}
   355  
   356  		backup_configuration {
   357  			enabled = "true"
   358  			start_time = "19:19"
   359  		}
   360  
   361  		activation_policy = "ON_DEMAND"
   362  	}
   363  }
   364  `, databaseId)
   365  
   366  // Note - this test is not feasible to run unless we generate
   367  // backups first.
   368  var testGoogleSqlDatabaseInstance_replica = fmt.Sprintf(`
   369  resource "google_sql_database_instance" "instance_master" {
   370  	name = "tf-lw-%d"
   371  	database_version = "MYSQL_5_6"
   372  	region = "us-east1"
   373  
   374  	settings {
   375  		tier = "D0"
   376  		crash_safe_replication = true
   377  
   378  		backup_configuration {
   379  			enabled = true
   380  			start_time = "00:00"
   381  			binary_log_enabled = true
   382  		}
   383  	}
   384  }
   385  
   386  resource "google_sql_database_instance" "instance" {
   387  	name = "tf-lw-%d"
   388  	database_version = "MYSQL_5_6"
   389  	region = "us-central"
   390  
   391  	settings {
   392  		tier = "D0"
   393  	}
   394  
   395  	master_instance_name = "${google_sql_database_instance.instance_master.name}"
   396  
   397  	replica_configuration {
   398  		ca_certificate = "${file("~/tmp/fake.pem")}"
   399  		client_certificate = "${file("~/tmp/fake.pem")}"
   400  		client_key = "${file("~/tmp/fake.pem")}"
   401  		connect_retry_interval = 100
   402  		master_heartbeat_period = 10000
   403  		password = "password"
   404  		username = "username"
   405  		ssl_cipher = "ALL"
   406  		verify_server_certificate = false
   407  	}
   408  }
   409  `, genRandInt(), genRandInt())