github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  
    36  	for _, tc := range cases {
    37  		_, errors := validateArmSqlDatabaseEdition(tc.Value, "azurerm_sql_database")
    38  
    39  		if len(errors) != tc.ErrCount {
    40  			t.Fatalf("Expected the Azure RM SQL Database edition to trigger a validation error")
    41  		}
    42  	}
    43  }
    44  
    45  func TestAccAzureRMSqlDatabase_basic(t *testing.T) {
    46  	ri := acctest.RandInt()
    47  	config := fmt.Sprintf(testAccAzureRMSqlDatabase_basic, ri, ri, ri)
    48  
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
    53  		Steps: []resource.TestStep{
    54  			resource.TestStep{
    55  				Config: config,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccAzureRMSqlDatabase_withTags(t *testing.T) {
    65  	ri := acctest.RandInt()
    66  	preConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTags, ri, ri, ri)
    67  	postConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTagsUpdate, ri, ri, ri)
    68  
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
    73  		Steps: []resource.TestStep{
    74  			resource.TestStep{
    75  				Config: preConfig,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
    78  					resource.TestCheckResourceAttr(
    79  						"azurerm_sql_database.test", "tags.%", "2"),
    80  				),
    81  			},
    82  
    83  			resource.TestStep{
    84  				Config: postConfig,
    85  				Check: resource.ComposeTestCheckFunc(
    86  					testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
    87  					resource.TestCheckResourceAttr(
    88  						"azurerm_sql_database.test", "tags.%", "1"),
    89  				),
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func testCheckAzureRMSqlDatabaseExists(name string) resource.TestCheckFunc {
    96  	return func(s *terraform.State) error {
    97  
    98  		rs, ok := s.RootModule().Resources[name]
    99  		if !ok {
   100  			return fmt.Errorf("Not found: %s", name)
   101  		}
   102  
   103  		conn := testAccProvider.Meta().(*ArmClient).rivieraClient
   104  
   105  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
   106  		readRequest.Command = &sql.GetDatabase{}
   107  
   108  		readResponse, err := readRequest.Execute()
   109  		if err != nil {
   110  			return fmt.Errorf("Bad: GetDatabase: %s", err)
   111  		}
   112  		if !readResponse.IsSuccessful() {
   113  			return fmt.Errorf("Bad: GetDatabase: %s", readResponse.Error)
   114  		}
   115  
   116  		return nil
   117  	}
   118  }
   119  
   120  func testCheckAzureRMSqlDatabaseDestroy(s *terraform.State) error {
   121  	conn := testAccProvider.Meta().(*ArmClient).rivieraClient
   122  
   123  	for _, rs := range s.RootModule().Resources {
   124  		if rs.Type != "azurerm_sql_database" {
   125  			continue
   126  		}
   127  
   128  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
   129  		readRequest.Command = &sql.GetDatabase{}
   130  
   131  		readResponse, err := readRequest.Execute()
   132  		if err != nil {
   133  			return fmt.Errorf("Bad: GetDatabase: %s", err)
   134  		}
   135  
   136  		if readResponse.IsSuccessful() {
   137  			return fmt.Errorf("Bad: SQL Database still exists: %s", readResponse.Error)
   138  		}
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  var testAccAzureRMSqlDatabase_basic = `
   145  resource "azurerm_resource_group" "test" {
   146      name = "acctest_rg_%d"
   147      location = "West US"
   148  }
   149  resource "azurerm_sql_server" "test" {
   150      name = "acctestsqlserver%d"
   151      resource_group_name = "${azurerm_resource_group.test.name}"
   152      location = "West US"
   153      version = "12.0"
   154      administrator_login = "mradministrator"
   155      administrator_login_password = "thisIsDog11"
   156  }
   157  
   158  resource "azurerm_sql_database" "test" {
   159      name = "acctestdb%d"
   160      resource_group_name = "${azurerm_resource_group.test.name}"
   161      server_name = "${azurerm_sql_server.test.name}"
   162      location = "West US"
   163      edition = "Standard"
   164      collation = "SQL_Latin1_General_CP1_CI_AS"
   165      max_size_bytes = "1073741824"
   166      requested_service_objective_name = "S0"
   167  }
   168  `
   169  
   170  var testAccAzureRMSqlDatabase_withTags = `
   171  resource "azurerm_resource_group" "test" {
   172      name = "acctest_rg_%d"
   173      location = "West US"
   174  }
   175  resource "azurerm_sql_server" "test" {
   176      name = "acctestsqlserver%d"
   177      resource_group_name = "${azurerm_resource_group.test.name}"
   178      location = "West US"
   179      version = "12.0"
   180      administrator_login = "mradministrator"
   181      administrator_login_password = "thisIsDog11"
   182  }
   183  
   184  resource "azurerm_sql_database" "test" {
   185      name = "acctestdb%d"
   186      resource_group_name = "${azurerm_resource_group.test.name}"
   187      server_name = "${azurerm_sql_server.test.name}"
   188      location = "West US"
   189      edition = "Standard"
   190      collation = "SQL_Latin1_General_CP1_CI_AS"
   191      max_size_bytes = "1073741824"
   192      requested_service_objective_name = "S0"
   193  
   194      tags {
   195      	environment = "staging"
   196      	database = "test"
   197      }
   198  }
   199  `
   200  
   201  var testAccAzureRMSqlDatabase_withTagsUpdate = `
   202  resource "azurerm_resource_group" "test" {
   203      name = "acctest_rg_%d"
   204      location = "West US"
   205  }
   206  resource "azurerm_sql_server" "test" {
   207      name = "acctestsqlserver%d"
   208      resource_group_name = "${azurerm_resource_group.test.name}"
   209      location = "West US"
   210      version = "12.0"
   211      administrator_login = "mradministrator"
   212      administrator_login_password = "thisIsDog11"
   213  }
   214  
   215  resource "azurerm_sql_database" "test" {
   216      name = "acctestdb%d"
   217      resource_group_name = "${azurerm_resource_group.test.name}"
   218      server_name = "${azurerm_sql_server.test.name}"
   219      location = "West US"
   220      edition = "Standard"
   221      collation = "SQL_Latin1_General_CP1_CI_AS"
   222      max_size_bytes = "1073741824"
   223      requested_service_objective_name = "S0"
   224  
   225      tags {
   226      	environment = "production"
   227      }
   228  }
   229  `