diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee1bfee8..bff49ac4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,7 @@ on: pull_request: branches: - main + - fix/update-acctests paths-ignore: - 'README.md' @@ -15,6 +16,7 @@ on: - 'README.md' branches: - main + - fix/update-acctests # Ensures only 1 action runs per PR and previous is canceled on new trigger concurrency: @@ -118,7 +120,7 @@ jobs: name: Matrix Acceptance Tests needs: build runs-on: ubuntu-latest - if: "!github.event.pull_request.head.repo.fork" +# if: "!github.event.pull_request.head.repo.fork" timeout-minutes: 15 strategy: fail-fast: false @@ -169,7 +171,7 @@ jobs: cleanup: name: Cleanup - if: "!github.event.pull_request.head.repo.fork" +# if: "!github.event.pull_request.head.repo.fork" needs: tests-acceptance runs-on: ubuntu-latest timeout-minutes: 15 diff --git a/twingate/internal/test/acctests/datasource/connector_test.go b/twingate/internal/test/acctests/datasource/connector_test.go index b4cc961a..84e1f0e5 100644 --- a/twingate/internal/test/acctests/datasource/connector_test.go +++ b/twingate/internal/test/acctests/datasource/connector_test.go @@ -13,35 +13,35 @@ import ( ) func TestAccDatasourceTwingateConnector_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Connector Basic", func(t *testing.T) { - networkName := test.RandomName() - connectorName := test.RandomConnectorName() - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateConnector(networkName, connectorName), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckOutput("my_connector", connectorName), - resource.TestCheckOutput("my_connector_notification_status", "true"), - ), - }, + t.Parallel() + + networkName := test.RandomName() + connectorName := test.RandomConnectorName() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateConnector(networkName, connectorName), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckOutput("my_connector", connectorName), + resource.TestCheckOutput("my_connector_notification_status", "true"), + ), }, - }) + }, }) } func testDatasourceTwingateConnector(remoteNetworkName, connectorName string) string { - return fmt.Sprintf(` + return acctests.Nprintf(` resource "twingate_remote_network" "test_dc1" { - name = "%s" + name = "${network_name}" } resource "twingate_connector" "test_dc1" { remote_network_id = twingate_remote_network.test_dc1.id - name = "%s" + name = "${connector_name}" } data "twingate_connector" "out_dc1" { @@ -55,25 +55,29 @@ func testDatasourceTwingateConnector(remoteNetworkName, connectorName string) st output "my_connector_notification_status" { value = data.twingate_connector.out_dc1.status_updates_enabled } - `, remoteNetworkName, connectorName) + `, + map[string]any{ + "network_name": remoteNetworkName, + "connector_name": connectorName, + }) } func TestAccDatasourceTwingateConnector_negative(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Connector - does not exists", func(t *testing.T) { - connectorID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Connector:%d", acctest.RandInt()))) + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateConnectorDoesNotExists(connectorID), - ExpectError: regexp.MustCompile("failed to read connector with id"), - }, + connectorID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Connector:%d", acctest.RandInt()))) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateConnectorDoesNotExists(connectorID), + ExpectError: regexp.MustCompile("failed to read connector with id"), }, - }) + }, }) } @@ -90,20 +94,21 @@ func testTwingateConnectorDoesNotExists(id string) string { } func TestAccDatasourceTwingateConnector_invalidID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Connector - failed parse ID", func(t *testing.T) { - connectorID := acctest.RandString(10) + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateConnectorDoesNotExists(connectorID), - ExpectError: regexp.MustCompile("failed to read connector with id"), - }, + connectorID := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateConnectorDoesNotExists(connectorID), + ExpectError: regexp.MustCompile("failed to read connector with id"), }, - }) + }, }) + } diff --git a/twingate/internal/test/acctests/datasource/connectors_test.go b/twingate/internal/test/acctests/datasource/connectors_test.go index 81e511fe..077fb7a3 100644 --- a/twingate/internal/test/acctests/datasource/connectors_test.go +++ b/twingate/internal/test/acctests/datasource/connectors_test.go @@ -14,73 +14,77 @@ import ( ) func TestAccDatasourceTwingateConnectors_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Connectors Basic", func(t *testing.T) { - acctests.SetPageLimit(1) - - networkName1 := test.RandomName() - networkName2 := test.RandomName() - connectorName := test.RandomConnectorName() - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateConnectors(networkName1, connectorName, networkName2, connectorName, connectorName), - Check: acctests.ComposeTestCheckFunc( - testCheckOutputLength("my_connectors", 2), - testCheckOutputAttr("my_connectors", 0, attr.Name, connectorName), - testCheckOutputAttr("my_connectors", 0, attr.StatusUpdatesEnabled, true), - ), - }, + acctests.SetPageLimit(t, 1) + + networkName1 := test.RandomName() + networkName2 := test.RandomName() + connectorName := test.RandomConnectorName() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateConnectors(networkName1, connectorName, networkName2, connectorName, connectorName), + Check: acctests.ComposeTestCheckFunc( + testCheckOutputLength("my_connectors", 2), + testCheckOutputAttr("my_connectors", 0, attr.Name, connectorName), + testCheckOutputAttr("my_connectors", 0, attr.StatusUpdatesEnabled, true), + ), }, - }) + }, }) } func testDatasourceTwingateConnectors(networkName1, connectorName1, networkName2, connectorName2, prefix string) string { - return fmt.Sprintf(` + return acctests.Nprintf(` resource "twingate_remote_network" "test_dcs1" { - name = "%s" + name = "${network_name_1}" } resource "twingate_connector" "test_dcs1" { remote_network_id = twingate_remote_network.test_dcs1.id - name = "%s" + name = "${connector_name_1}" } resource "twingate_remote_network" "test_dcs2" { - name = "%s" + name = "${network_name_2}" } resource "twingate_connector" "test_dcs2" { remote_network_id = twingate_remote_network.test_dcs2.id - name = "%s" + name = "${connector_name_1}" } data "twingate_connectors" "all" { depends_on = [twingate_connector.test_dcs1, twingate_connector.test_dcs2] } output "my_connectors" { - value = [for c in [for conn in data.twingate_connectors.all : conn if can(conn.*.name)][0] : c if length(regexall("%s.*", c.name)) > 0] + value = [for c in [for conn in data.twingate_connectors.all : conn if can(conn.*.name)][0] : c if length(regexall("${prefix}.*", c.name)) > 0] } - `, networkName1, connectorName1, networkName2, connectorName2, prefix) + `, + map[string]any{ + "network_name_1": networkName1, + "connector_name_1": connectorName1, + "network_name_2": networkName2, + "connector_name_2": connectorName2, + "prefix": prefix, + }) } func TestAccDatasourceTwingateConnectors_emptyResult(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Connectors - empty result", func(t *testing.T) { - prefix := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - Steps: []resource.TestStep{ - { - Config: testTwingateConnectorsDoesNotExists(prefix), - Check: resource.ComposeTestCheckFunc( - testCheckOutputLength("my_connectors_dcs2", 0), - ), - }, + t.Parallel() + prefix := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: testTwingateConnectorsDoesNotExists(prefix), + Check: resource.ComposeTestCheckFunc( + testCheckOutputLength("my_connectors_dcs2", 0), + ), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/group_test.go b/twingate/internal/test/acctests/datasource/group_test.go index d77ec314..b2d5b153 100644 --- a/twingate/internal/test/acctests/datasource/group_test.go +++ b/twingate/internal/test/acctests/datasource/group_test.go @@ -13,32 +13,31 @@ import ( ) func TestAccDatasourceTwingateGroup_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Group Basic", func(t *testing.T) { - groupName := test.RandomName() - - securityPolicies, err := acctests.ListSecurityPolicies() - if err != nil { - t.Skip("can't run test:", err) - } - - testPolicy := securityPolicies[0] - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateGroup(groupName, testPolicy.ID), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckOutput("my_group_dg1", groupName), - resource.TestCheckOutput("my_group_is_active_dg1", "true"), - resource.TestCheckOutput("my_group_type_dg1", "MANUAL"), - resource.TestCheckOutput("my_group_policy_dg1", testPolicy.ID), - ), - }, + t.Parallel() + + groupName := test.RandomName() + securityPolicies, err := acctests.ListSecurityPolicies() + if err != nil { + t.Skip("can't run test:", err) + } + + testPolicy := securityPolicies[0] + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateGroup(groupName, testPolicy.ID), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckOutput("my_group_dg1", groupName), + resource.TestCheckOutput("my_group_is_active_dg1", "true"), + resource.TestCheckOutput("my_group_type_dg1", "MANUAL"), + resource.TestCheckOutput("my_group_policy_dg1", testPolicy.ID), + ), }, - }) + }, }) } @@ -72,21 +71,21 @@ func testDatasourceTwingateGroup(name, securityPolicyID string) string { } func TestAccDatasourceTwingateGroup_negative(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Group - does not exists", func(t *testing.T) { - groupID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Group:%d", acctest.RandInt()))) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) + t.Parallel() + + groupID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Group:%d", acctest.RandInt()))) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateGroupDoesNotExists(groupID), + ExpectError: regexp.MustCompile("failed to read group with id"), }, - Steps: []resource.TestStep{ - { - Config: testTwingateGroupDoesNotExists(groupID), - ExpectError: regexp.MustCompile("failed to read group with id"), - }, - }, - }) + }, }) } @@ -99,20 +98,20 @@ func testTwingateGroupDoesNotExists(id string) string { } func TestAccDatasourceTwingateGroup_invalidGroupID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Group - failed parse group ID", func(t *testing.T) { - groupID := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateGroupDoesNotExists(groupID), - ExpectError: regexp.MustCompile("failed to read group with id"), - }, + t.Parallel() + + groupID := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateGroupDoesNotExists(groupID), + ExpectError: regexp.MustCompile("failed to read group with id"), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/groups_test.go b/twingate/internal/test/acctests/datasource/groups_test.go index 89a291fb..1a1bd26c 100644 --- a/twingate/internal/test/acctests/datasource/groups_test.go +++ b/twingate/internal/test/acctests/datasource/groups_test.go @@ -18,54 +18,57 @@ var ( ) func TestAccDatasourceTwingateGroups_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Groups Basic", func(t *testing.T) { - groupName := test.RandomName() - - const theDatasource = "data.twingate_groups.out_dgs1" - - securityPolicies, err := acctests.ListSecurityPolicies() - if err != nil { - t.Skip("can't run test:", err) - } + t.Parallel() - testPolicy := securityPolicies[0] + groupName := test.RandomName() + const theDatasource = "data.twingate_groups.out_dgs1" + securityPolicies, err := acctests.ListSecurityPolicies() + if err != nil { + t.Skip("can't run test:", err) + } - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateGroups(groupName, testPolicy.ID), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(theDatasource, groupsLen, "2"), - resource.TestCheckResourceAttr(theDatasource, groupNamePath, groupName), - resource.TestCheckResourceAttr(theDatasource, groupPolicyIDPath, testPolicy.ID), - ), - }, + testPolicy := securityPolicies[0] + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateGroups(groupName, testPolicy.ID), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(theDatasource, groupsLen, "2"), + resource.TestCheckResourceAttr(theDatasource, groupNamePath, groupName), + resource.TestCheckResourceAttr(theDatasource, groupPolicyIDPath, testPolicy.ID), + ), }, - }) + }, }) } func testDatasourceTwingateGroups(name, securityPolicyID string) string { - return fmt.Sprintf(` + return acctests.Nprintf(` resource "twingate_group" "test_dgs1_1" { - name = "%s" - security_policy_id = "%s" + name = "${name}" + security_policy_id = "${security_policy}" } resource "twingate_group" "test_dgs1_2" { - name = "%s" - security_policy_id = "%s" + name = "${name}" + security_policy_id = "${security_policy}" } data "twingate_groups" "out_dgs1" { - name = "%s" + name = "${name}" depends_on = [twingate_group.test_dgs1_1, twingate_group.test_dgs1_2] } - `, name, securityPolicyID, name, securityPolicyID, name) + `, + map[string]any{ + "name": name, + "security_policy": securityPolicyID, + }) + } func TestAccDatasourceTwingateGroups_emptyResult(t *testing.T) { @@ -96,7 +99,7 @@ func testTwingateGroupsDoesNotExists(name string) string { } func TestAccDatasourceTwingateGroupsWithFilters_basic(t *testing.T) { - acctests.SetPageLimit(1) + acctests.SetPageLimit(t, 1) groupName := test.RandomName() const theDatasource = "data.twingate_groups.out_dgs2" diff --git a/twingate/internal/test/acctests/datasource/remote-network_test.go b/twingate/internal/test/acctests/datasource/remote-network_test.go index 7446d2fc..8e5c1f6c 100644 --- a/twingate/internal/test/acctests/datasource/remote-network_test.go +++ b/twingate/internal/test/acctests/datasource/remote-network_test.go @@ -14,23 +14,22 @@ import ( ) func TestAccDatasourceTwingateRemoteNetwork_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Remote Network Basic", func(t *testing.T) { - - networkName := test.RandomName() - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateRemoteNetwork(networkName), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.twingate_remote_network.test_dn1_2", attr.Name, networkName), - ), - }, + t.Parallel() + + networkName := test.RandomName() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateRemoteNetwork(networkName), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.twingate_remote_network.test_dn1_2", attr.Name, networkName), + ), }, - }) + }, }) } @@ -51,57 +50,56 @@ func testDatasourceTwingateRemoteNetwork(name string) string { } func TestAccDatasourceTwingateRemoteNetworkByName_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Remote Network Basic", func(t *testing.T) { - - networkName := test.RandomName() - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateRemoteNetworkByName(networkName), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.twingate_remote_network.test_dn2_2", attr.Name, networkName), - ), - }, + t.Parallel() + + networkName := test.RandomName() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateRemoteNetworkByName(networkName), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.twingate_remote_network.test_dn2_2", attr.Name, networkName), + ), }, - }) + }, }) } func testDatasourceTwingateRemoteNetworkByName(name string) string { - return fmt.Sprintf(` + return acctests.Nprintf(` resource "twingate_remote_network" "test_dn2_1" { - name = "%s" + name = "${name}" } data "twingate_remote_network" "test_dn2_2" { - name = "%s" + name = "${name}" depends_on = [resource.twingate_remote_network.test_dn2_1] } output "my_network_dn2" { value = data.twingate_remote_network.test_dn2_2.name } - `, name, name) + `, map[string]any{"name": name}) } func TestAccDatasourceTwingateRemoteNetwork_negative(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Remote Network - does not exists", func(t *testing.T) { - networkID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("RemoteNetwork:%d", acctest.RandInt()))) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - Steps: []resource.TestStep{ - { - Config: testTwingateRemoteNetworkDoesNotExists(networkID), - ExpectError: regexp.MustCompile("failed to read remote network with id"), - }, + t.Parallel() + + networkID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("RemoteNetwork:%d", acctest.RandInt()))) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: testTwingateRemoteNetworkDoesNotExists(networkID), + ExpectError: regexp.MustCompile("failed to read remote network with id"), }, - }) + }, }) } @@ -118,39 +116,39 @@ func testTwingateRemoteNetworkDoesNotExists(id string) string { } func TestAccDatasourceTwingateRemoteNetwork_invalidNetworkID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Remote Network - failed parse network ID", func(t *testing.T) { - networkID := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - Steps: []resource.TestStep{ - { - Config: testTwingateRemoteNetworkDoesNotExists(networkID), - ExpectError: regexp.MustCompile("failed to read remote network with id"), - }, + t.Parallel() + + networkID := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: testTwingateRemoteNetworkDoesNotExists(networkID), + ExpectError: regexp.MustCompile("failed to read remote network with id"), }, - }) + }, }) } func TestAccDatasourceTwingateRemoteNetwork_bothNetworkIDAndName(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Remote Network - failed passing both network ID and name", func(t *testing.T) { - networkID := acctest.RandString(10) - networkName := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateRemoteNetworkValidationFailed(networkID, networkName), - ExpectError: regexp.MustCompile("Invalid Attribute Combination"), - }, + t.Parallel() + + networkID := acctest.RandString(10) + networkName := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateRemoteNetworkValidationFailed(networkID, networkName), + ExpectError: regexp.MustCompile("Invalid Attribute Combination"), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/remote-networks_test.go b/twingate/internal/test/acctests/datasource/remote-networks_test.go index b1eedfff..c6bc394d 100644 --- a/twingate/internal/test/acctests/datasource/remote-networks_test.go +++ b/twingate/internal/test/acctests/datasource/remote-networks_test.go @@ -1,7 +1,6 @@ package datasource import ( - "fmt" "testing" "github.com/Twingate/terraform-provider-twingate/twingate/internal/test" @@ -11,37 +10,35 @@ import ( ) func TestAccDatasourceTwingateRemoteNetworks_read(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Remote Networks Read", func(t *testing.T) { - acctests.SetPageLimit(1) + acctests.SetPageLimit(t, 1) - prefix := acctest.RandString(10) - networkName1 := test.RandomName(prefix) - networkName2 := test.RandomName(prefix) + prefix := acctest.RandString(10) + networkName1 := test.RandomName(prefix) + networkName2 := test.RandomName(prefix) - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateRemoteNetworks2(networkName1, networkName2, prefix), - Check: acctests.ComposeTestCheckFunc( - testCheckOutputLength("test_networks", 2), - ), - }, + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateRemoteNetworks2(networkName1, networkName2, prefix), + Check: acctests.ComposeTestCheckFunc( + testCheckOutputLength("test_networks", 2), + ), }, - }) + }, }) } func testDatasourceTwingateRemoteNetworks2(networkName1, networkName2, prefix string) string { - return fmt.Sprintf(` + return acctests.Nprintf(` resource "twingate_remote_network" "test_drn1" { - name = "%s" + name = "${name_1}" } resource "twingate_remote_network" "test_drn2" { - name = "%s" + name = "${name_2}" } data "twingate_remote_networks" "all" { @@ -49,7 +46,12 @@ func testDatasourceTwingateRemoteNetworks2(networkName1, networkName2, prefix st } output "test_networks" { - value = [for n in [for net in data.twingate_remote_networks.all : net if can(net.*.name)][0] : n if length(regexall("%s.*", n.name)) > 0] + value = [for n in [for net in data.twingate_remote_networks.all : net if can(net.*.name)][0] : n if length(regexall("${prefix}.*", n.name)) > 0] } - `, networkName1, networkName2, prefix) + `, + map[string]any{ + "name_1": networkName1, + "name_2": networkName2, + "prefix": prefix, + }) } diff --git a/twingate/internal/test/acctests/datasource/resource_test.go b/twingate/internal/test/acctests/datasource/resource_test.go index 2dd67132..aa5cb9f1 100644 --- a/twingate/internal/test/acctests/datasource/resource_test.go +++ b/twingate/internal/test/acctests/datasource/resource_test.go @@ -14,34 +14,34 @@ import ( ) func TestAccDatasourceTwingateResource_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Resource Basic", func(t *testing.T) { - networkName := test.RandomName() - resourceName := test.RandomResourceName() + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateResourceDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateResource(networkName, resourceName), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.twingate_resource.out_dr1", attr.Name, resourceName), - ), - }, + networkName := test.RandomName() + resourceName := test.RandomResourceName() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateResourceDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateResource(networkName, resourceName), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.twingate_resource.out_dr1", attr.Name, resourceName), + ), }, - }) + }, }) } func testDatasourceTwingateResource(networkName, resourceName string) string { - return fmt.Sprintf(` + return acctests.Nprintf(` resource "twingate_remote_network" "test_dr1" { - name = "%s" + name = "${network_name}" } resource "twingate_resource" "test_dr1" { - name = "%s" + name = "${resource_name}" address = "acc-test.com" remote_network_id = twingate_remote_network.test_dr1.id protocols { @@ -60,25 +60,28 @@ func testDatasourceTwingateResource(networkName, resourceName string) string { data "twingate_resource" "out_dr1" { id = twingate_resource.test_dr1.id } - `, networkName, resourceName) + `, map[string]any{ + "network_name": networkName, + "resource_name": resourceName, + }) } func TestAccDatasourceTwingateResource_negative(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Resource - does not exists", func(t *testing.T) { - resourceID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Resource:%d", acctest.RandInt()))) + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateResourceDoesNotExists(resourceID), - ExpectError: regexp.MustCompile("failed to read resource with id"), - }, + resourceID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("Resource:%d", acctest.RandInt()))) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateResourceDoesNotExists(resourceID), + ExpectError: regexp.MustCompile("failed to read resource with id"), }, - }) + }, }) } @@ -95,20 +98,19 @@ func testTwingateResourceDoesNotExists(id string) string { } func TestAccDatasourceTwingateResource_invalidID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Resource - failed parse resource ID", func(t *testing.T) { - networkID := acctest.RandString(10) + t.Parallel() + networkID := acctest.RandString(10) - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateResourceDoesNotExists(networkID), - ExpectError: regexp.MustCompile("failed to read resource with id"), - }, + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateResourceDoesNotExists(networkID), + ExpectError: regexp.MustCompile("failed to read resource with id"), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/resources_test.go b/twingate/internal/test/acctests/datasource/resources_test.go index ea031429..3c0bcb50 100644 --- a/twingate/internal/test/acctests/datasource/resources_test.go +++ b/twingate/internal/test/acctests/datasource/resources_test.go @@ -16,37 +16,35 @@ var ( ) func TestAccDatasourceTwingateResources_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Resources Basic", func(t *testing.T) { - acctests.SetPageLimit(1) - networkName := test.RandomName() - resourceName := test.RandomResourceName() - const theDatasource = "data.twingate_resources.out_drs1" + acctests.SetPageLimit(t, 1) + networkName := test.RandomName() + resourceName := test.RandomResourceName() + const theDatasource = "data.twingate_resources.out_drs1" - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateResourceDestroy, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateResources(networkName, resourceName), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(theDatasource, resourcesLen, "2"), - resource.TestCheckResourceAttr(theDatasource, resourceNamePath, resourceName), - ), - }, + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateResourceDestroy, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateResources(networkName, resourceName), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(theDatasource, resourcesLen, "2"), + resource.TestCheckResourceAttr(theDatasource, resourceNamePath, resourceName), + ), }, - }) + }, }) } func testDatasourceTwingateResources(networkName, resourceName string) string { - return fmt.Sprintf(` + return acctests.Nprintf(` resource "twingate_remote_network" "test_drs1" { - name = "%s" + name = "${network_name}" } resource "twingate_resource" "test_drs1_1" { - name = "%s" + name = "${resource_name}" address = "acc-test.com" remote_network_id = twingate_remote_network.test_drs1.id protocols { @@ -63,7 +61,7 @@ func testDatasourceTwingateResources(networkName, resourceName string) string { } resource "twingate_resource" "test_drs1_2" { - name = "%s" + name = "${resource_name}" address = "acc-test.com" remote_network_id = twingate_remote_network.test_drs1.id protocols { @@ -80,31 +78,35 @@ func testDatasourceTwingateResources(networkName, resourceName string) string { } data "twingate_resources" "out_drs1" { - name = "%s" + name = "${resource_name}" depends_on = [twingate_resource.test_drs1_1, twingate_resource.test_drs1_2] } - `, networkName, resourceName, resourceName, resourceName) + `, + map[string]any{ + "network_name": networkName, + "resource_name": resourceName, + }) } func TestAccDatasourceTwingateResources_emptyResult(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Resources - empty result", func(t *testing.T) { - resourceName := test.RandomResourceName() + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateResourcesDoesNotExists(resourceName), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.twingate_resources.out_drs2", resourcesLen, "0"), - ), - }, + resourceName := test.RandomResourceName() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateResourcesDoesNotExists(resourceName), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.twingate_resources.out_drs2", resourcesLen, "0"), + ), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/security-policies_test.go b/twingate/internal/test/acctests/datasource/security-policies_test.go index 8f4e1a0e..2342cbac 100644 --- a/twingate/internal/test/acctests/datasource/security-policies_test.go +++ b/twingate/internal/test/acctests/datasource/security-policies_test.go @@ -10,26 +10,24 @@ import ( ) func TestAccDatasourceTwingateSecurityPoliciesBasic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Security Policies - basic", func(t *testing.T) { - acctests.SetPageLimit(1) + acctests.SetPageLimit(t, 1) - securityPolicies, err := acctests.ListSecurityPolicies() - if err != nil { - t.Skip("can't run test:", err) - } + securityPolicies, err := acctests.ListSecurityPolicies() + if err != nil { + t.Skip("can't run test:", err) + } - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - Steps: []sdk.TestStep{ - { - Config: testDatasourceTwingateSecurityPolicies(), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr("data.twingate_security_policies.all", attr.Len(attr.SecurityPolicies), fmt.Sprintf("%d", len(securityPolicies))), - ), - }, + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + Steps: []sdk.TestStep{ + { + Config: testDatasourceTwingateSecurityPolicies(), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr("data.twingate_security_policies.all", attr.Len(attr.SecurityPolicies), fmt.Sprintf("%d", len(securityPolicies))), + ), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/security-policy_test.go b/twingate/internal/test/acctests/datasource/security-policy_test.go index 6d4298eb..8bb1ae82 100644 --- a/twingate/internal/test/acctests/datasource/security-policy_test.go +++ b/twingate/internal/test/acctests/datasource/security-policy_test.go @@ -12,21 +12,21 @@ import ( ) func TestAccDatasourceTwingateSecurityPolicyInvalidID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Security Policy - failed parse ID", func(t *testing.T) { - randStr := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateSecurityPolicy(randStr), - ExpectError: regexp.MustCompile("failed to read security policy"), - }, + t.Parallel() + + randStr := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateSecurityPolicy(randStr), + ExpectError: regexp.MustCompile("failed to read security policy"), }, - }) + }, }) } @@ -43,21 +43,21 @@ func testDatasourceTwingateSecurityPolicy(id string) string { } func TestAccDatasourceTwingateSecurityPolicyReadWithNameAndID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Security Policy - read with name and id", func(t *testing.T) { - randStr := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateSecurityPolicyWithNameAndID(randStr, randStr), - ExpectError: regexp.MustCompile("Error: Invalid Attribute Combination"), - }, + t.Parallel() + + randStr := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateSecurityPolicyWithNameAndID(randStr, randStr), + ExpectError: regexp.MustCompile("Error: Invalid Attribute Combination"), }, - }) + }, }) } @@ -75,47 +75,47 @@ func testDatasourceTwingateSecurityPolicyWithNameAndID(id, name string) string { } func TestAccDatasourceTwingateSecurityPolicyDoesNotExists(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Security Policy - does not exists", func(t *testing.T) { - securityPolicyID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("SecurityPolicy:%d", acctest.RandInt()))) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateSecurityPolicy(securityPolicyID), - ExpectError: regexp.MustCompile("failed to read security policy with id"), - }, + t.Parallel() + + securityPolicyID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("SecurityPolicy:%d", acctest.RandInt()))) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateSecurityPolicy(securityPolicyID), + ExpectError: regexp.MustCompile("failed to read security policy with id"), }, - }) + }, }) } func TestAccDatasourceTwingateSecurityPolicyReadOkByID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Security Policy - read Ok By ID", func(t *testing.T) { - securityPolicies, err := acctests.ListSecurityPolicies() - if err != nil { - t.Skip("can't run test:", err) - } - - testPolicy := securityPolicies[0] - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateSecurityPolicyByID(testPolicy.ID), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckOutput("security_policy_name", testPolicy.Name), - ), - }, + t.Parallel() + + securityPolicies, err := acctests.ListSecurityPolicies() + if err != nil { + t.Skip("can't run test:", err) + } + + testPolicy := securityPolicies[0] + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateSecurityPolicyByID(testPolicy.ID), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckOutput("security_policy_name", testPolicy.Name), + ), }, - }) + }, }) } @@ -132,28 +132,28 @@ func testDatasourceTwingateSecurityPolicyByID(id string) string { } func TestAccDatasourceTwingateSecurityPolicyReadOkByName(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Security Policy - read Ok By Name", func(t *testing.T) { - securityPolicies, err := acctests.ListSecurityPolicies() - if err != nil { - t.Skip("can't run test:", err) - } - - testPolicy := securityPolicies[0] - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateSecurityPolicyByName(testPolicy.Name), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckOutput("security_policy_id", testPolicy.ID), - ), - }, + t.Parallel() + + securityPolicies, err := acctests.ListSecurityPolicies() + if err != nil { + t.Skip("can't run test:", err) + } + + testPolicy := securityPolicies[0] + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateSecurityPolicyByName(testPolicy.Name), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckOutput("security_policy_id", testPolicy.ID), + ), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/service-accounts_test.go b/twingate/internal/test/acctests/datasource/service-accounts_test.go index e89b4711..092e8d0a 100644 --- a/twingate/internal/test/acctests/datasource/service-accounts_test.go +++ b/twingate/internal/test/acctests/datasource/service-accounts_test.go @@ -19,105 +19,103 @@ var ( ) func TestAccDatasourceTwingateServicesFilterByName(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Services - Filter By Name", func(t *testing.T) { + t.Parallel() - name := test.Prefix("orange") - const ( - terraformResourceName = "dts_service" - theDatasource = "data.twingate_service_accounts.out" - ) + name := test.Prefix("orange") + const ( + terraformResourceName = "dts_service" + theDatasource = "data.twingate_service_accounts.out" + ) - config := []terraformServiceConfig{ - { - serviceName: name, - terraformResourceName: test.TerraformRandName(terraformResourceName), - }, + config := []terraformServiceConfig{ + { + serviceName: name, + terraformResourceName: test.TerraformRandName(terraformResourceName), + }, + { + serviceName: test.Prefix("lemon"), + terraformResourceName: test.TerraformRandName(terraformResourceName), + }, + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []resource.TestStep{ { - serviceName: test.Prefix("lemon"), - terraformResourceName: test.TerraformRandName(terraformResourceName), + Config: terraformConfig( + createServices(config), + datasourceServices(name, config), + ), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(theDatasource, serviceAccountsLen, "1"), + resource.TestCheckResourceAttr(theDatasource, keyIDsLen, "1"), + resource.TestCheckResourceAttr(theDatasource, attr.ID, "service-by-name-"+name), + ), }, - } - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []resource.TestStep{ - { - Config: terraformConfig( - createServices(config), - datasourceServices(name, config), - ), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(theDatasource, serviceAccountsLen, "1"), - resource.TestCheckResourceAttr(theDatasource, keyIDsLen, "1"), - resource.TestCheckResourceAttr(theDatasource, attr.ID, "service-by-name-"+name), - ), - }, - }, - }) + }, }) } func TestAccDatasourceTwingateServicesAll(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Services - All", func(t *testing.T) { - prefix := test.Prefix() + acctest.RandString(4) - const ( - terraformResourceName = "dts_service" - theDatasource = "data.twingate_service_accounts.out" - ) - - config := []terraformServiceConfig{ + t.Parallel() + + prefix := test.Prefix() + acctest.RandString(4) + const ( + terraformResourceName = "dts_service" + theDatasource = "data.twingate_service_accounts.out" + ) + + config := []terraformServiceConfig{ + { + serviceName: prefix + "_orange", + terraformResourceName: test.TerraformRandName(terraformResourceName), + }, + { + serviceName: prefix + "_lemon", + terraformResourceName: test.TerraformRandName(terraformResourceName), + }, + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []resource.TestStep{ { - serviceName: prefix + "_orange", - terraformResourceName: test.TerraformRandName(terraformResourceName), + Config: filterDatasourceServices(prefix, config), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(theDatasource, attr.ID, "all-services"), + ), }, { - serviceName: prefix + "_lemon", - terraformResourceName: test.TerraformRandName(terraformResourceName), - }, - } - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []resource.TestStep{ - { - Config: filterDatasourceServices(prefix, config), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(theDatasource, attr.ID, "all-services"), - ), - }, - { - Config: filterDatasourceServices(prefix, config), - Check: acctests.ComposeTestCheckFunc( - testCheckOutputLength("my_services", 2), - ), - }, + Config: filterDatasourceServices(prefix, config), + Check: acctests.ComposeTestCheckFunc( + testCheckOutputLength("my_services", 2), + ), }, - }) + }, }) } func TestAccDatasourceTwingateServicesEmptyResult(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Services - Empty Result", func(t *testing.T) { - - const theDatasource = "data.twingate_service_accounts.out" - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []resource.TestStep{ - { - Config: datasourceServices(test.RandomName(), nil), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(theDatasource, serviceAccountsLen, "0"), - ), - }, + t.Parallel() + + const theDatasource = "data.twingate_service_accounts.out" + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []resource.TestStep{ + { + Config: datasourceServices(test.RandomName(), nil), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(theDatasource, serviceAccountsLen, "0"), + ), }, - }) + }, }) } @@ -164,152 +162,152 @@ func getTerraformServiceKeys(configs []terraformServiceConfig) string { ) } -func createServiceKey(terraformResourceName, serviceName string) string { - return fmt.Sprintf(` - %s +func createServiceKey(resourceName, serviceName string) string { + return acctests.Nprintf(` + ${service_account} - resource "twingate_service_account_key" "%s" { - service_account_id = twingate_service_account.%s.id + resource "twingate_service_account_key" "${service_account_key_resource}" { + service_account_id = twingate_service_account.${service_account_resource}.id } - `, createServiceAccount(terraformResourceName, serviceName), terraformResourceName, terraformResourceName) + `, + map[string]any{ + "service_account": createServiceAccount(resourceName, serviceName), + "service_account_key_resource": resourceName, + "service_account_resource": resourceName, + }) } -func createServiceAccount(terraformResourceName, serviceName string) string { - return fmt.Sprintf(` - resource "twingate_service_account" "%s" { - name = "%s" +func createServiceAccount(resourceName, serviceName string) string { + return acctests.Nprintf(` + resource "twingate_service_account" "${resource_name}" { + name = "${name}" } - `, terraformResourceName, serviceName) + `, + map[string]any{ + "resource_name": resourceName, + "name": serviceName, + }) } func filterDatasourceServices(prefix string, configs []terraformServiceConfig) string { - return fmt.Sprintf(` - %s + return acctests.Nprintf(` + ${services} data "twingate_service_accounts" "out" { } output "my_services" { - value = [for c in data.twingate_service_accounts.out.service_accounts : c if length(regexall("^%s", c.name)) > 0] + value = [for c in data.twingate_service_accounts.out.service_accounts : c if length(regexall("^${prefix}", c.name)) > 0] } - `, createServices(configs), prefix) + `, + map[string]any{ + "services": createServices(configs), + "prefix": prefix, + }) } func TestAccDatasourceTwingateServicesAllCursors(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Services - All Cursors", func(t *testing.T) { - acctests.SetPageLimit(1) - prefix := test.Prefix() + acctest.RandString(4) - const ( - theDatasource = "data.twingate_service_accounts.out" - ) - - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []resource.TestStep{ - { - Config: datasourceServicesConfig(prefix), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(theDatasource, attr.ID, "all-services"), - ), - }, - { - Config: datasourceServicesConfig(prefix), - Check: acctests.ComposeTestCheckFunc( - testCheckOutputLength("my_services", 3), - testCheckOutputNestedLen("my_services", 0, attr.ResourceIDs, 1), - testCheckOutputNestedLen("my_services", 0, attr.KeyIDs, 2), - ), - }, + acctests.SetPageLimit(t, 1) + prefix := test.Prefix() + acctest.RandString(4) + const theDatasource = "data.twingate_service_accounts.out" + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []resource.TestStep{ + { + Config: datasourceServicesConfig(prefix), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(theDatasource, attr.ID, "all-services"), + ), }, - }) + { + Config: datasourceServicesConfig(prefix), + Check: acctests.ComposeTestCheckFunc( + testCheckOutputLength("my_services", 3), + testCheckOutputNestedLen("my_services", 0, attr.ResourceIDs, 1), + testCheckOutputNestedLen("my_services", 0, attr.KeyIDs, 2), + ), + }, + }, }) } func datasourceServicesConfig(prefix string) string { - return fmt.Sprintf(` - resource "twingate_service_account" "%s_1" { - name = "%s-1" + return acctests.Nprintf(` + resource "twingate_service_account" "${prefix}_1" { + name = "${prefix}-1" } - resource "twingate_service_account" "%s_2" { - name = "%s-2" + resource "twingate_service_account" "${prefix}_2" { + name = "${prefix}-2" } - resource "twingate_service_account" "%s_3" { - name = "%s-3" + resource "twingate_service_account" "${prefix}_3" { + name = "${prefix}-3" } - resource "twingate_remote_network" "%s_1" { - name = "%s-1" + resource "twingate_remote_network" "${prefix}_1" { + name = "${prefix}-1" } - resource "twingate_remote_network" "%s_2" { - name = "%s-2" + resource "twingate_remote_network" "${prefix}_2" { + name = "${prefix}-2" } - resource "twingate_resource" "%s_1" { - name = "%s-1" + resource "twingate_resource" "${prefix}_1" { + name = "${prefix}-1" address = "acc-test.com" - remote_network_id = twingate_remote_network.%s_1.id + remote_network_id = twingate_remote_network.${prefix}_1.id access { - service_account_ids = [twingate_service_account.%s_1.id, twingate_service_account.%s_2.id] + service_account_ids = [twingate_service_account.${prefix}_1.id, twingate_service_account.${prefix}_2.id] } } - resource "twingate_resource" "%s_2" { - name = "%s-2" + resource "twingate_resource" "${prefix}_2" { + name = "${prefix}-2" address = "acc-test.com" - remote_network_id = twingate_remote_network.%s_2.id + remote_network_id = twingate_remote_network.${prefix}_2.id access { - service_account_ids = [twingate_service_account.%s_3.id] + service_account_ids = [twingate_service_account.${prefix}_3.id] } } - resource "twingate_service_account_key" "%s_1_1" { - service_account_id = twingate_service_account.%s_1.id + resource "twingate_service_account_key" "${prefix}_1_1" { + service_account_id = twingate_service_account.${prefix}_1.id } - resource "twingate_service_account_key" "%s_1_2" { - service_account_id = twingate_service_account.%s_1.id + resource "twingate_service_account_key" "${prefix}_1_2" { + service_account_id = twingate_service_account.${prefix}_1.id } - resource "twingate_service_account_key" "%s_2_1" { - service_account_id = twingate_service_account.%s_2.id + resource "twingate_service_account_key" "${prefix}_2_1" { + service_account_id = twingate_service_account.${prefix}_2.id } - resource "twingate_service_account_key" "%s_2_2" { - service_account_id = twingate_service_account.%s_2.id + resource "twingate_service_account_key" "${prefix}_2_2" { + service_account_id = twingate_service_account.${prefix}_2.id } - resource "twingate_service_account_key" "%s_3_1" { - service_account_id = twingate_service_account.%s_3.id + resource "twingate_service_account_key" "${prefix}_3_1" { + service_account_id = twingate_service_account.${prefix}_3.id } - resource "twingate_service_account_key" "%s_3_2" { - service_account_id = twingate_service_account.%s_3.id + resource "twingate_service_account_key" "${prefix}_3_2" { + service_account_id = twingate_service_account.${prefix}_3.id } data "twingate_service_accounts" "out" { - depends_on = [twingate_resource.%s_1, twingate_resource.%s_2] + depends_on = [twingate_resource.${prefix}_1, twingate_resource.${prefix}_2] } output "my_services" { - value = [for c in data.twingate_service_accounts.out.service_accounts : c if length(regexall("^%s", c.name)) > 0] + value = [for c in data.twingate_service_accounts.out.service_accounts : c if length(regexall("^${prefix}", c.name)) > 0] depends_on = [data.twingate_service_accounts.out] } -`, duplicate(prefix, 34)...) -} - -func duplicate(val string, n int) []any { - result := make([]any, 0, n) - for i := 0; i < n; i++ { - result = append(result, val) - } - - return result +`, map[string]any{"prefix": prefix}) } diff --git a/twingate/internal/test/acctests/datasource/user_test.go b/twingate/internal/test/acctests/datasource/user_test.go index 64688942..bf555e5d 100644 --- a/twingate/internal/test/acctests/datasource/user_test.go +++ b/twingate/internal/test/acctests/datasource/user_test.go @@ -12,24 +12,24 @@ import ( ) func TestAccDatasourceTwingateUser_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc User Basic", func(t *testing.T) { - user, err := acctests.GetTestUser() - if err != nil { - t.Skip("can't run test:", err) - } + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateUser(user.ID), - Check: acctests.ComposeTestCheckFunc( - resource.TestCheckOutput("my_user_email_du1", user.Email), - ), - }, + user, err := acctests.GetTestUser() + if err != nil { + t.Skip("can't run test:", err) + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateUser(user.ID), + Check: acctests.ComposeTestCheckFunc( + resource.TestCheckOutput("my_user_email_du1", user.Email), + ), }, - }) + }, }) } @@ -46,21 +46,21 @@ func testDatasourceTwingateUser(userID string) string { } func TestAccDatasourceTwingateUser_negative(t *testing.T) { - t.Run("Test Twingate Datasource : Acc User - does not exists", func(t *testing.T) { - userID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("User:%d", acctest.RandInt()))) + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateUserDoesNotExists(userID), - ExpectError: regexp.MustCompile("failed to read user with id"), - }, + userID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("User:%d", acctest.RandInt()))) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateUserDoesNotExists(userID), + ExpectError: regexp.MustCompile("failed to read user with id"), }, - }) + }, }) } @@ -73,20 +73,20 @@ func testTwingateUserDoesNotExists(id string) string { } func TestAccDatasourceTwingateUser_invalidID(t *testing.T) { - t.Run("Test Twingate Datasource : Acc User - failed parse ID", func(t *testing.T) { - userID := acctest.RandString(10) + t.Parallel() - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { - acctests.PreCheck(t) - }, - Steps: []resource.TestStep{ - { - Config: testTwingateUserDoesNotExists(userID), - ExpectError: regexp.MustCompile("failed to read user with id"), - }, + userID := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { + acctests.PreCheck(t) + }, + Steps: []resource.TestStep{ + { + Config: testTwingateUserDoesNotExists(userID), + ExpectError: regexp.MustCompile("failed to read user with id"), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/datasource/users_test.go b/twingate/internal/test/acctests/datasource/users_test.go index 5fa53c5a..6521810f 100644 --- a/twingate/internal/test/acctests/datasource/users_test.go +++ b/twingate/internal/test/acctests/datasource/users_test.go @@ -11,20 +11,18 @@ import ( ) func TestAccDatasourceTwingateUsers_basic(t *testing.T) { - t.Run("Test Twingate Datasource : Acc Users Basic", func(t *testing.T) { - acctests.SetPageLimit(1) - resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - Steps: []resource.TestStep{ - { - Config: testDatasourceTwingateUsers(), - Check: acctests.ComposeTestCheckFunc( - testCheckResourceAttrNotEqual("data.twingate_users.all", attr.Len(attr.Users), "0"), - ), - }, + acctests.SetPageLimit(t, 1) + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: testDatasourceTwingateUsers(), + Check: acctests.ComposeTestCheckFunc( + testCheckResourceAttrNotEqual("data.twingate_users.all", attr.Len(attr.Users), "0"), + ), }, - }) + }, }) } diff --git a/twingate/internal/test/acctests/helper.go b/twingate/internal/test/acctests/helper.go index b3eef07c..799a63e7 100644 --- a/twingate/internal/test/acctests/helper.go +++ b/twingate/internal/test/acctests/helper.go @@ -48,6 +48,18 @@ func ErrUsersLenMismatch(expected, actual int) error { return fmt.Errorf("expected %d users, actual - %d", expected, actual) //nolint } +// Nprintf - this is a Printf sibling (Nprintf; Named Printf), which handles strings like +// Nprintf("Hello %{target}!", map[string]interface{}{"target":"world"}) == "Hello world!". +// This is particularly useful for generated tests, where we don't want to use Printf, +// since that would require us to generate a very particular ordering of arguments. +func Nprintf(format string, params map[string]interface{}) string { + for key, val := range params { + format = strings.ReplaceAll(format, "${"+key+"}", fmt.Sprintf("%v", val)) + } + + return format +} + var providerClient = func() *client.Client { //nolint client, err := test.TwingateClient() if err != nil { @@ -80,10 +92,10 @@ var ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ //n }, } -func SetPageLimit(limit int) { - if err := os.Setenv(client.EnvPageLimit, strconv.Itoa(limit)); err != nil { - log.Fatal("failed to set page limit", err) - } +// SetPageLimit - changes page limit, can't be uses in parallel tests. +func SetPageLimit(t *testing.T, limit int) { + t.Helper() + t.Setenv(client.EnvPageLimit, strconv.Itoa(limit)) } const WaitDuration = 500 * time.Millisecond diff --git a/twingate/internal/test/acctests/resource/connector-tokens_test.go b/twingate/internal/test/acctests/resource/connector-tokens_test.go index e70ba415..e0b4bcc8 100644 --- a/twingate/internal/test/acctests/resource/connector-tokens_test.go +++ b/twingate/internal/test/acctests/resource/connector-tokens_test.go @@ -12,46 +12,49 @@ import ( ) func TestAccRemoteConnectorWithTokens(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector With Tokens", func(t *testing.T) { - const terraformResourceName = "test_t1" - theResource := acctests.TerraformConnectorTokens(terraformResourceName) - remoteNetworkName := test.RandomName() + t.Parallel() - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorTokensInvalidated, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnectorTokens(terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorTokensSet(theResource), - ), - }, + connectorName := test.RandomConnectorName() + theResource := acctests.TerraformConnectorTokens(connectorName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorTokensInvalidated, + Steps: []sdk.TestStep{ + { + Config: configConnectorTokens(connectorName, test.RandomName()), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorTokensSet(theResource), + ), }, - }) + }, }) } -func terraformResourceTwingateConnectorTokens(terraformResourceName, remoteNetworkName string) string { - return fmt.Sprintf(` - %s +func configConnectorTokens(terraformResource, networkName string) string { + return acctests.Nprintf(` + ${connector} - resource "twingate_connector_tokens" "%s" { - connector_id = twingate_connector.%s.id + resource "twingate_connector_tokens" "${connector_token_resource}" { + connector_id = twingate_connector.${connector_resource}.id keepers = { foo = "bar" } } - `, terraformResourceTwingateConnector(terraformResourceName, terraformResourceName, remoteNetworkName), terraformResourceName, terraformResourceName) + `, map[string]any{ + "connector": configConnector(terraformResource, terraformResource, networkName), + "connector_token_resource": terraformResource, + "connector_resource": terraformResource, + }) } -func checkTwingateConnectorTokensSet(connectorNameTokens string) sdk.TestCheckFunc { +func checkTwingateConnectorTokensSet(resourceName string) sdk.TestCheckFunc { return func(s *terraform.State) error { - connectorTokens, ok := s.RootModule().Resources[connectorNameTokens] + connectorTokens, ok := s.RootModule().Resources[resourceName] if !ok { - return fmt.Errorf("not found: %s", connectorNameTokens) + return fmt.Errorf("not found: %s", resourceName) } if connectorTokens.Primary.ID == "" { diff --git a/twingate/internal/test/acctests/resource/connector_test.go b/twingate/internal/test/acctests/resource/connector_test.go index 3b2e1acf..229e05aa 100644 --- a/twingate/internal/test/acctests/resource/connector_test.go +++ b/twingate/internal/test/acctests/resource/connector_test.go @@ -13,164 +13,169 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) -var testRegexp = regexp.MustCompile(test.Prefix() + ".*") - func TestAccRemoteConnectorCreate(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector", func(t *testing.T) { - const terraformResourceName = "test_c1" - theResource := acctests.TerraformConnector(terraformResourceName) - remoteNetworkName := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnector(terraformResourceName, terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformResourceName)), - sdk.TestCheckResourceAttrSet(theResource, attr.Name), - ), - }, + t.Parallel() + + connectorName := test.RandomConnectorName() + theResource := acctests.TerraformConnector(connectorName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configConnector(connectorName, connectorName, test.RandomName()), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + sdk.TestCheckResourceAttrSet(theResource, attr.Name), + ), }, - }) + }, }) } func TestAccRemoteConnectorWithCustomName(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector With Custom Name", func(t *testing.T) { - const terraformResourceName = "test_c2" - theResource := acctests.TerraformConnector(terraformResourceName) - remoteNetworkName := test.RandomName() - connectorName := test.RandomConnectorName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnectorWithName(terraformResourceName, remoteNetworkName, connectorName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformResourceName)), - sdk.TestMatchResourceAttr(theResource, attr.Name, regexp.MustCompile(connectorName)), - ), - }, + t.Parallel() + + connectorName := test.RandomConnectorName() + theResource := acctests.TerraformConnector(connectorName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configConnectorWithName(connectorName, test.RandomName(), connectorName), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + sdk.TestCheckResourceAttr(theResource, attr.Name, connectorName), + ), }, - }) + }, }) } func TestAccRemoteConnectorImport(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector - Import", func(t *testing.T) { - const terraformResourceName = "test_c3" - theResource := acctests.TerraformConnector(terraformResourceName) - remoteNetworkName := test.RandomName() - connectorName := test.RandomConnectorName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnectorWithName(terraformResourceName, remoteNetworkName, connectorName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformResourceName)), - sdk.TestMatchResourceAttr(theResource, attr.Name, testRegexp), - ), - }, - { - ResourceName: theResource, - ImportState: true, - ImportStateVerify: true, - }, + t.Parallel() + + connectorName := test.RandomConnectorName() + theResource := acctests.TerraformConnector(connectorName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configConnectorWithName(connectorName, test.RandomName(), connectorName), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + sdk.TestCheckResourceAttr(theResource, attr.Name, connectorName), + ), }, - }) + { + ImportState: true, + ImportStateVerify: true, + ResourceName: theResource, + ImportStateCheck: acctests.CheckImportState(map[string]string{ + attr.Name: connectorName, + }), + }, + }, }) } func TestAccRemoteConnectorNotAllowedToChangeRemoteNetworkId(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector - should fail on remote_network_id update", func(t *testing.T) { - const ( - terraformConnectorName = "test_c4" - terraformRemoteNetworkName1 = "test_c4_1" - terraformRemoteNetworkName2 = "test_c4_2" - ) - theResource := acctests.TerraformConnector(terraformConnectorName) - remoteNetworkName1 := test.RandomName() - remoteNetworkName2 := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnector(terraformRemoteNetworkName1, terraformConnectorName, remoteNetworkName1), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformRemoteNetworkName1)), - ), - }, - { - Config: terraformResourceTwingateConnector(terraformRemoteNetworkName2, terraformConnectorName, remoteNetworkName2), - ExpectError: regexp.MustCompile(resource.ErrNotAllowChangeRemoteNetworkID.Error()), - }, + t.Parallel() + + terraformConnectorName := test.RandomConnectorName() + terraformRemoteNetworkName1 := test.RandomNetworkName() + terraformRemoteNetworkName2 := test.RandomNetworkName() + theResource := acctests.TerraformConnector(terraformConnectorName) + remoteNetworkName1 := test.RandomName() + remoteNetworkName2 := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configConnector(terraformRemoteNetworkName1, terraformConnectorName, remoteNetworkName1), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformRemoteNetworkName1)), + ), }, - }) + { + Config: configConnector(terraformRemoteNetworkName2, terraformConnectorName, remoteNetworkName2), + ExpectError: regexp.MustCompile(resource.ErrNotAllowChangeRemoteNetworkID.Error()), + }, + }, }) } func TestAccTwingateConnectorReCreateAfterDeletion(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector ReCreate After Deletion", func(t *testing.T) { - const terraformResourceName = "test_c5" - theResource := acctests.TerraformConnector(terraformResourceName) - remoteNetworkName := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnector(terraformResourceName, terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformResourceName)), - acctests.DeleteTwingateResource(theResource, resource.TwingateConnector), - ), - ExpectNonEmptyPlan: true, - }, - { - Config: terraformResourceTwingateConnector(terraformResourceName, terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformResourceName)), - ), - }, + t.Parallel() + + connectorName := test.RandomConnectorName() + theResource := acctests.TerraformConnector(connectorName) + networkName := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configConnector(connectorName, connectorName, networkName), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + acctests.DeleteTwingateResource(theResource, resource.TwingateConnector), + ), + ExpectNonEmptyPlan: true, }, - }) + { + Config: configConnector(connectorName, connectorName, networkName), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + ), + }, + }, }) } -func terraformResourceTwingateConnector(terraformRemoteNetworkName, terraformConnectorName, remoteNetworkName string) string { - return fmt.Sprintf(` - %s +func configConnector(networkTR, connectorTR, networkName string) string { + return acctests.Nprintf(` + ${remote_network} - resource "twingate_connector" "%s" { - remote_network_id = twingate_remote_network.%s.id + resource "twingate_connector" "${connector_resource}" { + remote_network_id = twingate_remote_network.${remote_network_resource}.id } - `, terraformResourceRemoteNetwork(terraformRemoteNetworkName, remoteNetworkName), terraformConnectorName, terraformRemoteNetworkName) + `, + map[string]any{ + "remote_network": configRemoteNetwork(networkTR, networkName), + "connector_resource": connectorTR, + "remote_network_resource": networkTR, + }) } -func terraformResourceTwingateConnectorWithName(terraformResourceName, remoteNetworkName, connectorName string) string { - return fmt.Sprintf(` - %s +func configConnectorWithName(terraformResource, networkName, connectorName string) string { + return acctests.Nprintf(` + ${remote_network} - resource "twingate_connector" "%s" { - remote_network_id = twingate_remote_network.%s.id - name = "%s" + resource "twingate_connector" "${connector_resource}" { + remote_network_id = twingate_remote_network.${remote_network_resource}.id + name = "${connector_name}" } - `, terraformResourceRemoteNetwork(terraformResourceName, remoteNetworkName), terraformResourceName, terraformResourceName, connectorName) + `, + map[string]any{ + "remote_network": configRemoteNetwork(terraformResource, networkName), + "connector_resource": terraformResource, + "remote_network_resource": terraformResource, + "connector_name": connectorName, + }) } func checkTwingateConnectorSetWithRemoteNetwork(connectorResource, remoteNetworkResource string) sdk.TestCheckFunc { @@ -198,87 +203,94 @@ func checkTwingateConnectorSetWithRemoteNetwork(connectorResource, remoteNetwork } func TestAccRemoteConnectorUpdateName(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector Update Name", func(t *testing.T) { - const terraformResourceName = "test_c6" - theResource := acctests.TerraformConnector(terraformResourceName) - remoteNetworkName := test.RandomName() - connectorName := test.RandomConnectorName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnector(terraformResourceName, terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformResourceName)), - sdk.TestCheckResourceAttrSet(theResource, attr.Name), - ), - }, - { - Config: terraformResourceTwingateConnectorWithName(terraformResourceName, remoteNetworkName, connectorName), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, attr.Name, connectorName), - ), - }, + t.Parallel() + + connectorName := test.RandomConnectorName() + theResource := acctests.TerraformConnector(connectorName) + networkName := test.RandomName() + connectorNewName := test.RandomConnectorName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configConnector(connectorName, connectorName, networkName), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + sdk.TestCheckResourceAttrSet(theResource, attr.Name), + ), }, - }) + { + Config: configConnectorWithName(connectorName, networkName, connectorNewName), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, attr.Name, connectorNewName), + ), + }, + }, }) } func TestAccRemoteConnectorCreateWithNotificationStatus(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Connector With Notification Status", func(t *testing.T) { - const terraformResourceName = "test_c7" - theResource := acctests.TerraformConnector(terraformResourceName) - remoteNetworkName := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateConnector(terraformResourceName, terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(terraformResourceName)), - sdk.TestCheckResourceAttrSet(theResource, attr.Name), - ), - }, - { - // expecting no changes, as by default notifications enabled - PlanOnly: true, - Config: terraformResourceTwingateConnectorWithNotificationStatus(terraformResourceName, terraformResourceName, remoteNetworkName, true), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "true"), - ), - }, - { - Config: terraformResourceTwingateConnectorWithNotificationStatus(terraformResourceName, terraformResourceName, remoteNetworkName, false), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "false"), - ), - }, - { - // expecting no changes, when user removes `status_updates_enabled` field from terraform - PlanOnly: true, - Config: terraformResourceTwingateConnector(terraformResourceName, terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "false"), - ), - }, + t.Parallel() + + connectorName := test.RandomConnectorName() + theResource := acctests.TerraformConnector(connectorName) + networkName := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateConnectorAndRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configConnector(connectorName, connectorName, networkName), + Check: acctests.ComposeTestCheckFunc( + checkTwingateConnectorSetWithRemoteNetwork(theResource, acctests.TerraformRemoteNetwork(connectorName)), + sdk.TestCheckResourceAttrSet(theResource, attr.Name), + ), }, - }) + { + // expecting no changes, as by default notifications enabled + PlanOnly: true, + Config: configConnectorWithNotificationStatus(connectorName, connectorName, networkName, true), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "true"), + ), + }, + { + Config: configConnectorWithNotificationStatus(connectorName, connectorName, networkName, false), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "false"), + ), + }, + { + // expecting no changes, when user removes `status_updates_enabled` field from terraform + PlanOnly: true, + Config: configConnector(connectorName, connectorName, networkName), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, attr.StatusUpdatesEnabled, "false"), + ), + }, + }, }) } -func terraformResourceTwingateConnectorWithNotificationStatus(terraformRemoteNetworkName, terraformConnectorName, remoteNetworkName string, notificationStatus bool) string { - return fmt.Sprintf(` - %s +func configConnectorWithNotificationStatus(terraformRemoteNetworkName, terraformConnectorName, remoteNetworkName string, notificationStatus bool) string { + return acctests.Nprintf(` + ${remote_network} - resource "twingate_connector" "%s" { - remote_network_id = twingate_remote_network.%s.id - status_updates_enabled = %v + resource "twingate_connector" "${connector_resource}" { + remote_network_id = twingate_remote_network.${remote_network_resource}.id + status_updates_enabled = ${notification_status} } - `, terraformResourceRemoteNetwork(terraformRemoteNetworkName, remoteNetworkName), terraformConnectorName, terraformRemoteNetworkName, notificationStatus) + `, + map[string]any{ + "remote_network": configRemoteNetwork(terraformRemoteNetworkName, remoteNetworkName), + "connector_resource": terraformConnectorName, + "remote_network_resource": terraformRemoteNetworkName, + "notification_status": notificationStatus, + }) + } diff --git a/twingate/internal/test/acctests/resource/group_test.go b/twingate/internal/test/acctests/resource/group_test.go index 31317014..ab226b40 100644 --- a/twingate/internal/test/acctests/resource/group_test.go +++ b/twingate/internal/test/acctests/resource/group_test.go @@ -1,7 +1,6 @@ package resource import ( - "fmt" "strings" "testing" @@ -12,369 +11,374 @@ import ( sdk "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) -var ( - userIdsLen = attr.Len(attr.UserIDs) -) +var userIdsLen = attr.Len(attr.UserIDs) func TestAccTwingateGroupCreateUpdate(t *testing.T) { - t.Run("Test Twingate Resource : Acc Group Create/Update", func(t *testing.T) { - const terraformResourceName = "test001" - theResource := acctests.TerraformGroup(terraformResourceName) - nameBefore := test.RandomName() - nameAfter := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateGroup(terraformResourceName, nameBefore), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, nameBefore), - ), - }, - { - Config: terraformResourceTwingateGroup(terraformResourceName, nameAfter), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, nameAfter), - ), - }, + t.Parallel() + + groupResource := test.RandomGroupName() + theResource := acctests.TerraformGroup(groupResource) + name1 := test.RandomName() + name2 := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []sdk.TestStep{ + { + Config: configGroup(groupResource, name1), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name1), + ), }, - }) + { + Config: configGroup(groupResource, name2), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name2), + ), + }, + }, }) } -func terraformResourceTwingateGroup(terraformResourceName, name string) string { - return fmt.Sprintf(` - resource "twingate_group" "%s" { - name = "%s" +func configGroup(groupResource, name string) string { + return acctests.Nprintf(` + resource "twingate_group" "${group_resource}" { + name = "${name}" } - `, terraformResourceName, name) + `, map[string]any{ + "group_resource": groupResource, + "name": name, + }) } func TestAccTwingateGroupDeleteNonExisting(t *testing.T) { - t.Run("Test Twingate Resource : Acc Group Delete NonExisting", func(t *testing.T) { - const terraformResourceName = "test002" - theResource := acctests.TerraformGroup(terraformResourceName) - groupName := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateGroup(terraformResourceName, groupName), - Destroy: true, - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceDoesNotExists(theResource), - ), - }, + t.Parallel() + + groupResource := test.RandomGroupName() + theResource := acctests.TerraformGroup(groupResource) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []sdk.TestStep{ + { + Config: configGroup(groupResource, test.RandomName()), + Destroy: true, + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceDoesNotExists(theResource), + ), }, - }) + }, }) } func TestAccTwingateGroupReCreateAfterDeletion(t *testing.T) { - t.Run("Test Twingate Resource : Acc Group Create After Deletion", func(t *testing.T) { - const terraformResourceName = "test003" - theResource := acctests.TerraformGroup(terraformResourceName) - groupName := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateGroup(terraformResourceName, groupName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - acctests.DeleteTwingateResource(theResource, resource.TwingateGroup), - ), - ExpectNonEmptyPlan: true, - }, - { - Config: terraformResourceTwingateGroup(terraformResourceName, groupName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - ), - }, + t.Parallel() + + groupResource := test.RandomGroupName() + theResource := acctests.TerraformGroup(groupResource) + groupName := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []sdk.TestStep{ + { + Config: configGroup(groupResource, groupName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + acctests.DeleteTwingateResource(theResource, resource.TwingateGroup), + ), + ExpectNonEmptyPlan: true, }, - }) + { + Config: configGroup(groupResource, groupName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + ), + }, + }, }) } func TestAccTwingateGroupWithSecurityPolicy(t *testing.T) { - t.Run("Test Twingate Resource : Acc Group Create/Update - With Security Policy", func(t *testing.T) { - const terraformResourceName = "test004" - theResource := acctests.TerraformGroup(terraformResourceName) - name := test.RandomName() - - securityPolicies, err := acctests.ListSecurityPolicies() - if err != nil { - t.Skip("can't run test:", err) - } - - testPolicy := securityPolicies[0] - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateGroup(terraformResourceName, name), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, name), - ), - }, - { - Config: terraformResourceTwingateGroupWithSecurityPolicy(terraformResourceName, name, testPolicy.ID), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, name), - sdk.TestCheckResourceAttr(theResource, attr.SecurityPolicyID, testPolicy.ID), - ), - }, - { - // expecting no changes - PlanOnly: true, - Config: terraformResourceTwingateGroup(terraformResourceName, name), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, name), - ), - }, + t.Parallel() + + groupResource := test.RandomGroupName() + theResource := acctests.TerraformGroup(groupResource) + name := test.RandomName() + + securityPolicies, err := acctests.ListSecurityPolicies() + if err != nil { + t.Skip("can't run test:", err) + } + + testPolicy := securityPolicies[0] + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []sdk.TestStep{ + { + Config: configGroup(groupResource, name), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name), + ), }, - }) + { + Config: configGroupWithSecurityPolicy(groupResource, name, testPolicy.ID), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name), + sdk.TestCheckResourceAttr(theResource, attr.SecurityPolicyID, testPolicy.ID), + ), + }, + { + // expecting no changes + PlanOnly: true, + Config: configGroup(groupResource, name), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name), + ), + }, + }, }) } -func terraformResourceTwingateGroupWithSecurityPolicy(terraformResourceName, name, securityPolicyID string) string { - return fmt.Sprintf(` - resource "twingate_group" "%s" { - name = "%s" - security_policy_id = "%s" +func configGroupWithSecurityPolicy(terraformResourceName, name, securityPolicyID string) string { + return acctests.Nprintf(` + resource "twingate_group" "${group_resource}" { + name = "${name}" + security_policy_id = "${security_policy_id}" } - `, terraformResourceName, name, securityPolicyID) + `, + map[string]any{ + "group_resource": terraformResourceName, + "name": name, + "security_policy_id": securityPolicyID, + }) } func TestAccTwingateGroupUsersAuthoritativeByDefault(t *testing.T) { - t.Run("Test Twingate Resource : Acc Group Users Authoritative By Default", func(t *testing.T) { - const terraformResourceName = "test005" - theResource := acctests.TerraformGroup(terraformResourceName) - groupName := test.RandomName() - - users, userIDs := genNewUsers("u005", 3) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateGroupWithUsers(terraformResourceName, groupName, users, userIDs[:1]), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), - acctests.CheckGroupUsersLen(theResource, 1), - ), - }, - { - Config: terraformResourceTwingateGroupWithUsers(terraformResourceName, groupName, users, userIDs[:1]), - Check: acctests.ComposeTestCheckFunc( - // added new user to the group though API - acctests.AddGroupUser(theResource, groupName, userIDs[1]), - acctests.WaitTestFunc(), - acctests.CheckGroupUsersLen(theResource, 2), - ), - // expecting drift - terraform going to remove unknown user - ExpectNonEmptyPlan: true, - }, - { - Config: terraformResourceTwingateGroupWithUsers(terraformResourceName, groupName, users, userIDs[:1]), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), - acctests.CheckGroupUsersLen(theResource, 1), - ), - }, - { - // added 2 new users to the group though terraform - Config: terraformResourceTwingateGroupWithUsers(terraformResourceName, groupName, users, userIDs[:3]), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "3"), - acctests.CheckGroupUsersLen(theResource, 3), - ), - }, - { - Config: terraformResourceTwingateGroupWithUsers(terraformResourceName, groupName, users, userIDs[:3]), - Check: acctests.ComposeTestCheckFunc( - // delete one user from the group though API - acctests.DeleteGroupUser(theResource, userIDs[2]), - acctests.WaitTestFunc(), - sdk.TestCheckResourceAttr(theResource, userIdsLen, "3"), - acctests.CheckGroupUsersLen(theResource, 2), - ), - // expecting drift - terraform going to restore deleted user - ExpectNonEmptyPlan: true, - }, - { - Config: terraformResourceTwingateGroupWithUsers(terraformResourceName, groupName, users, userIDs[:3]), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "3"), - acctests.CheckGroupUsersLen(theResource, 3), - ), - }, - { - // remove 2 users from the group though terraform - Config: terraformResourceTwingateGroupWithUsers(terraformResourceName, groupName, users, userIDs[:1]), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), - acctests.CheckGroupUsersLen(theResource, 1), - ), - }, - { - // expecting no drift - Config: terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, groupName, users, userIDs[:1], true), - PlanOnly: true, - }, - { - Config: terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, groupName, users, userIDs[:2], true), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "2"), - acctests.CheckGroupUsersLen(theResource, 2), - ), - }, + t.Parallel() + + groupResource := test.RandomGroupName() + theResource := acctests.TerraformGroup(groupResource) + groupName := test.RandomName() + + users, userIDs := genNewUsers("u005", 3) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []sdk.TestStep{ + { + Config: configGroupWithUsers(groupResource, groupName, users, userIDs[:1]), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), + acctests.CheckGroupUsersLen(theResource, 1), + ), }, - }) + { + Config: configGroupWithUsers(groupResource, groupName, users, userIDs[:1]), + Check: acctests.ComposeTestCheckFunc( + // added new user to the group though API + acctests.AddGroupUser(theResource, groupName, userIDs[1]), + acctests.WaitTestFunc(), + acctests.CheckGroupUsersLen(theResource, 2), + ), + // expecting drift - terraform going to remove unknown user + ExpectNonEmptyPlan: true, + }, + { + Config: configGroupWithUsers(groupResource, groupName, users, userIDs[:1]), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), + acctests.CheckGroupUsersLen(theResource, 1), + ), + }, + { + // added 2 new users to the group though terraform + Config: configGroupWithUsers(groupResource, groupName, users, userIDs[:3]), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "3"), + acctests.CheckGroupUsersLen(theResource, 3), + ), + }, + { + Config: configGroupWithUsers(groupResource, groupName, users, userIDs[:3]), + Check: acctests.ComposeTestCheckFunc( + // delete one user from the group though API + acctests.DeleteGroupUser(theResource, userIDs[2]), + acctests.WaitTestFunc(), + sdk.TestCheckResourceAttr(theResource, userIdsLen, "3"), + acctests.CheckGroupUsersLen(theResource, 2), + ), + // expecting drift - terraform going to restore deleted user + ExpectNonEmptyPlan: true, + }, + { + Config: configGroupWithUsers(groupResource, groupName, users, userIDs[:3]), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "3"), + acctests.CheckGroupUsersLen(theResource, 3), + ), + }, + { + // remove 2 users from the group though terraform + Config: configGroupWithUsers(groupResource, groupName, users, userIDs[:1]), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), + acctests.CheckGroupUsersLen(theResource, 1), + ), + }, + { + // expecting no drift + Config: configGroupWithUsersAuthoritative(groupResource, groupName, users, userIDs[:1], true), + PlanOnly: true, + }, + { + Config: configGroupWithUsersAuthoritative(groupResource, groupName, users, userIDs[:2], true), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "2"), + acctests.CheckGroupUsersLen(theResource, 2), + ), + }, + }, }) } -func terraformResourceTwingateGroupWithUsers(terraformResourceName, name string, users, usersID []string) string { - return fmt.Sprintf(` - %s +func configGroupWithUsers(terraformResourceName, name string, users, usersID []string) string { + return acctests.Nprintf(` + ${users} - resource "twingate_group" "%s" { - name = "%s" - user_ids = [%s] + resource "twingate_group" "${group_resource}" { + name = "${name}" + user_ids = [${user_ids}] } - `, strings.Join(users, "\n"), terraformResourceName, name, strings.Join(usersID, ", ")) + `, + map[string]any{ + "users": strings.Join(users, "\n"), + "group_resource": terraformResourceName, + "name": name, + "user_ids": strings.Join(usersID, ", "), + }) } -func terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, name string, users, usersID []string, authoritative bool) string { - return fmt.Sprintf(` - %s +func configGroupWithUsersAuthoritative(terraformResourceName, name string, users, usersID []string, authoritative bool) string { + return acctests.Nprintf(` + ${users} - resource "twingate_group" "%s" { - name = "%s" - user_ids = [%s] - is_authoritative = %v + resource "twingate_group" "${group_resource}" { + name = "${name}" + user_ids = [${user_ids}] + is_authoritative = ${authoritative} } - `, strings.Join(users, "\n"), terraformResourceName, name, strings.Join(usersID, ", "), authoritative) + `, + map[string]any{ + "users": strings.Join(users, "\n"), + "group_resource": terraformResourceName, + "name": name, + "user_ids": strings.Join(usersID, ", "), + "authoritative": authoritative, + }) } func TestAccTwingateGroupUsersNotAuthoritative(t *testing.T) { - t.Run("Test Twingate Resource : Acc Group Users Not Authoritative", func(t *testing.T) { - const terraformResourceName = "test006" - theResource := acctests.TerraformGroup(terraformResourceName) - groupName := test.RandomName() - - users, userIDs := genNewUsers("u006", 3) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, groupName, users, userIDs[:1], false), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), - acctests.CheckGroupUsersLen(theResource, 1), - ), - }, - { - Config: terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, groupName, users, userIDs[:1], false), - Check: acctests.ComposeTestCheckFunc( - // added new user to the group though API - acctests.AddGroupUser(theResource, groupName, userIDs[2]), - acctests.WaitTestFunc(), - acctests.CheckGroupUsersLen(theResource, 2), - ), - }, - { - // added new user to the group though terraform - Config: terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, groupName, users, userIDs[:2], false), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "2"), - acctests.CheckGroupUsersLen(theResource, 3), - ), - }, - { - // remove one user from the group though terraform - Config: terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, groupName, users, userIDs[:1], false), - Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), - acctests.CheckGroupUsersLen(theResource, 2), - // remove one user from the group though API - acctests.DeleteGroupUser(theResource, userIDs[2]), - acctests.WaitTestFunc(), - acctests.CheckGroupUsersLen(theResource, 1), - ), - }, - { - // expecting no drift - empty plan - Config: terraformResourceTwingateGroupWithUsersAuthoritative(terraformResourceName, groupName, users, userIDs[:1], false), - PlanOnly: true, - }, + t.Parallel() + + groupResource := test.RandomGroupName() + theResource := acctests.TerraformGroup(groupResource) + groupName := test.RandomName() + + users, userIDs := genNewUsers("u006", 3) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []sdk.TestStep{ + { + Config: configGroupWithUsersAuthoritative(groupResource, groupName, users, userIDs[:1], false), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), + acctests.CheckGroupUsersLen(theResource, 1), + ), }, - }) + { + Config: configGroupWithUsersAuthoritative(groupResource, groupName, users, userIDs[:1], false), + Check: acctests.ComposeTestCheckFunc( + // added new user to the group though API + acctests.AddGroupUser(theResource, groupName, userIDs[2]), + acctests.WaitTestFunc(), + acctests.CheckGroupUsersLen(theResource, 2), + ), + }, + { + // added new user to the group though terraform + Config: configGroupWithUsersAuthoritative(groupResource, groupName, users, userIDs[:2], false), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "2"), + acctests.CheckGroupUsersLen(theResource, 3), + ), + }, + { + // remove one user from the group though terraform + Config: configGroupWithUsersAuthoritative(groupResource, groupName, users, userIDs[:1], false), + Check: acctests.ComposeTestCheckFunc( + sdk.TestCheckResourceAttr(theResource, userIdsLen, "1"), + acctests.CheckGroupUsersLen(theResource, 2), + // remove one user from the group though API + acctests.DeleteGroupUser(theResource, userIDs[2]), + acctests.WaitTestFunc(), + acctests.CheckGroupUsersLen(theResource, 1), + ), + }, + { + // expecting no drift - empty plan + Config: configGroupWithUsersAuthoritative(groupResource, groupName, users, userIDs[:1], false), + PlanOnly: true, + }, + }, }) } func TestAccTwingateGroupUsersCursor(t *testing.T) { - t.Run("Test Twingate Resource : Acc Group Users Cursor", func(t *testing.T) { - acctests.SetPageLimit(1) - - const terraformResourceName = "test007" - theResource := acctests.TerraformGroup(terraformResourceName) - groupName := test.RandomName() - - users, userIDs := genNewUsers("u007", 3) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateGroupDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateGroupAndUsers(terraformResourceName, groupName, users, userIDs), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckGroupUsersLen(theResource, len(users)), - ), - }, - { - Config: terraformResourceTwingateGroupAndUsers(terraformResourceName, groupName, users[:2], userIDs[:2]), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckGroupUsersLen(theResource, 2), - ), - }, - }, - }) - }) -} + acctests.SetPageLimit(t, 1) -func terraformResourceTwingateGroupAndUsers(terraformResourceName, name string, users, userIDs []string) string { - return fmt.Sprintf(` - %s + groupResource := test.RandomGroupName() + theResource := acctests.TerraformGroup(groupResource) + groupName := test.RandomName() - resource "twingate_group" "%s" { - name = "%s" - user_ids = [%s] - } - `, strings.Join(users, "\n"), terraformResourceName, name, strings.Join(userIDs, ", ")) + users, userIDs := genNewUsers("u007", 3) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateGroupDestroy, + Steps: []sdk.TestStep{ + { + Config: configGroupWithUsers(groupResource, groupName, users, userIDs), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckGroupUsersLen(theResource, len(users)), + ), + }, + { + Config: configGroupWithUsers(groupResource, groupName, users[:2], userIDs[:2]), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckGroupUsersLen(theResource, 2), + ), + }, + }, + }) } diff --git a/twingate/internal/test/acctests/resource/remote-network_test.go b/twingate/internal/test/acctests/resource/remote-network_test.go index b8e35ebd..aa5e2a7c 100644 --- a/twingate/internal/test/acctests/resource/remote-network_test.go +++ b/twingate/internal/test/acctests/resource/remote-network_test.go @@ -1,7 +1,6 @@ package resource import ( - "fmt" "testing" "github.com/Twingate/terraform-provider-twingate/twingate/internal/attr" @@ -13,161 +12,168 @@ import ( ) func TestAccTwingateRemoteNetworkCreate(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Network Create", func(t *testing.T) { - const terraformResourceName = "test000" - theResource := acctests.TerraformRemoteNetwork(terraformResourceName) - networkName := test.RandomName() - networkLocation := model.LocationAzure - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: createRemoteNetworkWithLocation(terraformResourceName, networkName, networkLocation), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, networkName), - sdk.TestCheckResourceAttr(theResource, attr.Location, networkLocation), - ), - }, + t.Parallel() + + networkResource := test.RandomNetworkName() + theResource := acctests.TerraformRemoteNetwork(networkResource) + networkName := test.RandomName() + networkLocation := model.LocationAzure + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configRemoteNetworkWithLocation(networkResource, networkName, networkLocation), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, networkName), + sdk.TestCheckResourceAttr(theResource, attr.Location, networkLocation), + ), }, - }) + }, }) } -func createRemoteNetworkWithLocation(terraformResourceName, name, location string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" - location = "%s" +func configRemoteNetworkWithLocation(networkResource, name, location string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${name}" + location = "${location}" } - `, terraformResourceName, name, location) + `, map[string]any{ + "network_resource": networkResource, + "name": name, + "location": location, + }) } func TestAccTwingateRemoteNetworkUpdate(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Network Update", func(t *testing.T) { - const terraformResourceName = "test001" - theResource := acctests.TerraformRemoteNetwork(terraformResourceName) - nameBefore := test.RandomName() - nameAfter := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceRemoteNetwork(terraformResourceName, nameBefore), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, nameBefore), - sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationOther), - ), - }, - { - Config: createRemoteNetworkWithLocation(terraformResourceName, nameAfter, model.LocationAWS), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, nameAfter), - sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationAWS), - ), - }, + t.Parallel() + + networkResource := test.RandomNetworkName() + theResource := acctests.TerraformRemoteNetwork(networkResource) + name1 := test.RandomName() + name2 := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configRemoteNetwork(networkResource, name1), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name1), + sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationOther), + ), }, - }) + { + Config: configRemoteNetworkWithLocation(networkResource, name2, model.LocationAWS), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name2), + sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationAWS), + ), + }, + }, }) } -func terraformResourceRemoteNetwork(terraformResourceName, name string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" +func configRemoteNetwork(networkResource, name string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${name}" } - `, terraformResourceName, name) + `, + map[string]any{ + "network_resource": networkResource, + "name": name, + }) } func TestAccTwingateRemoteNetworkDeleteNonExisting(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Network Delete NonExisting", func(t *testing.T) { - const terraformResourceName = "test002" - theResource := acctests.TerraformRemoteNetwork(terraformResourceName) - remoteNetworkNameBefore := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceRemoteNetwork(terraformResourceName, remoteNetworkNameBefore), - Destroy: true, - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceDoesNotExists(theResource), - ), - }, + t.Parallel() + + networkResource := test.RandomNetworkName() + theResource := acctests.TerraformRemoteNetwork(networkResource) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configRemoteNetwork(networkResource, test.RandomName()), + Destroy: true, + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceDoesNotExists(theResource), + ), }, - }) + }, }) } func TestAccTwingateRemoteNetworkReCreateAfterDeletion(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Network Re Create After Deletion", func(t *testing.T) { - const terraformResourceName = "test003" - theResource := acctests.TerraformRemoteNetwork(terraformResourceName) - remoteNetworkName := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceRemoteNetwork(terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - acctests.DeleteTwingateResource(theResource, resource.TwingateRemoteNetwork), - ), - ExpectNonEmptyPlan: true, - }, - { - Config: terraformResourceRemoteNetwork(terraformResourceName, remoteNetworkName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - ), - }, + t.Parallel() + + networkResource := test.RandomNetworkName() + theResource := acctests.TerraformRemoteNetwork(networkResource) + networkName := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configRemoteNetwork(networkResource, networkName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + acctests.DeleteTwingateResource(theResource, resource.TwingateRemoteNetwork), + ), + ExpectNonEmptyPlan: true, }, - }) + { + Config: configRemoteNetwork(networkResource, networkName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + ), + }, + }, }) } func TestAccTwingateRemoteNetworkUpdateWithTheSameName(t *testing.T) { - t.Run("Test Twingate Resource : Acc Remote Network Update With The Same Name", func(t *testing.T) { - const terraformResourceName = "test004" - theResource := acctests.TerraformRemoteNetwork(terraformResourceName) - name := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceRemoteNetwork(terraformResourceName, name), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, name), - sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationOther), - ), - }, - { - Config: createRemoteNetworkWithLocation(terraformResourceName, name, model.LocationAWS), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, name), - sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationAWS), - ), - }, + t.Parallel() + + networkResource := test.RandomNetworkName() + theResource := acctests.TerraformRemoteNetwork(networkResource) + name := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateRemoteNetworkDestroy, + Steps: []sdk.TestStep{ + { + Config: configRemoteNetwork(networkResource, name), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name), + sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationOther), + ), }, - }) + { + Config: configRemoteNetworkWithLocation(networkResource, name, model.LocationAWS), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name), + sdk.TestCheckResourceAttr(theResource, attr.Location, model.LocationAWS), + ), + }, + }, }) } diff --git a/twingate/internal/test/acctests/resource/resource_test.go b/twingate/internal/test/acctests/resource/resource_test.go index a5a97811..17758c70 100644 --- a/twingate/internal/test/acctests/resource/resource_test.go +++ b/twingate/internal/test/acctests/resource/resource_test.go @@ -27,10 +27,12 @@ var ( ) func TestAccTwingateResourceCreate(t *testing.T) { - const terraformResourceName = "test1" - theResource := acctests.TerraformResource(terraformResourceName) - remoteNetworkName := test.RandomName() + t.Parallel() + resourceName := test.RandomResourceName() + theResource := acctests.TerraformResource(resourceName) + remoteNetworkName := test.RandomName() + name := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -38,12 +40,12 @@ func TestAccTwingateResourceCreate(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceOnlyWithNetwork(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(resourceName, remoteNetworkName, name), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckNoResourceAttr(theResource, accessGroupIdsLen), - sdk.TestCheckResourceAttr(acctests.TerraformRemoteNetwork(terraformResourceName), attr.Name, remoteNetworkName), - sdk.TestCheckResourceAttr(theResource, attr.Name, resourceName), + sdk.TestCheckResourceAttr(acctests.TerraformRemoteNetwork(resourceName), attr.Name, remoteNetworkName), + sdk.TestCheckResourceAttr(theResource, attr.Name, name), sdk.TestCheckResourceAttr(theResource, attr.Address, "acc-test.com"), ), }, @@ -52,10 +54,12 @@ func TestAccTwingateResourceCreate(t *testing.T) { } func TestAccTwingateResourceUpdateProtocols(t *testing.T) { - const terraformResourceName = "test1u" - theResource := acctests.TerraformResource(terraformResourceName) - remoteNetworkName := test.RandomName() + t.Parallel() + resourceName := test.RandomResourceName() + theResource := acctests.TerraformResource(resourceName) + remoteNetworkName := test.RandomName() + name := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -63,19 +67,19 @@ func TestAccTwingateResourceUpdateProtocols(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceOnlyWithNetwork(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(resourceName, remoteNetworkName, name), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), }, { - Config: createResourceWithSimpleProtocols(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceWithSimpleProtocols(resourceName, remoteNetworkName, name), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), }, { - Config: createResourceOnlyWithNetwork(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(resourceName, remoteNetworkName, name), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), @@ -84,48 +88,63 @@ func TestAccTwingateResourceUpdateProtocols(t *testing.T) { }) } -func createResourceOnlyWithNetwork(terraformResourceName, networkName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" +func configResourceBasic(terraformResource, networkName, name string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } - resource "twingate_resource" "%s" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "acc-test.com" - remote_network_id = twingate_remote_network.%s.id + remote_network_id = twingate_remote_network.${network_resource}.id } - `, terraformResourceName, networkName, terraformResourceName, resourceName, terraformResourceName) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": name, + }) } -func createResourceWithSimpleProtocols(terraformResourceName, networkName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" +func configResourceWithSimpleProtocols(terraformResource, networkName, name string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } - resource "twingate_resource" "%s" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "acc-test.com" - remote_network_id = twingate_remote_network.%s.id + remote_network_id = twingate_remote_network.${network_resource}.id protocols { - allow_icmp = true - tcp { - policy = "DENY_ALL" - } - udp { - policy = "DENY_ALL" - } - } + allow_icmp = true + tcp { + policy = "DENY_ALL" + } + udp { + policy = "DENY_ALL" + } + } } - `, terraformResourceName, networkName, terraformResourceName, resourceName, terraformResourceName) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": name, + }) } func TestAccTwingateResourceCreateWithProtocolsAndGroups(t *testing.T) { - const theResource = "twingate_resource.test2" - remoteNetworkName := test.RandomName() - groupName1 := test.RandomGroupName() - groupName2 := test.RandomGroupName() - resourceName := test.RandomResourceName() + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) + networkName := test.RandomName() + group1 := test.RandomGroupName() + group2 := test.RandomGroupName() + name := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -133,7 +152,7 @@ func TestAccTwingateResourceCreateWithProtocolsAndGroups(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithProtocolsAndGroups(remoteNetworkName, groupName1, groupName2, resourceName), + Config: configResourceWithProtocolsAndGroups(terraformResource, networkName, group1, group2, name), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, attr.Address, "new-acc-test.com"), @@ -146,33 +165,33 @@ func TestAccTwingateResourceCreateWithProtocolsAndGroups(t *testing.T) { }) } -func createResourceWithProtocolsAndGroups(networkName, groupName1, groupName2, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test2" { - name = "%s" +func configResourceWithProtocolsAndGroups(terraformResource, networkName, groupName1, groupName2, resourceName string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } resource "twingate_group" "g21" { - name = "%s" + name = "${group_1}" } resource "twingate_group" "g22" { - name = "%s" + name = "${group_2}" } - resource "twingate_resource" "test2" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "new-acc-test.com" - remote_network_id = twingate_remote_network.test2.id + remote_network_id = twingate_remote_network.${network_resource}.id protocols { allow_icmp = true tcp { - policy = "%s" + policy = "${tcp_policy}" ports = ["80", "82-83"] } udp { - policy = "%s" + policy = "${udp_policy}" } } @@ -180,14 +199,27 @@ func createResourceWithProtocolsAndGroups(networkName, groupName1, groupName2, r group_ids = [twingate_group.g21.id, twingate_group.g22.id] } } - `, networkName, groupName1, groupName2, resourceName, model.PolicyRestricted, model.PolicyAllowAll) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "group_1": groupName1, + "group_2": groupName2, + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + }) } func TestAccTwingateResourceFullCreationFlow(t *testing.T) { - const theResource = "twingate_resource.test3" - remoteNetworkName := test.RandomName() + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) + networkName := test.RandomName() groupName := test.RandomGroupName() - resourceName := test.RandomResourceName() + name := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -195,104 +227,127 @@ func TestAccTwingateResourceFullCreationFlow(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: resourceFullCreationFlow(remoteNetworkName, groupName, resourceName), + Config: configCompleteResource(terraformResource, networkName, groupName, name), Check: acctests.ComposeTestCheckFunc( - sdk.TestCheckResourceAttr("twingate_remote_network.test3", attr.Name, remoteNetworkName), - sdk.TestCheckResourceAttr(theResource, attr.Name, resourceName), - sdk.TestMatchResourceAttr("twingate_connector_tokens.test31", attr.AccessToken, regexp.MustCompile(".+")), + sdk.TestCheckResourceAttr(acctests.TerraformRemoteNetwork(terraformResource), attr.Name, networkName), + sdk.TestCheckResourceAttr(theResource, attr.Name, name), + sdk.TestMatchResourceAttr(acctests.TerraformConnectorTokens(terraformResource), attr.AccessToken, regexp.MustCompile(".+")), ), }, }, }) } -func resourceFullCreationFlow(networkName, groupName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test3" { - name = "%s" +func configCompleteResource(terraformResource, networkName, groupName, resourceName string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } - resource "twingate_connector" "test31" { - remote_network_id = twingate_remote_network.test3.id + resource "twingate_connector" "${connector_resource}" { + remote_network_id = twingate_remote_network.${network_resource}.id } - resource "twingate_connector_tokens" "test31" { - connector_id = twingate_connector.test31.id + resource "twingate_connector_tokens" "${connector_tokens_resource}" { + connector_id = twingate_connector.${connector_resource}.id } - resource "twingate_connector" "test32" { - remote_network_id = twingate_remote_network.test3.id + resource "twingate_connector" "${connector_resource}-2" { + remote_network_id = twingate_remote_network.${network_resource}.id } - resource "twingate_connector_tokens" "test32" { - connector_id = twingate_connector.test32.id + resource "twingate_connector_tokens" "${connector_tokens_resource}-2" { + connector_id = twingate_connector.${connector_resource}-2.id } - resource "twingate_group" "test3" { - name = "%s" + resource "twingate_group" "${group_resource}" { + name = "${group_name}" } - resource "twingate_resource" "test3" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "acc-test.com" - remote_network_id = twingate_remote_network.test3.id + remote_network_id = twingate_remote_network.${network_resource}.id protocols { allow_icmp = true tcp { - policy = "%s" + policy = "${tcp_policy}" ports = ["3306"] } udp { - policy = "%s" + policy = "${udp_policy}" } } access { - group_ids = [twingate_group.test3.id] + group_ids = [twingate_group.${group_resource}.id] } } - `, networkName, groupName, resourceName, model.PolicyRestricted, model.PolicyAllowAll) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "connector_resource": terraformResource, + "connector_tokens_resource": terraformResource, + "group_resource": terraformResource, + "group_name": groupName, + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + }) } func TestAccTwingateResourceWithInvalidGroupId(t *testing.T) { - resourceName := test.RandomResourceName() + t.Parallel() + + terraformResource := test.RandomResourceName() networkName := test.RandomResourceName() + name := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, Steps: []sdk.TestStep{ { - Config: createResourceWithInvalidGroupId(networkName, resourceName), + Config: configResourceWithInvalidGroupId(terraformResource, networkName, name), ExpectError: regexp.MustCompile("failed to create resource: Field 'groupIds' Unable to parse global ID"), }, }, }) } -func createResourceWithInvalidGroupId(networkName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test4" { +func configResourceWithInvalidGroupId(terraformResource, networkName, resourceName string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { name = "%s" } - resource "twingate_resource" "test4" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "acc-test.com" access { group_ids = ["foo", "bar"] } - remote_network_id = twingate_remote_network.test4.id + remote_network_id = twingate_remote_network.${network_resource}.id } - `, networkName, resourceName) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": resourceName, + }) } func TestAccTwingateResourceWithTcpDenyAllPolicy(t *testing.T) { - const theResource = "twingate_resource.test5" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) resourceName := test.RandomResourceName() networkName := test.RandomResourceName() - groupName := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -300,7 +355,7 @@ func TestAccTwingateResourceWithTcpDenyAllPolicy(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithTcpDenyAllPolicy(networkName, groupName, resourceName), + Config: configResourceWithPolicy(terraformResource, networkName, resourceName, model.PolicyDenyAll, model.PolicyAllowAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyDenyAll), @@ -308,47 +363,52 @@ func TestAccTwingateResourceWithTcpDenyAllPolicy(t *testing.T) { }, // expecting no changes - empty plan { - Config: createResourceWithTcpDenyAllPolicy(networkName, groupName, resourceName), + Config: configResourceWithPolicy(terraformResource, networkName, resourceName, model.PolicyDenyAll, model.PolicyAllowAll), PlanOnly: true, }, }, }) } -func createResourceWithTcpDenyAllPolicy(networkName, groupName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test5" { - name = "%s" - } - - resource "twingate_group" "g5" { - name = "%s" +func configResourceWithPolicy(terraformResource, networkName, resourceName, tcpPolicy, udpPolicy string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } - resource "twingate_resource" "test5" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "new-acc-test.com" - remote_network_id = twingate_remote_network.test5.id - access { - group_ids = [twingate_group.g5.id] - } + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { allow_icmp = true tcp { - policy = "%s" + policy = "${tcp_policy}" } udp { - policy = "%s" + policy = "${udp_policy}" } } } - `, networkName, groupName, resourceName, model.PolicyDenyAll, model.PolicyAllowAll) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "group_resource": terraformResource, + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": tcpPolicy, + "udp_policy": udpPolicy, + }) } func TestAccTwingateResourceWithUdpDenyAllPolicy(t *testing.T) { - const theResource = "twingate_resource.test6" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() - groupName := test.RandomGroupName() resourceName := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ @@ -357,7 +417,7 @@ func TestAccTwingateResourceWithUdpDenyAllPolicy(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithUdpDenyAllPolicy(remoteNetworkName, groupName, resourceName), + Config: configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, model.PolicyAllowAll, model.PolicyDenyAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, udpPolicy, model.PolicyDenyAll), @@ -365,45 +425,18 @@ func TestAccTwingateResourceWithUdpDenyAllPolicy(t *testing.T) { }, // expecting no changes - empty plan { - Config: createResourceWithUdpDenyAllPolicy(remoteNetworkName, groupName, resourceName), + Config: configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, model.PolicyAllowAll, model.PolicyDenyAll), PlanOnly: true, }, }, }) } -func createResourceWithUdpDenyAllPolicy(networkName, groupName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test6" { - name = "%s" - } - - resource "twingate_group" "g6" { - name = "%s" - } - - resource "twingate_resource" "test6" { - name = "%s" - address = "acc-test.com" - remote_network_id = twingate_remote_network.test6.id - access { - group_ids = [twingate_group.g6.id] - } - protocols { - allow_icmp = true - tcp { - policy = "%s" - } - udp { - policy = "%s" - } - } - } - `, networkName, groupName, resourceName, model.PolicyAllowAll, model.PolicyDenyAll) -} - func TestAccTwingateResourceWithDenyAllPolicyAndEmptyPortsList(t *testing.T) { - const theResource = "twingate_resource.test7" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() groupName := test.RandomGroupName() resourceName := test.RandomResourceName() @@ -414,7 +447,7 @@ func TestAccTwingateResourceWithDenyAllPolicyAndEmptyPortsList(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithDenyAllPolicyAndEmptyPortsList(remoteNetworkName, groupName, resourceName), + Config: configResourceWithPolicyAndEmptyTCPPortsList(terraformResource, remoteNetworkName, groupName, resourceName, model.PolicyDenyAll, model.PolicyDenyAll), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.Name, resourceName), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyDenyAll), @@ -427,44 +460,57 @@ func TestAccTwingateResourceWithDenyAllPolicyAndEmptyPortsList(t *testing.T) { }) } -func createResourceWithDenyAllPolicyAndEmptyPortsList(networkName, groupName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test7" { - name = "%s" - } +func configResourceWithPolicyAndEmptyTCPPortsList(terraformResource, networkName, groupName, resourceName, tcpPolicy, udpPolicy string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - resource "twingate_group" "test7" { - name = "%s" + resource "twingate_group" "${group_resource}" { + name = "${group_name}" } - resource "twingate_resource" "test7" { - name = "%s" - address = "new-acc-test.com" - remote_network_id = twingate_remote_network.test7.id - access { - group_ids = [twingate_group.test7.id] - } - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = [] - } - udp { - policy = "%s" - } - } - } - `, networkName, groupName, resourceName, model.PolicyDenyAll, model.PolicyDenyAll) + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "new-acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + access { + group_ids = [twingate_group.${group_resource}.id] + } + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = [] + } + udp { + policy = "${udp_policy}" + } + } + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "group_resource": terraformResource, + "group_name": groupName, + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": tcpPolicy, + "udp_policy": udpPolicy, + }) } func TestAccTwingateResourceWithInvalidPortRange(t *testing.T) { + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() expectedError := regexp.MustCompile("failed to parse protocols port range") genConfig := func(portRange string) string { - return createResourceWithRestrictedPolicyAndPortRange(remoteNetworkName, resourceName, portRange) + return configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, portRange) } sdk.Test(t, sdk.TestCase{ @@ -507,32 +553,11 @@ func TestAccTwingateResourceWithInvalidPortRange(t *testing.T) { }) } -func createResourceWithRestrictedPolicyAndPortRange(networkName, resourceName, portRange string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test8" { - name = "%s" - } - - resource "twingate_resource" "test8" { - name = "%s" - address = "new-acc-test.com" - remote_network_id = twingate_remote_network.test8.id - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = [%s] - } - udp { - policy = "%s" - } - } - } - `, networkName, resourceName, model.PolicyRestricted, portRange, model.PolicyAllowAll) -} - func TestAccTwingateResourcePortReorderingCreatesNoChanges(t *testing.T) { - const theResource = "twingate_resource.test9" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -542,7 +567,7 @@ func TestAccTwingateResourcePortReorderingCreatesNoChanges(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"80", "82-83"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"80", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, firstTCPPort, "80"), @@ -551,17 +576,17 @@ func TestAccTwingateResourcePortReorderingCreatesNoChanges(t *testing.T) { }, // no changes { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"82-83", "80"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"82-83", "80"`), PlanOnly: true, }, // no changes { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"82", "83", "80"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"82", "83", "80"`), PlanOnly: true, }, // new changes applied { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"70", "82-83"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"70", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, firstTCPPort, "70"), @@ -572,33 +597,45 @@ func TestAccTwingateResourcePortReorderingCreatesNoChanges(t *testing.T) { }) } -func createResourceWithPortRange(networkName, resourceName, portRange string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test9" { - name = "%s" - } +func configResourceWithPortRange(terraformResource, networkName, resourceName, portRange string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - resource "twingate_resource" "test9" { - name = "%s" - address = "acc-test.com" - remote_network_id = twingate_remote_network.test9.id - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = [%s] - } - udp { - policy = "%s" - ports = [%s] - } - } - } - `, networkName, resourceName, model.PolicyRestricted, portRange, model.PolicyRestricted, portRange) + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "new-acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = [${port_range}] + } + udp { + policy = "${udp_policy}" + ports = [${port_range}] + } + } + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyRestricted, + "port_range": portRange, + }) } func TestAccTwingateResourcePortsRepresentationChanged(t *testing.T) { - const theResource = "twingate_resource.test9" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -608,7 +645,7 @@ func TestAccTwingateResourcePortsRepresentationChanged(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"82", "83", "80"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"82", "83", "80"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPortsLen, "3"), @@ -619,7 +656,10 @@ func TestAccTwingateResourcePortsRepresentationChanged(t *testing.T) { } func TestAccTwingateResourcePortsNotChanged(t *testing.T) { - const theResource = "twingate_resource.test9" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -629,7 +669,7 @@ func TestAccTwingateResourcePortsNotChanged(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"82", "83", "80"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"82", "83", "80"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPortsLen, "3"), @@ -637,7 +677,7 @@ func TestAccTwingateResourcePortsNotChanged(t *testing.T) { }, { PlanOnly: true, - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"80", "82-83"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"80", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPortsLen, "2"), @@ -648,7 +688,10 @@ func TestAccTwingateResourcePortsNotChanged(t *testing.T) { } func TestAccTwingateResourcePortReorderingNoChanges(t *testing.T) { - const theResource = "twingate_resource.test9" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -658,7 +701,7 @@ func TestAccTwingateResourcePortReorderingNoChanges(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"82", "83", "80"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"82", "83", "80"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, firstTCPPort, "82"), @@ -667,12 +710,12 @@ func TestAccTwingateResourcePortReorderingNoChanges(t *testing.T) { }, // no changes { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"82-83", "80"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"82-83", "80"`), PlanOnly: true, }, // no changes { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"82-83", "80"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"82-83", "80"`), PlanOnly: true, Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, udpPortsLen, "2"), @@ -680,7 +723,7 @@ func TestAccTwingateResourcePortReorderingNoChanges(t *testing.T) { }, // new changes applied { - Config: createResourceWithPortRange(remoteNetworkName, resourceName, `"70", "82-83"`), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"70", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, firstTCPPort, "70"), @@ -692,8 +735,10 @@ func TestAccTwingateResourcePortReorderingNoChanges(t *testing.T) { } func TestAccTwingateResourceSetActiveStateOnUpdate(t *testing.T) { - const terraformResourceName = "test10" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -703,7 +748,7 @@ func TestAccTwingateResourceSetActiveStateOnUpdate(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceOnlyWithNetwork(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), acctests.DeactivateTwingateResource(theResource), @@ -712,7 +757,7 @@ func TestAccTwingateResourceSetActiveStateOnUpdate(t *testing.T) { ), }, { - Config: createResourceOnlyWithNetwork(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceActiveState(theResource, true), ), @@ -722,8 +767,10 @@ func TestAccTwingateResourceSetActiveStateOnUpdate(t *testing.T) { } func TestAccTwingateResourceReCreateAfterDeletion(t *testing.T) { - const terraformResourceName = "test10" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -733,7 +780,7 @@ func TestAccTwingateResourceReCreateAfterDeletion(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceOnlyWithNetwork(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), acctests.DeleteTwingateResource(theResource, resource.TwingateResource), @@ -741,7 +788,7 @@ func TestAccTwingateResourceReCreateAfterDeletion(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: createResourceOnlyWithNetwork(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), @@ -751,10 +798,11 @@ func TestAccTwingateResourceReCreateAfterDeletion(t *testing.T) { } func TestAccTwingateResourceImport(t *testing.T) { - const theResource = "twingate_resource.test12" + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() - groupName := test.RandomGroupName() - groupName2 := test.RandomGroupName() resourceName := test.RandomResourceName() sdk.Test(t, sdk.TestCase{ @@ -763,7 +811,7 @@ func TestAccTwingateResourceImport(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource12(remoteNetworkName, groupName, groupName2, resourceName), + Config: configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, `"80", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), @@ -772,61 +820,24 @@ func TestAccTwingateResourceImport(t *testing.T) { ImportState: true, ResourceName: theResource, ImportStateCheck: acctests.CheckImportState(map[string]string{ - attr.Address: "acc-test.com.12", + attr.Address: "new-acc-test.com", tcpPolicy: model.PolicyRestricted, tcpPortsLen: "2", firstTCPPort: "80", - udpPolicy: model.PolicyAllowAll, - udpPortsLen: "0", }), }, }, }) } -func createResource12(networkName, groupName1, groupName2, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test12" { - name = "%s" - } - - resource "twingate_group" "g121" { - name = "%s" - } - - resource "twingate_group" "g122" { - name = "%s" - } - - resource "twingate_resource" "test12" { - name = "%s" - address = "acc-test.com.12" - remote_network_id = twingate_remote_network.test12.id - access { - group_ids = [twingate_group.g121.id, twingate_group.g122.id] - } - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } - } - `, networkName, groupName1, groupName2, resourceName, model.PolicyRestricted, model.PolicyAllowAll) -} - func genNewGroups(resourcePrefix string, count int) ([]string, []string) { groups := make([]string, 0, count) groupsID := make([]string, 0, count) for i := 0; i < count; i++ { resourceName := fmt.Sprintf("%s_%d", resourcePrefix, i+1) - groups = append(groups, newTerraformGroup(resourceName, test.RandomName())) - groupsID = append(groupsID, fmt.Sprintf("twingate_group.%s.id", resourceName)) + groups = append(groups, configGroup(resourceName, test.RandomName())) + groupsID = append(groupsID, acctests.TerraformGroup(resourceName)+".id") } return groups, groupsID @@ -847,26 +858,20 @@ func genNewServiceAccounts(resourcePrefix string, count int) ([]string, []string for i := 0; i < count; i++ { resourceName := fmt.Sprintf("%s_%d", resourcePrefix, i+1) - serviceAccounts = append(serviceAccounts, createServiceAccount(resourceName, test.RandomName())) + serviceAccounts = append(serviceAccounts, configServiceAccount(resourceName, test.RandomName())) serviceAccountIDs = append(serviceAccountIDs, acctests.TerraformServiceAccount(resourceName)+".id") } return serviceAccounts, serviceAccountIDs } -func newTerraformGroup(resourceName, groupName string) string { - return fmt.Sprintf(` - resource "twingate_group" "%s" { - name = "%s" - } - `, resourceName, groupName) -} - func TestAccTwingateResourceAddAccessServiceAccounts(t *testing.T) { - const theResource = "twingate_resource.test15" - remoteNetworkName := test.RandomName() - resourceName := test.RandomResourceName() - serviceAccountName := test.RandomName("s15") + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) + serviceAccountConfig := configServiceAccount(terraformResource, test.RandomName()) + serviceAccountID := acctests.TerraformServiceAccount(terraformResource) + ".id" sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -874,7 +879,7 @@ func TestAccTwingateResourceAddAccessServiceAccounts(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource15(remoteNetworkName, resourceName, createServiceAccount(resourceName, serviceAccountName)), + Config: configResourceWithServiceAccount(terraformResource, test.RandomName(), test.RandomResourceName(), serviceAccountConfig, serviceAccountID), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), @@ -884,44 +889,55 @@ func TestAccTwingateResourceAddAccessServiceAccounts(t *testing.T) { }) } -func createResource15(networkName, resourceName string, terraformServiceAccount string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test15" { - name = "%s" - } +func configResourceWithServiceAccount(terraformResource, networkName, resourceName, serviceAccountConfig, serviceAccountID string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - %s + ${service_account} - resource "twingate_resource" "test15" { - name = "%s" - address = "acc-test.com.15" - remote_network_id = twingate_remote_network.test15.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = ["80", "82-83"] + } + udp { + policy = "${udp_policy}" + } + } access { - service_account_ids = [%s] + service_account_ids = [${service_account_id}] } - - } - `, networkName, terraformServiceAccount, resourceName, model.PolicyRestricted, model.PolicyAllowAll, acctests.TerraformServiceAccount(resourceName)+".id") + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "service_account": serviceAccountConfig, + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + "service_account_id": serviceAccountID, + }) } func TestAccTwingateResourceAddAccessGroupsAndServiceAccounts(t *testing.T) { - const theResource = "twingate_resource.test16" - remoteNetworkName := test.RandomName() - resourceName := test.RandomResourceName() - serviceAccountName := test.RandomName("s16") - groups, groupsID := genNewGroups("g16", 1) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) + + groups, groupsID := genNewGroups(terraformResource, 1) + serviceAccountConfig := []string{configServiceAccount(terraformResource, test.RandomName())} + serviceAccountID := []string{acctests.TerraformServiceAccount(terraformResource) + ".id"} sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -929,7 +945,7 @@ func TestAccTwingateResourceAddAccessGroupsAndServiceAccounts(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource16(remoteNetworkName, resourceName, groups, groupsID, createServiceAccount(resourceName, serviceAccountName)), + Config: configResourceWithGroupsAndServiceAccounts(terraformResource, test.RandomName(), test.RandomResourceName(), groups, groupsID, serviceAccountConfig, serviceAccountID), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), @@ -940,57 +956,72 @@ func TestAccTwingateResourceAddAccessGroupsAndServiceAccounts(t *testing.T) { }) } -func createResource16(networkName, resourceName string, groups, groupsID []string, terraformServiceAccount string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test16" { - name = "%s" - } +func configResourceWithGroupsAndServiceAccounts(terraformResource, networkName, resourceName string, groups, groupIDs, serviceAccounts, serviceAccountIDs []string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - %s + ${group} - %s + ${service_account} - resource "twingate_resource" "test16" { - name = "%s" - address = "acc-test.com.16" - remote_network_id = twingate_remote_network.test16.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = ["80", "82-83"] + } + udp { + policy = "${udp_policy}" + } + } access { - group_ids = [%s] - service_account_ids = [%s] + group_ids = [${group_id}] + service_account_ids = [${service_account_id}] } - - } - `, networkName, strings.Join(groups, "\n"), terraformServiceAccount, resourceName, model.PolicyRestricted, model.PolicyAllowAll, strings.Join(groupsID, ", "), acctests.TerraformServiceAccount(resourceName)+".id") + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "service_account": strings.Join(serviceAccounts, "\n"), + "group": strings.Join(groups, "\n"), + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + "service_account_id": strings.Join(serviceAccountIDs, ", "), + "group_id": strings.Join(groupIDs, ", "), + }) } func TestAccTwingateResourceAccessServiceAccountsNotAuthoritative(t *testing.T) { t.Parallel() - const theResource = "twingate_resource.test17" + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - serviceAccounts, serviceAccountIDs := genNewServiceAccounts("s17", 3) - + serviceAccounts, serviceAccountIDs := genNewServiceAccounts(terraformResource, 3) serviceAccountResource := getResourceNameFromID(serviceAccountIDs[2]) + config := func(serviceAccountIDs []string) string { + return configResourceWithServiceAccountsAndAuthoritativeFlag(terraformResource, remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs, false) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource17(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), @@ -1003,7 +1034,7 @@ func TestAccTwingateResourceAccessServiceAccountsNotAuthoritative(t *testing.T) }, { // expecting no drift - empty plan - Config: createResource17(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), PlanOnly: true, Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), @@ -1012,7 +1043,7 @@ func TestAccTwingateResourceAccessServiceAccountsNotAuthoritative(t *testing.T) }, { // added a new service account to the resource using terraform - Config: createResource17(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:2]), + Config: config(serviceAccountIDs[:2]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "2"), acctests.CheckResourceServiceAccountsLen(theResource, 3), @@ -1020,7 +1051,7 @@ func TestAccTwingateResourceAccessServiceAccountsNotAuthoritative(t *testing.T) }, { // remove one service account from the resource using terraform - Config: createResource17(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), acctests.CheckResourceServiceAccountsLen(theResource, 2), @@ -1028,7 +1059,7 @@ func TestAccTwingateResourceAccessServiceAccountsNotAuthoritative(t *testing.T) }, { // expecting no drift - empty plan - Config: createResource17(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), PlanOnly: true, Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), @@ -1041,7 +1072,7 @@ func TestAccTwingateResourceAccessServiceAccountsNotAuthoritative(t *testing.T) }, { // expecting no drift - empty plan - Config: createResource17(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), PlanOnly: true, Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), @@ -1052,55 +1083,69 @@ func TestAccTwingateResourceAccessServiceAccountsNotAuthoritative(t *testing.T) }) } -func createResource17(networkName, resourceName string, serviceAccounts, serviceAccountIDs []string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test17" { - name = "%s" - } +func configResourceWithServiceAccountsAndAuthoritativeFlag(terraformResource, networkName, resourceName string, serviceAccounts, serviceAccountIDs []string, authoritative bool) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - %s + ${service_account} - resource "twingate_resource" "test17" { - name = "%s" - address = "acc-test.com.17" - remote_network_id = twingate_remote_network.test17.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = ["80", "82-83"] + } + udp { + policy = "${udp_policy}" + } + } - is_authoritative = false + is_authoritative = ${authoritative} access { - service_account_ids = [%s] + service_account_ids = [${service_account_id}] } - - } - `, networkName, strings.Join(serviceAccounts, "\n"), resourceName, model.PolicyRestricted, model.PolicyAllowAll, strings.Join(serviceAccountIDs, ", ")) + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "service_account": strings.Join(serviceAccounts, "\n"), + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + "service_account_id": strings.Join(serviceAccountIDs, ", "), + "authoritative": authoritative, + }) } func TestAccTwingateResourceAccessServiceAccountsAuthoritative(t *testing.T) { t.Parallel() - const theResource = "twingate_resource.test13" + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - serviceAccounts, serviceAccountIDs := genNewServiceAccounts("s13", 3) - + serviceAccounts, serviceAccountIDs := genNewServiceAccounts(terraformResource, 3) serviceAccountResource := getResourceNameFromID(serviceAccountIDs[2]) + config := func(serviceAccountIDs []string) string { + return configResourceWithServiceAccountsAndAuthoritativeFlag(terraformResource, remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs, true) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource13(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), @@ -1114,7 +1159,7 @@ func TestAccTwingateResourceAccessServiceAccountsAuthoritative(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: createResource13(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), acctests.CheckResourceServiceAccountsLen(theResource, 1), @@ -1122,14 +1167,14 @@ func TestAccTwingateResourceAccessServiceAccountsAuthoritative(t *testing.T) { }, { // added 2 new service accounts to the resource using terraform - Config: createResource13(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs), + Config: config(serviceAccountIDs), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "3"), acctests.CheckResourceServiceAccountsLen(theResource, 3), ), }, { - Config: createResource13(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs), + Config: config(serviceAccountIDs), Check: acctests.ComposeTestCheckFunc( // delete one service account from the resource using API acctests.DeleteResourceServiceAccount(theResource, serviceAccountResource), @@ -1141,7 +1186,7 @@ func TestAccTwingateResourceAccessServiceAccountsAuthoritative(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: createResource13(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs), + Config: config(serviceAccountIDs), Check: acctests.ComposeTestCheckFunc( acctests.CheckResourceServiceAccountsLen(theResource, 3), sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "3"), @@ -1149,7 +1194,7 @@ func TestAccTwingateResourceAccessServiceAccountsAuthoritative(t *testing.T) { }, { // remove 2 service accounts from the resource using terraform - Config: createResource13(remoteNetworkName, resourceName, serviceAccounts, serviceAccountIDs[:1]), + Config: config(serviceAccountIDs[:1]), Check: acctests.ComposeTestCheckFunc( acctests.CheckResourceServiceAccountsLen(theResource, 1), sdk.TestCheckResourceAttr(theResource, accessServiceAccountIdsLen, "1"), @@ -1159,40 +1204,10 @@ func TestAccTwingateResourceAccessServiceAccountsAuthoritative(t *testing.T) { }) } -func createResource13(networkName, resourceName string, serviceAccounts, serviceAccountIDs []string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test13" { - name = "%s" - } - - %s - - resource "twingate_resource" "test13" { - name = "%s" - address = "acc-test.com.13" - remote_network_id = twingate_remote_network.test13.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } - - is_authoritative = true - access { - service_account_ids = [%s] - } - - } - `, networkName, strings.Join(serviceAccounts, "\n"), resourceName, model.PolicyRestricted, model.PolicyAllowAll, strings.Join(serviceAccountIDs, ", ")) -} - func TestAccTwingateResourceAccessWithEmptyGroups(t *testing.T) { + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -1202,44 +1217,57 @@ func TestAccTwingateResourceAccessWithEmptyGroups(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource18(remoteNetworkName, resourceName), + Config: configResourceWithGroups(terraformResource, remoteNetworkName, resourceName, nil, nil), ExpectError: regexp.MustCompile("Error: Not enough list items"), }, }, }) } -func createResource18(networkName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test18" { - name = "%s" - } +func configResourceWithGroups(terraformResource, networkName, resourceName string, groups, groupsID []string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - resource "twingate_resource" "test18" { - name = "%s" - address = "acc-test.com.18" - remote_network_id = twingate_remote_network.test18.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } + ${group} + + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = ["80", "82-83"] + } + udp { + policy = "${udp_policy}" + } + } access { - group_ids = [] + group_ids = [${group_id}] } - - } - `, networkName, resourceName, model.PolicyRestricted, model.PolicyAllowAll) + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "group": strings.Join(groups, "\n"), + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + "group_id": strings.Join(groupsID, ", "), + }) } func TestAccTwingateResourceAccessWithEmptyServiceAccounts(t *testing.T) { + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -1249,44 +1277,17 @@ func TestAccTwingateResourceAccessWithEmptyServiceAccounts(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource19(remoteNetworkName, resourceName), + Config: configResourceWithServiceAccount(terraformResource, remoteNetworkName, resourceName, "", ""), ExpectError: regexp.MustCompile("Error: Not enough list items"), }, }, }) } -func createResource19(networkName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test19" { - name = "%s" - } - - resource "twingate_resource" "test19" { - name = "%s" - address = "acc-test.com.19" - remote_network_id = twingate_remote_network.test19.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } - - access { - service_account_ids = [] - } - - } - `, networkName, resourceName, model.PolicyRestricted, model.PolicyAllowAll) -} - func TestAccTwingateResourceAccessWithEmptyBlock(t *testing.T) { + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -1296,58 +1297,69 @@ func TestAccTwingateResourceAccessWithEmptyBlock(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource20(remoteNetworkName, resourceName), + Config: configResourceWithEmptyAccessBlock(terraformResource, remoteNetworkName, resourceName), ExpectError: regexp.MustCompile("Missing required argument"), }, }, }) } -func createResource20(networkName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test20" { - name = "%s" - } +func configResourceWithEmptyAccessBlock(terraformResource, networkName, resourceName string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - resource "twingate_resource" "test20" { - name = "%s" - address = "acc-test.com.20" - remote_network_id = twingate_remote_network.test20.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = ["80", "82-83"] + } + udp { + policy = "${udp_policy}" + } + } access { } - - } - `, networkName, resourceName, model.PolicyRestricted, model.PolicyAllowAll) + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + }) } func TestAccTwingateResourceAccessGroupsNotAuthoritative(t *testing.T) { t.Parallel() - const theResource = "twingate_resource.test22" + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - groups, groupsID := genNewGroups("g22", 3) - + groups, groupsID := genNewGroups(terraformResource, 3) groupResource := getResourceNameFromID(groupsID[2]) + config := func(groupsID []string) string { + return configResourceWithGroupsAndAuthoritativeFlag(terraformResource, remoteNetworkName, resourceName, groups, groupsID, false) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource22(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), @@ -1360,7 +1372,7 @@ func TestAccTwingateResourceAccessGroupsNotAuthoritative(t *testing.T) { }, { // expecting no drift - empty plan - Config: createResource22(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), PlanOnly: true, Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), @@ -1369,7 +1381,7 @@ func TestAccTwingateResourceAccessGroupsNotAuthoritative(t *testing.T) { }, { // added a new group to the resource using terraform - Config: createResource22(remoteNetworkName, resourceName, groups, groupsID[:2]), + Config: config(groupsID[:2]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "2"), acctests.CheckResourceGroupsLen(theResource, 3), @@ -1377,7 +1389,7 @@ func TestAccTwingateResourceAccessGroupsNotAuthoritative(t *testing.T) { }, { // remove one group from the resource using terraform - Config: createResource22(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), acctests.CheckResourceGroupsLen(theResource, 2), @@ -1385,7 +1397,7 @@ func TestAccTwingateResourceAccessGroupsNotAuthoritative(t *testing.T) { }, { // expecting no drift - empty plan - Config: createResource22(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), PlanOnly: true, Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), @@ -1398,7 +1410,7 @@ func TestAccTwingateResourceAccessGroupsNotAuthoritative(t *testing.T) { }, { // expecting no drift - empty plan - Config: createResource22(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), PlanOnly: true, Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), @@ -1409,55 +1421,69 @@ func TestAccTwingateResourceAccessGroupsNotAuthoritative(t *testing.T) { }) } -func createResource22(networkName, resourceName string, groups, groupsID []string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test22" { - name = "%s" - } +func configResourceWithGroupsAndAuthoritativeFlag(terraformResource, networkName, resourceName string, groups, groupIDs []string, authoritative bool) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - %s + ${groups} - resource "twingate_resource" "test22" { - name = "%s" - address = "acc-test.com.22" - remote_network_id = twingate_remote_network.test22.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = ["80", "82-83"] + } + udp { + policy = "${udp_policy}" + } + } - is_authoritative = false + is_authoritative = ${authoritative} access { - group_ids = [%s] + group_ids = [${group_ids}] } - - } - `, networkName, strings.Join(groups, "\n"), resourceName, model.PolicyRestricted, model.PolicyAllowAll, strings.Join(groupsID, ", ")) + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "groups": strings.Join(groups, "\n"), + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + "group_ids": strings.Join(groupIDs, ", "), + "authoritative": authoritative, + }) } func TestAccTwingateResourceAccessGroupsAuthoritative(t *testing.T) { t.Parallel() - const theResource = "twingate_resource.test23" + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - groups, groupsID := genNewGroups("g23", 3) - + groups, groupsID := genNewGroups(terraformResource, 3) groupResource := getResourceNameFromID(groupsID[2]) + config := func(groupsID []string) string { + return configResourceWithGroupsAndAuthoritativeFlag(terraformResource, remoteNetworkName, resourceName, groups, groupsID, true) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource23(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), @@ -1471,7 +1497,7 @@ func TestAccTwingateResourceAccessGroupsAuthoritative(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: createResource23(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), acctests.CheckResourceGroupsLen(theResource, 1), @@ -1479,14 +1505,14 @@ func TestAccTwingateResourceAccessGroupsAuthoritative(t *testing.T) { }, { // added 2 new groups to the resource using terraform - Config: createResource23(remoteNetworkName, resourceName, groups, groupsID), + Config: config(groupsID), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "3"), acctests.CheckResourceGroupsLen(theResource, 3), ), }, { - Config: createResource23(remoteNetworkName, resourceName, groups, groupsID), + Config: config(groupsID), Check: acctests.ComposeTestCheckFunc( // delete one group from the resource using API acctests.DeleteResourceGroup(theResource, groupResource), @@ -1498,7 +1524,7 @@ func TestAccTwingateResourceAccessGroupsAuthoritative(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: createResource23(remoteNetworkName, resourceName, groups, groupsID), + Config: config(groupsID), Check: acctests.ComposeTestCheckFunc( acctests.CheckResourceGroupsLen(theResource, 3), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "3"), @@ -1506,7 +1532,7 @@ func TestAccTwingateResourceAccessGroupsAuthoritative(t *testing.T) { }, { // remove 2 groups from the resource using terraform - Config: createResource23(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), acctests.CheckResourceGroupsLen(theResource, 1), @@ -1516,39 +1542,6 @@ func TestAccTwingateResourceAccessGroupsAuthoritative(t *testing.T) { }) } -func createResource23(networkName, resourceName string, groups, groupsID []string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test23" { - name = "%s" - } - - %s - - resource "twingate_resource" "test23" { - name = "%s" - address = "acc-test.com.23" - remote_network_id = twingate_remote_network.test23.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } - - is_authoritative = true - access { - group_ids = [%s] - } - - } - `, networkName, strings.Join(groups, "\n"), resourceName, model.PolicyRestricted, model.PolicyAllowAll, strings.Join(groupsID, ", ")) -} - func TestGetResourceNameFromID(t *testing.T) { cases := []struct { input string @@ -1575,8 +1568,10 @@ func TestGetResourceNameFromID(t *testing.T) { } func TestAccTwingateCreateResourceWithFlagIsVisible(t *testing.T) { - const terraformResourceName = "test24" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -1586,7 +1581,7 @@ func TestAccTwingateCreateResourceWithFlagIsVisible(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createSimpleResource(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckNoResourceAttr(theResource, attr.IsVisible), @@ -1595,13 +1590,13 @@ func TestAccTwingateCreateResourceWithFlagIsVisible(t *testing.T) { { // expecting no changes - default value on the backend side is `true` PlanOnly: true, - Config: createResourceWithFlagIsVisible(terraformResourceName, remoteNetworkName, resourceName, true), + Config: configResourceWithVisibleFlag(terraformResource, remoteNetworkName, resourceName, true), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.IsVisible, "true"), ), }, { - Config: createResourceWithFlagIsVisible(terraformResourceName, remoteNetworkName, resourceName, false), + Config: configResourceWithVisibleFlag(terraformResource, remoteNetworkName, resourceName, false), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.IsVisible, "false"), ), @@ -1609,7 +1604,7 @@ func TestAccTwingateCreateResourceWithFlagIsVisible(t *testing.T) { { // expecting no changes - no drift after re-applying changes PlanOnly: true, - Config: createResourceWithFlagIsVisible(terraformResourceName, remoteNetworkName, resourceName, false), + Config: configResourceWithVisibleFlag(terraformResource, remoteNetworkName, resourceName, false), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.IsVisible, "false"), ), @@ -1617,7 +1612,7 @@ func TestAccTwingateCreateResourceWithFlagIsVisible(t *testing.T) { { // expecting no changes - flag not set PlanOnly: true, - Config: createSimpleResource(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckNoResourceAttr(theResource, attr.IsVisible), ), @@ -1639,23 +1634,32 @@ func createSimpleResource(terraformResourceName, networkName, resourceName strin `, terraformResourceName, networkName, terraformResourceName, resourceName, terraformResourceName) } -func createResourceWithFlagIsVisible(terraformResourceName, networkName, resourceName string, isVisible bool) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" +func configResourceWithVisibleFlag(terraformResource, networkName, name string, isVisible bool) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } - resource "twingate_resource" "%s" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "acc-test.com" - remote_network_id = twingate_remote_network.%s.id - is_visible = %v + remote_network_id = twingate_remote_network.${network_resource}.id + is_visible = ${visible} } - `, terraformResourceName, networkName, terraformResourceName, resourceName, terraformResourceName, isVisible) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": name, + "visible": isVisible, + }) } func TestAccTwingateCreateResourceWithFlagIsBrowserShortcutEnabled(t *testing.T) { - const terraformResourceName = "test25" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -1665,7 +1669,7 @@ func TestAccTwingateCreateResourceWithFlagIsBrowserShortcutEnabled(t *testing.T) CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createSimpleResource(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckNoResourceAttr(theResource, attr.IsBrowserShortcutEnabled), @@ -1674,13 +1678,13 @@ func TestAccTwingateCreateResourceWithFlagIsBrowserShortcutEnabled(t *testing.T) { // expecting no changes - default value on the backend side is `true` PlanOnly: true, - Config: createResourceWithFlagIsBrowserShortcutEnabled(terraformResourceName, remoteNetworkName, resourceName, true), + Config: configResourceWithBrowserShortcutEnabledFlag(terraformResource, remoteNetworkName, resourceName, true), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.IsBrowserShortcutEnabled, "true"), ), }, { - Config: createResourceWithFlagIsBrowserShortcutEnabled(terraformResourceName, remoteNetworkName, resourceName, false), + Config: configResourceWithBrowserShortcutEnabledFlag(terraformResource, remoteNetworkName, resourceName, false), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.IsBrowserShortcutEnabled, "false"), ), @@ -1688,7 +1692,7 @@ func TestAccTwingateCreateResourceWithFlagIsBrowserShortcutEnabled(t *testing.T) { // expecting no changes - no drift after re-applying changes PlanOnly: true, - Config: createResourceWithFlagIsBrowserShortcutEnabled(terraformResourceName, remoteNetworkName, resourceName, false), + Config: configResourceWithBrowserShortcutEnabledFlag(terraformResource, remoteNetworkName, resourceName, false), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.IsBrowserShortcutEnabled, "false"), ), @@ -1696,7 +1700,7 @@ func TestAccTwingateCreateResourceWithFlagIsBrowserShortcutEnabled(t *testing.T) { // expecting no changes - flag not set PlanOnly: true, - Config: createSimpleResource(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckNoResourceAttr(theResource, attr.IsBrowserShortcutEnabled), ), @@ -1705,36 +1709,48 @@ func TestAccTwingateCreateResourceWithFlagIsBrowserShortcutEnabled(t *testing.T) }) } -func createResourceWithFlagIsBrowserShortcutEnabled(terraformResourceName, networkName, resourceName string, isBrowserShortcutEnabled bool) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" +func configResourceWithBrowserShortcutEnabledFlag(terraformResource, networkName, name string, isBrowserShortcutEnabled bool) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } - resource "twingate_resource" "%s" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "acc-test.com" - remote_network_id = twingate_remote_network.%s.id - is_browser_shortcut_enabled = %v + remote_network_id = twingate_remote_network.${network_resource}.id + is_browser_shortcut_enabled = ${browser_shortcut_enabled} } - `, terraformResourceName, networkName, terraformResourceName, resourceName, terraformResourceName, isBrowserShortcutEnabled) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": name, + "browser_shortcut_enabled": isBrowserShortcutEnabled, + }) } func TestAccTwingateResourceGroupsAuthoritativeByDefault(t *testing.T) { t.Parallel() - const theResource = "twingate_resource.test26" + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - groups, groupsID := genNewGroups("g26", 3) - + groups, groupsID := genNewGroups(terraformResource, 3) groupResource := getResourceNameFromID(groupsID[2]) + config := func(groupsID []string) string { + return configResourceWithGroups(terraformResource, remoteNetworkName, resourceName, groups, groupsID) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource26(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), @@ -1748,7 +1764,7 @@ func TestAccTwingateResourceGroupsAuthoritativeByDefault(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: createResource26(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), acctests.CheckResourceGroupsLen(theResource, 1), @@ -1756,14 +1772,14 @@ func TestAccTwingateResourceGroupsAuthoritativeByDefault(t *testing.T) { }, { // added 2 new groups to the resource using terraform - Config: createResource26(remoteNetworkName, resourceName, groups, groupsID), + Config: config(groupsID), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "3"), acctests.CheckResourceGroupsLen(theResource, 3), ), }, { - Config: createResource26(remoteNetworkName, resourceName, groups, groupsID), + Config: config(groupsID), Check: acctests.ComposeTestCheckFunc( // delete one group from the resource using API acctests.DeleteResourceGroup(theResource, groupResource), @@ -1775,7 +1791,7 @@ func TestAccTwingateResourceGroupsAuthoritativeByDefault(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: createResource26(remoteNetworkName, resourceName, groups, groupsID), + Config: config(groupsID), Check: acctests.ComposeTestCheckFunc( acctests.CheckResourceGroupsLen(theResource, 3), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "3"), @@ -1783,7 +1799,7 @@ func TestAccTwingateResourceGroupsAuthoritativeByDefault(t *testing.T) { }, { // remove 2 groups from the resource using terraform - Config: createResource26(remoteNetworkName, resourceName, groups, groupsID[:1]), + Config: config(groupsID[:1]), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "1"), acctests.CheckResourceGroupsLen(theResource, 1), @@ -1793,43 +1809,13 @@ func TestAccTwingateResourceGroupsAuthoritativeByDefault(t *testing.T) { }) } -func createResource26(networkName, resourceName string, groups, groupsID []string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test26" { - name = "%s" - } - - %s - - resource "twingate_resource" "test26" { - name = "%s" - address = "acc-test.com.26" - remote_network_id = twingate_remote_network.test26.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } - - access { - group_ids = [%s] - } - - } - `, networkName, strings.Join(groups, "\n"), resourceName, model.PolicyRestricted, model.PolicyAllowAll, strings.Join(groupsID, ", ")) -} - func TestAccTwingateResourceDoesNotSupportOldGroups(t *testing.T) { + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - - groups, groupsID := genNewGroups("g28", 2) + groups, groupsID := genNewGroups(terraformResource, 2) sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -1837,46 +1823,56 @@ func TestAccTwingateResourceDoesNotSupportOldGroups(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource28(remoteNetworkName, resourceName, groups, groupsID), + Config: configResourceWithOldGroups(terraformResource, remoteNetworkName, resourceName, groups, groupsID), ExpectError: regexp.MustCompile("Error: Unsupported argument"), }, }, }) } -func createResource28(networkName, resourceName string, groups, groupsID []string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test28" { - name = "%s" - } - - %s +func configResourceWithOldGroups(terraformResource, networkName, resourceName string, groups, groupsID []string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } - resource "twingate_resource" "test28" { - name = "%s" - address = "acc-test.com.28" - remote_network_id = twingate_remote_network.test28.id - - group_ids = [%s] + ${group} - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + protocols { + allow_icmp = true + tcp { + policy = "${tcp_policy}" + ports = ["80", "82-83"] + } + udp { + policy = "${udp_policy}" + } + } - } - `, networkName, strings.Join(groups, "\n"), resourceName, strings.Join(groupsID, ", "), model.PolicyRestricted, model.PolicyAllowAll) + group_ids = [${group_id}] + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "group": strings.Join(groups, "\n"), + "resource_resource": terraformResource, + "resource_name": resourceName, + "tcp_policy": model.PolicyRestricted, + "udp_policy": model.PolicyAllowAll, + "group_id": strings.Join(groupsID, ", "), + }) } func TestAccTwingateResourceCreateWithAlias(t *testing.T) { - const terraformResourceName = "test29" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() const aliasName = "test.com" @@ -1887,7 +1883,7 @@ func TestAccTwingateResourceCreateWithAlias(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResource29(terraformResourceName, remoteNetworkName, resourceName, aliasName), + Config: configResourceWithAlias(terraformResource, remoteNetworkName, resourceName, aliasName), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, attr.Name, resourceName), @@ -1896,14 +1892,14 @@ func TestAccTwingateResourceCreateWithAlias(t *testing.T) { }, { // alias attr commented out, means state keeps the same value without changes - Config: createResource29WithoutAlias(terraformResourceName, remoteNetworkName, resourceName), + Config: configResourceBasic(terraformResource, remoteNetworkName, resourceName), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.Alias, aliasName), ), }, { // alias attr set with emtpy string - Config: createResource29(terraformResourceName, remoteNetworkName, resourceName, ""), + Config: configResourceWithAlias(terraformResource, remoteNetworkName, resourceName, ""), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, attr.Alias, ""), ), @@ -1912,43 +1908,40 @@ func TestAccTwingateResourceCreateWithAlias(t *testing.T) { }) } -func createResource29(terraformResourceName, networkName, resourceName, aliasName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" - } - resource "twingate_resource" "%s" { - name = "%s" - address = "acc-test.com" - remote_network_id = twingate_remote_network.%s.id - alias = "%s" - } - `, terraformResourceName, networkName, terraformResourceName, resourceName, terraformResourceName, aliasName) -} - -func createResource29WithoutAlias(terraformResourceName, networkName, resourceName string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" +func configResourceWithAlias(terraformResource, networkName, name, alias string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" } - resource "twingate_resource" "%s" { - name = "%s" + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" address = "acc-test.com" - remote_network_id = twingate_remote_network.%s.id - # alias = "some.value" + remote_network_id = twingate_remote_network.${network_resource}.id + alias = "${alias}" } - `, terraformResourceName, networkName, terraformResourceName, resourceName, terraformResourceName) + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": name, + "alias": alias, + }) } func TestAccTwingateResourceGroupsCursor(t *testing.T) { - acctests.SetPageLimit(1) + acctests.SetPageLimit(t, 1) - const terraformResourceName = "test27" - theResource := acctests.TerraformResource(terraformResourceName) + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - groups, groupsID := genNewGroups("g27", 3) - serviceAccounts, serviceAccountIDs := genNewServiceAccounts("s27", 3) + groups, groupsID := genNewGroups(terraformResource, 3) + serviceAccounts, serviceAccountIDs := genNewServiceAccounts(terraformResource, 3) + + config := func(groupsID, serviceAccountIDs []string) string { + return configResourceWithGroupsAndServiceAccounts(terraformResource, remoteNetworkName, resourceName, groups, groupsID, serviceAccounts, serviceAccountIDs) + } sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -1956,7 +1949,7 @@ func TestAccTwingateResourceGroupsCursor(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithGroupsAndServiceAccounts(terraformResourceName, remoteNetworkName, resourceName, groups, groupsID, serviceAccounts, serviceAccountIDs), + Config: config(groupsID, serviceAccountIDs), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "3"), @@ -1964,7 +1957,7 @@ func TestAccTwingateResourceGroupsCursor(t *testing.T) { ), }, { - Config: createResourceWithGroupsAndServiceAccounts(terraformResourceName, remoteNetworkName, resourceName, groups, groupsID[:2], serviceAccounts, serviceAccountIDs[:2]), + Config: config(groupsID[:2], serviceAccountIDs[:2]), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, accessGroupIdsLen, "2"), @@ -1975,111 +1968,67 @@ func TestAccTwingateResourceGroupsCursor(t *testing.T) { }) } -func createResourceWithGroupsAndServiceAccounts(name, networkName, resourceName string, groups, groupsID, serviceAccounts, serviceAccountIDs []string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%s" { - name = "%s" - } - - %s - - %s - - resource "twingate_resource" "%s" { - name = "%s" - address = "acc-test.com.26" - remote_network_id = twingate_remote_network.%s.id - - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["80", "82-83"] - } - udp { - policy = "%s" - } - } - - access { - group_ids = [%s] - service_account_ids = [%s] - } - - } - `, name, networkName, strings.Join(groups, "\n"), strings.Join(serviceAccounts, "\n"), name, resourceName, name, model.PolicyRestricted, model.PolicyAllowAll, strings.Join(groupsID, ", "), strings.Join(serviceAccountIDs, ", ")) -} - func TestAccTwingateResourceCreateWithPort(t *testing.T) { + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() + config := func(portRange string) string { + return configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, portRange) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPort(remoteNetworkName, resourceName, "0"), + Config: config(`"0"`), ExpectError: regexp.MustCompile("port 0 not in the range of 1-65535"), }, { - Config: createResourceWithPort(remoteNetworkName, resourceName, "65536"), + Config: config(`"65536"`), ExpectError: regexp.MustCompile("port 65536 not in the range"), }, { - Config: createResourceWithPort(remoteNetworkName, resourceName, "0-10"), + Config: config(`"0-10"`), ExpectError: regexp.MustCompile("port 0 not in the range"), }, { - Config: createResourceWithPort(remoteNetworkName, resourceName, "65535-65536"), + Config: config(`"65535-65536"`), ExpectError: regexp.MustCompile("port 65536 not in the[\\n\\s]+range"), }, }, }) } -func createResourceWithPort(networkName, resourceName, port string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "test30" { - name = "%s" - } - resource "twingate_resource" "test30" { - name = "%s" - address = "new-acc-test.com" - remote_network_id = twingate_remote_network.test30.id - protocols { - allow_icmp = true - tcp { - policy = "%s" - ports = ["%s"] - } - udp { - policy = "%s" - } - } - } - `, networkName, resourceName, model.PolicyRestricted, port, model.PolicyAllowAll) -} - func TestAccTwingateResourceUpdateWithPort(t *testing.T) { - theResource := acctests.TerraformResource("test30") + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() + config := func(portRange string) string { + return configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, portRange) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPort(remoteNetworkName, resourceName, "1"), + Config: config(`"1"`), Check: acctests.ComposeTestCheckFunc( sdk.TestCheckResourceAttr(theResource, firstTCPPort, "1"), ), }, { - Config: createResourceWithPort(remoteNetworkName, resourceName, "0"), + Config: config(`"0"`), ExpectError: regexp.MustCompile("port 0 not in the range of 1-65535"), }, }, @@ -2087,56 +2036,78 @@ func TestAccTwingateResourceUpdateWithPort(t *testing.T) { } func TestAccTwingateResourceWithPortsFailsForAllowAllAndDenyAllPolicy(t *testing.T) { - const terraformResourceName = "test28" + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() + config := func(policy string) string { + return configResourceWithPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, policy, `"80", "82-83"`) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyAllowAll), + Config: config(model.PolicyAllowAll), ExpectError: regexp.MustCompile(resource.ErrPortsWithPolicyAllowAll.Error()), }, { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyDenyAll), + Config: config(model.PolicyDenyAll), ExpectError: regexp.MustCompile(resource.ErrPortsWithPolicyDenyAll.Error()), }, }, }) } -func createResourceWithPorts(name, networkName, resourceName, policy string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%[1]s" { - name = "%[2]s" - } - resource "twingate_resource" "%[1]s" { - name = "%[3]s" - address = "acc-test-%[1]s.com" - remote_network_id = twingate_remote_network.%[1]s.id - - protocols { - allow_icmp = true - tcp { - policy = "%[4]s" - ports = ["80", "82-83"] - } - udp { - policy = "%[5]s" - } - } - } - `, name, networkName, resourceName, policy, model.PolicyAllowAll) +func configResourceWithPolicyAndPortRange(terraformResource, networkName, resourceName, policy, portRange string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } + + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "new-acc-test.com" + remote_network_id = twingate_remote_network.${network_resource}.id + + protocols { + allow_icmp = true + tcp { + policy = "${policy}" + ports = [${port_range}] + } + udp { + policy = "${policy}" + ports = [${port_range}] + } + } + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": resourceName, + "policy": policy, + "port_range": portRange, + }) } func TestAccTwingateResourceWithoutPortsOkForAllowAllAndDenyAllPolicy(t *testing.T) { - const terraformResourceName = "test29" + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - theResource := acctests.TerraformResource(terraformResourceName) + theResource := acctests.TerraformResource(terraformResource) + + config := func(policy string) string { + return configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, policy, policy) + } sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -2144,7 +2115,7 @@ func TestAccTwingateResourceWithoutPortsOkForAllowAllAndDenyAllPolicy(t *testing CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyAllowAll), + Config: config(model.PolicyAllowAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyAllowAll), @@ -2152,7 +2123,7 @@ func TestAccTwingateResourceWithoutPortsOkForAllowAllAndDenyAllPolicy(t *testing ), }, { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyDenyAll), + Config: config(model.PolicyDenyAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyDenyAll), @@ -2163,34 +2134,17 @@ func TestAccTwingateResourceWithoutPortsOkForAllowAllAndDenyAllPolicy(t *testing }) } -func createResourceWithoutPorts(name, networkName, resourceName, policy string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%[1]s" { - name = "%[2]s" - } - resource "twingate_resource" "%[1]s" { - name = "%[3]s" - address = "acc-test-%[1]s.com" - remote_network_id = twingate_remote_network.%[1]s.id - - protocols { - allow_icmp = true - tcp { - policy = "%[4]s" - } - udp { - policy = "%[5]s" - } - } - } - `, name, networkName, resourceName, policy, model.PolicyAllowAll) -} - func TestAccTwingateResourceWithRestrictedPolicy(t *testing.T) { - const terraformResourceName = "test30" + t.Parallel() + + terraformResource := test.RandomResourceName() remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() - theResource := acctests.TerraformResource(terraformResourceName) + theResource := acctests.TerraformResource(terraformResource) + + config := func(policy string) string { + return configResourceWithPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, policy, `"80", "82-83"`) + } sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, @@ -2198,7 +2152,7 @@ func TestAccTwingateResourceWithRestrictedPolicy(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyRestricted), + Config: config(model.PolicyRestricted), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyRestricted), @@ -2210,8 +2164,10 @@ func TestAccTwingateResourceWithRestrictedPolicy(t *testing.T) { } func TestAccTwingateResourcePolicyTransitionDenyAllToRestricted(t *testing.T) { - const terraformResourceName = "test31" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -2221,7 +2177,7 @@ func TestAccTwingateResourcePolicyTransitionDenyAllToRestricted(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyDenyAll), + Config: configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, model.PolicyDenyAll, model.PolicyDenyAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyDenyAll), @@ -2229,7 +2185,7 @@ func TestAccTwingateResourcePolicyTransitionDenyAllToRestricted(t *testing.T) { ), }, { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyRestricted), + Config: configResourceWithPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, model.PolicyRestricted, `"80", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyRestricted), @@ -2241,18 +2197,24 @@ func TestAccTwingateResourcePolicyTransitionDenyAllToRestricted(t *testing.T) { } func TestAccTwingateResourcePolicyTransitionDenyAllToAllowAll(t *testing.T) { - const terraformResourceName = "test32" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() + config := func(policy string) string { + return configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, policy, policy) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyDenyAll), + Config: config(model.PolicyDenyAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyDenyAll), @@ -2260,7 +2222,7 @@ func TestAccTwingateResourcePolicyTransitionDenyAllToAllowAll(t *testing.T) { ), }, { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyAllowAll), + Config: config(model.PolicyAllowAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyAllowAll), @@ -2272,8 +2234,10 @@ func TestAccTwingateResourcePolicyTransitionDenyAllToAllowAll(t *testing.T) { } func TestAccTwingateResourcePolicyTransitionRestrictedToDenyAll(t *testing.T) { - const terraformResourceName = "test33" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -2283,7 +2247,7 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToDenyAll(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyRestricted), + Config: configResourceWithPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, model.PolicyRestricted, `"80", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyRestricted), @@ -2291,7 +2255,7 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToDenyAll(t *testing.T) { ), }, { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyDenyAll), + Config: configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, model.PolicyDenyAll, model.PolicyDenyAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyDenyAll), @@ -2303,8 +2267,10 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToDenyAll(t *testing.T) { } func TestAccTwingateResourcePolicyTransitionRestrictedToAllowAll(t *testing.T) { - const terraformResourceName = "test34" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -2314,7 +2280,7 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToAllowAll(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyRestricted), + Config: configResourceWithPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, model.PolicyRestricted, `"80", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyRestricted), @@ -2322,7 +2288,7 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToAllowAll(t *testing.T) { ), }, { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyAllowAll), + Config: configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, model.PolicyAllowAll, model.PolicyAllowAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyAllowAll), @@ -2334,18 +2300,24 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToAllowAll(t *testing.T) { } func TestAccTwingateResourcePolicyTransitionRestrictedToAllowAllWithPortsShouldFail(t *testing.T) { - const terraformResourceName = "test35" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() + config := func(policy string) string { + return configResourceWithPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, policy, `"80", "82-83"`) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyRestricted), + Config: config(model.PolicyRestricted), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyRestricted), @@ -2353,7 +2325,7 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToAllowAllWithPortsShouldF ), }, { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyAllowAll), + Config: config(model.PolicyAllowAll), ExpectError: regexp.MustCompile(resource.ErrPortsWithPolicyAllowAll.Error()), }, }, @@ -2361,8 +2333,10 @@ func TestAccTwingateResourcePolicyTransitionRestrictedToAllowAllWithPortsShouldF } func TestAccTwingateResourcePolicyTransitionAllowAllToRestricted(t *testing.T) { - const terraformResourceName = "test36" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() @@ -2372,7 +2346,7 @@ func TestAccTwingateResourcePolicyTransitionAllowAllToRestricted(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyAllowAll), + Config: configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, model.PolicyAllowAll, model.PolicyAllowAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyAllowAll), @@ -2380,7 +2354,7 @@ func TestAccTwingateResourcePolicyTransitionAllowAllToRestricted(t *testing.T) { ), }, { - Config: createResourceWithPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyRestricted), + Config: configResourceWithPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, model.PolicyRestricted, `"80", "82-83"`), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyRestricted), @@ -2392,18 +2366,24 @@ func TestAccTwingateResourcePolicyTransitionAllowAllToRestricted(t *testing.T) { } func TestAccTwingateResourcePolicyTransitionAllowAllToDenyAll(t *testing.T) { - const terraformResourceName = "test37" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() + config := func(policy string) string { + return configResourceWithPolicy(terraformResource, remoteNetworkName, resourceName, policy, policy) + } + sdk.Test(t, sdk.TestCase{ ProtoV6ProviderFactories: acctests.ProviderFactories, PreCheck: func() { acctests.PreCheck(t) }, CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyAllowAll), + Config: config(model.PolicyAllowAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyAllowAll), @@ -2411,7 +2391,7 @@ func TestAccTwingateResourcePolicyTransitionAllowAllToDenyAll(t *testing.T) { ), }, { - Config: createResourceWithoutPorts(terraformResourceName, remoteNetworkName, resourceName, model.PolicyDenyAll), + Config: config(model.PolicyDenyAll), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), sdk.TestCheckResourceAttr(theResource, tcpPolicy, model.PolicyDenyAll), @@ -2423,8 +2403,10 @@ func TestAccTwingateResourcePolicyTransitionAllowAllToDenyAll(t *testing.T) { } func TestAccTwingateResourceWithBrowserOption(t *testing.T) { - const terraformResourceName = "test40" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() wildcardAddress := "*.acc-test.com" @@ -2435,19 +2417,19 @@ func TestAccTwingateResourceWithBrowserOption(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithoutBrowserOption(terraformResourceName, remoteNetworkName, resourceName, wildcardAddress), + Config: configResourceWithAddress(terraformResource, remoteNetworkName, resourceName, wildcardAddress), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), }, { - Config: createResourceWithBrowserOption(terraformResourceName, remoteNetworkName, resourceName, wildcardAddress, false), + Config: configResourceWithAddressAndBrowserOption(terraformResource, remoteNetworkName, resourceName, wildcardAddress, false), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), }, { - Config: createResourceWithBrowserOption(terraformResourceName, remoteNetworkName, resourceName, wildcardAddress, true), + Config: configResourceWithAddressAndBrowserOption(terraformResource, remoteNetworkName, resourceName, wildcardAddress, true), ExpectError: regexp.MustCompile(resource.ErrWildcardAddressWithEnabledShortcut.Error()), }, }, @@ -2455,8 +2437,10 @@ func TestAccTwingateResourceWithBrowserOption(t *testing.T) { } func TestAccTwingateResourceWithBrowserOptionFailOnUpdate(t *testing.T) { - const terraformResourceName = "test41" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() wildcardAddress := "*.acc-test.com" @@ -2468,19 +2452,19 @@ func TestAccTwingateResourceWithBrowserOptionFailOnUpdate(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithoutBrowserOption(terraformResourceName, remoteNetworkName, resourceName, simpleAddress), + Config: configResourceWithAddress(terraformResource, remoteNetworkName, resourceName, simpleAddress), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), }, { - Config: createResourceWithBrowserOption(terraformResourceName, remoteNetworkName, resourceName, simpleAddress, true), + Config: configResourceWithAddressAndBrowserOption(terraformResource, remoteNetworkName, resourceName, simpleAddress, true), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), }, { - Config: createResourceWithBrowserOption(terraformResourceName, remoteNetworkName, resourceName, wildcardAddress, true), + Config: configResourceWithAddressAndBrowserOption(terraformResource, remoteNetworkName, resourceName, wildcardAddress, true), ExpectError: regexp.MustCompile(resource.ErrWildcardAddressWithEnabledShortcut.Error()), }, }, @@ -2488,8 +2472,10 @@ func TestAccTwingateResourceWithBrowserOptionFailOnUpdate(t *testing.T) { } func TestAccTwingateResourceWithBrowserOptionRecovered(t *testing.T) { - const terraformResourceName = "test42" - theResource := acctests.TerraformResource(terraformResourceName) + t.Parallel() + + terraformResource := test.RandomResourceName() + theResource := acctests.TerraformResource(terraformResource) remoteNetworkName := test.RandomName() resourceName := test.RandomResourceName() wildcardAddress := "*.acc-test.com" @@ -2501,13 +2487,13 @@ func TestAccTwingateResourceWithBrowserOptionRecovered(t *testing.T) { CheckDestroy: acctests.CheckTwingateResourceDestroy, Steps: []sdk.TestStep{ { - Config: createResourceWithBrowserOption(terraformResourceName, remoteNetworkName, resourceName, simpleAddress, true), + Config: configResourceWithAddressAndBrowserOption(terraformResource, remoteNetworkName, resourceName, simpleAddress, true), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), }, { - Config: createResourceWithoutBrowserOption(terraformResourceName, remoteNetworkName, resourceName, wildcardAddress), + Config: configResourceWithAddress(terraformResource, remoteNetworkName, resourceName, wildcardAddress), Check: acctests.ComposeTestCheckFunc( acctests.CheckTwingateResourceExists(theResource), ), @@ -2516,29 +2502,44 @@ func TestAccTwingateResourceWithBrowserOptionRecovered(t *testing.T) { }) } -func createResourceWithoutBrowserOption(name, networkName, resourceName, address string) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%[1]s" { - name = "%[2]s" - } - resource "twingate_resource" "%[1]s" { - name = "%[3]s" - address = "%[4]s" - remote_network_id = twingate_remote_network.%[1]s.id - } - `, name, networkName, resourceName, address) -} - -func createResourceWithBrowserOption(name, networkName, resourceName, address string, browserOption bool) string { - return fmt.Sprintf(` - resource "twingate_remote_network" "%[1]s" { - name = "%[2]s" - } - resource "twingate_resource" "%[1]s" { - name = "%[3]s" - address = "%[4]s" - remote_network_id = twingate_remote_network.%[1]s.id - is_browser_shortcut_enabled = %[5]v - } - `, name, networkName, resourceName, address, browserOption) +func configResourceWithAddress(terraformResource, networkName, name, address string) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "${address}" + remote_network_id = twingate_remote_network.${network_resource}.id + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": name, + "address": address, + }) +} + +func configResourceWithAddressAndBrowserOption(terraformResource, networkName, name, address string, browserFlag bool) string { + return acctests.Nprintf(` + resource "twingate_remote_network" "${network_resource}" { + name = "${network_name}" + } + resource "twingate_resource" "${resource_resource}" { + name = "${resource_name}" + address = "${address}" + remote_network_id = twingate_remote_network.${network_resource}.id + is_browser_shortcut_enabled = ${browser_flag} + } + `, + map[string]any{ + "network_resource": terraformResource, + "network_name": networkName, + "resource_resource": terraformResource, + "resource_name": name, + "address": address, + "browser_flag": browserFlag, + }) } diff --git a/twingate/internal/test/acctests/resource/service-account_test.go b/twingate/internal/test/acctests/resource/service-account_test.go index 4ac40fce..f4c65528 100644 --- a/twingate/internal/test/acctests/resource/service-account_test.go +++ b/twingate/internal/test/acctests/resource/service-account_test.go @@ -1,7 +1,6 @@ package resource import ( - "fmt" "testing" "github.com/Twingate/terraform-provider-twingate/twingate/internal/attr" @@ -11,95 +10,99 @@ import ( sdk "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) -func createServiceAccount(resourceName, serviceAccountName string) string { - return fmt.Sprintf(` - resource "twingate_service_account" "%s" { - name = "%s" +func configServiceAccount(resourceName, serviceAccountName string) string { + return acctests.Nprintf(` + resource "twingate_service_account" "${service_account_resource}" { + name = "${name}" } - `, resourceName, serviceAccountName) + `, + map[string]any{ + "service_account_resource": resourceName, + "name": serviceAccountName, + }) } func TestAccTwingateServiceAccountCreateUpdate(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Account Create/Update", func(t *testing.T) { - const terraformResourceName = "test01" - theResource := acctests.TerraformServiceAccount(terraformResourceName) - nameBefore := test.RandomName() - nameAfter := test.RandomName() + t.Parallel() + + resourceName := test.RandomServiceAccountName() + theResource := acctests.TerraformServiceAccount(resourceName) + name1 := test.RandomName() + name2 := test.RandomName() - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceAccount(terraformResourceName, nameBefore), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, nameBefore), - ), - }, - { - Config: createServiceAccount(terraformResourceName, nameAfter), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Name, nameAfter), - ), - }, + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceAccount(resourceName, name1), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name1), + ), }, - }) + { + Config: configServiceAccount(resourceName, name2), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Name, name2), + ), + }, + }, }) } func TestAccTwingateServiceAccountDeleteNonExisting(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Account Delete NonExisting", func(t *testing.T) { - const terraformResourceName = "test02" - theResource := acctests.TerraformServiceAccount(terraformResourceName) - name := test.RandomName() + t.Parallel() + + resourceName := test.RandomServiceAccountName() + theResource := acctests.TerraformServiceAccount(resourceName) + name := test.RandomName() - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceAccount(terraformResourceName, name), - Destroy: true, - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceDoesNotExists(theResource), - ), - }, + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceAccount(resourceName, name), + Destroy: true, + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceDoesNotExists(theResource), + ), }, - }) + }, }) } func TestAccTwingateServiceAccountReCreateAfterDeletion(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Account Create After Deletion", func(t *testing.T) { - const terraformResourceName = "test03" - theResource := acctests.TerraformServiceAccount(terraformResourceName) - name := test.RandomName() + t.Parallel() + + resourceName := test.RandomServiceAccountName() + theResource := acctests.TerraformServiceAccount(resourceName) + name := test.RandomName() - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceAccount(terraformResourceName, name), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - acctests.DeleteTwingateResource(theResource, resource.TwingateServiceAccount), - acctests.WaitTestFunc(), - ), - ExpectNonEmptyPlan: true, - }, - { - Config: createServiceAccount(terraformResourceName, name), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - ), - }, + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceAccount(resourceName, name), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + acctests.DeleteTwingateResource(theResource, resource.TwingateServiceAccount), + acctests.WaitTestFunc(), + ), + ExpectNonEmptyPlan: true, }, - }) + { + Config: configServiceAccount(resourceName, name), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + ), + }, + }, }) } diff --git a/twingate/internal/test/acctests/resource/service-key_test.go b/twingate/internal/test/acctests/resource/service-key_test.go index c81e3c84..d9b1cef4 100644 --- a/twingate/internal/test/acctests/resource/service-key_test.go +++ b/twingate/internal/test/acctests/resource/service-key_test.go @@ -2,7 +2,6 @@ package resource import ( "errors" - "fmt" "regexp" "testing" @@ -16,36 +15,53 @@ import ( var ErrEmptyValue = errors.New("empty value") -func createServiceKey(terraformResourceName, serviceAccountName string) string { - return fmt.Sprintf(` - %s +func configServiceKey(resourceName, serviceAccountName string) string { + return acctests.Nprintf(` + ${service_account} - resource "twingate_service_account_key" "%s" { - service_account_id = twingate_service_account.%s.id + resource "twingate_service_account_key" "${service_account_key_resource}" { + service_account_id = twingate_service_account.${service_account_resource}.id } - `, createServiceAccount(terraformResourceName, serviceAccountName), terraformResourceName, terraformResourceName) + `, + map[string]any{ + "service_account": configServiceAccount(resourceName, serviceAccountName), + "service_account_key_resource": resourceName, + "service_account_resource": resourceName, + }) } -func createServiceKeyWithName(terraformResourceName, serviceAccountName, serviceKeyName string) string { - return fmt.Sprintf(` - %s +func configServiceKeyWithName(resourceName, serviceAccountName, serviceKeyName string) string { + return acctests.Nprintf(` + ${service_account} - resource "twingate_service_account_key" "%s" { - service_account_id = twingate_service_account.%s.id - name = "%s" + resource "twingate_service_account_key" "${service_account_key_resource}" { + service_account_id = twingate_service_account.${service_account_resource}.id + name = "${name}" } - `, createServiceAccount(terraformResourceName, serviceAccountName), terraformResourceName, terraformResourceName, serviceKeyName) + `, + map[string]any{ + "service_account": configServiceAccount(resourceName, serviceAccountName), + "service_account_key_resource": resourceName, + "service_account_resource": resourceName, + "name": serviceKeyName, + }) } -func createServiceKeyWithExpiration(terraformResourceName, serviceAccountName string, expirationTime int) string { - return fmt.Sprintf(` - %s +func configServiceKeyWithExpiration(resourceName, serviceAccountName string, expirationTime int) string { + return acctests.Nprintf(` + ${service_account} - resource "twingate_service_account_key" "%s" { - service_account_id = twingate_service_account.%s.id - expiration_time = %v + resource "twingate_service_account_key" "${service_account_key_resource}" { + service_account_id = twingate_service_account.${service_account_resource}.id + expiration_time = ${expiration_time} } - `, createServiceAccount(terraformResourceName, serviceAccountName), terraformResourceName, terraformResourceName, expirationTime) + `, + map[string]any{ + "service_account": configServiceAccount(resourceName, serviceAccountName), + "service_account_key_resource": resourceName, + "service_account_resource": resourceName, + "expiration_time": expirationTime, + }) } func nonEmptyValue(value string) error { @@ -57,357 +73,365 @@ func nonEmptyValue(value string) error { } func TestAccTwingateServiceKeyCreateUpdate(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key Create/Update", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) - serviceKey := acctests.TerraformServiceKey(terraformResourceName) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKey(terraformResourceName, serviceAccountName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - ), - }, - { - Config: createServiceKey(terraformResourceName, serviceAccountName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) + serviceKey := acctests.TerraformServiceKey(terraformResourceName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKey(terraformResourceName, serviceAccountName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceAccount), + sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + ), }, - }) + { + Config: configServiceKey(terraformResourceName, serviceAccountName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceAccount), + sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + ), + }, + }, }) } func TestAccTwingateServiceKeyCreateUpdateWithName(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key Create/Update With Name", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) - serviceKey := acctests.TerraformServiceKey(terraformResourceName) - beforeName := test.RandomName() - afterName := test.RandomName() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKeyWithName(terraformResourceName, serviceAccountName, beforeName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.Name, beforeName), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - ), - }, - { - Config: createServiceKeyWithName(terraformResourceName, serviceAccountName, afterName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.Name, afterName), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - acctests.WaitTestFunc(), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) + serviceKey := acctests.TerraformServiceKey(terraformResourceName) + name1 := test.RandomName() + name2 := test.RandomName() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKeyWithName(terraformResourceName, serviceAccountName, name1), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceAccount), + sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttr(serviceKey, attr.Name, name1), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + ), }, - }) + { + Config: configServiceKeyWithName(terraformResourceName, serviceAccountName, name2), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceAccount), + sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttr(serviceKey, attr.Name, name2), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + acctests.WaitTestFunc(), + ), + }, + }, }) } func TestAccTwingateServiceKeyWontReCreateAfterInactive(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key Won't ReCreate After Inactive", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) - - resourceID := new(string) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKey(terraformResourceName, serviceAccountName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - acctests.GetTwingateResourceID(serviceKey, &resourceID), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - acctests.RevokeTwingateServiceKey(serviceKey), - acctests.WaitTestFunc(), - acctests.CheckTwingateServiceKeyStatus(serviceKey, model.StatusRevoked), - ), - }, - { - Config: createServiceKey(terraformResourceName, serviceAccountName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.IsActive, "false"), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { - if *resourceID == "" { - return errors.New("failed to fetch resource id") - } - - if value != *resourceID { - return errors.New("resource was re-created") - } - - return nil - }), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + serviceKey := acctests.TerraformServiceKey(terraformResourceName) + + resourceID := new(string) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKey(terraformResourceName, serviceAccountName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceKey), + acctests.GetTwingateResourceID(serviceKey, &resourceID), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + acctests.RevokeTwingateServiceKey(serviceKey), + acctests.WaitTestFunc(), + acctests.CheckTwingateServiceKeyStatus(serviceKey, model.StatusRevoked), + ), }, - }) + { + Config: configServiceKey(terraformResourceName, serviceAccountName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttr(serviceKey, attr.IsActive, "false"), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { + if *resourceID == "" { + return errors.New("failed to fetch resource id") + } + + if value != *resourceID { + return errors.New("resource was re-created") + } + + return nil + }), + ), + }, + }, }) } func TestAccTwingateServiceKeyDelete(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key Delete", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKey(terraformResourceName, serviceAccountName), - Destroy: true, - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceDoesNotExists(serviceKey), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + serviceKey := acctests.TerraformServiceKey(terraformResourceName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKey(terraformResourceName, serviceAccountName), + Destroy: true, + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceDoesNotExists(serviceKey), + ), }, - }) + }, }) } func TestAccTwingateServiceKeyReCreateAfterDeletion(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key ReCreate After Delete", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKey(terraformResourceName, serviceAccountName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - acctests.RevokeTwingateServiceKey(serviceKey), - acctests.DeleteTwingateResource(serviceKey, resource.TwingateServiceAccountKey), - ), - ExpectNonEmptyPlan: true, - }, - { - Config: createServiceKey(terraformResourceName, serviceAccountName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + serviceKey := acctests.TerraformServiceKey(terraformResourceName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKey(terraformResourceName, serviceAccountName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceKey), + acctests.RevokeTwingateServiceKey(serviceKey), + acctests.DeleteTwingateResource(serviceKey, resource.TwingateServiceAccountKey), + ), + ExpectNonEmptyPlan: true, }, - }) + { + Config: configServiceKey(terraformResourceName, serviceAccountName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + ), + }, + }, }) } func TestAccTwingateServiceKeyCreateWithInvalidExpiration(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key Create With Invalid Expiration", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKeyWithExpiration(terraformResourceName, serviceAccountName, -1), - ExpectError: regexp.MustCompile(resource.ErrInvalidExpirationTime.Error()), - }, - { - Config: createServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 366), - ExpectError: regexp.MustCompile(resource.ErrInvalidExpirationTime.Error()), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, -1), + ExpectError: regexp.MustCompile(resource.ErrInvalidExpirationTime.Error()), }, - }) + { + Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 366), + ExpectError: regexp.MustCompile(resource.ErrInvalidExpirationTime.Error()), + }, + }, }) } func TestAccTwingateServiceKeyCreateWithExpiration(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key Create With Expiration", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) - serviceKey := acctests.TerraformServiceKey(terraformResourceName) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 365), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttr(serviceKey, attr.IsActive, "true"), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + serviceAccount := acctests.TerraformServiceAccount(terraformResourceName) + serviceKey := acctests.TerraformServiceKey(terraformResourceName) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 365), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceAccount), + sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttr(serviceKey, attr.IsActive, "true"), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + ), }, - }) + }, }) } func TestAccTwingateServiceKeyReCreateAfterChangingExpirationTime(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key ReCreate After Changing Expiration Time", func(t *testing.T) { - serviceAccountName := test.RandomName() - terraformResourceName := test.TerraformRandName("test_key") - serviceKey := acctests.TerraformServiceKey(terraformResourceName) - - resourceID := new(string) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 1), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - acctests.GetTwingateResourceID(serviceKey, &resourceID), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - ), - }, - { - Config: createServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 2), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { - if *resourceID == "" { - return errors.New("failed to fetch resource id") - } - - if value == *resourceID { - return errors.New("resource was not re-created") - } - - return nil - }), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + terraformResourceName := test.TerraformRandName("test_key") + serviceKey := acctests.TerraformServiceKey(terraformResourceName) + + resourceID := new(string) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 1), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceKey), + acctests.GetTwingateResourceID(serviceKey, &resourceID), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + ), }, - }) + { + Config: configServiceKeyWithExpiration(terraformResourceName, serviceAccountName, 2), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { + if *resourceID == "" { + return errors.New("failed to fetch resource id") + } + + if value == *resourceID { + return errors.New("resource was not re-created") + } + + return nil + }), + ), + }, + }, }) } func TestAccTwingateServiceKeyAndServiceAccountLifecycle(t *testing.T) { - t.Run("Test Twingate Resource : Acc Service Key and Service Account Lifecycle", func(t *testing.T) { - serviceAccountName := test.RandomName() - serviceAccountNameV2 := test.RandomName() - terraformServiceAccountName := test.TerraformRandName("test_acc") - terraformServiceAccountNameV2 := test.TerraformRandName("test_acc_v2") - terraformServiceAccountKeyName := test.TerraformRandName("test_key") - serviceAccount := acctests.TerraformServiceAccount(terraformServiceAccountName) - serviceAccountV2 := acctests.TerraformServiceAccount(terraformServiceAccountNameV2) - serviceKey := acctests.TerraformServiceKey(terraformServiceAccountKeyName) - - serviceKeyResourceID := new(string) - serviceAccountResourceID := new(string) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, - Steps: []sdk.TestStep{ - { - Config: createServiceKeyV1(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, terraformServiceAccountName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccount), - sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - acctests.GetTwingateResourceID(serviceKey, &serviceKeyResourceID), - acctests.GetTwingateResourceID(serviceKey, &serviceAccountResourceID), - ), - }, - { - Config: createServiceKeyV1(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, terraformServiceAccountNameV2), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(serviceAccountV2), - sdk.TestCheckResourceAttr(serviceAccountV2, attr.Name, serviceAccountNameV2), - acctests.CheckTwingateResourceExists(serviceKey), - sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), - - // test resources were re-created - sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { - if *serviceKeyResourceID == "" { - return errors.New("failed to fetch service_key resource id") - } - - if value == *serviceKeyResourceID { - return errors.New("service_key resource was not re-created") - } - - return nil - }), - - sdk.TestCheckResourceAttrWith(serviceAccountV2, attr.ID, func(value string) error { - if *serviceAccountResourceID == "" { - return errors.New("failed to fetch service_account resource id") - } - - if value == *serviceAccountResourceID { - return errors.New("service_account resource was not re-created") - } - - return nil - }), - ), - }, + t.Parallel() + + serviceAccountName := test.RandomName() + serviceAccountNameV2 := test.RandomName() + terraformServiceAccountName := test.TerraformRandName("test_acc") + terraformServiceAccountNameV2 := test.TerraformRandName("test_acc_v2") + terraformServiceAccountKeyName := test.TerraformRandName("test_key") + serviceAccount := acctests.TerraformServiceAccount(terraformServiceAccountName) + serviceAccountV2 := acctests.TerraformServiceAccount(terraformServiceAccountNameV2) + serviceKey := acctests.TerraformServiceKey(terraformServiceAccountKeyName) + + serviceKeyResourceID := new(string) + serviceAccountResourceID := new(string) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateServiceAccountDestroy, + Steps: []sdk.TestStep{ + { + Config: configTwoServiceAccounts(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, terraformServiceAccountName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceAccount), + sdk.TestCheckResourceAttr(serviceAccount, attr.Name, serviceAccountName), + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + acctests.GetTwingateResourceID(serviceKey, &serviceKeyResourceID), + acctests.GetTwingateResourceID(serviceKey, &serviceAccountResourceID), + ), }, - }) + { + Config: configTwoServiceAccounts(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, terraformServiceAccountNameV2), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(serviceAccountV2), + sdk.TestCheckResourceAttr(serviceAccountV2, attr.Name, serviceAccountNameV2), + acctests.CheckTwingateResourceExists(serviceKey), + sdk.TestCheckResourceAttrWith(serviceKey, attr.Token, nonEmptyValue), + + // test resources were re-created + sdk.TestCheckResourceAttrWith(serviceKey, attr.ID, func(value string) error { + if *serviceKeyResourceID == "" { + return errors.New("failed to fetch service_key resource id") + } + + if value == *serviceKeyResourceID { + return errors.New("service_key resource was not re-created") + } + + return nil + }), + + sdk.TestCheckResourceAttrWith(serviceAccountV2, attr.ID, func(value string) error { + if *serviceAccountResourceID == "" { + return errors.New("failed to fetch service_account resource id") + } + + if value == *serviceAccountResourceID { + return errors.New("service_account resource was not re-created") + } + + return nil + }), + ), + }, + }, }) } -func createServiceKeyV1(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, serviceAccount string) string { - return fmt.Sprintf(` - resource "twingate_service_account" "%s" { - name = "%s" +func configTwoServiceAccounts(terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, serviceAccount string) string { + return acctests.Nprintf(` + resource "twingate_service_account" "${service_account_resource_1}" { + name = "${name_1}" } - resource "twingate_service_account" "%s" { - name = "%s" + resource "twingate_service_account" "${service_account_resource_2}" { + name = "${name_2}" } - resource "twingate_service_account_key" "%s" { - service_account_id = twingate_service_account.%s.id + resource "twingate_service_account_key" "${service_account_key_resource}" { + service_account_id = twingate_service_account.${service_account_resource}.id } - `, terraformServiceAccountName, serviceAccountName, terraformServiceAccountNameV2, serviceAccountNameV2, terraformServiceAccountKeyName, serviceAccount) + `, + map[string]any{ + "service_account_resource_1": terraformServiceAccountName, + "name_1": serviceAccountName, + "service_account_resource_2": terraformServiceAccountNameV2, + "name_2": serviceAccountNameV2, + "service_account_key_resource": terraformServiceAccountKeyName, + "service_account_resource": serviceAccount, + }) } diff --git a/twingate/internal/test/acctests/resource/user_test.go b/twingate/internal/test/acctests/resource/user_test.go index 172a9883..5bf33f8c 100644 --- a/twingate/internal/test/acctests/resource/user_test.go +++ b/twingate/internal/test/acctests/resource/user_test.go @@ -14,300 +14,322 @@ import ( ) func TestAccTwingateUserCreateUpdate(t *testing.T) { - t.Run("Test Twingate Resource : Acc User Create/Update", func(t *testing.T) { - const terraformResourceName = "test001" - theResource := acctests.TerraformUser(terraformResourceName) - email := test.RandomEmail() - firstName := test.RandomName() - lastName := test.RandomName() - role := model.UserRoleSupport - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUser(terraformResourceName, email), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email), - ), - }, - { - Config: terraformResourceTwingateUserWithFirstName(terraformResourceName, email, firstName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email), - sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), - ), - }, - { - Config: terraformResourceTwingateUserWithLastName(terraformResourceName, email, lastName), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email), - sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), - sdk.TestCheckResourceAttr(theResource, attr.LastName, lastName), - ), - }, - { - Config: terraformResourceTwingateUserWithRole(terraformResourceName, email, role), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email), - sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), - sdk.TestCheckResourceAttr(theResource, attr.LastName, lastName), - sdk.TestCheckResourceAttr(theResource, attr.Role, role), - ), - }, + t.Parallel() + + userResource := test.RandomUserName() + theResource := acctests.TerraformUser(userResource) + email := test.RandomEmail() + firstName := test.RandomName() + lastName := test.RandomName() + role := model.UserRoleSupport + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUser(userResource, email), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email), + ), + }, + { + Config: configUserWithFirstName(userResource, email, firstName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email), + sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), + ), + }, + { + Config: configUserWithLastName(userResource, email, lastName), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email), + sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), + sdk.TestCheckResourceAttr(theResource, attr.LastName, lastName), + ), }, - }) + { + Config: configUserWithRole(userResource, email, role), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email), + sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), + sdk.TestCheckResourceAttr(theResource, attr.LastName, lastName), + sdk.TestCheckResourceAttr(theResource, attr.Role, role), + ), + }, + }, }) } -func terraformResourceTwingateUser(terraformResourceName, email string) string { - return fmt.Sprintf(` - resource "twingate_user" "%s" { - email = "%s" +func configUser(userResource, email string) string { + return acctests.Nprintf(` + resource "twingate_user" "${user_resource}" { + email = "${email}" send_invite = false } - `, terraformResourceName, email) + `, map[string]any{ + "user_resource": userResource, + "email": email, + }) } -func terraformResourceTwingateUserWithFirstName(terraformResourceName, email, firstName string) string { - return fmt.Sprintf(` - resource "twingate_user" "%s" { - email = "%s" - first_name = "%s" +func configUserWithFirstName(userResource, email, firstName string) string { + return acctests.Nprintf(` + resource "twingate_user" "${user_resource}" { + email = "${email}" + first_name = "${first_name}" send_invite = false } - `, terraformResourceName, email, firstName) + `, map[string]any{ + "user_resource": userResource, + "email": email, + "first_name": firstName, + }) } -func terraformResourceTwingateUserWithLastName(terraformResourceName, email, lastName string) string { - return fmt.Sprintf(` - resource "twingate_user" "%s" { - email = "%s" - last_name = "%s" +func configUserWithLastName(userResource, email, lastName string) string { + return acctests.Nprintf(` + resource "twingate_user" "${user_resource}" { + email = "${email}" + last_name = "${last_name}" send_invite = false } - `, terraformResourceName, email, lastName) + `, map[string]any{ + "user_resource": userResource, + "email": email, + "last_name": lastName, + }) } -func terraformResourceTwingateUserWithRole(terraformResourceName, email, role string) string { - return fmt.Sprintf(` - resource "twingate_user" "%s" { - email = "%s" - role = "%s" +func configUserWithRole(userResource, email, role string) string { + return acctests.Nprintf(` + resource "twingate_user" "${user_resource}" { + email = "${email}" + role = "${role}" send_invite = false } - `, terraformResourceName, email, role) + `, map[string]any{ + "user_resource": userResource, + "email": email, + "role": role, + }) } func TestAccTwingateUserFullCreate(t *testing.T) { - t.Run("Test Twingate Resource : Acc User Full Create", func(t *testing.T) { - const terraformResourceName = "test002" - theResource := acctests.TerraformUser(terraformResourceName) - email := test.RandomEmail() - firstName := test.RandomName() - lastName := test.RandomName() - role := test.RandomUserRole() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUserFull(terraformResourceName, email, firstName, lastName, role), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email), - sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), - sdk.TestCheckResourceAttr(theResource, attr.LastName, lastName), - sdk.TestCheckResourceAttr(theResource, attr.Role, role), - ), - }, + t.Parallel() + + userResource := test.RandomUserName() + theResource := acctests.TerraformUser(userResource) + email := test.RandomEmail() + firstName := test.RandomName() + lastName := test.RandomName() + role := test.RandomUserRole() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUserFull(userResource, email, firstName, lastName, role), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email), + sdk.TestCheckResourceAttr(theResource, attr.FirstName, firstName), + sdk.TestCheckResourceAttr(theResource, attr.LastName, lastName), + sdk.TestCheckResourceAttr(theResource, attr.Role, role), + ), }, - }) + }, }) } -func terraformResourceTwingateUserFull(terraformResourceName, email, firstName, lastName, role string) string { - return fmt.Sprintf(` - resource "twingate_user" "%s" { - email = "%s" - first_name = "%s" - last_name = "%s" - role = "%s" +func configUserFull(userResource, email, firstName, lastName, role string) string { + return acctests.Nprintf(` + resource "twingate_user" "${user_resource}" { + email = "${email}" + first_name = "${first_name}" + last_name = "${last_name}" + role = "${role}" send_invite = false } - `, terraformResourceName, email, firstName, lastName, role) + `, map[string]any{ + "user_resource": userResource, + "email": email, + "first_name": firstName, + "last_name": lastName, + "role": role, + }) } func TestAccTwingateUserReCreation(t *testing.T) { - t.Run("Test Twingate Resource : Acc User ReCreation", func(t *testing.T) { - const terraformResourceName = "test003" - theResource := acctests.TerraformUser(terraformResourceName) - email1 := test.RandomEmail() - email2 := test.RandomEmail() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUser(terraformResourceName, email1), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email1), - ), - }, - { - Config: terraformResourceTwingateUser(terraformResourceName, email2), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email2), - ), - }, + t.Parallel() + + userResource := test.RandomUserName() + theResource := acctests.TerraformUser(userResource) + email1 := test.RandomEmail() + email2 := test.RandomEmail() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUser(userResource, email1), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email1), + ), }, - }) + { + Config: configUser(userResource, email2), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email2), + ), + }, + }, }) } func TestAccTwingateUserUpdateState(t *testing.T) { - t.Run("Test Twingate Resource : Acc User Update State", func(t *testing.T) { - const terraformResourceName = "test004" - theResource := acctests.TerraformUser(terraformResourceName) - email := test.RandomEmail() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUser(terraformResourceName, email), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - sdk.TestCheckResourceAttr(theResource, attr.Email, email), - ), - }, - { - Config: terraformResourceTwingateUserDisabled(terraformResourceName, email), - ExpectError: regexp.MustCompile(`User in PENDING state`), - }, + t.Parallel() + + userResource := test.RandomUserName() + theResource := acctests.TerraformUser(userResource) + email := test.RandomEmail() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUser(userResource, email), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + sdk.TestCheckResourceAttr(theResource, attr.Email, email), + ), + }, + { + Config: configUserDisabled(userResource, email), + ExpectError: regexp.MustCompile(`User in PENDING state`), }, - }) + }, }) } -func terraformResourceTwingateUserDisabled(terraformResourceName, email string) string { - return fmt.Sprintf(` - resource "twingate_user" "%s" { - email = "%s" +func configUserDisabled(userResource, email string) string { + return acctests.Nprintf(` + resource "twingate_user" "${user_resource}" { + email = "${email}" send_invite = false is_active = false } - `, terraformResourceName, email) + `, map[string]any{ + "user_resource": userResource, + "email": email, + }) } func TestAccTwingateUserDelete(t *testing.T) { - t.Run("Test Twingate Resource : Acc User Delete", func(t *testing.T) { - const terraformResourceName = "test005" - theResource := acctests.TerraformUser(terraformResourceName) - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUser(terraformResourceName, test.RandomEmail()), - Destroy: true, - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceDoesNotExists(theResource), - ), - }, + t.Parallel() + + userResource := test.RandomUserName() + theResource := acctests.TerraformUser(userResource) + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUser(userResource, test.RandomEmail()), + Destroy: true, + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceDoesNotExists(theResource), + ), }, - }) + }, }) } func TestAccTwingateUserReCreateAfterDeletion(t *testing.T) { - t.Run("Test Twingate Resource : Acc User Create After Deletion", func(t *testing.T) { - const terraformResourceName = "test006" - theResource := acctests.TerraformUser(terraformResourceName) - email := test.RandomEmail() - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUser(terraformResourceName, email), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - acctests.DeleteTwingateResource(theResource, resource.TwingateUser), - ), - ExpectNonEmptyPlan: true, - }, - { - Config: terraformResourceTwingateUser(terraformResourceName, email), - Check: acctests.ComposeTestCheckFunc( - acctests.CheckTwingateResourceExists(theResource), - ), - }, + t.Parallel() + + userResource := test.RandomUserName() + theResource := acctests.TerraformUser(userResource) + email := test.RandomEmail() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUser(userResource, email), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + acctests.DeleteTwingateResource(theResource, resource.TwingateUser), + ), + ExpectNonEmptyPlan: true, + }, + { + Config: configUser(userResource, email), + Check: acctests.ComposeTestCheckFunc( + acctests.CheckTwingateResourceExists(theResource), + ), }, - }) + }, }) } func TestAccTwingateUserCreateWithUnknownRole(t *testing.T) { - t.Run("Test Twingate Resource : Acc User Create With Unknown Role", func(t *testing.T) { - const terraformResourceName = "test007" - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUserWithRole(terraformResourceName, test.RandomEmail(), "UnknownRole"), - ExpectError: regexp.MustCompile(`Attribute role value must be one of`), - }, + t.Parallel() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUserWithRole(test.RandomUserName(), test.RandomEmail(), "UnknownRole"), + ExpectError: regexp.MustCompile(`Attribute role value must be one of`), }, - }) + }, }) } func TestAccTwingateUserCreateWithoutEmail(t *testing.T) { - t.Run("Test Twingate Resource : Acc User Create Without Email", func(t *testing.T) { - const terraformResourceName = "test008" - - sdk.Test(t, sdk.TestCase{ - ProtoV6ProviderFactories: acctests.ProviderFactories, - PreCheck: func() { acctests.PreCheck(t) }, - CheckDestroy: acctests.CheckTwingateUserDestroy, - Steps: []sdk.TestStep{ - { - Config: terraformResourceTwingateUserWithoutEmail(terraformResourceName), - ExpectError: regexp.MustCompile("Error: Missing required argument"), - }, + t.Parallel() + + sdk.Test(t, sdk.TestCase{ + ProtoV6ProviderFactories: acctests.ProviderFactories, + PreCheck: func() { acctests.PreCheck(t) }, + CheckDestroy: acctests.CheckTwingateUserDestroy, + Steps: []sdk.TestStep{ + { + Config: configUserWithoutEmail(test.RandomUserName()), + ExpectError: regexp.MustCompile("Error: Missing required argument"), }, - }) + }, }) } -func terraformResourceTwingateUserWithoutEmail(terraformResourceName string) string { - return fmt.Sprintf(` - resource "twingate_user" "%s" { +func configUserWithoutEmail(userResource string) string { + return acctests.Nprintf(` + resource "twingate_user" "${user_resource}" { send_invite = false } - `, terraformResourceName) + `, map[string]any{ + "user_resource": userResource, + }) } func genNewUsers(resourcePrefix string, count int) ([]string, []string) { @@ -316,7 +338,7 @@ func genNewUsers(resourcePrefix string, count int) ([]string, []string) { for i := 0; i < count; i++ { resourceName := fmt.Sprintf("%s_%d", resourcePrefix, i+1) - users = append(users, terraformResourceTwingateUser(resourceName, test.RandomEmail())) + users = append(users, configUser(resourceName, test.RandomEmail())) userIDs = append(userIDs, fmt.Sprintf("twingate_user.%s.id", resourceName)) } diff --git a/twingate/internal/test/helper.go b/twingate/internal/test/helper.go index b6b5aa13..8412f971 100644 --- a/twingate/internal/test/helper.go +++ b/twingate/internal/test/helper.go @@ -33,6 +33,18 @@ func RandomGroupName() string { return RandomName("group") } +func RandomNetworkName() string { + return RandomName("network") +} + +func RandomUserName() string { + return RandomName("user") +} + +func RandomServiceAccountName() string { + return RandomName("service_account") +} + func RandomName(names ...string) string { return acctest.RandomWithPrefix(Prefix(names...)) }