Skip to content

Commit e470ffd

Browse files
committed
AzureRM storage container and blob tests and documentation
1 parent 4a57ab4 commit e470ffd

6 files changed

Lines changed: 506 additions & 4 deletions

File tree

builtin/providers/azurerm/resource_arm_storage_blob.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func resourceArmStorageBlob() *schema.Resource {
4343
ValidateFunc: validateArmStorageBlobType,
4444
},
4545
"size": &schema.Schema{
46-
Type: schema.TypeInt,
47-
Optional: true,
48-
ForceNew: true,
49-
Default: 0,
46+
Type: schema.TypeInt,
47+
Optional: true,
48+
ForceNew: true,
49+
Default: 0,
50+
ValidateFunc: validateArmStorageBlobSize,
5051
},
5152
"url": &schema.Schema{
5253
Type: schema.TypeString,
@@ -56,6 +57,16 @@ func resourceArmStorageBlob() *schema.Resource {
5657
}
5758
}
5859

60+
func validateArmStorageBlobSize(v interface{}, k string) (ws []string, errors []error) {
61+
value := v.(int)
62+
63+
if value%512 != 0 {
64+
errors = append(errors, fmt.Errorf("Blob Size %q is invalid, must be a multiple of 512", value))
65+
}
66+
67+
return
68+
}
69+
5970
func validateArmStorageBlobType(v interface{}, k string) (ws []string, errors []error) {
6071
value := strings.ToLower(v.(string))
6172
validTypes := map[string]struct{}{
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"strings"
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 TestResourceAzureRMStorageBlobType_validation(t *testing.T) {
15+
cases := []struct {
16+
Value string
17+
ErrCount int
18+
}{
19+
{
20+
Value: "unknown",
21+
ErrCount: 1,
22+
},
23+
{
24+
Value: "page",
25+
ErrCount: 0,
26+
},
27+
{
28+
Value: "blob",
29+
ErrCount: 0,
30+
},
31+
{
32+
Value: "BLOB",
33+
ErrCount: 0,
34+
},
35+
{
36+
Value: "Blob",
37+
ErrCount: 0,
38+
},
39+
}
40+
41+
for _, tc := range cases {
42+
_, errors := validateArmStorageBlobType(tc.Value, "azurerm_storage_blob")
43+
44+
if len(errors) != tc.ErrCount {
45+
t.Fatalf("Expected the Azure RM Storage Blob type to trigger a validation error")
46+
}
47+
}
48+
}
49+
50+
func TestResourceAzureRMStorageBlobSize_validation(t *testing.T) {
51+
cases := []struct {
52+
Value int
53+
ErrCount int
54+
}{
55+
{
56+
Value: 511,
57+
ErrCount: 1,
58+
},
59+
{
60+
Value: 512,
61+
ErrCount: 0,
62+
},
63+
{
64+
Value: 1024,
65+
ErrCount: 0,
66+
},
67+
{
68+
Value: 2048,
69+
ErrCount: 0,
70+
},
71+
{
72+
Value: 5120,
73+
ErrCount: 0,
74+
},
75+
}
76+
77+
for _, tc := range cases {
78+
_, errors := validateArmStorageBlobSize(tc.Value, "azurerm_storage_blob")
79+
80+
if len(errors) != tc.ErrCount {
81+
t.Fatalf("Expected the Azure RM Storage Blob size to trigger a validation error")
82+
}
83+
}
84+
}
85+
86+
func TestAccAzureRMStorageBlob_basic(t *testing.T) {
87+
ri := acctest.RandInt()
88+
rs := strings.ToLower(acctest.RandString(11))
89+
config := fmt.Sprintf(testAccAzureRMStorageBlob_basic, ri, rs)
90+
91+
resource.Test(t, resource.TestCase{
92+
PreCheck: func() { testAccPreCheck(t) },
93+
Providers: testAccProviders,
94+
CheckDestroy: testCheckAzureRMStorageBlobDestroy,
95+
Steps: []resource.TestStep{
96+
resource.TestStep{
97+
Config: config,
98+
Check: resource.ComposeTestCheckFunc(
99+
testCheckAzureRMStorageBlobExists("azurerm_storage_blob.test"),
100+
),
101+
},
102+
},
103+
})
104+
}
105+
106+
func testCheckAzureRMStorageBlobExists(name string) resource.TestCheckFunc {
107+
return func(s *terraform.State) error {
108+
109+
rs, ok := s.RootModule().Resources[name]
110+
if !ok {
111+
return fmt.Errorf("Not found: %s", name)
112+
}
113+
114+
name := rs.Primary.Attributes["name"]
115+
storageAccountName := rs.Primary.Attributes["storage_account_name"]
116+
storageContainerName := rs.Primary.Attributes["storage_container_name"]
117+
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
118+
if !hasResourceGroup {
119+
return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
120+
}
121+
122+
armClient := testAccProvider.Meta().(*ArmClient)
123+
blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
124+
if err != nil {
125+
return err
126+
}
127+
128+
exists, err := blobClient.BlobExists(storageContainerName, name)
129+
if err != nil {
130+
return err
131+
}
132+
133+
if !exists {
134+
return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName)
135+
}
136+
137+
return nil
138+
}
139+
}
140+
141+
func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error {
142+
for _, rs := range s.RootModule().Resources {
143+
if rs.Type != "azurerm_storage_blob" {
144+
continue
145+
}
146+
147+
name := rs.Primary.Attributes["name"]
148+
storageAccountName := rs.Primary.Attributes["storage_account_name"]
149+
storageContainerName := rs.Primary.Attributes["storage_container_name"]
150+
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
151+
if !hasResourceGroup {
152+
return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
153+
}
154+
155+
armClient := testAccProvider.Meta().(*ArmClient)
156+
blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
157+
if err != nil {
158+
return nil
159+
}
160+
161+
exists, err := blobClient.BlobExists(storageContainerName, name)
162+
if err != nil {
163+
return nil
164+
}
165+
166+
if exists {
167+
return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName)
168+
}
169+
}
170+
171+
return nil
172+
}
173+
174+
var testAccAzureRMStorageBlob_basic = `
175+
resource "azurerm_resource_group" "test" {
176+
name = "acctestrg-%d"
177+
location = "westus"
178+
}
179+
180+
resource "azurerm_storage_account" "test" {
181+
name = "acctestacc%s"
182+
resource_group_name = "${azurerm_resource_group.test.name}"
183+
location = "westus"
184+
account_type = "Standard_LRS"
185+
186+
tags {
187+
environment = "staging"
188+
}
189+
}
190+
191+
resource "azurerm_storage_container" "test" {
192+
name = "vhds"
193+
resource_group_name = "${azurerm_resource_group.test.name}"
194+
storage_account_name = "${azurerm_storage_account.test.name}"
195+
container_access_type = "private"
196+
}
197+
198+
resource "azurerm_storage_blob" "test" {
199+
name = "herpderp1.vhd"
200+
201+
resource_group_name = "${azurerm_resource_group.test.name}"
202+
storage_account_name = "${azurerm_storage_account.test.name}"
203+
storage_container_name = "${azurerm_storage_container.test.name}"
204+
205+
type = "page"
206+
size = 5120
207+
}
208+
`
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/Azure/azure-sdk-for-go/storage"
9+
"github.com/hashicorp/terraform/helper/acctest"
10+
"github.com/hashicorp/terraform/helper/resource"
11+
"github.com/hashicorp/terraform/terraform"
12+
)
13+
14+
func TestAccAzureRMStorageContainer_basic(t *testing.T) {
15+
ri := acctest.RandInt()
16+
rs := strings.ToLower(acctest.RandString(11))
17+
config := fmt.Sprintf(testAccAzureRMStorageContainer_basic, ri, rs)
18+
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheck(t) },
21+
Providers: testAccProviders,
22+
CheckDestroy: testCheckAzureRMStorageContainerDestroy,
23+
Steps: []resource.TestStep{
24+
resource.TestStep{
25+
Config: config,
26+
Check: resource.ComposeTestCheckFunc(
27+
testCheckAzureRMStorageContainerExists("azurerm_storage_container.test"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
func testCheckAzureRMStorageContainerExists(name string) resource.TestCheckFunc {
35+
return func(s *terraform.State) error {
36+
37+
rs, ok := s.RootModule().Resources[name]
38+
if !ok {
39+
return fmt.Errorf("Not found: %s", name)
40+
}
41+
42+
name := rs.Primary.Attributes["name"]
43+
storageAccountName := rs.Primary.Attributes["storage_account_name"]
44+
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
45+
if !hasResourceGroup {
46+
return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name)
47+
}
48+
49+
armClient := testAccProvider.Meta().(*ArmClient)
50+
blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
51+
if err != nil {
52+
return err
53+
}
54+
55+
containers, err := blobClient.ListContainers(storage.ListContainersParameters{
56+
Prefix: name,
57+
Timeout: 90,
58+
})
59+
60+
if len(containers.Containers) == 0 {
61+
return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName)
62+
}
63+
64+
var found bool
65+
for _, container := range containers.Containers {
66+
if container.Name == name {
67+
found = true
68+
}
69+
}
70+
71+
if !found {
72+
return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName)
73+
}
74+
75+
return nil
76+
}
77+
}
78+
79+
func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error {
80+
for _, rs := range s.RootModule().Resources {
81+
if rs.Type != "azurerm_storage_container" {
82+
continue
83+
}
84+
85+
name := rs.Primary.Attributes["name"]
86+
storageAccountName := rs.Primary.Attributes["storage_account_name"]
87+
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
88+
if !hasResourceGroup {
89+
return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name)
90+
}
91+
92+
armClient := testAccProvider.Meta().(*ArmClient)
93+
blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName)
94+
if err != nil {
95+
//If we can't get keys then the blob can't exist
96+
return nil
97+
}
98+
99+
containers, err := blobClient.ListContainers(storage.ListContainersParameters{
100+
Prefix: name,
101+
Timeout: 90,
102+
})
103+
104+
if err != nil {
105+
return nil
106+
}
107+
108+
var found bool
109+
for _, container := range containers.Containers {
110+
if container.Name == name {
111+
found = true
112+
}
113+
}
114+
115+
if found {
116+
return fmt.Errorf("Bad: Storage Container %q (storage account: %q) still exist", name, storageAccountName)
117+
}
118+
}
119+
120+
return nil
121+
}
122+
123+
var testAccAzureRMStorageContainer_basic = `
124+
resource "azurerm_resource_group" "test" {
125+
name = "acctestrg-%d"
126+
location = "westus"
127+
}
128+
129+
resource "azurerm_storage_account" "test" {
130+
name = "acctestacc%s"
131+
resource_group_name = "${azurerm_resource_group.test.name}"
132+
location = "westus"
133+
account_type = "Standard_LRS"
134+
135+
tags {
136+
environment = "staging"
137+
}
138+
}
139+
140+
resource "azurerm_storage_container" "test" {
141+
name = "vhds"
142+
resource_group_name = "${azurerm_resource_group.test.name}"
143+
storage_account_name = "${azurerm_storage_account.test.name}"
144+
container_access_type = "private"
145+
}
146+
`

0 commit comments

Comments
 (0)