github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_sql_database_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  
    10  	"google.golang.org/api/sqladmin/v1beta4"
    11  )
    12  
    13  func TestAccGoogleSqlDatabase_basic(t *testing.T) {
    14  	var database sqladmin.Database
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testGoogleSqlDatabase_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckGoogleSqlDatabaseExists(
    25  						"google_sql_database.database", &database),
    26  					testAccCheckGoogleSqlDatabaseEquals(
    27  						"google_sql_database.database", &database),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func testAccCheckGoogleSqlDatabaseEquals(n string,
    35  	database *sqladmin.Database) resource.TestCheckFunc {
    36  	return func(s *terraform.State) error {
    37  		rs, ok := s.RootModule().Resources[n]
    38  		if !ok {
    39  			return fmt.Errorf("Resource not found: %s", n)
    40  		}
    41  
    42  		database_name := rs.Primary.Attributes["name"]
    43  		instance_name := rs.Primary.Attributes["instance"]
    44  
    45  		if database_name != database.Name {
    46  			return fmt.Errorf("Error name mismatch, (%s, %s)", database_name, database.Name)
    47  		}
    48  
    49  		if instance_name != database.Instance {
    50  			return fmt.Errorf("Error instance_name mismatch, (%s, %s)", instance_name, database.Instance)
    51  		}
    52  
    53  		return nil
    54  	}
    55  }
    56  
    57  func testAccCheckGoogleSqlDatabaseExists(n string,
    58  	database *sqladmin.Database) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		config := testAccProvider.Meta().(*Config)
    61  		rs, ok := s.RootModule().Resources[n]
    62  		if !ok {
    63  			return fmt.Errorf("Resource not found: %s", n)
    64  		}
    65  
    66  		database_name := rs.Primary.Attributes["name"]
    67  		instance_name := rs.Primary.Attributes["instance"]
    68  		found, err := config.clientSqlAdmin.Databases.Get(config.Project,
    69  			instance_name, database_name).Do()
    70  
    71  		if err != nil {
    72  			return fmt.Errorf("Not found: %s: %s", n, err)
    73  		}
    74  
    75  		*database = *found
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccGoogleSqlDatabaseDestroy(s *terraform.State) error {
    82  	for _, rs := range s.RootModule().Resources {
    83  		config := testAccProvider.Meta().(*Config)
    84  		if rs.Type != "google_sql_database" {
    85  			continue
    86  		}
    87  
    88  		database_name := rs.Primary.Attributes["name"]
    89  		instance_name := rs.Primary.Attributes["instance"]
    90  		_, err := config.clientSqlAdmin.Databases.Get(config.Project,
    91  			instance_name, database_name).Do()
    92  
    93  		if err == nil {
    94  			return fmt.Errorf("Database resource still exists")
    95  		}
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  var testGoogleSqlDatabase_basic = fmt.Sprintf(`
   102  resource "google_sql_database_instance" "instance" {
   103  	name = "tf-lw-%d"
   104  	region = "us-central"
   105  	settings {
   106  		tier = "D0"
   107  	}
   108  }
   109  
   110  resource "google_sql_database" "database" {
   111  	name = "database1"
   112  	instance = "${google_sql_database_instance.instance.name}"
   113  }
   114  `, genRandInt())