github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/azure/resource_azure_hosted_service_test.go (about)

     1  package azure
     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  )
    11  
    12  func TestAccAzureHostedServiceBasic(t *testing.T) {
    13  	name := "azure_hosted_service.foo"
    14  
    15  	hostedServiceName := fmt.Sprintf("terraform-testing-service%d", acctest.RandInt())
    16  	config := fmt.Sprintf(testAccAzureHostedServiceBasic, hostedServiceName)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAzureHostedServiceDestroyed,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAzureHostedServiceExists(name),
    27  					resource.TestCheckResourceAttr(name, "name", hostedServiceName),
    28  					resource.TestCheckResourceAttr(name, "location", "North Europe"),
    29  					resource.TestCheckResourceAttr(name, "ephemeral_contents", "false"),
    30  					resource.TestCheckResourceAttr(name, "description", "very discriptive"),
    31  					resource.TestCheckResourceAttr(name, "label", "very identifiable"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccAzureHostedServiceUpdate(t *testing.T) {
    39  	name := "azure_hosted_service.foo"
    40  
    41  	hostedServiceName := fmt.Sprintf("terraform-testing-service%d", acctest.RandInt())
    42  
    43  	basicConfig := fmt.Sprintf(testAccAzureHostedServiceBasic, hostedServiceName)
    44  	updateConfig := fmt.Sprintf(testAccAzureHostedServiceUpdate, hostedServiceName)
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck:     func() { testAccPreCheck(t) },
    48  		Providers:    testAccProviders,
    49  		CheckDestroy: testAccCheckAzureHostedServiceDestroyed,
    50  		Steps: []resource.TestStep{
    51  			resource.TestStep{
    52  				Config: basicConfig,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckAzureHostedServiceExists(name),
    55  					resource.TestCheckResourceAttr(name, "name", hostedServiceName),
    56  					resource.TestCheckResourceAttr(name, "location", "North Europe"),
    57  					resource.TestCheckResourceAttr(name, "ephemeral_contents", "false"),
    58  					resource.TestCheckResourceAttr(name, "description", "very discriptive"),
    59  					resource.TestCheckResourceAttr(name, "label", "very identifiable"),
    60  				),
    61  			},
    62  
    63  			resource.TestStep{
    64  				Config: updateConfig,
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckAzureHostedServiceExists(name),
    67  					resource.TestCheckResourceAttr(name, "name", hostedServiceName),
    68  					resource.TestCheckResourceAttr(name, "location", "North Europe"),
    69  					resource.TestCheckResourceAttr(name, "ephemeral_contents", "true"),
    70  					resource.TestCheckResourceAttr(name, "description", "very discriptive"),
    71  					resource.TestCheckResourceAttr(name, "label", "very identifiable"),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  func testAccCheckAzureHostedServiceExists(name string) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		resource, ok := s.RootModule().Resources[name]
    81  		if !ok {
    82  			return fmt.Errorf("Hosted Service resource not found.")
    83  		}
    84  
    85  		if resource.Primary.ID == "" {
    86  			return fmt.Errorf("Resource's ID is not set.")
    87  		}
    88  
    89  		hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient
    90  		_, err := hostedServiceClient.GetHostedService(resource.Primary.ID)
    91  		return err
    92  	}
    93  }
    94  
    95  func testAccCheckAzureHostedServiceDestroyed(s *terraform.State) error {
    96  	hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient
    97  
    98  	for _, resource := range s.RootModule().Resources {
    99  		if resource.Type != "azure_hosted_service" {
   100  			continue
   101  		}
   102  
   103  		if resource.Primary.ID == "" {
   104  			return fmt.Errorf("No Azure Hosted Service Resource found.")
   105  		}
   106  
   107  		_, err := hostedServiceClient.GetHostedService(resource.Primary.ID)
   108  
   109  		return testAccResourceDestroyedErrorFilter("Hosted Service", err)
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  const testAccAzureHostedServiceBasic = `
   116  resource "azure_hosted_service" "foo" {
   117  	name = "%s"
   118  	location = "North Europe"
   119      ephemeral_contents = false
   120  	description = "very discriptive"
   121      label = "very identifiable"
   122  }
   123  `
   124  const testAccAzureHostedServiceUpdate = `
   125  resource "azure_hosted_service" "foo" {
   126  	name = "%s"
   127  	location = "North Europe"
   128      ephemeral_contents = true
   129  	description = "very discriptive"
   130      label = "very identifiable"
   131  }
   132  `