/azurerm/internal/services/storage/tests/resource_arm_storage_container_test.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 533 lines · 478 code · 55 blank · 0 comment · 36 complexity · afd2932037523b1bab741907f38ffb22 MD5 · raw file

  1. package tests
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
  7. "github.com/hashicorp/terraform-plugin-sdk/terraform"
  8. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
  9. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
  10. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
  11. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
  12. )
  13. func TestAccAzureRMStorageContainer_basic(t *testing.T) {
  14. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  15. resource.ParallelTest(t, resource.TestCase{
  16. PreCheck: func() { acceptance.PreCheck(t) },
  17. Providers: acceptance.SupportedProviders,
  18. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  19. Steps: []resource.TestStep{
  20. {
  21. Config: testAccAzureRMStorageContainer_basic(data),
  22. Check: resource.ComposeTestCheckFunc(
  23. testCheckAzureRMStorageContainerExists(data.ResourceName),
  24. ),
  25. },
  26. data.ImportStep(),
  27. },
  28. })
  29. }
  30. func TestAccAzureRMStorageContainer_deleteAndRecreate(t *testing.T) {
  31. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  32. resource.ParallelTest(t, resource.TestCase{
  33. PreCheck: func() { acceptance.PreCheck(t) },
  34. Providers: acceptance.SupportedProviders,
  35. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  36. Steps: []resource.TestStep{
  37. {
  38. Config: testAccAzureRMStorageContainer_basic(data),
  39. Check: resource.ComposeTestCheckFunc(
  40. testCheckAzureRMStorageContainerExists(data.ResourceName),
  41. ),
  42. },
  43. data.ImportStep(),
  44. {
  45. Config: testAccAzureRMStorageContainer_template(data),
  46. },
  47. {
  48. Config: testAccAzureRMStorageContainer_basic(data),
  49. Check: resource.ComposeTestCheckFunc(
  50. testCheckAzureRMStorageContainerExists(data.ResourceName),
  51. ),
  52. },
  53. data.ImportStep(),
  54. },
  55. })
  56. }
  57. func TestAccAzureRMStorageContainer_basicAzureADAuth(t *testing.T) {
  58. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  59. resource.ParallelTest(t, resource.TestCase{
  60. PreCheck: func() { acceptance.PreCheck(t) },
  61. Providers: acceptance.SupportedProviders,
  62. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  63. Steps: []resource.TestStep{
  64. {
  65. Config: testAccAzureRMStorageContainer_basicAzureADAuth(data),
  66. Check: resource.ComposeTestCheckFunc(
  67. testCheckAzureRMStorageContainerExists(data.ResourceName),
  68. ),
  69. },
  70. data.ImportStep(),
  71. },
  72. })
  73. }
  74. func TestAccAzureRMStorageContainer_requiresImport(t *testing.T) {
  75. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  76. resource.ParallelTest(t, resource.TestCase{
  77. PreCheck: func() { acceptance.PreCheck(t) },
  78. Providers: acceptance.SupportedProviders,
  79. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  80. Steps: []resource.TestStep{
  81. {
  82. Config: testAccAzureRMStorageContainer_basic(data),
  83. Check: resource.ComposeTestCheckFunc(
  84. testCheckAzureRMStorageContainerExists(data.ResourceName),
  85. ),
  86. },
  87. data.RequiresImportErrorStep(testAccAzureRMStorageContainer_requiresImport),
  88. },
  89. })
  90. }
  91. func TestAccAzureRMStorageContainer_update(t *testing.T) {
  92. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  93. resource.ParallelTest(t, resource.TestCase{
  94. PreCheck: func() { acceptance.PreCheck(t) },
  95. Providers: acceptance.SupportedProviders,
  96. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  97. Steps: []resource.TestStep{
  98. {
  99. Config: testAccAzureRMStorageContainer_update(data, "private"),
  100. Check: resource.ComposeTestCheckFunc(
  101. testCheckAzureRMStorageContainerExists(data.ResourceName),
  102. resource.TestCheckResourceAttr(data.ResourceName, "container_access_type", "private"),
  103. ),
  104. },
  105. {
  106. Config: testAccAzureRMStorageContainer_update(data, "container"),
  107. Check: resource.ComposeTestCheckFunc(
  108. testCheckAzureRMStorageContainerExists(data.ResourceName),
  109. resource.TestCheckResourceAttr(data.ResourceName, "container_access_type", "container"),
  110. ),
  111. },
  112. data.ImportStep(),
  113. },
  114. })
  115. }
  116. func TestAccAzureRMStorageContainer_metaData(t *testing.T) {
  117. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  118. resource.ParallelTest(t, resource.TestCase{
  119. PreCheck: func() { acceptance.PreCheck(t) },
  120. Providers: acceptance.SupportedProviders,
  121. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  122. Steps: []resource.TestStep{
  123. {
  124. Config: testAccAzureRMStorageContainer_metaData(data),
  125. Check: resource.ComposeTestCheckFunc(
  126. testCheckAzureRMStorageContainerExists(data.ResourceName),
  127. ),
  128. },
  129. data.ImportStep(),
  130. {
  131. Config: testAccAzureRMStorageContainer_metaDataUpdated(data),
  132. Check: resource.ComposeTestCheckFunc(
  133. testCheckAzureRMStorageContainerExists(data.ResourceName),
  134. ),
  135. },
  136. data.ImportStep(),
  137. {
  138. Config: testAccAzureRMStorageContainer_metaDataEmpty(data),
  139. Check: resource.ComposeTestCheckFunc(
  140. testCheckAzureRMStorageContainerExists(data.ResourceName),
  141. ),
  142. },
  143. data.ImportStep(),
  144. },
  145. })
  146. }
  147. func TestAccAzureRMStorageContainer_disappears(t *testing.T) {
  148. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  149. resource.ParallelTest(t, resource.TestCase{
  150. PreCheck: func() { acceptance.PreCheck(t) },
  151. Providers: acceptance.SupportedProviders,
  152. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  153. Steps: []resource.TestStep{
  154. {
  155. Config: testAccAzureRMStorageContainer_basic(data),
  156. Check: resource.ComposeTestCheckFunc(
  157. testCheckAzureRMStorageContainerExists(data.ResourceName),
  158. testAccARMStorageContainerDisappears(data.ResourceName),
  159. ),
  160. ExpectNonEmptyPlan: true,
  161. },
  162. },
  163. })
  164. }
  165. func TestAccAzureRMStorageContainer_root(t *testing.T) {
  166. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  167. resource.ParallelTest(t, resource.TestCase{
  168. PreCheck: func() { acceptance.PreCheck(t) },
  169. Providers: acceptance.SupportedProviders,
  170. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  171. Steps: []resource.TestStep{
  172. {
  173. Config: testAccAzureRMStorageContainer_root(data),
  174. Check: resource.ComposeTestCheckFunc(
  175. testCheckAzureRMStorageContainerExists(data.ResourceName),
  176. resource.TestCheckResourceAttr(data.ResourceName, "name", "$root"),
  177. ),
  178. },
  179. data.ImportStep(),
  180. },
  181. })
  182. }
  183. func TestAccAzureRMStorageContainer_web(t *testing.T) {
  184. data := acceptance.BuildTestData(t, "azurerm_storage_container", "test")
  185. resource.ParallelTest(t, resource.TestCase{
  186. PreCheck: func() { acceptance.PreCheck(t) },
  187. Providers: acceptance.SupportedProviders,
  188. CheckDestroy: testCheckAzureRMStorageContainerDestroy,
  189. Steps: []resource.TestStep{
  190. {
  191. Config: testAccAzureRMStorageContainer_web(data),
  192. Check: resource.ComposeTestCheckFunc(
  193. testCheckAzureRMStorageContainerExists(data.ResourceName),
  194. resource.TestCheckResourceAttr(data.ResourceName, "name", "$web"),
  195. ),
  196. },
  197. data.ImportStep(),
  198. },
  199. })
  200. }
  201. func testCheckAzureRMStorageContainerExists(resourceName string) resource.TestCheckFunc {
  202. return func(s *terraform.State) error {
  203. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  204. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  205. rs, ok := s.RootModule().Resources[resourceName]
  206. if !ok {
  207. return fmt.Errorf("Not found: %s", resourceName)
  208. }
  209. containerName := rs.Primary.Attributes["name"]
  210. accountName := rs.Primary.Attributes["storage_account_name"]
  211. account, err := storageClient.FindAccount(ctx, accountName)
  212. if err != nil {
  213. return fmt.Errorf("Error retrieving Account %q for Container %q: %s", accountName, containerName, err)
  214. }
  215. if account == nil {
  216. return fmt.Errorf("Unable to locate Storage Account %q!", accountName)
  217. }
  218. client, err := storageClient.ContainersClient(ctx, *account)
  219. if err != nil {
  220. return fmt.Errorf("Error building Containers Client: %s", err)
  221. }
  222. resp, err := client.GetProperties(ctx, accountName, containerName)
  223. if err != nil {
  224. if utils.ResponseWasNotFound(resp.Response) {
  225. return fmt.Errorf("Bad: Container %q (Account %q / Resource Group %q) does not exist", containerName, accountName, account.ResourceGroup)
  226. }
  227. return fmt.Errorf("Bad: Get on ContainersClient: %+v", err)
  228. }
  229. return nil
  230. }
  231. }
  232. func testAccARMStorageContainerDisappears(resourceName string) resource.TestCheckFunc {
  233. return func(s *terraform.State) error {
  234. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  235. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  236. rs, ok := s.RootModule().Resources[resourceName]
  237. if !ok {
  238. return fmt.Errorf("Not found: %s", resourceName)
  239. }
  240. containerName := rs.Primary.Attributes["name"]
  241. accountName := rs.Primary.Attributes["storage_account_name"]
  242. account, err := storageClient.FindAccount(ctx, accountName)
  243. if err != nil {
  244. return fmt.Errorf("Error retrieving Account %q for Container %q: %s", accountName, containerName, err)
  245. }
  246. if account == nil {
  247. return fmt.Errorf("Unable to locate Storage Account %q!", accountName)
  248. }
  249. client, err := storageClient.ContainersClient(ctx, *account)
  250. if err != nil {
  251. return fmt.Errorf("Error building Containers Client: %s", err)
  252. }
  253. if _, err := client.Delete(ctx, accountName, containerName); err != nil {
  254. return fmt.Errorf("Error deleting Container %q (Account %q): %s", containerName, accountName, err)
  255. }
  256. return nil
  257. }
  258. }
  259. func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error {
  260. storageClient := acceptance.AzureProvider.Meta().(*clients.Client).Storage
  261. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  262. for _, rs := range s.RootModule().Resources {
  263. if rs.Type != "azurerm_storage_container" {
  264. continue
  265. }
  266. containerName := rs.Primary.Attributes["name"]
  267. accountName := rs.Primary.Attributes["storage_account_name"]
  268. account, err := storageClient.FindAccount(ctx, accountName)
  269. if err != nil {
  270. return fmt.Errorf("Error retrieving Account %q for Container %q: %s", accountName, containerName, err)
  271. }
  272. if account == nil {
  273. return nil
  274. }
  275. client, err := storageClient.ContainersClient(ctx, *account)
  276. if err != nil {
  277. return fmt.Errorf("Error building Containers Client: %s", err)
  278. }
  279. props, err := client.GetProperties(ctx, accountName, containerName)
  280. if err != nil {
  281. return nil
  282. }
  283. return fmt.Errorf("Container still exists: %+v", props)
  284. }
  285. return nil
  286. }
  287. func testAccAzureRMStorageContainer_basic(data acceptance.TestData) string {
  288. template := testAccAzureRMStorageContainer_template(data)
  289. return fmt.Sprintf(`
  290. %s
  291. resource "azurerm_storage_container" "test" {
  292. name = "vhds"
  293. storage_account_name = azurerm_storage_account.test.name
  294. container_access_type = "private"
  295. }
  296. `, template)
  297. }
  298. func testAccAzureRMStorageContainer_basicAzureADAuth(data acceptance.TestData) string {
  299. return fmt.Sprintf(`
  300. provider "azurerm" {
  301. storage_use_azuread = true
  302. features {}
  303. }
  304. resource "azurerm_resource_group" "test" {
  305. name = "acctestRG-%d"
  306. location = "%s"
  307. }
  308. resource "azurerm_storage_account" "test" {
  309. name = "acctestacc%s"
  310. resource_group_name = azurerm_resource_group.test.name
  311. location = azurerm_resource_group.test.location
  312. account_tier = "Standard"
  313. account_replication_type = "LRS"
  314. tags = {
  315. environment = "staging"
  316. }
  317. }
  318. resource "azurerm_storage_container" "test" {
  319. name = "vhds"
  320. storage_account_name = azurerm_storage_account.test.name
  321. container_access_type = "private"
  322. }
  323. `, data.RandomInteger, data.Locations.Primary, data.RandomString)
  324. }
  325. func testAccAzureRMStorageContainer_requiresImport(data acceptance.TestData) string {
  326. template := testAccAzureRMStorageContainer_basic(data)
  327. return fmt.Sprintf(`
  328. %s
  329. resource "azurerm_storage_container" "import" {
  330. name = azurerm_storage_container.test.name
  331. storage_account_name = azurerm_storage_container.test.storage_account_name
  332. container_access_type = azurerm_storage_container.test.container_access_type
  333. }
  334. `, template)
  335. }
  336. func testAccAzureRMStorageContainer_update(data acceptance.TestData, accessType string) string {
  337. template := testAccAzureRMStorageContainer_template(data)
  338. return fmt.Sprintf(`
  339. %s
  340. resource "azurerm_storage_container" "test" {
  341. name = "vhds"
  342. storage_account_name = azurerm_storage_account.test.name
  343. container_access_type = "%s"
  344. }
  345. `, template, accessType)
  346. }
  347. func testAccAzureRMStorageContainer_metaData(data acceptance.TestData) string {
  348. template := testAccAzureRMStorageContainer_template(data)
  349. return fmt.Sprintf(`
  350. %s
  351. resource "azurerm_storage_container" "test" {
  352. name = "vhds"
  353. storage_account_name = azurerm_storage_account.test.name
  354. container_access_type = "private"
  355. metadata = {
  356. hello = "world"
  357. }
  358. }
  359. `, template)
  360. }
  361. func testAccAzureRMStorageContainer_metaDataUpdated(data acceptance.TestData) string {
  362. template := testAccAzureRMStorageContainer_template(data)
  363. return fmt.Sprintf(`
  364. %s
  365. resource "azurerm_storage_container" "test" {
  366. name = "vhds"
  367. storage_account_name = azurerm_storage_account.test.name
  368. container_access_type = "private"
  369. metadata = {
  370. hello = "world"
  371. panda = "pops"
  372. }
  373. }
  374. `, template)
  375. }
  376. func testAccAzureRMStorageContainer_metaDataEmpty(data acceptance.TestData) string {
  377. template := testAccAzureRMStorageContainer_template(data)
  378. return fmt.Sprintf(`
  379. %s
  380. resource "azurerm_storage_container" "test" {
  381. name = "vhds"
  382. storage_account_name = azurerm_storage_account.test.name
  383. container_access_type = "private"
  384. metadata = {}
  385. }
  386. `, template)
  387. }
  388. func testAccAzureRMStorageContainer_root(data acceptance.TestData) string {
  389. template := testAccAzureRMStorageContainer_template(data)
  390. return fmt.Sprintf(`
  391. %s
  392. resource "azurerm_storage_container" "test" {
  393. name = "$root"
  394. storage_account_name = azurerm_storage_account.test.name
  395. container_access_type = "private"
  396. }
  397. `, template)
  398. }
  399. func testAccAzureRMStorageContainer_web(data acceptance.TestData) string {
  400. template := testAccAzureRMStorageContainer_template(data)
  401. return fmt.Sprintf(`
  402. %s
  403. resource "azurerm_storage_container" "test" {
  404. name = "$web"
  405. storage_account_name = azurerm_storage_account.test.name
  406. container_access_type = "private"
  407. }
  408. `, template)
  409. }
  410. func testAccAzureRMStorageContainer_template(data acceptance.TestData) string {
  411. return fmt.Sprintf(`
  412. provider "azurerm" {
  413. features {}
  414. }
  415. resource "azurerm_resource_group" "test" {
  416. name = "acctestRG-%d"
  417. location = "%s"
  418. }
  419. resource "azurerm_storage_account" "test" {
  420. name = "acctestacc%s"
  421. resource_group_name = azurerm_resource_group.test.name
  422. location = azurerm_resource_group.test.location
  423. account_tier = "Standard"
  424. account_replication_type = "LRS"
  425. tags = {
  426. environment = "staging"
  427. }
  428. }
  429. `, data.RandomInteger, data.Locations.Primary, data.RandomString)
  430. }
  431. func TestValidateArmStorageContainerName(t *testing.T) {
  432. validNames := []string{
  433. "valid-name",
  434. "valid02-name",
  435. "$root",
  436. "$web",
  437. }
  438. for _, v := range validNames {
  439. _, errors := validate.StorageContainerName(v, "name")
  440. if len(errors) != 0 {
  441. t.Fatalf("%q should be a valid Storage Container Name: %q", v, errors)
  442. }
  443. }
  444. invalidNames := []string{
  445. "InvalidName1",
  446. "-invalidname1",
  447. "invalid_name",
  448. "invalid!",
  449. "ww",
  450. "$notroot",
  451. "$notweb",
  452. strings.Repeat("w", 65),
  453. }
  454. for _, v := range invalidNames {
  455. _, errors := validate.StorageContainerName(v, "name")
  456. if len(errors) == 0 {
  457. t.Fatalf("%q should be an invalid Storage Container Name", v)
  458. }
  459. }
  460. }