github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azure/resource_azure_sql_database_server_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  // testAccAzureSqlServerName is a helper variable in which to store
    12  // the randomly-generated name of the SQL Server after it is created.
    13  // The anonymous function is there because go is too good to &"" directly.
    14  var testAccAzureSqlServerName *string = func(s string) *string { return &s }("")
    15  var testAccAzureSqlServerNames []string = []string{}
    16  
    17  func TestAccAzureSqlDatabaseServer(t *testing.T) {
    18  	name := "azure_sql_database_server.foo"
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAzureSqlDatabaseServerDeleted,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAzureSqlDatabaseServerConfig,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccAzureSqlDatabaseServerGetName,
    28  					testAccCheckAzureSqlDatabaseServerExists(name),
    29  					resource.TestCheckResourceAttrPtr(name, "name", testAccAzureSqlServerName),
    30  					resource.TestCheckResourceAttr(name, "username", "SuperUser"),
    31  					resource.TestCheckResourceAttr(name, "password", "SuperSEKR3T"),
    32  					resource.TestCheckResourceAttr(name, "version", "2.0"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckAzureSqlDatabaseServerExists(name string) resource.TestCheckFunc {
    40  	return func(s *terraform.State) error {
    41  		resource, ok := s.RootModule().Resources[name]
    42  		if !ok {
    43  			return fmt.Errorf("SQL Server %s doesn't exist.", name)
    44  		}
    45  
    46  		if resource.Primary.ID == "" {
    47  			return fmt.Errorf("SQL Server %s resource ID not set.", name)
    48  		}
    49  
    50  		sqlClient := testAccProvider.Meta().(*Client).sqlClient
    51  		servers, err := sqlClient.ListServers()
    52  		if err != nil {
    53  			return fmt.Errorf("Error issuing Azure SQL Server list request: %s", err)
    54  		}
    55  
    56  		for _, srv := range servers.DatabaseServers {
    57  			if srv.Name == resource.Primary.ID {
    58  				return nil
    59  			}
    60  		}
    61  
    62  		return fmt.Errorf("SQL Server %s doesn't exist.", name)
    63  	}
    64  }
    65  
    66  func testAccCheckAzureSqlDatabaseServerDeleted(s *terraform.State) error {
    67  	for _, resource := range s.RootModule().Resources {
    68  		if resource.Type != "azure_sql_database_server" {
    69  			continue
    70  		}
    71  
    72  		if resource.Primary.ID == "" {
    73  			return fmt.Errorf("SQL Server resource ID not set.")
    74  		}
    75  
    76  		sqlClient := testAccProvider.Meta().(*Client).sqlClient
    77  		servers, err := sqlClient.ListServers()
    78  		if err != nil {
    79  			return fmt.Errorf("Error issuing Azure SQL Server list request: %s", err)
    80  		}
    81  
    82  		for _, srv := range servers.DatabaseServers {
    83  			if srv.Name == resource.Primary.ID {
    84  				return fmt.Errorf("SQL Server %s still exists.", resource.Primary.ID)
    85  			}
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  // testAccAzureSqlDatabaseServerGetName is ahelper function which reads the current
    92  // state form Terraform and sets the testAccAzureSqlServerName variable
    93  // to the ID (which is actually the name) of the newly created server.
    94  // It is modeled as a resource.TestCheckFunc so as to be easily-embeddable in
    95  // test cases and run live.
    96  func testAccAzureSqlDatabaseServerGetName(s *terraform.State) error {
    97  	for _, resource := range s.RootModule().Resources {
    98  		if resource.Type != "azure_sql_database_server" {
    99  			continue
   100  		}
   101  
   102  		if resource.Primary.ID == "" {
   103  			return fmt.Errorf("Azure SQL Server resource ID not set.")
   104  		}
   105  
   106  		*testAccAzureSqlServerName = resource.Primary.ID
   107  		return nil
   108  	}
   109  
   110  	return fmt.Errorf("No Azure SQL Servers found.")
   111  }
   112  
   113  // testAccAzureSqlDatabaseServerGetNames is the same as the above; only it gets
   114  // all the servers' names.
   115  func testAccAzureSqlDatabaseServerGetNames(s *terraform.State) error {
   116  	testAccAzureSqlServerNames = []string{}
   117  
   118  	for _, resource := range s.RootModule().Resources {
   119  		if resource.Type != "azure_sql_database_server" {
   120  			continue
   121  		}
   122  
   123  		if resource.Primary.ID == "" {
   124  			return fmt.Errorf("Azure SQL Server resource ID not set.")
   125  		}
   126  
   127  		testAccAzureSqlServerNames = append(testAccAzureSqlServerNames, resource.Primary.ID)
   128  	}
   129  
   130  	if len(testAccAzureSqlServerNames) == 0 {
   131  		return fmt.Errorf("No Azure SQL Servers found.")
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  // testAccAzureSqlDatabaseServersNumber checks if the numbers of servers is
   138  // exactly equal to the given number. It is modeled as a resource.TestCheckFunc
   139  // to be easily embeddable in test checks.
   140  func testAccAzureSqlDatabaseServersNumber(n int) resource.TestCheckFunc {
   141  	return func(_ *terraform.State) error {
   142  		if len(testAccAzureSqlServerNames) != n {
   143  			return fmt.Errorf("Erroneous number of Azure Sql Database Servers. Expected %d; have %d.", n,
   144  				len(testAccAzureSqlServerNames))
   145  		}
   146  
   147  		return nil
   148  	}
   149  }
   150  
   151  const testAccAzureSqlDatabaseServerConfig = `
   152  resource "azure_sql_database_server" "foo" {
   153      location = "West US"
   154      username = "SuperUser"
   155      password = "SuperSEKR3T"
   156      version = "2.0"
   157  }
   158  `