github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_container_registry_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 TestAccAzureRMContainerRegistryName_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "four",
    20  			ErrCount: 1,
    21  		},
    22  		{
    23  			Value:    "5five",
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    "hello-world",
    28  			ErrCount: 1,
    29  		},
    30  		{
    31  			Value:    "hello_world",
    32  			ErrCount: 1,
    33  		},
    34  		{
    35  			Value:    "helloWorld",
    36  			ErrCount: 0,
    37  		},
    38  		{
    39  			Value:    "helloworld12",
    40  			ErrCount: 0,
    41  		},
    42  		{
    43  			Value:    "hello@world",
    44  			ErrCount: 1,
    45  		},
    46  		{
    47  			Value:    "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd3324120",
    48  			ErrCount: 0,
    49  		},
    50  		{
    51  			Value:    "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd33241202",
    52  			ErrCount: 0,
    53  		},
    54  		{
    55  			Value:    "qfvbdsbvipqdbwsbddbdcwqfjjfewsqwcdw21ddwqwd3324120",
    56  			ErrCount: 1,
    57  		},
    58  	}
    59  
    60  	for _, tc := range cases {
    61  		_, errors := validateAzureRMContainerRegistryName(tc.Value, "azurerm_container_registry")
    62  
    63  		if len(errors) != tc.ErrCount {
    64  			t.Fatalf("Expected the Azure RM Container Registry Name to trigger a validation error: %v", errors)
    65  		}
    66  	}
    67  }
    68  
    69  func TestAccAzureRMContainerRegistry_basic(t *testing.T) {
    70  	ri := acctest.RandInt()
    71  	rs := acctest.RandString(4)
    72  	config := fmt.Sprintf(testAccAzureRMContainerRegistry_basic, ri, rs, ri)
    73  
    74  	resource.Test(t, resource.TestCase{
    75  		PreCheck:     func() { testAccPreCheck(t) },
    76  		Providers:    testAccProviders,
    77  		CheckDestroy: testCheckAzureRMContainerRegistryDestroy,
    78  		Steps: []resource.TestStep{
    79  			{
    80  				Config: config,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testCheckAzureRMContainerRegistryExists("azurerm_container_registry.test"),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func TestAccAzureRMContainerRegistry_complete(t *testing.T) {
    90  	ri := acctest.RandInt()
    91  	rs := acctest.RandString(4)
    92  	config := fmt.Sprintf(testAccAzureRMContainerRegistry_complete, ri, rs, ri)
    93  
    94  	resource.Test(t, resource.TestCase{
    95  		PreCheck:     func() { testAccPreCheck(t) },
    96  		Providers:    testAccProviders,
    97  		CheckDestroy: testCheckAzureRMContainerRegistryDestroy,
    98  		Steps: []resource.TestStep{
    99  			{
   100  				Config: config,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testCheckAzureRMContainerRegistryExists("azurerm_container_registry.test"),
   103  				),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func testCheckAzureRMContainerRegistryDestroy(s *terraform.State) error {
   110  	conn := testAccProvider.Meta().(*ArmClient).containerRegistryClient
   111  
   112  	for _, rs := range s.RootModule().Resources {
   113  		if rs.Type != "azurerm_container_registry" {
   114  			continue
   115  		}
   116  
   117  		name := rs.Primary.Attributes["name"]
   118  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   119  
   120  		resp, err := conn.GetProperties(resourceGroup, name)
   121  
   122  		if err != nil {
   123  			return nil
   124  		}
   125  
   126  		if resp.StatusCode != http.StatusNotFound {
   127  			return fmt.Errorf("Container Registry still exists:\n%#v", resp)
   128  		}
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func testCheckAzureRMContainerRegistryExists(name string) resource.TestCheckFunc {
   135  	return func(s *terraform.State) error {
   136  		// Ensure we have enough information in state to look up in API
   137  		rs, ok := s.RootModule().Resources[name]
   138  		if !ok {
   139  			return fmt.Errorf("Not found: %s", name)
   140  		}
   141  
   142  		name := rs.Primary.Attributes["name"]
   143  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   144  		if !hasResourceGroup {
   145  			return fmt.Errorf("Bad: no resource group found in state for Container Registry: %s", name)
   146  		}
   147  
   148  		conn := testAccProvider.Meta().(*ArmClient).containerRegistryClient
   149  
   150  		resp, err := conn.GetProperties(resourceGroup, name)
   151  		if err != nil {
   152  			return fmt.Errorf("Bad: Get on containerRegistryClient: %s", err)
   153  		}
   154  
   155  		if resp.StatusCode == http.StatusNotFound {
   156  			return fmt.Errorf("Bad: Container Registry %q (resource group: %q) does not exist", name, resourceGroup)
   157  		}
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  var testAccAzureRMContainerRegistry_basic = `
   164  resource "azurerm_resource_group" "test" {
   165    name     = "testAccRg-%d"
   166    location = "West US"
   167  }
   168  
   169  resource "azurerm_storage_account" "test" {
   170    name                = "testaccsa%s"
   171    resource_group_name = "${azurerm_resource_group.test.name}"
   172    location            = "${azurerm_resource_group.test.location}"
   173    account_type        = "Standard_LRS"
   174  }
   175  
   176  resource "azurerm_container_registry" "test" {
   177    name                = "testacccr%d"
   178    resource_group_name = "${azurerm_resource_group.test.name}"
   179    location            = "${azurerm_resource_group.test.location}"
   180  
   181    storage_account {
   182      name       = "${azurerm_storage_account.test.name}"
   183      access_key = "${azurerm_storage_account.test.primary_access_key}"
   184    }
   185  }
   186  `
   187  
   188  var testAccAzureRMContainerRegistry_complete = `
   189  resource "azurerm_resource_group" "test" {
   190    name     = "testAccRg-%d"
   191    location = "West US"
   192  }
   193  
   194  resource "azurerm_storage_account" "test" {
   195    name                = "testaccsa%s"
   196    resource_group_name = "${azurerm_resource_group.test.name}"
   197    location            = "${azurerm_resource_group.test.location}"
   198    account_type        = "Standard_LRS"
   199  }
   200  
   201  resource "azurerm_container_registry" "test" {
   202    name                = "testacccr%d"
   203    resource_group_name = "${azurerm_resource_group.test.name}"
   204    location            = "${azurerm_resource_group.test.location}"
   205    admin_enabled       = false
   206  
   207    storage_account {
   208      name       = "${azurerm_storage_account.test.name}"
   209      access_key = "${azurerm_storage_account.test.primary_access_key}"
   210    }
   211  
   212    tags {
   213      environment = "production"
   214    }
   215  }
   216  `