github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/postgresql/resource_postgresql_extension_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 TestAccPostgresqlExtension_Basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckPostgresqlExtensionDestroy, 17 Steps: []resource.TestStep{ 18 { 19 Config: testAccPostgresqlExtensionConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckPostgresqlExtensionExists("postgresql_extension.myextension"), 22 resource.TestCheckResourceAttr( 23 "postgresql_extension.myextension", "name", "pg_trgm"), 24 resource.TestCheckResourceAttr( 25 "postgresql_extension.myextension", "schema", "public"), 26 27 // NOTE(sean): Version 1.3 is what's 28 // shipped with PostgreSQL 9.6.1. This 29 // version number may drift in the 30 // future. 31 resource.TestCheckResourceAttr( 32 "postgresql_extension.myextension", "version", "1.3"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func testAccCheckPostgresqlExtensionDestroy(s *terraform.State) error { 40 client := testAccProvider.Meta().(*Client) 41 42 for _, rs := range s.RootModule().Resources { 43 if rs.Type != "postgresql_extension" { 44 continue 45 } 46 47 exists, err := checkExtensionExists(client, rs.Primary.ID) 48 49 if err != nil { 50 return fmt.Errorf("Error checking extension %s", err) 51 } 52 53 if exists { 54 return fmt.Errorf("Extension still exists after destroy") 55 } 56 } 57 58 return nil 59 } 60 61 func testAccCheckPostgresqlExtensionExists(n string) resource.TestCheckFunc { 62 return func(s *terraform.State) error { 63 rs, ok := s.RootModule().Resources[n] 64 if !ok { 65 return fmt.Errorf("Resource not found: %s", n) 66 } 67 68 if rs.Primary.ID == "" { 69 return fmt.Errorf("No ID is set") 70 } 71 72 client := testAccProvider.Meta().(*Client) 73 exists, err := checkExtensionExists(client, rs.Primary.ID) 74 75 if err != nil { 76 return fmt.Errorf("Error checking extension %s", err) 77 } 78 79 if !exists { 80 return fmt.Errorf("Extension not found") 81 } 82 83 return nil 84 } 85 } 86 87 func TestAccPostgresqlExtension_SchemaRename(t *testing.T) { 88 resource.Test(t, resource.TestCase{ 89 PreCheck: func() { testAccPreCheck(t) }, 90 Providers: testAccProviders, 91 CheckDestroy: testAccCheckPostgresqlExtensionDestroy, 92 Steps: []resource.TestStep{ 93 { 94 Config: testAccPostgresqlExtensionSchemaChange1, 95 Check: resource.ComposeTestCheckFunc( 96 testAccCheckPostgresqlExtensionExists("postgresql_extension.ext1trgm"), 97 resource.TestCheckResourceAttr( 98 "postgresql_schema.ext1foo", "name", "foo"), 99 resource.TestCheckResourceAttr( 100 "postgresql_extension.ext1trgm", "name", "pg_trgm"), 101 resource.TestCheckResourceAttr( 102 "postgresql_extension.ext1trgm", "name", "pg_trgm"), 103 resource.TestCheckResourceAttr( 104 "postgresql_extension.ext1trgm", "schema", "foo"), 105 ), 106 }, 107 { 108 Config: testAccPostgresqlExtensionSchemaChange2, 109 Check: resource.ComposeTestCheckFunc( 110 testAccCheckPostgresqlExtensionExists("postgresql_extension.ext1trgm"), 111 resource.TestCheckResourceAttr( 112 "postgresql_schema.ext1foo", "name", "bar"), 113 resource.TestCheckResourceAttr( 114 "postgresql_extension.ext1trgm", "name", "pg_trgm"), 115 resource.TestCheckResourceAttr( 116 "postgresql_extension.ext1trgm", "schema", "bar"), 117 ), 118 }, 119 }, 120 }) 121 } 122 123 func checkExtensionExists(client *Client, extensionName string) (bool, error) { 124 conn, err := client.Connect() 125 if err != nil { 126 return false, err 127 } 128 defer conn.Close() 129 130 var _rez bool 131 err = conn.QueryRow("SELECT TRUE from pg_catalog.pg_extension d WHERE extname=$1", extensionName).Scan(&_rez) 132 switch { 133 case err == sql.ErrNoRows: 134 return false, nil 135 case err != nil: 136 return false, fmt.Errorf("Error reading info about extension: %s", err) 137 default: 138 return true, nil 139 } 140 } 141 142 var testAccPostgresqlExtensionConfig = ` 143 resource "postgresql_extension" "myextension" { 144 name = "pg_trgm" 145 } 146 ` 147 148 var testAccPostgresqlExtensionSchemaChange1 = ` 149 resource "postgresql_schema" "ext1foo" { 150 name = "foo" 151 } 152 153 resource "postgresql_extension" "ext1trgm" { 154 name = "pg_trgm" 155 schema = "${postgresql_schema.ext1foo.name}" 156 } 157 ` 158 159 var testAccPostgresqlExtensionSchemaChange2 = ` 160 resource "postgresql_schema" "ext1foo" { 161 name = "bar" 162 } 163 164 resource "postgresql_extension" "ext1trgm" { 165 name = "pg_trgm" 166 schema = "${postgresql_schema.ext1foo.name}" 167 } 168 `