Skip to content

Commit

Permalink
chore: make tests less flaky due to random naming of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
filipowm committed Jan 8, 2025
1 parent 6d1ee00 commit 027c3e9
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 358 deletions.
18 changes: 1 addition & 17 deletions internal/provider/data_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAccDataAccount_mac(t *testing.T) {
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccDataMacAccountConfig(mac),
Config: testAccDataAccountConfig(mac, mac),
Check: resource.ComposeTestCheckFunc(),
},
},
Expand All @@ -58,19 +58,3 @@ depends_on = [
}
`, name, password)
}

func testAccDataMacAccountConfig(mac string) string {
return fmt.Sprintf(`
resource "unifi_account" "test" {
name = "%[1]s"
password = "%[1]s"
}
data "unifi_account" "test" {
name = "%[1]s"
depends_on = [
unifi_account.test
]
}
`, mac)
}
6 changes: 4 additions & 2 deletions internal/provider/data_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -12,15 +13,16 @@ import (
func TestAccDataUser_default(t *testing.T) {
mac, unallocateTestMac := allocateTestMac(t)
defer unallocateTestMac()
name := acctest.RandomWithPrefix("tfacc")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
//preCheck(t)

_, err := testClient.CreateUser(context.Background(), "default", &unifi.User{
MAC: mac,
Name: "tfacc-User-Data",
Note: "tfacc-User-Data",
Name: name,
Note: name,
})
if err != nil {
t.Fatal(err)
Expand Down
18 changes: 11 additions & 7 deletions internal/provider/resource_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package provider

import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccAccount_basic(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccAccountConfig("tfacc", "secure"),
Config: testAccAccountConfig(name, "secure"),
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
resource.TestCheckResourceAttr("unifi_account.test", "name", "tfacc"),
resource.TestCheckResourceAttr("unifi_account.test", "name", name),
),
},
importStep("unifi_account.test"),
Expand All @@ -26,17 +28,19 @@ func TestAccAccount_basic(t *testing.T) {
}

func TestAccAccount_mac(t *testing.T) {
mac, unallocateMac := allocateTestMac(t)
defer unallocateMac()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccAccountConfig("00B0D06FC226", "00B0D06FC226"),
Config: testAccAccountConfig(mac, mac),
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
resource.TestCheckResourceAttr("unifi_account.test", "name", "00B0D06FC226"),
resource.TestCheckResourceAttr("unifi_account.test", "password", "00B0D06FC226"),
resource.TestCheckResourceAttr("unifi_account.test", "name", mac),
resource.TestCheckResourceAttr("unifi_account.test", "password", mac),
),
},
importStep("unifi_account.test"),
Expand All @@ -47,8 +51,8 @@ func TestAccAccount_mac(t *testing.T) {
func testAccAccountConfig(name, password string) string {
return fmt.Sprintf(`
resource "unifi_account" "test" {
name = "%s"
password = "%s"
name = "%[1]s"
password = "%[2]s"
}
`, name, password)
}
13 changes: 8 additions & 5 deletions internal/provider/resource_firewall_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"regexp"
"strings"
"testing"
Expand All @@ -10,45 +11,47 @@ import (
)

func TestAccFirewallGroup_port_group(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccFirewallGroupConfig("testpg", "port-group", nil),
Config: testAccFirewallGroupConfig(name, "port-group", nil),
// Check: resource.ComposeTestCheckFunc(
// // testCheckFirewallGroupExists(t, "name"),
// ),
},
importStep("unifi_firewall_group.test"),
{
Config: testAccFirewallGroupConfig("testpg", "port-group", []string{"80", "443"}),
Config: testAccFirewallGroupConfig(name, "port-group", []string{"80", "443"}),
},
importStep("unifi_firewall_group.test"),
},
})
}

func TestAccFirewallGroup_address_group(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccFirewallGroupConfig("testag", "address-group", nil),
Config: testAccFirewallGroupConfig(name, "address-group", nil),
// Check: resource.ComposeTestCheckFunc(
// // testCheckFirewallGroupExists(t, "name"),
// ),
},
importStep("unifi_firewall_group.test"),
{
Config: testAccFirewallGroupConfig("testag", "address-group", []string{"10.0.0.1", "10.0.0.2"}),
Config: testAccFirewallGroupConfig(name, "address-group", []string{"10.0.0.1", "10.0.0.2"}),
},
importStep("unifi_firewall_group.test"),
{
Config: testAccFirewallGroupConfig("testag", "address-group", []string{"10.0.0.0/24"}),
Config: testAccFirewallGroupConfig(name, "address-group", []string{"10.0.0.0/24"}),
},
importStep("unifi_firewall_group.test"),
},
Expand Down
18 changes: 12 additions & 6 deletions internal/provider/resource_port_forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,41 @@ package provider

import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccPortForward_basic(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
name2 := acctest.RandomWithPrefix("tfacc")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccPortForwardConfig("22", false, "10.1.1.1", "22", "fwd name"),
Config: testAccPortForwardConfig("22", false, "10.1.1.1", "22", name),
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
resource.TestCheckResourceAttr("unifi_port_forward.test", "dst_port", "22"),
),
},
importStep("unifi_port_forward.test"),
{
Config: testAccPortForwardConfig("22", false, "10.1.1.2", "8022", "fwd name"),
Config: testAccPortForwardConfig("22", false, "10.1.1.2", "8022", name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_port_forward.test", "fwd_port", "8022"),
resource.TestCheckResourceAttr("unifi_port_forward.test", "fwd_ip", "10.1.1.2"),
),
},
importStep("unifi_port_forward.test"),
{
Config: testAccPortForwardConfig("22", false, "10.1.1.1", "22", "fwd name 2"),
Config: testAccPortForwardConfig("22", false, "10.1.1.1", "22", name2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_port_forward.test", "name", "fwd name 2"),
resource.TestCheckResourceAttr("unifi_port_forward.test", "name", name2),
),
},
importStep("unifi_port_forward.test"),
Expand All @@ -41,13 +45,14 @@ func TestAccPortForward_basic(t *testing.T) {
}

func TestAccPortForward_src_ip(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccPortForwardConfigSrc("22", false, "10.1.1.1", "22", "fwd name", "192.168.1.0"),
Config: testAccPortForwardConfigSrc("22", false, "10.1.1.1", "22", name, "192.168.1.0"),
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
resource.TestCheckResourceAttr("unifi_port_forward.test", "dst_port", "22"),
Expand All @@ -59,13 +64,14 @@ func TestAccPortForward_src_ip(t *testing.T) {
}

func TestAccPortForward_src_cidr(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccPortForwardConfigSrc("22", false, "10.1.1.1", "22", "fwd name", "192.168.1.0/20"),
Config: testAccPortForwardConfigSrc("22", false, "10.1.1.1", "22", name, "192.168.1.0/20"),
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
resource.TestCheckResourceAttr("unifi_port_forward.test", "dst_port", "22"),
Expand Down
14 changes: 10 additions & 4 deletions internal/provider/resource_port_profile_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package provider

import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccPortProfile_basic(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
Expand All @@ -16,22 +19,25 @@ func TestAccPortProfile_basic(t *testing.T) {
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccPortProfileConfig,
Config: testAccPortProfileConfig(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_port_profile.test", "poe_mode", "off"),
resource.TestCheckResourceAttr("unifi_port_profile.test", "name", name),
),
},
importStep("unifi_port_profile.test"),
},
})
}

const testAccPortProfileConfig = `
func testAccPortProfileConfig(name string) string {
return fmt.Sprintf(`
resource "unifi_port_profile" "test" {
name = "provider created"
name = "%s"
poe_mode = "off"
speed = 1000
stp_port_mode = false
}
`
`, name)
}
19 changes: 11 additions & 8 deletions internal/provider/resource_radius_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package provider

import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccRadiusProfile_basic(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccRadiusProfileConfig("test"),
Config: testAccRadiusProfileConfig(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_radius_profile.test", "name", "test"),
resource.TestCheckResourceAttr("unifi_radius_profile.test", "name", name),
),
},
importStep("unifi_radius_profile.test"),
Expand All @@ -25,15 +27,16 @@ func TestAccRadiusProfile_basic(t *testing.T) {
}

func TestAccRadiusProfile_servers(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccRadiusProfileConfigServer(),
Config: testAccRadiusProfileConfigServer(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_radius_profile.test", "name", "test"),
resource.TestCheckResourceAttr("unifi_radius_profile.test", "name", name),
),
},
importStep("unifi_radius_profile.test"),
Expand Down Expand Up @@ -61,10 +64,10 @@ func TestAccRadiusProfile_importByName(t *testing.T) {
})
}

func testAccRadiusProfileConfigServer() string {
return `
func testAccRadiusProfileConfigServer(name string) string {
return fmt.Sprintf(`
resource "unifi_radius_profile" "test" {
name = "test"
name = "%s"
auth_server {
ip = "192.168.1.1"
xsecret = "securepw1"
Expand All @@ -86,7 +89,7 @@ resource "unifi_radius_profile" "test" {
use_usg_acct_server = false
use_usg_auth_server = false
}
`
`, name)
}

func testAccRadiusProfileConfig(name string) string {
Expand Down
Loading

0 comments on commit 027c3e9

Please sign in to comment.