github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_eventhub_namespace_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "regexp" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAzureRMEventHubNamespaceCapacity_validation(t *testing.T) { 15 cases := []struct { 16 Value int 17 ErrCount int 18 }{ 19 { 20 Value: 17, 21 ErrCount: 1, 22 }, 23 { 24 Value: 1, 25 ErrCount: 0, 26 }, 27 { 28 Value: 2, 29 ErrCount: 0, 30 }, 31 { 32 Value: 3, 33 ErrCount: 1, 34 }, 35 { 36 Value: 4, 37 ErrCount: 0, 38 }, 39 } 40 41 for _, tc := range cases { 42 _, errors := validateEventHubNamespaceCapacity(tc.Value, "azurerm_eventhub_namespace") 43 44 if len(errors) != tc.ErrCount { 45 t.Fatalf("Expected the Azure RM EventHub Namespace Capacity to trigger a validation error") 46 } 47 } 48 } 49 50 func TestAccAzureRMEventHubNamespaceSku_validation(t *testing.T) { 51 cases := []struct { 52 Value string 53 ErrCount int 54 }{ 55 { 56 Value: "Basic", 57 ErrCount: 0, 58 }, 59 { 60 Value: "Standard", 61 ErrCount: 0, 62 }, 63 { 64 Value: "Premium", 65 ErrCount: 1, 66 }, 67 { 68 Value: "Random", 69 ErrCount: 1, 70 }, 71 } 72 73 for _, tc := range cases { 74 _, errors := validateEventHubNamespaceSku(tc.Value, "azurerm_eventhub_namespace") 75 76 if len(errors) != tc.ErrCount { 77 t.Fatalf("Expected the Azure RM EventHub Namespace Sku to trigger a validation error") 78 } 79 } 80 } 81 82 func TestAccAzureRMEventHubNamespace_basic(t *testing.T) { 83 84 ri := acctest.RandInt() 85 config := fmt.Sprintf(testAccAzureRMEventHubNamespace_basic, ri, ri) 86 87 resource.Test(t, resource.TestCase{ 88 PreCheck: func() { testAccPreCheck(t) }, 89 Providers: testAccProviders, 90 CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy, 91 Steps: []resource.TestStep{ 92 { 93 Config: config, 94 Check: resource.ComposeTestCheckFunc( 95 testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"), 96 ), 97 }, 98 }, 99 }) 100 } 101 102 func TestAccAzureRMEventHubNamespace_standard(t *testing.T) { 103 104 ri := acctest.RandInt() 105 config := fmt.Sprintf(testAccAzureRMEventHubNamespace_standard, ri, ri) 106 107 resource.Test(t, resource.TestCase{ 108 PreCheck: func() { testAccPreCheck(t) }, 109 Providers: testAccProviders, 110 CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy, 111 Steps: []resource.TestStep{ 112 { 113 Config: config, 114 Check: resource.ComposeTestCheckFunc( 115 testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"), 116 ), 117 }, 118 }, 119 }) 120 } 121 122 func TestAccAzureRMEventHubNamespace_readDefaultKeys(t *testing.T) { 123 ri := acctest.RandInt() 124 config := fmt.Sprintf(testAccAzureRMEventHubNamespace_basic, ri, ri) 125 126 resource.Test(t, resource.TestCase{ 127 PreCheck: func() { testAccPreCheck(t) }, 128 Providers: testAccProviders, 129 CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy, 130 Steps: []resource.TestStep{ 131 { 132 Config: config, 133 Check: resource.ComposeTestCheckFunc( 134 testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"), 135 resource.TestMatchResourceAttr( 136 "azurerm_eventhub_namespace.test", "default_primary_connection_string", regexp.MustCompile("Endpoint=.+")), 137 resource.TestMatchResourceAttr( 138 "azurerm_eventhub_namespace.test", "default_secondary_connection_string", regexp.MustCompile("Endpoint=.+")), 139 resource.TestMatchResourceAttr( 140 "azurerm_eventhub_namespace.test", "default_primary_key", regexp.MustCompile(".+")), 141 resource.TestMatchResourceAttr( 142 "azurerm_eventhub_namespace.test", "default_secondary_key", regexp.MustCompile(".+")), 143 ), 144 }, 145 }, 146 }) 147 } 148 149 func testCheckAzureRMEventHubNamespaceDestroy(s *terraform.State) error { 150 conn := testAccProvider.Meta().(*ArmClient).eventHubNamespacesClient 151 152 for _, rs := range s.RootModule().Resources { 153 if rs.Type != "azurerm_eventhub_namespace" { 154 continue 155 } 156 157 name := rs.Primary.Attributes["name"] 158 resourceGroup := rs.Primary.Attributes["resource_group_name"] 159 160 resp, err := conn.Get(resourceGroup, name) 161 162 if err != nil { 163 return nil 164 } 165 166 if resp.StatusCode != http.StatusNotFound { 167 return fmt.Errorf("EventHub Namespace still exists:\n%#v", resp.NamespaceProperties) 168 } 169 } 170 171 return nil 172 } 173 174 func testCheckAzureRMEventHubNamespaceExists(name string) resource.TestCheckFunc { 175 return func(s *terraform.State) error { 176 // Ensure we have enough information in state to look up in API 177 rs, ok := s.RootModule().Resources[name] 178 if !ok { 179 return fmt.Errorf("Not found: %s", name) 180 } 181 182 namespaceName := rs.Primary.Attributes["name"] 183 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 184 if !hasResourceGroup { 185 return fmt.Errorf("Bad: no resource group found in state for Event Hub Namespace: %s", namespaceName) 186 } 187 188 conn := testAccProvider.Meta().(*ArmClient).eventHubNamespacesClient 189 190 resp, err := conn.Get(resourceGroup, namespaceName) 191 if err != nil { 192 return fmt.Errorf("Bad: Get on eventHubNamespacesClient: %s", err) 193 } 194 195 if resp.StatusCode == http.StatusNotFound { 196 return fmt.Errorf("Bad: Event Hub Namespace %q (resource group: %q) does not exist", namespaceName, resourceGroup) 197 } 198 199 return nil 200 } 201 } 202 203 var testAccAzureRMEventHubNamespace_basic = ` 204 resource "azurerm_resource_group" "test" { 205 name = "acctestRG-%d" 206 location = "West US" 207 } 208 resource "azurerm_eventhub_namespace" "test" { 209 name = "acctesteventhubnamespace-%d" 210 location = "${azurerm_resource_group.test.location}" 211 resource_group_name = "${azurerm_resource_group.test.name}" 212 sku = "Basic" 213 } 214 ` 215 216 var testAccAzureRMEventHubNamespace_standard = ` 217 resource "azurerm_resource_group" "test" { 218 name = "acctestRG-%d" 219 location = "West US" 220 } 221 resource "azurerm_eventhub_namespace" "test" { 222 name = "acctesteventhubnamespace-%d" 223 location = "${azurerm_resource_group.test.location}" 224 resource_group_name = "${azurerm_resource_group.test.name}" 225 sku = "Standard" 226 capacity = "2" 227 } 228 `