github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/postgresql/resource_postgresql_database_test.go (about)

     1  package postgresql
     2  
     3  import (
     4  	"database/sql"
     5  	"errors"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccPostgresqlDatabase_Basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: testAccPostgreSQLDatabaseConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb"),
    23  					resource.TestCheckResourceAttr(
    24  						"postgresql_database.mydb", "name", "mydb"),
    25  					resource.TestCheckResourceAttr(
    26  						"postgresql_database.mydb", "owner", "myrole"),
    27  					resource.TestCheckResourceAttr(
    28  						"postgresql_database.default_opts", "owner", "myrole"),
    29  					resource.TestCheckResourceAttr(
    30  						"postgresql_database.default_opts", "name", "default_opts_name"),
    31  					resource.TestCheckResourceAttr(
    32  						"postgresql_database.default_opts", "template", "template0"),
    33  					resource.TestCheckResourceAttr(
    34  						"postgresql_database.default_opts", "encoding", "UTF8"),
    35  					resource.TestCheckResourceAttr(
    36  						"postgresql_database.default_opts", "lc_collate", "C"),
    37  					resource.TestCheckResourceAttr(
    38  						"postgresql_database.default_opts", "lc_ctype", "C"),
    39  					resource.TestCheckResourceAttr(
    40  						"postgresql_database.default_opts", "tablespace_name", "pg_default"),
    41  					resource.TestCheckResourceAttr(
    42  						"postgresql_database.default_opts", "connection_limit", "-1"),
    43  					resource.TestCheckResourceAttr(
    44  						"postgresql_database.default_opts", "allow_connections", "true"),
    45  					resource.TestCheckResourceAttr(
    46  						"postgresql_database.default_opts", "is_template", "false"),
    47  
    48  					resource.TestCheckResourceAttr(
    49  						"postgresql_database.modified_opts", "owner", "myrole"),
    50  					resource.TestCheckResourceAttr(
    51  						"postgresql_database.modified_opts", "name", "custom_template_db"),
    52  					resource.TestCheckResourceAttr(
    53  						"postgresql_database.modified_opts", "template", "template0"),
    54  					resource.TestCheckResourceAttr(
    55  						"postgresql_database.modified_opts", "encoding", "UTF8"),
    56  					resource.TestCheckResourceAttr(
    57  						"postgresql_database.modified_opts", "lc_collate", "en_US.UTF-8"),
    58  					resource.TestCheckResourceAttr(
    59  						"postgresql_database.modified_opts", "lc_ctype", "en_US.UTF-8"),
    60  					resource.TestCheckResourceAttr(
    61  						"postgresql_database.modified_opts", "tablespace_name", "pg_default"),
    62  					resource.TestCheckResourceAttr(
    63  						"postgresql_database.modified_opts", "connection_limit", "10"),
    64  					resource.TestCheckResourceAttr(
    65  						"postgresql_database.modified_opts", "allow_connections", "false"),
    66  					resource.TestCheckResourceAttr(
    67  						"postgresql_database.modified_opts", "is_template", "true"),
    68  
    69  					resource.TestCheckResourceAttr(
    70  						"postgresql_database.pathological_opts", "owner", "myrole"),
    71  					resource.TestCheckResourceAttr(
    72  						"postgresql_database.pathological_opts", "name", "bad_template_db"),
    73  					resource.TestCheckResourceAttr(
    74  						"postgresql_database.pathological_opts", "template", "template0"),
    75  					resource.TestCheckResourceAttr(
    76  						"postgresql_database.pathological_opts", "encoding", "LATIN1"),
    77  					resource.TestCheckResourceAttr(
    78  						"postgresql_database.pathological_opts", "lc_collate", "C"),
    79  					resource.TestCheckResourceAttr(
    80  						"postgresql_database.pathological_opts", "lc_ctype", "C"),
    81  					resource.TestCheckResourceAttr(
    82  						"postgresql_database.pathological_opts", "tablespace_name", "pg_default"),
    83  					resource.TestCheckResourceAttr(
    84  						"postgresql_database.pathological_opts", "connection_limit", "0"),
    85  					resource.TestCheckResourceAttr(
    86  						"postgresql_database.pathological_opts", "allow_connections", "true"),
    87  					resource.TestCheckResourceAttr(
    88  						"postgresql_database.pathological_opts", "is_template", "true"),
    89  				),
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func TestAccPostgresqlDatabase_DefaultOwner(t *testing.T) {
    96  	resource.Test(t, resource.TestCase{
    97  		PreCheck:     func() { testAccPreCheck(t) },
    98  		Providers:    testAccProviders,
    99  		CheckDestroy: testAccCheckPostgresqlDatabaseDestroy,
   100  		Steps: []resource.TestStep{
   101  			{
   102  				Config: testAccPostgreSQLDatabaseConfig,
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testAccCheckPostgresqlDatabaseExists("postgresql_database.mydb_default_owner"),
   105  					resource.TestCheckResourceAttr(
   106  						"postgresql_database.mydb_default_owner", "name", "mydb_default_owner"),
   107  					resource.TestCheckResourceAttrSet(
   108  						"postgresql_database.mydb_default_owner", "owner"),
   109  				),
   110  			},
   111  		},
   112  	})
   113  }
   114  
   115  func testAccCheckPostgresqlDatabaseDestroy(s *terraform.State) error {
   116  	client := testAccProvider.Meta().(*Client)
   117  
   118  	for _, rs := range s.RootModule().Resources {
   119  		if rs.Type != "postgresql_database" {
   120  			continue
   121  		}
   122  
   123  		exists, err := checkDatabaseExists(client, rs.Primary.ID)
   124  
   125  		if err != nil {
   126  			return fmt.Errorf("Error checking db %s", err)
   127  		}
   128  
   129  		if exists {
   130  			return errors.New("Db still exists after destroy")
   131  		}
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  func testAccCheckPostgresqlDatabaseExists(n string) resource.TestCheckFunc {
   138  	return func(s *terraform.State) error {
   139  		rs, ok := s.RootModule().Resources[n]
   140  		if !ok {
   141  			return fmt.Errorf("Resource not found: %s", n)
   142  		}
   143  
   144  		if rs.Primary.ID == "" {
   145  			return errors.New("No ID is set")
   146  		}
   147  
   148  		client := testAccProvider.Meta().(*Client)
   149  		exists, err := checkDatabaseExists(client, rs.Primary.ID)
   150  
   151  		if err != nil {
   152  			return fmt.Errorf("Error checking db %s", err)
   153  		}
   154  
   155  		if !exists {
   156  			return errors.New("Db not found")
   157  		}
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  func checkDatabaseExists(client *Client, dbName string) (bool, error) {
   164  	conn, err := client.Connect()
   165  	if err != nil {
   166  		return false, err
   167  	}
   168  	defer conn.Close()
   169  
   170  	var _rez int
   171  	err = conn.QueryRow("SELECT 1 from pg_database d WHERE datname=$1", dbName).Scan(&_rez)
   172  	switch {
   173  	case err == sql.ErrNoRows:
   174  		return false, nil
   175  	case err != nil:
   176  		return false, fmt.Errorf("Error reading info about database: %s", err)
   177  	default:
   178  		return true, nil
   179  	}
   180  }
   181  
   182  var testAccPostgreSQLDatabaseConfig = `
   183  resource "postgresql_role" "myrole" {
   184    name = "myrole"
   185    login = true
   186  }
   187  
   188  resource "postgresql_database" "mydb" {
   189     name = "mydb"
   190     owner = "${postgresql_role.myrole.name}"
   191  }
   192  
   193  resource "postgresql_database" "mydb2" {
   194     name = "mydb2"
   195     owner = "${postgresql_role.myrole.name}"
   196  }
   197  
   198  resource "postgresql_database" "default_opts" {
   199     name = "default_opts_name"
   200     owner = "${postgresql_role.myrole.name}"
   201     template = "template0"
   202     encoding = "UTF8"
   203     lc_collate = "C"
   204     lc_ctype = "C"
   205     tablespace_name = "pg_default"
   206     connection_limit = -1
   207     allow_connections = true
   208     is_template = false
   209  }
   210  
   211  resource "postgresql_database" "modified_opts" {
   212     name = "custom_template_db"
   213     owner = "${postgresql_role.myrole.name}"
   214     template = "template0"
   215     encoding = "UTF8"
   216     lc_collate = "en_US.UTF-8"
   217     lc_ctype = "en_US.UTF-8"
   218     tablespace_name = "pg_default"
   219     connection_limit = 10
   220     allow_connections = false
   221     is_template = true
   222  }
   223  
   224  resource "postgresql_database" "pathological_opts" {
   225     name = "bad_template_db"
   226     owner = "${postgresql_role.myrole.name}"
   227     template = "template0"
   228     encoding = "LATIN1"
   229     lc_collate = "C"
   230     lc_ctype = "C"
   231     tablespace_name = "pg_default"
   232     connection_limit = 0
   233     allow_connections = true
   234     is_template = true
   235  }
   236  
   237  resource "postgresql_database" "mydb_default_owner" {
   238     name = "mydb_default_owner"
   239  }
   240  
   241  `