diff --git a/Makefile b/Makefile index b3bd18d..01eb751 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,6 @@ .PHONY: test -export TF_PATH +export EXAMPLE test: - cd tests && go test -v -timeout 60m -run TestApplyNoError/$(TF_PATH) ./rsv_test.go - -#test_extended: - #cd tests && env go test -v -timeout 60m -run TestCosmosDbAccount ./rsv_extended_test.go - + cd tests && go test -v -timeout 60m -run TestApplyNoError/$(EXAMPLE) ./rsv_test.go diff --git a/README.md b/README.md index 98ff08f..7ff835d 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ End-to-end testing is not conducted on these modules, as they are individual com | `vault` | describes recovery services vault related configuration | | `naming` | used for naming purposes | string | yes | | `location` | default azure region tho be used | string | no | -| `resourcegroup` | default resource group to be used | string | no | +| `resource_group` | default resource group to be used | string | no | | `tags` | tags to be added to the resources | map(string) | no | ## Outputs diff --git a/examples/default/main.tf b/examples/default/main.tf index 98700c7..a3a354f 100644 --- a/examples/default/main.tf +++ b/examples/default/main.tf @@ -7,23 +7,23 @@ module "naming" { module "rg" { source = "cloudnationhq/rg/azure" - version = "~> 0.1" + version = "~> 2.0" groups = { demo = { - name = module.naming.resource_group.name - region = "westeurope" + name = module.naming.resource_group.name + location = "westeurope" } } } module "rsv" { source = "cloudnationhq/rsv/azure" - version = "~> 0.1" + version = "~> 1.0" vault = { - name = module.naming.recovery_services_vault.name - location = module.rg.groups.demo.location - resourcegroup = module.rg.groups.demo.name + name = module.naming.recovery_services_vault.name + location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name } } diff --git a/examples/default/terraform.tf b/examples/default/terraform.tf index 9867469..17397b4 100644 --- a/examples/default/terraform.tf +++ b/examples/default/terraform.tf @@ -4,7 +4,7 @@ terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.61" + version = "~> 4.0" } } } diff --git a/examples/policies/main.tf b/examples/policies/main.tf index 3a79af7..42112eb 100644 --- a/examples/policies/main.tf +++ b/examples/policies/main.tf @@ -7,26 +7,26 @@ module "naming" { module "rg" { source = "cloudnationhq/rg/azure" - version = "~> 0.1" + version = "~> 2.0" groups = { demo = { - name = module.naming.resource_group.name - region = "westeurope" + name = module.naming.resource_group.name + location = "westeurope" } } } module "rsv" { source = "cloudnationhq/rsv/azure" - version = "~> 0.1" + version = "~> 1.0" naming = local.naming vault = { - name = module.naming.recovery_services_vault.name - location = module.rg.groups.demo.location - resourcegroup = module.rg.groups.demo.name + name = module.naming.recovery_services_vault.name + location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name policies = { file_shares = { diff --git a/examples/policies/locals.tf b/examples/policies/naming.tf similarity index 100% rename from examples/policies/locals.tf rename to examples/policies/naming.tf diff --git a/examples/policies/terraform.tf b/examples/policies/terraform.tf index 9867469..17397b4 100644 --- a/examples/policies/terraform.tf +++ b/examples/policies/terraform.tf @@ -4,7 +4,7 @@ terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.61" + version = "~> 4.0" } } } diff --git a/examples/private-endpoint/locals.tf b/examples/private-endpoint/endpoints.tf similarity index 64% rename from examples/private-endpoint/locals.tf rename to examples/private-endpoint/endpoints.tf index b277e8d..cac0c3c 100644 --- a/examples/private-endpoint/locals.tf +++ b/examples/private-endpoint/endpoints.tf @@ -1,12 +1,3 @@ -locals { - naming = { - # lookup outputs to have consistent naming - for type in local.naming_types : type => lookup(module.naming, type).name - } - - naming_types = ["subnet", "network_security_group"] -} - locals { endpoints = { vault = { diff --git a/examples/private-endpoint/main.tf b/examples/private-endpoint/main.tf index 8f501d2..00913af 100644 --- a/examples/private-endpoint/main.tf +++ b/examples/private-endpoint/main.tf @@ -7,27 +7,27 @@ module "naming" { module "rg" { source = "cloudnationhq/rg/azure" - version = "~> 0.1" + version = "~> 2.0" groups = { demo = { - name = module.naming.resource_group.name - region = "westeurope" + name = module.naming.resource_group.name + location = "westeurope" } } } module "network" { source = "cloudnationhq/vnet/azure" - version = "~> 2.0" + version = "~> 4.0" naming = local.naming vnet = { - name = module.naming.virtual_network.name - location = module.rg.groups.demo.location - resourcegroup = module.rg.groups.demo.name - cidr = ["10.19.0.0/16"] + name = module.naming.virtual_network.name + location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name + cidr = ["10.19.0.0/16"] subnets = { sn1 = { @@ -40,12 +40,12 @@ module "network" { module "rsv" { source = "cloudnationhq/rsv/azure" - version = "~> 0.1" + version = "~> 1.0" vault = { - name = module.naming.recovery_services_vault.name - location = module.rg.groups.demo.location - resourcegroup = module.rg.groups.demo.name + name = module.naming.recovery_services_vault.name + location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name public_network_access_enabled = false } @@ -53,9 +53,9 @@ module "rsv" { module "private_dns" { source = "cloudnationhq/pdns/azure" - version = "~> 0.1" + version = "~> 2.0" - resourcegroup = module.rg.groups.demo.name + resource_group = module.rg.groups.demo.name zones = { vault = { @@ -72,10 +72,10 @@ module "private_dns" { module "privatelink" { source = "cloudnationhq/pe/azure" - version = "~> 0.1" + version = "~> 1.0" - resourcegroup = module.rg.groups.demo.name - location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name + location = module.rg.groups.demo.location endpoints = local.endpoints } diff --git a/examples/private-endpoint/naming.tf b/examples/private-endpoint/naming.tf new file mode 100644 index 0000000..c282590 --- /dev/null +++ b/examples/private-endpoint/naming.tf @@ -0,0 +1,8 @@ +locals { + naming = { + # lookup outputs to have consistent naming + for type in local.naming_types : type => lookup(module.naming, type).name + } + + naming_types = ["subnet", "network_security_group"] +} diff --git a/examples/private-endpoint/terraform.tf b/examples/private-endpoint/terraform.tf index 9867469..17397b4 100644 --- a/examples/private-endpoint/terraform.tf +++ b/examples/private-endpoint/terraform.tf @@ -4,7 +4,7 @@ terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.61" + version = "~> 4.0" } } } diff --git a/examples/protected_vms/main.tf b/examples/protected_vms/main.tf index cfd6fd2..89624ae 100644 --- a/examples/protected_vms/main.tf +++ b/examples/protected_vms/main.tf @@ -7,27 +7,27 @@ module "naming" { module "rg" { source = "cloudnationhq/rg/azure" - version = "~> 0.1" + version = "~> 2.0" groups = { demo = { - name = module.naming.resource_group.name - region = "westeurope" + name = module.naming.resource_group.name + location = "westeurope" } } } module "network" { source = "cloudnationhq/vnet/azure" - version = "~> 2.0" + version = "~> 4.0" naming = local.naming vnet = { - name = module.naming.virtual_network.name - location = module.rg.groups.demo.location - resourcegroup = module.rg.groups.demo.name - cidr = ["10.18.0.0/16"] + name = module.naming.virtual_network.name + location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name + cidr = ["10.18.0.0/16"] subnets = { int = { cidr = ["10.18.1.0/24"] @@ -43,26 +43,26 @@ module "network" { module "kv" { source = "cloudnationhq/kv/azure" - version = "~> 0.1" + version = "~> 2.0" naming = local.naming vault = { - name = module.naming.key_vault.name_unique - location = module.rg.groups.demo.location - resourcegroup = module.rg.groups.demo.name + name = module.naming.key_vault.name_unique + location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name } } module "vm" { source = "cloudnationhq/vm/azure" - version = "~> 0.1" + version = "~> 4.0" - naming = local.naming - keyvault = module.kv.vault.id - resourcegroup = module.rg.groups.demo.name - location = module.rg.groups.demo.location - depends_on = [module.kv] + naming = local.naming + keyvault = module.kv.vault.id + resource_group = module.rg.groups.demo.name + location = module.rg.groups.demo.location + depends_on = [module.kv] for_each = local.vms @@ -71,12 +71,12 @@ module "vm" { module "rsv" { source = "cloudnationhq/rsv/azure" - version = "~> 0.1" + version = "~> 1.0" vault = { - name = module.naming.recovery_services_vault.name - location = module.rg.groups.demo.location - resourcegroup = module.rg.groups.demo.name - policies = local.policies + name = module.naming.recovery_services_vault.name + location = module.rg.groups.demo.location + resource_group = module.rg.groups.demo.name + policies = local.policies } } diff --git a/examples/protected_vms/naming.tf b/examples/protected_vms/naming.tf new file mode 100644 index 0000000..af61f0d --- /dev/null +++ b/examples/protected_vms/naming.tf @@ -0,0 +1,8 @@ +locals { + naming = { + # lookup outputs to have consistent naming + for type in local.naming_types : type => lookup(module.naming, type).name + } + + naming_types = ["subnet", "network_security_group", "key_vault_secret"] +} diff --git a/examples/protected_vms/terraform.tf b/examples/protected_vms/terraform.tf index 9867469..17397b4 100644 --- a/examples/protected_vms/terraform.tf +++ b/examples/protected_vms/terraform.tf @@ -4,7 +4,7 @@ terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.61" + version = "~> 4.0" } } } diff --git a/examples/protected_vms/vms.tf b/examples/protected_vms/vms.tf index 6cee252..bce4e9a 100644 --- a/examples/protected_vms/vms.tf +++ b/examples/protected_vms/vms.tf @@ -1,12 +1,3 @@ -locals { - naming = { - # lookup outputs to have consistent naming - for type in local.naming_types : type => lookup(module.naming, type).name - } - - naming_types = ["subnet", "network_security_group", "key_vault_secret"] -} - locals { vms = { dcroot001 = { diff --git a/locals.tf b/locals.tf index 163d423..5efe283 100644 --- a/locals.tf +++ b/locals.tf @@ -2,7 +2,7 @@ locals { protected_vms_map = merge([ for policy_name, policy in lookup(var.vault, "policies", {}) != {} ? lookup(var.vault.policies, "vms", {}) : {} : { for vm_name, vm_details in lookup(policy, "protected_vms", {}) : "${policy_name}-${vm_name}" => { - resource_group_name = var.vault.resourcegroup + resource_group_name = var.vault.resource_group recovery_vault_name = var.vault.name, source_vm_id = vm_details.id, name = try(policy_name, var.naming.recovery_services_vault_backup_policy) diff --git a/main.tf b/main.tf index 10bcd34..c480907 100644 --- a/main.tf +++ b/main.tf @@ -1,7 +1,7 @@ # recovery vault resource "azurerm_recovery_services_vault" "vault" { name = var.vault.name - resource_group_name = coalesce(lookup(var.vault, "resourcegroup", null), var.resourcegroup) + resource_group_name = coalesce(lookup(var.vault, "resource_group", null), var.resource_group) location = coalesce(lookup(var.vault, "location", null), var.location) sku = try(var.vault.sku, "Standard") soft_delete_enabled = try(var.vault.soft_delete_enabled, false) @@ -20,7 +20,7 @@ resource "azurerm_backup_policy_file_share" "policy" { ) name = try(each.value.name, join("-", [var.naming.recovery_services_vault_backup_policy, each.key])) - resource_group_name = coalesce(try(var.vault.resourcegroup, null), var.resourcegroup) + resource_group_name = coalesce(try(var.vault.resource_group, null), var.resource_group) recovery_vault_name = azurerm_recovery_services_vault.vault.name timezone = try(each.value.timezone, "UTC") @@ -77,7 +77,7 @@ resource "azurerm_backup_policy_vm" "policy" { ) name = try(each.value.name, join("-", [var.naming.recovery_services_vault_backup_policy, each.key])) - resource_group_name = coalesce(try(var.vault.resourcegroup, null), var.resourcegroup) + resource_group_name = coalesce(try(var.vault.resource_group, null), var.resource_group) recovery_vault_name = azurerm_recovery_services_vault.vault.name timezone = try(each.value.timezone, "UTC") policy_type = try(each.value.policy_type, "V1") diff --git a/terraform.tf b/terraform.tf index 6c1c905..89c3bd4 100644 --- a/terraform.tf +++ b/terraform.tf @@ -4,7 +4,7 @@ terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = "~> 3.61" + version = "~> 4.0" } } } diff --git a/tests/go.mod b/tests/go.mod index eec80b5..f394c4f 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -1,8 +1,8 @@ module github.com/cloudnationhq/terraform-azure-rsv -go 1.20 +go 1.23.1 -require github.com/gruntwork-io/terratest v0.47.0 +require github.com/gruntwork-io/terratest v0.47.1 require ( cloud.google.com/go v0.110.0 // indirect @@ -23,7 +23,7 @@ require ( github.com/googleapis/gax-go/v2 v2.7.1 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect - github.com/hashicorp/go-getter v1.7.5 // indirect + github.com/hashicorp/go-getter v1.7.6 // indirect github.com/hashicorp/go-multierror v1.1.0 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect github.com/hashicorp/go-version v1.6.0 // indirect diff --git a/tests/go.sum b/tests/go.sum index 8117435..e5dd8d4 100644 --- a/tests/go.sum +++ b/tests/go.sum @@ -116,6 +116,7 @@ cloud.google.com/go/language v1.6.0/go.mod h1:6dJ8t3B+lUYfStgls25GusK04NLh3eDLQn cloud.google.com/go/lifesciences v0.5.0/go.mod h1:3oIKy8ycWGPUyZDR/8RNnTOYevhaMLqh5vLUXs9zvT8= cloud.google.com/go/lifesciences v0.6.0/go.mod h1:ddj6tSX/7BOnhxCSd3ZcETvtNr8NZ6t/iPhY2Tyfu08= cloud.google.com/go/longrunning v0.4.1 h1:v+yFJOfKC3yZdY6ZUI933pIYdhyhV8S3NpWrXWmg7jM= +cloud.google.com/go/longrunning v0.4.1/go.mod h1:4iWDqhBZ70CvZ6BfETbvam3T8FMvLK+eFj0E6AaRQTo= cloud.google.com/go/mediatranslation v0.5.0/go.mod h1:jGPUhGTybqsPQn91pNXw0xVHfuJ3leR1wj37oU3y1f4= cloud.google.com/go/mediatranslation v0.6.0/go.mod h1:hHdBCTYNigsBxshbznuIMFNe5QXEowAuNmmC7h8pu5w= cloud.google.com/go/memcache v1.4.0/go.mod h1:rTOfiGZtJX1AaFUrOgsMHX5kAzaTQ8azHiuDoTPzNsE= @@ -300,6 +301,7 @@ github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIG github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw= +github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= @@ -336,14 +338,14 @@ github.com/googleapis/gax-go/v2 v2.7.1 h1:gF4c0zjUP2H/s/hEGyLA3I0fA2ZWjzYiONAD6c github.com/googleapis/gax-go/v2 v2.7.1/go.mod h1:4orTrqY6hXxxaUL4LHIPl6lGo8vAE38/qKbhSAKP6QI= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/gruntwork-io/terratest v0.47.0 h1:xIy1pT7NbGVlMLDZEHl3+3iSnvffh8tN2pL6idn448c= -github.com/gruntwork-io/terratest v0.47.0/go.mod h1:oywHw1cFKXSYvKPm27U7quZVzDUlA22H2xUrKCe26xM= +github.com/gruntwork-io/terratest v0.47.1 h1:qOaxnL7Su5+KpDHYUN/ek1jn8ImvCKtOkaY4OSMS4tI= +github.com/gruntwork-io/terratest v0.47.1/go.mod h1:LnYX8BN5WxUMpDr8rtD39oToSL4CBERWSCusbJ0d/64= github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hashicorp/go-getter v1.7.5 h1:dT58k9hQ/vbxNMwoI5+xFYAJuv6152UNvdHokfI5wE4= -github.com/hashicorp/go-getter v1.7.5/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= +github.com/hashicorp/go-getter v1.7.6 h1:5jHuM+aH373XNtXl9TNTUH5Qd69Trve11tHIrB+6yj4= +github.com/hashicorp/go-getter v1.7.6/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo= @@ -653,6 +655,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= +golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -954,6 +957,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tests/rsv_test.go b/tests/rsv_test.go index 8cb433c..8a4d914 100644 --- a/tests/rsv_test.go +++ b/tests/rsv_test.go @@ -2,27 +2,71 @@ package main import ( "os" + "path/filepath" "testing" - "github.com/cloudnationhq/terraform-azure-rsv/shared" "github.com/gruntwork-io/terratest/modules/terraform" ) -func TestApplyNoError(t *testing.T) { - t.Parallel() +type TerraformModule struct { + Name string + Path string + Options *terraform.Options +} - tests := []shared.TestCase{ - {Name: os.Getenv("TF_PATH"), Path: "../examples/" + os.Getenv("TF_PATH")}, +func NewTerraformModule(name, path string) *TerraformModule { + return &TerraformModule{ + Name: name, + Path: path, + Options: &terraform.Options{ + TerraformDir: path, + NoColor: true, + }, } +} + +func (m *TerraformModule) Apply(t *testing.T) { + t.Logf("Applying Terraform module: %s", m.Name) + terraform.WithDefaultRetryableErrors(t, m.Options) + terraform.InitAndApply(t, m.Options) +} + +func (m *TerraformModule) Destroy(t *testing.T) { + t.Logf("Destroying Terraform module: %s", m.Name) + terraform.Destroy(t, m.Options) + m.cleanupFiles(t) +} - for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - terraformOptions := shared.GetTerraformOptions(test.Path) +func (m *TerraformModule) cleanupFiles(t *testing.T) { + t.Logf("Cleaning up in: %s", m.Options.TerraformDir) + filesToCleanup := []string{"*.terraform*", "*tfstate*"} + for _, pattern := range filesToCleanup { + matches, err := filepath.Glob(filepath.Join(m.Options.TerraformDir, pattern)) + if err != nil { + t.Errorf("Error matching pattern %s: %v", pattern, err) + continue + } + for _, filePath := range matches { + if err := os.RemoveAll(filePath); err != nil { + t.Errorf("Failed to remove %s: %v", filePath, err) + } + } + } +} - terraform.WithDefaultRetryableErrors(t, &terraform.Options{}) +func TestApplyNoError(t *testing.T) { + t.Parallel() - defer shared.Cleanup(t, terraformOptions) - terraform.InitAndApply(t, terraformOptions) - }) + example := os.Getenv("EXAMPLE") + if example == "" { + t.Fatal("EXAMPLE environment variable is not set") } + + modulePath := filepath.Join("..", "examples", example) + module := NewTerraformModule(example, modulePath) + + t.Run(module.Name, func(t *testing.T) { + defer module.Destroy(t) + module.Apply(t) + }) } diff --git a/tests/shared/shared.go b/tests/shared/shared.go deleted file mode 100644 index 4a038e7..0000000 --- a/tests/shared/shared.go +++ /dev/null @@ -1,49 +0,0 @@ -package shared - -import ( - "os" - "path/filepath" - "testing" - - "github.com/gruntwork-io/terratest/modules/terraform" -) - -var filesToCleanup = []string{ - "*.terraform*", - "*tfstate*", -} - -type TestCase struct { - Name string - Path string -} - -func GetTerraformOptions(terraformDir string) *terraform.Options { - return &terraform.Options{ - TerraformDir: terraformDir, - NoColor: true, - Parallelism: 20, - } -} - -func Cleanup(t *testing.T, tfOpts *terraform.Options) { - terraform.Destroy(t, tfOpts) - CleanupFiles(t, tfOpts.TerraformDir) -} - -func CleanupFiles(t *testing.T, dir string) { - for _, pattern := range filesToCleanup { - matches, err := filepath.Glob(filepath.Join(dir, pattern)) - if err != nil { - t.Logf("Error: %v", err) - continue - } - for _, filePath := range matches { - if err := os.RemoveAll(filePath); err != nil { - t.Logf("Failed to remove %s: %v\n", filePath, err) - } else { - t.Logf("Successfully removed %s\n", filePath) - } - } - } -} diff --git a/variables.tf b/variables.tf index e9d3c32..fbf0d8a 100644 --- a/variables.tf +++ b/variables.tf @@ -15,7 +15,7 @@ variable "location" { default = null } -variable "resourcegroup" { +variable "resource_group" { description = "default resource group to be used." type = string default = null