github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_sql_database_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/jen20/riviera/sql"
    11  )
    12  
    13  func TestResourceAzureRMSqlDatabaseEdition_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "Random",
    20  			ErrCount: 1,
    21  		},
    22  		{
    23  			Value:    "Basic",
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    "Standard",
    28  			ErrCount: 0,
    29  		},
    30  		{
    31  			Value:    "Premium",
    32  			ErrCount: 0,
    33  		},
    34  		{
    35  			Value:    "DataWarehouse",
    36  			ErrCount: 0,
    37  		},
    38  	}
    39  
    40  	for _, tc := range cases {
    41  		_, errors := validateArmSqlDatabaseEdition(tc.Value, "azurerm_sql_database")
    42  
    43  		if len(errors) != tc.ErrCount {
    44  			t.Fatalf("Expected the Azure RM SQL Database edition to trigger a validation error")
    45  		}
    46  	}
    47  }
    48  
    49  func TestAccAzureRMSqlDatabase_basic(t *testing.T) {
    50  	ri := acctest.RandInt()
    51  	config := fmt.Sprintf(testAccAzureRMSqlDatabase_basic, ri, ri, ri)
    52  
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck:     func() { testAccPreCheck(t) },
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
    57  		Steps: []resource.TestStep{
    58  			{
    59  				Config: config,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func TestAccAzureRMSqlDatabase_withTags(t *testing.T) {
    69  	ri := acctest.RandInt()
    70  	preConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTags, ri, ri, ri)
    71  	postConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTagsUpdate, ri, ri, ri)
    72  
    73  	resource.Test(t, resource.TestCase{
    74  		PreCheck:     func() { testAccPreCheck(t) },
    75  		Providers:    testAccProviders,
    76  		CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
    77  		Steps: []resource.TestStep{
    78  			{
    79  				Config: preConfig,
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
    82  					resource.TestCheckResourceAttr(
    83  						"azurerm_sql_database.test", "tags.%", "2"),
    84  				),
    85  			},
    86  			{
    87  				Config: postConfig,
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
    90  					resource.TestCheckResourceAttr(
    91  						"azurerm_sql_database.test", "tags.%", "1"),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func TestAccAzureRMSqlDatabase_datawarehouse(t *testing.T) {
    99  	ri := acctest.RandInt()
   100  	config := fmt.Sprintf(testAccAzureRMSqlDatabase_datawarehouse, ri, ri, ri)
   101  
   102  	resource.Test(t, resource.TestCase{
   103  		PreCheck:     func() { testAccPreCheck(t) },
   104  		Providers:    testAccProviders,
   105  		CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
   106  		Steps: []resource.TestStep{
   107  			{
   108  				Config: config,
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
   111  				),
   112  			},
   113  		},
   114  	})
   115  }
   116  
   117  func testCheckAzureRMSqlDatabaseExists(name string) resource.TestCheckFunc {
   118  	return func(s *terraform.State) error {
   119  
   120  		rs, ok := s.RootModule().Resources[name]
   121  		if !ok {
   122  			return fmt.Errorf("Not found: %s", name)
   123  		}
   124  
   125  		conn := testAccProvider.Meta().(*ArmClient).rivieraClient
   126  
   127  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
   128  		readRequest.Command = &sql.GetDatabase{}
   129  
   130  		readResponse, err := readRequest.Execute()
   131  		if err != nil {
   132  			return fmt.Errorf("Bad: GetDatabase: %s", err)
   133  		}
   134  		if !readResponse.IsSuccessful() {
   135  			return fmt.Errorf("Bad: GetDatabase: %s", readResponse.Error)
   136  		}
   137  
   138  		return nil
   139  	}
   140  }
   141  
   142  func testCheckAzureRMSqlDatabaseDestroy(s *terraform.State) error {
   143  	conn := testAccProvider.Meta().(*ArmClient).rivieraClient
   144  
   145  	for _, rs := range s.RootModule().Resources {
   146  		if rs.Type != "azurerm_sql_database" {
   147  			continue
   148  		}
   149  
   150  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
   151  		readRequest.Command = &sql.GetDatabase{}
   152  
   153  		readResponse, err := readRequest.Execute()
   154  		if err != nil {
   155  			return fmt.Errorf("Bad: GetDatabase: %s", err)
   156  		}
   157  
   158  		if readResponse.IsSuccessful() {
   159  			return fmt.Errorf("Bad: SQL Database still exists: %s", readResponse.Error)
   160  		}
   161  	}
   162  
   163  	return nil
   164  }
   165  
   166  var testAccAzureRMSqlDatabase_basic = `
   167  resource "azurerm_resource_group" "test" {
   168      name = "acctestRG_%d"
   169      location = "West US"
   170  }
   171  resource "azurerm_sql_server" "test" {
   172      name = "acctestsqlserver%d"
   173      resource_group_name = "${azurerm_resource_group.test.name}"
   174      location = "West US"
   175      version = "12.0"
   176      administrator_login = "mradministrator"
   177      administrator_login_password = "thisIsDog11"
   178  }
   179  
   180  resource "azurerm_sql_database" "test" {
   181      name = "acctestdb%d"
   182      resource_group_name = "${azurerm_resource_group.test.name}"
   183      server_name = "${azurerm_sql_server.test.name}"
   184      location = "West US"
   185      edition = "Standard"
   186      collation = "SQL_Latin1_General_CP1_CI_AS"
   187      max_size_bytes = "1073741824"
   188      requested_service_objective_name = "S0"
   189  }
   190  `
   191  
   192  var testAccAzureRMSqlDatabase_withTags = `
   193  resource "azurerm_resource_group" "test" {
   194      name = "acctestRG_%d"
   195      location = "West US"
   196  }
   197  resource "azurerm_sql_server" "test" {
   198      name = "acctestsqlserver%d"
   199      resource_group_name = "${azurerm_resource_group.test.name}"
   200      location = "West US"
   201      version = "12.0"
   202      administrator_login = "mradministrator"
   203      administrator_login_password = "thisIsDog11"
   204  }
   205  
   206  resource "azurerm_sql_database" "test" {
   207      name = "acctestdb%d"
   208      resource_group_name = "${azurerm_resource_group.test.name}"
   209      server_name = "${azurerm_sql_server.test.name}"
   210      location = "West US"
   211      edition = "Standard"
   212      collation = "SQL_Latin1_General_CP1_CI_AS"
   213      max_size_bytes = "1073741824"
   214      requested_service_objective_name = "S0"
   215  
   216      tags {
   217      	environment = "staging"
   218      	database = "test"
   219      }
   220  }
   221  `
   222  
   223  var testAccAzureRMSqlDatabase_withTagsUpdate = `
   224  resource "azurerm_resource_group" "test" {
   225      name = "acctestRG_%d"
   226      location = "West US"
   227  }
   228  resource "azurerm_sql_server" "test" {
   229      name = "acctestsqlserver%d"
   230      resource_group_name = "${azurerm_resource_group.test.name}"
   231      location = "West US"
   232      version = "12.0"
   233      administrator_login = "mradministrator"
   234      administrator_login_password = "thisIsDog11"
   235  }
   236  
   237  resource "azurerm_sql_database" "test" {
   238      name = "acctestdb%d"
   239      resource_group_name = "${azurerm_resource_group.test.name}"
   240      server_name = "${azurerm_sql_server.test.name}"
   241      location = "West US"
   242      edition = "Standard"
   243      collation = "SQL_Latin1_General_CP1_CI_AS"
   244      max_size_bytes = "1073741824"
   245      requested_service_objective_name = "S0"
   246  
   247      tags {
   248      	environment = "production"
   249      }
   250  }
   251  `
   252  
   253  var testAccAzureRMSqlDatabase_datawarehouse = `
   254  resource "azurerm_resource_group" "test" {
   255      name = "acctest_rg_%d"
   256      location = "West US"
   257  }
   258  resource "azurerm_sql_server" "test" {
   259      name = "acctestsqlserver%d"
   260      resource_group_name = "${azurerm_resource_group.test.name}"
   261      location = "West US"
   262      version = "12.0"
   263      administrator_login = "mradministrator"
   264      administrator_login_password = "thisIsDog11"
   265  }
   266  
   267  resource "azurerm_sql_database" "test" {
   268      name = "acctestdb%d"
   269      resource_group_name = "${azurerm_resource_group.test.name}"
   270      server_name = "${azurerm_sql_server.test.name}"
   271      location = "West US"
   272      edition = "DataWarehouse"
   273      collation = "SQL_Latin1_General_CP1_CI_AS"
   274      requested_service_objective_name = "DW400"
   275  }
   276  `