github.com/ffrizzo/terraform@v0.8.2-0.20161219200057-992e12335f3d/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  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckPostgresqlRoleDestroy,
    17  		Steps: []resource.TestStep{
    18  			{
    19  				Config: testAccPostgresqlRoleConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckPostgresqlRoleExists("postgresql_role.myrole2", "true"),
    22  					resource.TestCheckResourceAttr(
    23  						"postgresql_role.myrole2", "name", "myrole2"),
    24  					resource.TestCheckResourceAttr(
    25  						"postgresql_role.myrole2", "login", "true"),
    26  
    27  					resource.TestCheckResourceAttr(
    28  						"postgresql_role.role_with_defaults", "name", "testing_role_with_defaults"),
    29  					resource.TestCheckResourceAttr(
    30  						"postgresql_role.role_with_defaults", "superuser", "false"),
    31  					resource.TestCheckResourceAttr(
    32  						"postgresql_role.role_with_defaults", "create_database", "false"),
    33  					resource.TestCheckResourceAttr(
    34  						"postgresql_role.role_with_defaults", "create_role", "false"),
    35  					resource.TestCheckResourceAttr(
    36  						"postgresql_role.role_with_defaults", "inherit", "false"),
    37  					resource.TestCheckResourceAttr(
    38  						"postgresql_role.role_with_defaults", "replication", "false"),
    39  					resource.TestCheckResourceAttr(
    40  						"postgresql_role.role_with_defaults", "bypass_row_level_security", "false"),
    41  					resource.TestCheckResourceAttr(
    42  						"postgresql_role.role_with_defaults", "connection_limit", "-1"),
    43  					resource.TestCheckResourceAttr(
    44  						"postgresql_role.role_with_defaults", "encrypted_password", "true"),
    45  					resource.TestCheckResourceAttr(
    46  						"postgresql_role.role_with_defaults", "password", ""),
    47  					resource.TestCheckResourceAttr(
    48  						"postgresql_role.role_with_defaults", "valid_until", "infinity"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckPostgresqlRoleDestroy(s *terraform.State) error {
    56  	client := testAccProvider.Meta().(*Client)
    57  
    58  	for _, rs := range s.RootModule().Resources {
    59  		if rs.Type != "postgresql_role" {
    60  			continue
    61  		}
    62  
    63  		exists, err := checkRoleExists(client, rs.Primary.ID)
    64  
    65  		if err != nil {
    66  			return fmt.Errorf("Error checking role %s", err)
    67  		}
    68  
    69  		if exists {
    70  			return fmt.Errorf("Role still exists after destroy")
    71  		}
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  func testAccCheckPostgresqlRoleExists(n string, canLogin string) resource.TestCheckFunc {
    78  	return func(s *terraform.State) error {
    79  		rs, ok := s.RootModule().Resources[n]
    80  		if !ok {
    81  			return fmt.Errorf("Resource not found: %s", n)
    82  		}
    83  
    84  		if rs.Primary.ID == "" {
    85  			return fmt.Errorf("No ID is set")
    86  		}
    87  
    88  		actualCanLogin := rs.Primary.Attributes["login"]
    89  		if actualCanLogin != canLogin {
    90  			return fmt.Errorf("Wrong value for login expected %s got %s", canLogin, actualCanLogin)
    91  		}
    92  
    93  		client := testAccProvider.Meta().(*Client)
    94  		exists, err := checkRoleExists(client, rs.Primary.ID)
    95  
    96  		if err != nil {
    97  			return fmt.Errorf("Error checking role %s", err)
    98  		}
    99  
   100  		if !exists {
   101  			return fmt.Errorf("Role not found")
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  func checkRoleExists(client *Client, roleName string) (bool, error) {
   109  	conn, err := client.Connect()
   110  	if err != nil {
   111  		return false, err
   112  	}
   113  	defer conn.Close()
   114  
   115  	var _rez int
   116  	err = conn.QueryRow("SELECT 1 from pg_roles d WHERE rolname=$1", roleName).Scan(&_rez)
   117  	switch {
   118  	case err == sql.ErrNoRows:
   119  		return false, nil
   120  	case err != nil:
   121  		return false, fmt.Errorf("Error reading info about role: %s", err)
   122  	default:
   123  		return true, nil
   124  	}
   125  }
   126  
   127  var testAccPostgresqlRoleConfig = `
   128  resource "postgresql_role" "myrole2" {
   129    name = "myrole2"
   130    login = true
   131  }
   132  
   133  resource "postgresql_role" "role_with_pwd" {
   134    name = "role_with_pwd"
   135    login = true
   136    password = "mypass"
   137  }
   138  
   139  resource "postgresql_role" "role_with_pwd_encr" {
   140    name = "role_with_pwd_encr"
   141    login = true
   142    password = "mypass"
   143    encrypted = true
   144  }
   145  
   146  resource "postgresql_role" "role_with_pwd_no_login" {
   147    name = "role_with_pwd_no_login"
   148    password = "mypass"
   149  }
   150  
   151  resource "postgresql_role" "role_simple" {
   152    name = "role_simple"
   153  }
   154  
   155  resource "postgresql_role" "role_with_defaults" {
   156    name = "testing_role_with_defaults"
   157    superuser = false
   158    create_database = false
   159    create_role = false
   160    inherit = false
   161    login = false
   162    replication = false
   163    bypass_row_level_security = false
   164    connection_limit = -1
   165    encrypted_password = true
   166    password = ""
   167    valid_until = "infinity"
   168  }
   169  `