github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/postgresql/resource_postgresql_role_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 TestAccPostgresqlRole_Basic(t *testing.T) { 13 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckPostgresqlRoleDestroy, 18 Steps: []resource.TestStep{ 19 { 20 Config: testAccPostgresqlRoleConfig, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckPostgresqlRoleExists("postgresql_role.myrole2", "true"), 23 resource.TestCheckResourceAttr( 24 "postgresql_role.myrole2", "name", "myrole2"), 25 resource.TestCheckResourceAttr( 26 "postgresql_role.myrole2", "login", "true"), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckPostgresqlRoleDestroy(s *terraform.State) error { 34 client := testAccProvider.Meta().(*Client) 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "postgresql_role" { 38 continue 39 } 40 41 exists, err := checkRoleExists(client, rs.Primary.ID) 42 43 if err != nil { 44 return fmt.Errorf("Error checking role %s", err) 45 } 46 47 if exists { 48 return fmt.Errorf("Role still exists after destroy") 49 } 50 } 51 52 return nil 53 } 54 55 func testAccCheckPostgresqlRoleExists(n string, canLogin string) resource.TestCheckFunc { 56 return func(s *terraform.State) error { 57 rs, ok := s.RootModule().Resources[n] 58 if !ok { 59 return fmt.Errorf("Resource not found: %s", n) 60 } 61 62 if rs.Primary.ID == "" { 63 return fmt.Errorf("No ID is set") 64 } 65 66 actualCanLogin := rs.Primary.Attributes["login"] 67 if actualCanLogin != canLogin { 68 return fmt.Errorf("Wrong value for login expected %s got %s", canLogin, actualCanLogin) 69 } 70 71 client := testAccProvider.Meta().(*Client) 72 exists, err := checkRoleExists(client, rs.Primary.ID) 73 74 if err != nil { 75 return fmt.Errorf("Error checking role %s", err) 76 } 77 78 if !exists { 79 return fmt.Errorf("Role not found") 80 } 81 82 return nil 83 } 84 } 85 86 func checkRoleExists(client *Client, roleName string) (bool, error) { 87 conn, err := client.Connect() 88 if err != nil { 89 return false, err 90 } 91 defer conn.Close() 92 93 var _rez int 94 err = conn.QueryRow("SELECT 1 from pg_roles d WHERE rolname=$1", roleName).Scan(&_rez) 95 switch { 96 case err == sql.ErrNoRows: 97 return false, nil 98 case err != nil: 99 return false, fmt.Errorf("Error reading info about role: %s", err) 100 default: 101 return true, nil 102 } 103 } 104 105 var testAccPostgresqlRoleConfig = ` 106 resource "postgresql_role" "myrole2" { 107 name = "myrole2" 108 login = true 109 } 110 111 resource "postgresql_role" "role_with_pwd" { 112 name = "role_with_pwd" 113 login = true 114 password = "mypass" 115 } 116 117 resource "postgresql_role" "role_with_pwd_encr" { 118 name = "role_with_pwd_encr" 119 login = true 120 password = "mypass" 121 encrypted = true 122 } 123 124 resource "postgresql_role" "role_with_pwd_no_login" { 125 name = "role_with_pwd_no_login" 126 password = "mypass" 127 } 128 129 resource "postgresql_role" "role_simple" { 130 name = "role_simple" 131 } 132 `