github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/azurerm/resource_arm_public_ip_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"net/http"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestResourceAzureRMPublicIpAllocation_validation(t *testing.T) {
    15  	cases := []struct {
    16  		Value    string
    17  		ErrCount int
    18  	}{
    19  		{
    20  			Value:    "Random",
    21  			ErrCount: 1,
    22  		},
    23  		{
    24  			Value:    "Static",
    25  			ErrCount: 0,
    26  		},
    27  		{
    28  			Value:    "Dynamic",
    29  			ErrCount: 0,
    30  		},
    31  		{
    32  			Value:    "STATIC",
    33  			ErrCount: 0,
    34  		},
    35  		{
    36  			Value:    "static",
    37  			ErrCount: 0,
    38  		},
    39  	}
    40  
    41  	for _, tc := range cases {
    42  		_, errors := validatePublicIpAllocation(tc.Value, "azurerm_public_ip")
    43  
    44  		if len(errors) != tc.ErrCount {
    45  			t.Fatalf("Expected the Azure RM Public IP allocation to trigger a validation error")
    46  		}
    47  	}
    48  }
    49  
    50  func TestResourceAzureRMPublicIpDomainNameLabel_validation(t *testing.T) {
    51  	cases := []struct {
    52  		Value    string
    53  		ErrCount int
    54  	}{
    55  		{
    56  			Value:    "tEsting123",
    57  			ErrCount: 1,
    58  		},
    59  		{
    60  			Value:    "testing123!",
    61  			ErrCount: 1,
    62  		},
    63  		{
    64  			Value:    "testing123-",
    65  			ErrCount: 1,
    66  		},
    67  		{
    68  			Value:    randomString(80),
    69  			ErrCount: 1,
    70  		},
    71  	}
    72  
    73  	for _, tc := range cases {
    74  		_, errors := validatePublicIpDomainNameLabel(tc.Value, "azurerm_public_ip")
    75  
    76  		if len(errors) != tc.ErrCount {
    77  			t.Fatalf("Expected the Azure RM Public IP Domain Name Label to trigger a validation error")
    78  		}
    79  	}
    80  }
    81  
    82  func TestAccAzureRMPublicIpStatic_basic(t *testing.T) {
    83  
    84  	resource.Test(t, resource.TestCase{
    85  		PreCheck:     func() { testAccPreCheck(t) },
    86  		Providers:    testAccProviders,
    87  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
    88  		Steps: []resource.TestStep{
    89  			resource.TestStep{
    90  				Config: testAccAzureRMVPublicIpStatic_basic,
    91  				Check: resource.ComposeTestCheckFunc(
    92  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
    93  				),
    94  			},
    95  		},
    96  	})
    97  }
    98  
    99  func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) {
   100  
   101  	resource.Test(t, resource.TestCase{
   102  		PreCheck:     func() { testAccPreCheck(t) },
   103  		Providers:    testAccProviders,
   104  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   105  		Steps: []resource.TestStep{
   106  			resource.TestStep{
   107  				Config: testAccAzureRMVPublicIpStatic_withTags,
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   110  					resource.TestCheckResourceAttr(
   111  						"azurerm_public_ip.test", "tags.#", "2"),
   112  					resource.TestCheckResourceAttr(
   113  						"azurerm_public_ip.test", "tags.environment", "Production"),
   114  					resource.TestCheckResourceAttr(
   115  						"azurerm_public_ip.test", "tags.cost_center", "MSFT"),
   116  				),
   117  			},
   118  
   119  			resource.TestStep{
   120  				Config: testAccAzureRMVPublicIpStatic_withTagsUpdate,
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   123  					resource.TestCheckResourceAttr(
   124  						"azurerm_public_ip.test", "tags.#", "1"),
   125  					resource.TestCheckResourceAttr(
   126  						"azurerm_public_ip.test", "tags.environment", "staging"),
   127  				),
   128  			},
   129  		},
   130  	})
   131  }
   132  
   133  func TestAccAzureRMPublicIpStatic_update(t *testing.T) {
   134  
   135  	resource.Test(t, resource.TestCase{
   136  		PreCheck:     func() { testAccPreCheck(t) },
   137  		Providers:    testAccProviders,
   138  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   139  		Steps: []resource.TestStep{
   140  			resource.TestStep{
   141  				Config: testAccAzureRMVPublicIpStatic_basic,
   142  				Check: resource.ComposeTestCheckFunc(
   143  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   144  				),
   145  			},
   146  
   147  			resource.TestStep{
   148  				Config: testAccAzureRMVPublicIpStatic_update,
   149  				Check: resource.ComposeTestCheckFunc(
   150  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   151  					resource.TestCheckResourceAttr(
   152  						"azurerm_public_ip.test", "domain_name_label", "mylabel01"),
   153  				),
   154  			},
   155  		},
   156  	})
   157  }
   158  
   159  func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) {
   160  
   161  	resource.Test(t, resource.TestCase{
   162  		PreCheck:     func() { testAccPreCheck(t) },
   163  		Providers:    testAccProviders,
   164  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   165  		Steps: []resource.TestStep{
   166  			resource.TestStep{
   167  				Config: testAccAzureRMVPublicIpDynamic_basic,
   168  				Check: resource.ComposeTestCheckFunc(
   169  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   170  				),
   171  			},
   172  		},
   173  	})
   174  }
   175  
   176  func testCheckAzureRMPublicIpExists(name string) resource.TestCheckFunc {
   177  	return func(s *terraform.State) error {
   178  		// Ensure we have enough information in state to look up in API
   179  		rs, ok := s.RootModule().Resources[name]
   180  		if !ok {
   181  			return fmt.Errorf("Not found: %s", name)
   182  		}
   183  
   184  		availSetName := rs.Primary.Attributes["name"]
   185  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   186  		if !hasResourceGroup {
   187  			return fmt.Errorf("Bad: no resource group found in state for public ip: %s", availSetName)
   188  		}
   189  
   190  		conn := testAccProvider.Meta().(*ArmClient).publicIPClient
   191  
   192  		resp, err := conn.Get(resourceGroup, availSetName, "")
   193  		if err != nil {
   194  			return fmt.Errorf("Bad: Get on publicIPClient: %s", err)
   195  		}
   196  
   197  		if resp.StatusCode == http.StatusNotFound {
   198  			return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", name, resourceGroup)
   199  		}
   200  
   201  		return nil
   202  	}
   203  }
   204  
   205  func testCheckAzureRMPublicIpDestroy(s *terraform.State) error {
   206  	conn := testAccProvider.Meta().(*ArmClient).publicIPClient
   207  
   208  	for _, rs := range s.RootModule().Resources {
   209  		if rs.Type != "azurerm_public_ip" {
   210  			continue
   211  		}
   212  
   213  		name := rs.Primary.Attributes["name"]
   214  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   215  
   216  		resp, err := conn.Get(resourceGroup, name, "")
   217  
   218  		if err != nil {
   219  			return nil
   220  		}
   221  
   222  		if resp.StatusCode != http.StatusNotFound {
   223  			return fmt.Errorf("Public IP still exists:\n%#v", resp.Properties)
   224  		}
   225  	}
   226  
   227  	return nil
   228  }
   229  
   230  func randomString(strlen int) string {
   231  	rand.Seed(time.Now().UTC().UnixNano())
   232  	const chars = "abcdefghijklmnopqrstuvwxyz0123456789-"
   233  	result := make([]byte, strlen)
   234  	for i := 0; i < strlen; i++ {
   235  		result[i] = chars[rand.Intn(len(chars))]
   236  	}
   237  	return string(result)
   238  }
   239  
   240  var testAccAzureRMVPublicIpStatic_basic = `
   241  resource "azurerm_resource_group" "test" {
   242      name = "acceptanceTestResourceGroup1"
   243      location = "West US"
   244  }
   245  resource "azurerm_public_ip" "test" {
   246      name = "acceptanceTestPublicIp1"
   247      location = "West US"
   248      resource_group_name = "${azurerm_resource_group.test.name}"
   249      public_ip_address_allocation = "static"
   250  }
   251  `
   252  
   253  var testAccAzureRMVPublicIpStatic_update = `
   254  resource "azurerm_resource_group" "test" {
   255      name = "acceptanceTestResourceGroup1"
   256      location = "West US"
   257  }
   258  resource "azurerm_public_ip" "test" {
   259      name = "acceptanceTestPublicIp1"
   260      location = "West US"
   261      resource_group_name = "${azurerm_resource_group.test.name}"
   262      public_ip_address_allocation = "static"
   263      domain_name_label = "mylabel01"
   264  }
   265  `
   266  
   267  var testAccAzureRMVPublicIpDynamic_basic = `
   268  resource "azurerm_resource_group" "test" {
   269      name = "acceptanceTestResourceGroup2"
   270      location = "West US"
   271  }
   272  resource "azurerm_public_ip" "test" {
   273      name = "acceptanceTestPublicIp2"
   274      location = "West US"
   275      resource_group_name = "${azurerm_resource_group.test.name}"
   276      public_ip_address_allocation = "dynamic"
   277  }
   278  `
   279  
   280  var testAccAzureRMVPublicIpStatic_withTags = `
   281  resource "azurerm_resource_group" "test" {
   282      name = "acceptanceTestResourceGroup1"
   283      location = "West US"
   284  }
   285  resource "azurerm_public_ip" "test" {
   286      name = "acceptanceTestPublicIp1"
   287      location = "West US"
   288      resource_group_name = "${azurerm_resource_group.test.name}"
   289      public_ip_address_allocation = "static"
   290  
   291      tags {
   292  	environment = "Production"
   293  	cost_center = "MSFT"
   294      }
   295  }
   296  `
   297  
   298  var testAccAzureRMVPublicIpStatic_withTagsUpdate = `
   299  resource "azurerm_resource_group" "test" {
   300      name = "acceptanceTestResourceGroup1"
   301      location = "West US"
   302  }
   303  resource "azurerm_public_ip" "test" {
   304      name = "acceptanceTestPublicIp1"
   305      location = "West US"
   306      resource_group_name = "${azurerm_resource_group.test.name}"
   307      public_ip_address_allocation = "static"
   308  
   309      tags {
   310  	environment = "staging"
   311      }
   312  }
   313  `