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