github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/mysql/resource_database_test.go (about)

     1  package mysql
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	mysqlc "github.com/ziutek/mymysql/mysql"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccDatabase(t *testing.T) {
    15  	var dbName string
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccDatabaseCheckDestroy(dbName),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccDatabaseConfig_basic,
    23  				Check: testAccDatabaseCheck(
    24  					"mysql_database.test", &dbName,
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccDatabaseCheck(rn string, name *string) resource.TestCheckFunc {
    32  	return func(s *terraform.State) error {
    33  		rs, ok := s.RootModule().Resources[rn]
    34  		if !ok {
    35  			return fmt.Errorf("resource not found: %s", rn)
    36  		}
    37  
    38  		if rs.Primary.ID == "" {
    39  			return fmt.Errorf("database id not set")
    40  		}
    41  
    42  		conn := testAccProvider.Meta().(*providerConfiguration).Conn
    43  		rows, _, err := conn.Query("SHOW CREATE DATABASE terraform_acceptance_test")
    44  		if err != nil {
    45  			return fmt.Errorf("error reading database: %s", err)
    46  		}
    47  		if len(rows) != 1 {
    48  			return fmt.Errorf("expected 1 row reading database but got %d", len(rows))
    49  		}
    50  
    51  		row := rows[0]
    52  		createSQL := string(row[1].([]byte))
    53  
    54  		if strings.Index(createSQL, "CHARACTER SET utf8") == -1 {
    55  			return fmt.Errorf("database default charset isn't utf8")
    56  		}
    57  		if strings.Index(createSQL, "COLLATE utf8_bin") == -1 {
    58  			return fmt.Errorf("database default collation isn't utf8_bin")
    59  		}
    60  
    61  		*name = rs.Primary.ID
    62  
    63  		return nil
    64  	}
    65  }
    66  
    67  func testAccDatabaseCheckDestroy(name string) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		conn := testAccProvider.Meta().(*providerConfiguration).Conn
    70  
    71  		_, _, err := conn.Query("SHOW CREATE DATABASE terraform_acceptance_test")
    72  		if err == nil {
    73  			return fmt.Errorf("database still exists after destroy")
    74  		}
    75  		if mysqlErr, ok := err.(*mysqlc.Error); ok {
    76  			if mysqlErr.Code == mysqlc.ER_BAD_DB_ERROR {
    77  				return nil
    78  			}
    79  		}
    80  
    81  		return fmt.Errorf("got unexpected error: %s", err)
    82  	}
    83  }
    84  
    85  const testAccDatabaseConfig_basic = `
    86  resource "mysql_database" "test" {
    87      name = "terraform_acceptance_test"
    88      default_character_set = "utf8"
    89      default_collation = "utf8_bin"
    90  }
    91  `