Skip to content

Commit

Permalink
updated resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanilo committed Nov 24, 2023
1 parent 68659da commit aab3aea
Showing 1 changed file with 78 additions and 121 deletions.
199 changes: 78 additions & 121 deletions twingate/internal/test/acctests/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func TestAccTwingateResourceWithInvalidPortRange(t *testing.T) {
expectedError := regexp.MustCompile("failed to parse protocols port range")

genConfig := func(portRange string) string {
return configResourceWithRestrictedPolicyAndPortRange(terraformResource, remoteNetworkName, resourceName, portRange)
return configResourceWithPortRange(terraformResource, remoteNetworkName, resourceName, portRange)
}

sdk.Test(t, sdk.TestCase{
Expand Down Expand Up @@ -562,41 +562,11 @@ func TestAccTwingateResourceWithInvalidPortRange(t *testing.T) {
})
}

func configResourceWithRestrictedPolicyAndPortRange(terraformResource, networkName, resourceName, 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 = "${tcp_policy}"
ports = [${port_range}]
}
udp {
policy = "${udp_policy}"
}
}
}
`,
map[string]any{
"network_resource": terraformResource,
"network_name": networkName,
"port_range": portRange,
"resource_resource": terraformResource,
"resource_name": resourceName,
"tcp_policy": model.PolicyRestricted,
"udp_policy": 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()

Expand All @@ -606,7 +576,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"),
Expand All @@ -615,17 +585,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"),
Expand All @@ -636,33 +606,46 @@ 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" "${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,
})

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)
}

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()

Expand All @@ -672,7 +655,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"),
Expand All @@ -683,7 +666,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()

Expand All @@ -693,15 +679,15 @@ 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"),
),
},
{
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"),
Expand All @@ -712,7 +698,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()

Expand All @@ -722,7 +711,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"),
Expand All @@ -731,20 +720,20 @@ 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"),
),
},
// 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"),
Expand All @@ -756,8 +745,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()

Expand All @@ -767,7 +758,7 @@ func TestAccTwingateResourceSetActiveStateOnUpdate(t *testing.T) {
CheckDestroy: acctests.CheckTwingateResourceDestroy,
Steps: []sdk.TestStep{
{
Config: configResourceWithNetwork(terraformResourceName, remoteNetworkName, resourceName),
Config: configResourceWithNetwork(terraformResource, remoteNetworkName, resourceName),
Check: acctests.ComposeTestCheckFunc(
acctests.CheckTwingateResourceExists(theResource),
acctests.DeactivateTwingateResource(theResource),
Expand All @@ -776,7 +767,7 @@ func TestAccTwingateResourceSetActiveStateOnUpdate(t *testing.T) {
),
},
{
Config: configResourceWithNetwork(terraformResourceName, remoteNetworkName, resourceName),
Config: configResourceWithNetwork(terraformResource, remoteNetworkName, resourceName),
Check: acctests.ComposeTestCheckFunc(
acctests.CheckTwingateResourceActiveState(theResource, true),
),
Expand All @@ -786,8 +777,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()

Expand All @@ -797,15 +790,15 @@ func TestAccTwingateResourceReCreateAfterDeletion(t *testing.T) {
CheckDestroy: acctests.CheckTwingateResourceDestroy,
Steps: []sdk.TestStep{
{
Config: configResourceWithNetwork(terraformResourceName, remoteNetworkName, resourceName),
Config: configResourceWithNetwork(terraformResource, remoteNetworkName, resourceName),
Check: acctests.ComposeTestCheckFunc(
acctests.CheckTwingateResourceExists(theResource),
acctests.DeleteTwingateResource(theResource, resource.TwingateResource),
),
ExpectNonEmptyPlan: true,
},
{
Config: configResourceWithNetwork(terraformResourceName, remoteNetworkName, resourceName),
Config: configResourceWithNetwork(terraformResource, remoteNetworkName, resourceName),
Check: acctests.ComposeTestCheckFunc(
acctests.CheckTwingateResourceExists(theResource),
),
Expand All @@ -815,10 +808,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{
Expand All @@ -827,7 +821,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),
),
Expand All @@ -836,53 +830,16 @@ 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)
Expand Down

0 comments on commit aab3aea

Please sign in to comment.