github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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("postgresql_role.role_all_without_grant", "name", "role_all_without_grant"),
    23  					resource.TestCheckResourceAttr("postgresql_role.role_all_without_grant", "login", "true"),
    24  
    25  					resource.TestCheckResourceAttr("postgresql_role.role_all_with_grant", "name", "role_all_with_grant"),
    26  
    27  					resource.TestCheckResourceAttr("postgresql_schema.test1", "name", "foo"),
    28  
    29  					resource.TestCheckResourceAttr("postgresql_schema.test2", "name", "bar"),
    30  					resource.TestCheckResourceAttr("postgresql_schema.test2", "owner", "role_all_without_grant"),
    31  					resource.TestCheckResourceAttr("postgresql_schema.test2", "if_not_exists", "false"),
    32  					resource.TestCheckResourceAttr("postgresql_schema.test2", "policy.#", "1"),
    33  					resource.TestCheckResourceAttr("postgresql_schema.test2", "policy.1948480595.create", "true"),
    34  					resource.TestCheckResourceAttr("postgresql_schema.test2", "policy.1948480595.create_with_grant", "false"),
    35  					resource.TestCheckResourceAttr("postgresql_schema.test2", "policy.1948480595.usage", "true"),
    36  					resource.TestCheckResourceAttr("postgresql_schema.test2", "policy.1948480595.usage_with_grant", "false"),
    37  					resource.TestCheckResourceAttr("postgresql_schema.test2", "policy.1948480595.role", "role_all_without_grant"),
    38  
    39  					resource.TestCheckResourceAttr("postgresql_schema.test3", "name", "baz"),
    40  					resource.TestCheckResourceAttr("postgresql_schema.test3", "owner", "role_all_without_grant"),
    41  					resource.TestCheckResourceAttr("postgresql_schema.test3", "if_not_exists", "true"),
    42  					resource.TestCheckResourceAttr("postgresql_schema.test3", "policy.#", "2"),
    43  					resource.TestCheckResourceAttr("postgresql_schema.test3", "policy.1013320538.create_with_grant", "true"),
    44  					resource.TestCheckResourceAttr("postgresql_schema.test3", "policy.1013320538.usage_with_grant", "true"),
    45  					resource.TestCheckResourceAttr("postgresql_schema.test3", "policy.1013320538.role", "role_all_with_grant"),
    46  					resource.TestCheckResourceAttr("postgresql_schema.test3", "policy.1948480595.create", "true"),
    47  					resource.TestCheckResourceAttr("postgresql_schema.test3", "policy.1948480595.usage", "true"),
    48  					resource.TestCheckResourceAttr("postgresql_schema.test3", "policy.1948480595.role", "role_all_without_grant"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccPostgresqlSchema_AddPolicy(t *testing.T) {
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckPostgresqlSchemaDestroy,
    60  		Steps: []resource.TestStep{
    61  			{
    62  				Config: testAccPostgresqlSchemaGrant1,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckPostgresqlSchemaExists("postgresql_schema.test4", "test4"),
    65  
    66  					resource.TestCheckResourceAttr("postgresql_role.all_without_grant_stay", "name", "all_without_grant_stay"),
    67  					resource.TestCheckResourceAttr("postgresql_role.all_without_grant_drop", "name", "all_without_grant_drop"),
    68  					resource.TestCheckResourceAttr("postgresql_role.policy_compose", "name", "policy_compose"),
    69  					resource.TestCheckResourceAttr("postgresql_role.policy_move", "name", "policy_move"),
    70  
    71  					resource.TestCheckResourceAttr("postgresql_role.all_with_grantstay", "name", "all_with_grantstay"),
    72  					resource.TestCheckResourceAttr("postgresql_role.all_with_grantdrop", "name", "all_with_grantdrop"),
    73  
    74  					resource.TestCheckResourceAttr("postgresql_schema.test4", "name", "test4"),
    75  					resource.TestCheckResourceAttr("postgresql_schema.test4", "owner", "all_without_grant_stay"),
    76  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.#", "7"),
    77  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.create", "false"),
    78  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.create_with_grant", "true"),
    79  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.role", "all_with_grantstay"),
    80  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.usage", "false"),
    81  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.usage_with_grant", "true"),
    82  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1417738359.create", "true"),
    83  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1417738359.create_with_grant", "false"),
    84  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1417738359.role", "policy_move"),
    85  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1417738359.usage", "true"),
    86  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1417738359.usage_with_grant", "false"),
    87  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1762357194.create", "true"),
    88  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1762357194.create_with_grant", "false"),
    89  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1762357194.role", "all_without_grant_drop"),
    90  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1762357194.usage", "true"),
    91  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.1762357194.usage_with_grant", "false"),
    92  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.create", "true"),
    93  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.create_with_grant", "false"),
    94  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.role", "all_without_grant_stay"),
    95  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.usage", "true"),
    96  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.usage_with_grant", "false"),
    97  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.create", "false"),
    98  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.create_with_grant", "true"),
    99  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.role", "policy_compose"),
   100  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.usage", "false"),
   101  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.usage_with_grant", "true"),
   102  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.create", "false"),
   103  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.create_with_grant", "true"),
   104  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.role", "all_with_grantdrop"),
   105  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.usage", "false"),
   106  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.usage_with_grant", "true"),
   107  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.create", "true"),
   108  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.create_with_grant", "false"),
   109  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.role", "policy_compose"),
   110  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.usage", "true"),
   111  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.usage_with_grant", "false"),
   112  				),
   113  			},
   114  			{
   115  				Config: testAccPostgresqlSchemaGrant2,
   116  				Check: resource.ComposeTestCheckFunc(
   117  					testAccCheckPostgresqlSchemaExists("postgresql_schema.test4", "test4"),
   118  					resource.TestCheckResourceAttr("postgresql_role.all_without_grant_stay", "name", "all_without_grant_stay"),
   119  					resource.TestCheckResourceAttr("postgresql_role.all_without_grant_drop", "name", "all_without_grant_drop"),
   120  					resource.TestCheckResourceAttr("postgresql_role.policy_compose", "name", "policy_compose"),
   121  					resource.TestCheckResourceAttr("postgresql_role.policy_move", "name", "policy_move"),
   122  
   123  					resource.TestCheckResourceAttr("postgresql_role.all_with_grantstay", "name", "all_with_grantstay"),
   124  
   125  					resource.TestCheckResourceAttr("postgresql_schema.test4", "name", "test4"),
   126  					resource.TestCheckResourceAttr("postgresql_schema.test4", "owner", "all_without_grant_stay"),
   127  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.#", "6"),
   128  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.create", "false"),
   129  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.create_with_grant", "true"),
   130  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.role", "all_with_grantstay"),
   131  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.usage", "false"),
   132  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.usage_with_grant", "true"),
   133  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.create", "true"),
   134  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.create_with_grant", "false"),
   135  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.role", "all_without_grant_stay"),
   136  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.usage", "true"),
   137  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.2524457447.usage_with_grant", "false"),
   138  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3831594020.create", "false"),
   139  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3831594020.create_with_grant", "true"),
   140  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3831594020.role", "policy_move"),
   141  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3831594020.usage", "false"),
   142  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3831594020.usage_with_grant", "true"),
   143  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.create", "false"),
   144  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.create_with_grant", "true"),
   145  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.role", "policy_compose"),
   146  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.usage", "false"),
   147  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.usage_with_grant", "true"),
   148  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.468685299.create", "true"),
   149  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.468685299.create_with_grant", "false"),
   150  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.468685299.role", "policy_new"),
   151  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.468685299.usage", "true"),
   152  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.468685299.usage_with_grant", "false"),
   153  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.create", "true"),
   154  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.create_with_grant", "false"),
   155  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.role", "policy_compose"),
   156  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.usage", "true"),
   157  					resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.usage_with_grant", "false"),
   158  				),
   159  			},
   160  		},
   161  	})
   162  }
   163  
   164  func testAccCheckPostgresqlSchemaDestroy(s *terraform.State) error {
   165  	client := testAccProvider.Meta().(*Client)
   166  
   167  	for _, rs := range s.RootModule().Resources {
   168  		if rs.Type != "postgresql_schema" {
   169  			continue
   170  		}
   171  
   172  		exists, err := checkSchemaExists(client, rs.Primary.ID)
   173  		if err != nil {
   174  			return fmt.Errorf("Error checking schema %s", err)
   175  		}
   176  
   177  		if exists {
   178  			return fmt.Errorf("Schema still exists after destroy")
   179  		}
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  func testAccCheckPostgresqlSchemaExists(n string, schemaName string) resource.TestCheckFunc {
   186  	return func(s *terraform.State) error {
   187  		rs, ok := s.RootModule().Resources[n]
   188  		if !ok {
   189  			return fmt.Errorf("Resource not found: %s", n)
   190  		}
   191  
   192  		if rs.Primary.ID == "" {
   193  			return fmt.Errorf("No ID is set")
   194  		}
   195  
   196  		actualSchemaName := rs.Primary.Attributes["name"]
   197  		if actualSchemaName != schemaName {
   198  			return fmt.Errorf("Wrong value for schema name expected %s got %s", schemaName, actualSchemaName)
   199  		}
   200  
   201  		client := testAccProvider.Meta().(*Client)
   202  		exists, err := checkSchemaExists(client, rs.Primary.ID)
   203  
   204  		if err != nil {
   205  			return fmt.Errorf("Error checking schema %s", err)
   206  		}
   207  
   208  		if !exists {
   209  			return fmt.Errorf("Schema not found")
   210  		}
   211  
   212  		return nil
   213  	}
   214  }
   215  
   216  func checkSchemaExists(client *Client, schemaName string) (bool, error) {
   217  	conn, err := client.Connect()
   218  	if err != nil {
   219  		return false, err
   220  	}
   221  	defer conn.Close()
   222  
   223  	var _rez string
   224  	err = conn.QueryRow("SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname=$1", schemaName).Scan(&_rez)
   225  	switch {
   226  	case err == sql.ErrNoRows:
   227  		return false, nil
   228  	case err != nil:
   229  		return false, fmt.Errorf("Error reading info about schema: %s", err)
   230  	default:
   231  		return true, nil
   232  	}
   233  }
   234  
   235  const testAccPostgresqlSchemaConfig = `
   236  resource "postgresql_role" "role_all_without_grant" {
   237    name = "role_all_without_grant"
   238    login = true
   239  }
   240  
   241  resource "postgresql_role" "role_all_with_grant" {
   242    name = "role_all_with_grant"
   243  }
   244  
   245  resource "postgresql_schema" "test1" {
   246    name = "foo"
   247  }
   248  
   249  resource "postgresql_schema" "test2" {
   250    name = "bar"
   251    owner = "${postgresql_role.role_all_without_grant.name}"
   252    if_not_exists = false
   253  
   254    policy {
   255      create = true
   256      usage = true
   257      role = "${postgresql_role.role_all_without_grant.name}"
   258    }
   259  }
   260  
   261  resource "postgresql_schema" "test3" {
   262    name = "baz"
   263    owner = "${postgresql_role.role_all_without_grant.name}"
   264    if_not_exists = true
   265  
   266    policy {
   267      create_with_grant = true
   268      usage_with_grant = true
   269      role = "${postgresql_role.role_all_with_grant.name}"
   270    }
   271  
   272    policy {
   273      create = true
   274      usage = true
   275      role = "${postgresql_role.role_all_without_grant.name}"
   276    }
   277  }
   278  `
   279  
   280  const testAccPostgresqlSchemaGrant1 = `
   281  resource "postgresql_role" "all_without_grant_stay" {
   282    name = "all_without_grant_stay"
   283  }
   284  
   285  resource "postgresql_role" "all_without_grant_drop" {
   286    name = "all_without_grant_drop"
   287  }
   288  
   289  resource "postgresql_role" "policy_compose" {
   290    name = "policy_compose"
   291  }
   292  
   293  resource "postgresql_role" "policy_move" {
   294    name = "policy_move"
   295  }
   296  
   297  resource "postgresql_role" "all_with_grantstay" {
   298    name = "all_with_grantstay"
   299  }
   300  
   301  resource "postgresql_role" "all_with_grantdrop" {
   302    name = "all_with_grantdrop"
   303  }
   304  
   305  resource "postgresql_schema" "test4" {
   306    name = "test4"
   307    owner = "${postgresql_role.all_without_grant_stay.name}"
   308  
   309    policy {
   310      create = true
   311      usage = true
   312      role = "${postgresql_role.all_without_grant_stay.name}"
   313    }
   314  
   315    policy {
   316      create = true
   317      usage = true
   318      role = "${postgresql_role.all_without_grant_drop.name}"
   319    }
   320  
   321    policy {
   322      create = true
   323      usage = true
   324      role = "${postgresql_role.policy_compose.name}"
   325    }
   326  
   327    policy {
   328      create = true
   329      usage = true
   330      role = "${postgresql_role.policy_move.name}"
   331    }
   332  
   333    policy {
   334      create_with_grant = true
   335      usage_with_grant = true
   336      role = "${postgresql_role.all_with_grantstay.name}"
   337    }
   338  
   339    policy {
   340      create_with_grant = true
   341      usage_with_grant = true
   342      role = "${postgresql_role.all_with_grantdrop.name}"
   343    }
   344  
   345    policy {
   346      create_with_grant = true
   347      usage_with_grant = true
   348      role = "${postgresql_role.policy_compose.name}"
   349    }
   350  }
   351  `
   352  
   353  const testAccPostgresqlSchemaGrant2 = `
   354  resource "postgresql_role" "all_without_grant_stay" {
   355    name = "all_without_grant_stay"
   356  }
   357  
   358  resource "postgresql_role" "all_without_grant_drop" {
   359    name = "all_without_grant_drop"
   360  }
   361  
   362  resource "postgresql_role" "policy_compose" {
   363    name = "policy_compose"
   364  }
   365  
   366  resource "postgresql_role" "policy_move" {
   367    name = "policy_move"
   368  }
   369  
   370  resource "postgresql_role" "all_with_grantstay" {
   371    name = "all_with_grantstay"
   372  }
   373  
   374  resource "postgresql_role" "policy_new" {
   375    name = "policy_new"
   376  }
   377  
   378  resource "postgresql_schema" "test4" {
   379    name = "test4"
   380    owner = "${postgresql_role.all_without_grant_stay.name}"
   381  
   382    policy {
   383      create = true
   384      usage = true
   385      role = "${postgresql_role.all_without_grant_stay.name}"
   386    }
   387  
   388    policy {
   389      create = true
   390      usage = true
   391      role = "${postgresql_role.policy_compose.name}"
   392    }
   393  
   394    policy {
   395      create_with_grant = true
   396      usage_with_grant = true
   397      role = "${postgresql_role.all_with_grantstay.name}"
   398    }
   399  
   400    policy {
   401      create_with_grant = true
   402      usage_with_grant = true
   403      role = "${postgresql_role.policy_compose.name}"
   404    }
   405  
   406    policy {
   407      create_with_grant = true
   408      usage_with_grant = true
   409      role = "${postgresql_role.policy_move.name}"
   410    }
   411  
   412    policy {
   413      create = true
   414      usage = true
   415      role = "${postgresql_role.policy_new.name}"
   416    }
   417  }
   418  `