github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_cdn_profile_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestResourceAzureRMCdnProfileSKU_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:    "Standard_Verizon",
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    "Premium_Verizon",
    28  			ErrCount: 0,
    29  		},
    30  		{
    31  			Value:    "Standard_Akamai",
    32  			ErrCount: 0,
    33  		},
    34  		{
    35  			Value:    "STANDARD_AKAMAI",
    36  			ErrCount: 0,
    37  		},
    38  		{
    39  			Value:    "standard_akamai",
    40  			ErrCount: 0,
    41  		},
    42  	}
    43  
    44  	for _, tc := range cases {
    45  		_, errors := validateCdnProfileSku(tc.Value, "azurerm_cdn_profile")
    46  
    47  		if len(errors) != tc.ErrCount {
    48  			t.Fatalf("Expected the Azure RM CDN Profile SKU to trigger a validation error")
    49  		}
    50  	}
    51  }
    52  
    53  func TestAccAzureRMCdnProfile_basic(t *testing.T) {
    54  
    55  	ri := acctest.RandInt()
    56  	config := fmt.Sprintf(testAccAzureRMCdnProfile_basic, ri, ri)
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testCheckAzureRMCdnProfileDestroy,
    62  		Steps: []resource.TestStep{
    63  			{
    64  				Config: config,
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
    67  				),
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func TestAccAzureRMCdnProfile_withTags(t *testing.T) {
    74  
    75  	ri := acctest.RandInt()
    76  	preConfig := fmt.Sprintf(testAccAzureRMCdnProfile_withTags, ri, ri)
    77  	postConfig := fmt.Sprintf(testAccAzureRMCdnProfile_withTagsUpdate, ri, ri)
    78  
    79  	resource.Test(t, resource.TestCase{
    80  		PreCheck:     func() { testAccPreCheck(t) },
    81  		Providers:    testAccProviders,
    82  		CheckDestroy: testCheckAzureRMCdnProfileDestroy,
    83  		Steps: []resource.TestStep{
    84  			{
    85  				Config: preConfig,
    86  				Check: resource.ComposeTestCheckFunc(
    87  					testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
    88  					resource.TestCheckResourceAttr(
    89  						"azurerm_cdn_profile.test", "tags.%", "2"),
    90  					resource.TestCheckResourceAttr(
    91  						"azurerm_cdn_profile.test", "tags.environment", "Production"),
    92  					resource.TestCheckResourceAttr(
    93  						"azurerm_cdn_profile.test", "tags.cost_center", "MSFT"),
    94  				),
    95  			},
    96  
    97  			{
    98  				Config: postConfig,
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
   101  					resource.TestCheckResourceAttr(
   102  						"azurerm_cdn_profile.test", "tags.%", "1"),
   103  					resource.TestCheckResourceAttr(
   104  						"azurerm_cdn_profile.test", "tags.environment", "staging"),
   105  				),
   106  			},
   107  		},
   108  	})
   109  }
   110  
   111  func testCheckAzureRMCdnProfileExists(name string) resource.TestCheckFunc {
   112  	return func(s *terraform.State) error {
   113  		// Ensure we have enough information in state to look up in API
   114  		rs, ok := s.RootModule().Resources[name]
   115  		if !ok {
   116  			return fmt.Errorf("Not found: %s", name)
   117  		}
   118  
   119  		name := rs.Primary.Attributes["name"]
   120  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   121  		if !hasResourceGroup {
   122  			return fmt.Errorf("Bad: no resource group found in state for cdn profile: %s", name)
   123  		}
   124  
   125  		conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient
   126  
   127  		resp, err := conn.Get(resourceGroup, name)
   128  		if err != nil {
   129  			return fmt.Errorf("Bad: Get on cdnProfilesClient: %s", err)
   130  		}
   131  
   132  		if resp.StatusCode == http.StatusNotFound {
   133  			return fmt.Errorf("Bad: CDN Profile %q (resource group: %q) does not exist", name, resourceGroup)
   134  		}
   135  
   136  		return nil
   137  	}
   138  }
   139  
   140  func testCheckAzureRMCdnProfileDestroy(s *terraform.State) error {
   141  	conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient
   142  
   143  	for _, rs := range s.RootModule().Resources {
   144  		if rs.Type != "azurerm_cdn_profile" {
   145  			continue
   146  		}
   147  
   148  		name := rs.Primary.Attributes["name"]
   149  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   150  
   151  		resp, err := conn.Get(resourceGroup, name)
   152  
   153  		if err != nil {
   154  			return nil
   155  		}
   156  
   157  		if resp.StatusCode != http.StatusNotFound {
   158  			return fmt.Errorf("CDN Profile still exists:\n%#v", resp.ProfileProperties)
   159  		}
   160  	}
   161  
   162  	return nil
   163  }
   164  
   165  var testAccAzureRMCdnProfile_basic = `
   166  resource "azurerm_resource_group" "test" {
   167      name = "acctestRG-%d"
   168      location = "West US"
   169  }
   170  resource "azurerm_cdn_profile" "test" {
   171      name = "acctestcdnprof%d"
   172      location = "West US"
   173      resource_group_name = "${azurerm_resource_group.test.name}"
   174      sku = "Standard_Verizon"
   175  }
   176  `
   177  
   178  var testAccAzureRMCdnProfile_withTags = `
   179  resource "azurerm_resource_group" "test" {
   180      name = "acctestRG-%d"
   181      location = "West US"
   182  }
   183  resource "azurerm_cdn_profile" "test" {
   184      name = "acctestcdnprof%d"
   185      location = "West US"
   186      resource_group_name = "${azurerm_resource_group.test.name}"
   187      sku = "Standard_Verizon"
   188  
   189      tags {
   190  	environment = "Production"
   191  	cost_center = "MSFT"
   192      }
   193  }
   194  `
   195  
   196  var testAccAzureRMCdnProfile_withTagsUpdate = `
   197  resource "azurerm_resource_group" "test" {
   198      name = "acctestRG-%d"
   199      location = "West US"
   200  }
   201  resource "azurerm_cdn_profile" "test" {
   202      name = "acctestcdnprof%d"
   203      location = "West US"
   204      resource_group_name = "${azurerm_resource_group.test.name}"
   205      sku = "Standard_Verizon"
   206  
   207      tags {
   208  	environment = "staging"
   209      }
   210  }
   211  `