github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/azure/resource_azure_sql_database_service_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccAzureSqlDatabaseServiceBasic(t *testing.T) {
    12  	name := "azure_sql_database_service.foo"
    13  
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckAzureSqlDatabaseServiceDeleted,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccAzureSqlDatabaseServiceConfigBasic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccAzureSqlDatabaseServerGetName,
    23  					testAccCheckAzureSqlDatabaseServiceExists(name),
    24  					resource.TestCheckResourceAttr(name, "name", "terraform-testing-db"),
    25  					resource.TestCheckResourceAttrPtr(name, "database_server_name",
    26  						testAccAzureSqlServerName),
    27  					resource.TestCheckResourceAttr(name, "collation",
    28  						"SQL_Latin1_General_CP1_CI_AS"),
    29  					resource.TestCheckResourceAttr(name, "edition", "Standard"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccAzureSqlDatabaseServiceAdvanced(t *testing.T) {
    37  	name := "azure_sql_database_service.foo"
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckAzureSqlDatabaseServiceDeleted,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccAzureSqlDatabaseServiceConfigAdvanced,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccAzureSqlDatabaseServerGetName,
    48  					testAccCheckAzureSqlDatabaseServiceExists(name),
    49  					resource.TestCheckResourceAttr(name, "name", "terraform-testing-db"),
    50  					resource.TestCheckResourceAttrPtr(name, "database_server_name",
    51  						testAccAzureSqlServerName),
    52  					resource.TestCheckResourceAttr(name, "edition", "Premium"),
    53  					resource.TestCheckResourceAttr(name, "collation",
    54  						"Arabic_BIN"),
    55  					resource.TestCheckResourceAttr(name, "max_size_bytes", "10737418240"),
    56  					resource.TestCheckResourceAttr(name, "service_level_id",
    57  						"7203483a-c4fb-4304-9e9f-17c71c904f5d"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccAzureSqlDatabaseServiceUpdate(t *testing.T) {
    65  	name := "azure_sql_database_service.foo"
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckAzureSqlDatabaseServiceDeleted,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccAzureSqlDatabaseServiceConfigAdvanced,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccAzureSqlDatabaseServerGetName,
    76  					testAccCheckAzureSqlDatabaseServiceExists(name),
    77  					resource.TestCheckResourceAttr(name, "name", "terraform-testing-db"),
    78  					resource.TestCheckResourceAttrPtr(name, "database_server_name",
    79  						testAccAzureSqlServerName),
    80  					resource.TestCheckResourceAttr(name, "edition", "Premium"),
    81  					resource.TestCheckResourceAttr(name, "collation",
    82  						"Arabic_BIN"),
    83  					resource.TestCheckResourceAttr(name, "max_size_bytes", "10737418240"),
    84  					resource.TestCheckResourceAttr(name, "service_level_id",
    85  						"7203483a-c4fb-4304-9e9f-17c71c904f5d"),
    86  				),
    87  			},
    88  			resource.TestStep{
    89  				Config: testAccAzureSqlDatabaseServiceConfigUpdate,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccAzureSqlDatabaseServerGetName,
    92  					testAccCheckAzureSqlDatabaseServiceExists(name),
    93  					resource.TestCheckResourceAttr(name, "name",
    94  						"terraform-testing-db-renamed"),
    95  					resource.TestCheckResourceAttrPtr(name, "database_server_name",
    96  						testAccAzureSqlServerName),
    97  					resource.TestCheckResourceAttr(name, "edition", "Standard"),
    98  					resource.TestCheckResourceAttr(name, "collation",
    99  						"SQL_Latin1_General_CP1_CI_AS"),
   100  					resource.TestCheckResourceAttr(name, "max_size_bytes", "5368709120"),
   101  					resource.TestCheckResourceAttr(name, "service_level_id",
   102  						"f1173c43-91bd-4aaa-973c-54e79e15235b"),
   103  				),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func testAccCheckAzureSqlDatabaseServiceExists(name string) resource.TestCheckFunc {
   110  	return func(s *terraform.State) error {
   111  		resource, ok := s.RootModule().Resources[name]
   112  		if !ok {
   113  			return fmt.Errorf("SQL Service %s doesn't exist.", name)
   114  		}
   115  
   116  		if resource.Primary.ID == "" {
   117  			return fmt.Errorf("SQL Service %s resource ID not set.", name)
   118  		}
   119  
   120  		sqlClient := testAccProvider.Meta().(*Client).sqlClient
   121  		dbs, err := sqlClient.ListDatabases(*testAccAzureSqlServerName)
   122  		if err != nil {
   123  			return fmt.Errorf("Error issuing Azure SQL Service list request: %s", err)
   124  		}
   125  
   126  		for _, srv := range dbs.ServiceResources {
   127  			if srv.Name == resource.Primary.ID {
   128  				return nil
   129  			}
   130  		}
   131  
   132  		return fmt.Errorf("SQL Service %s doesn't exist.", name)
   133  	}
   134  }
   135  
   136  func testAccCheckAzureSqlDatabaseServiceDeleted(s *terraform.State) error {
   137  	for _, resource := range s.RootModule().Resources {
   138  		if resource.Type != "azure_sql_database_service" {
   139  			continue
   140  		}
   141  
   142  		if resource.Primary.ID == "" {
   143  			return fmt.Errorf("SQL Service resource ID not set.")
   144  		}
   145  
   146  		sqlClient := testAccProvider.Meta().(*Client).sqlClient
   147  		dbs, err := sqlClient.ListDatabases(*testAccAzureSqlServerName)
   148  		if err != nil {
   149  			return fmt.Errorf("Error issuing Azure SQL Service list request: %s", err)
   150  		}
   151  
   152  		for _, srv := range dbs.ServiceResources {
   153  			if srv.Name == resource.Primary.ID {
   154  				fmt.Errorf("SQL Service %s still exists.", resource.Primary.ID)
   155  			}
   156  		}
   157  	}
   158  
   159  	return nil
   160  }
   161  
   162  const testAccAzureSqlDatabaseServiceConfigBasic = testAccAzureSqlDatabaseServerConfig + `
   163  resource "azure_sql_database_service" "foo" {
   164      name = "terraform-testing-db"
   165      database_server_name = "${azure_sql_database_server.foo.name}"
   166      edition = "Standard"
   167  }
   168  `
   169  
   170  const testAccAzureSqlDatabaseServiceConfigAdvanced = testAccAzureSqlDatabaseServerConfig + `
   171  resource "azure_sql_database_service" "foo" {
   172      name = "terraform-testing-db"
   173      database_server_name = "${azure_sql_database_server.foo.name}"
   174      edition = "Premium"
   175      collation = "Arabic_BIN"
   176      max_size_bytes = "10737418240"
   177      service_level_id = "7203483a-c4fb-4304-9e9f-17c71c904f5d"
   178  }
   179  `
   180  
   181  const testAccAzureSqlDatabaseServiceConfigUpdate = testAccAzureSqlDatabaseServerConfig + `
   182  resource "azure_sql_database_service" "foo" {
   183      name = "terraform-testing-db-renamed"
   184      database_server_name = "${azure_sql_database_server.foo.name}"
   185      edition = "Standard"
   186      collation = "SQL_Latin1_General_CP1_CI_AS"
   187      max_size_bytes = "5368709120"
   188      service_level_id = "f1173c43-91bd-4aaa-973c-54e79e15235b"
   189  }
   190  `