github.com/ffrizzo/terraform@v0.8.2-0.20161219200057-992e12335f3d/builtin/providers/postgresql/resource_postgresql_schema_test.go (about)

     1  package postgresql
     2  
     3  import (
     4  	"database/sql"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccPostgresqlSchema_Basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckPostgresqlSchemaDestroy,
    17  		Steps: []resource.TestStep{
    18  			{
    19  				Config: testAccPostgresqlSchemaConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckPostgresqlSchemaExists("postgresql_schema.test1", "foo"),
    22  					resource.TestCheckResourceAttr(
    23  						"postgresql_role.myrole3", "name", "myrole3"),
    24  					resource.TestCheckResourceAttr(
    25  						"postgresql_role.myrole3", "login", "true"),
    26  
    27  					resource.TestCheckResourceAttr(
    28  						"postgresql_schema.test1", "name", "foo"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckPostgresqlSchemaDestroy(s *terraform.State) error {
    36  	client := testAccProvider.Meta().(*Client)
    37  
    38  	for _, rs := range s.RootModule().Resources {
    39  		if rs.Type != "postgresql_schema" {
    40  			continue
    41  		}
    42  
    43  		exists, err := checkSchemaExists(client, rs.Primary.ID)
    44  		if err != nil {
    45  			return fmt.Errorf("Error checking schema %s", err)
    46  		}
    47  
    48  		if exists {
    49  			return fmt.Errorf("Schema still exists after destroy")
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccCheckPostgresqlSchemaExists(n string, schemaName string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[n]
    59  		if !ok {
    60  			return fmt.Errorf("Resource not found: %s", n)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No ID is set")
    65  		}
    66  
    67  		actualSchemaName := rs.Primary.Attributes["name"]
    68  		if actualSchemaName != schemaName {
    69  			return fmt.Errorf("Wrong value for schema name expected %s got %s", schemaName, actualSchemaName)
    70  		}
    71  
    72  		client := testAccProvider.Meta().(*Client)
    73  		exists, err := checkSchemaExists(client, rs.Primary.ID)
    74  
    75  		if err != nil {
    76  			return fmt.Errorf("Error checking schema %s", err)
    77  		}
    78  
    79  		if !exists {
    80  			return fmt.Errorf("Schema not found")
    81  		}
    82  
    83  		return nil
    84  	}
    85  }
    86  
    87  func checkSchemaExists(client *Client, schemaName string) (bool, error) {
    88  	conn, err := client.Connect()
    89  	if err != nil {
    90  		return false, err
    91  	}
    92  	defer conn.Close()
    93  
    94  	var _rez string
    95  	err = conn.QueryRow("SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname=$1", schemaName).Scan(&_rez)
    96  	switch {
    97  	case err == sql.ErrNoRows:
    98  		return false, nil
    99  	case err != nil:
   100  		return false, fmt.Errorf("Error reading info about schema: %s", err)
   101  	default:
   102  		return true, nil
   103  	}
   104  }
   105  
   106  var testAccPostgresqlSchemaConfig = `
   107  resource "postgresql_role" "myrole3" {
   108    name = "myrole3"
   109    login = true
   110  }
   111  
   112  resource "postgresql_schema" "test1" {
   113    name = "foo"
   114  }
   115  `