Skip to content

Commit b87bc19

Browse files
authored
chore(account): add support for cassettes testing (#640)
1 parent d507b3f commit b87bc19

File tree

4 files changed

+853
-58
lines changed

4 files changed

+853
-58
lines changed

scaleway/data_source_account_ssh_key_test.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
11
package scaleway
22

33
import (
4+
"fmt"
45
"testing"
56

6-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
88
)
99

1010
func TestAccScalewayDataSourceAccountSSHKey_Basic(t *testing.T) {
11-
sshKeyName := acctest.RandString(10)
11+
dataSourceAccountSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILHy/M5FVm5ydLGcal3e5LNcfTalbeN7QL/ZGCvDEdqJ [email protected]"
12+
sshKeyName := "TestAccScalewayDataSourceAccountSSHKey_Basic"
13+
tt := NewTestTools(t)
14+
defer tt.Cleanup()
1215
resource.ParallelTest(t, resource.TestCase{
13-
PreCheck: func() { testAccPreCheck(t) },
14-
Providers: testAccProviders,
15-
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy,
16+
PreCheck: func() { testAccPreCheck(t) },
17+
ProviderFactories: tt.ProviderFactories,
18+
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy(tt),
1619
Steps: []resource.TestStep{
1720
{
18-
Config: testAccDataSourceScalewayAccountSSHKeyConfig(sshKeyName),
21+
Config: fmt.Sprintf(`
22+
resource "scaleway_account_ssh_key" "main" {
23+
name = "%s"
24+
public_key = "%s"
25+
}
26+
`, sshKeyName, dataSourceAccountSSHKey),
27+
},
28+
{
29+
Config: fmt.Sprintf(`
30+
resource "scaleway_account_ssh_key" "main" {
31+
name = "%s"
32+
public_key = "%s"
33+
}
34+
35+
data "scaleway_account_ssh_key" "prod" {
36+
name = "${scaleway_account_ssh_key.main.name}"
37+
}
38+
39+
data "scaleway_account_ssh_key" "stg" {
40+
ssh_key_id = "${scaleway_account_ssh_key.main.id}"
41+
}`, sshKeyName, dataSourceAccountSSHKey),
1942
Check: resource.ComposeTestCheckFunc(
20-
testAccCheckScalewayAccountSSHKeyExists("data.scaleway_account_ssh_key.prod"),
43+
testAccCheckScalewayAccountSSHKeyExists(tt, "data.scaleway_account_ssh_key.prod"),
2144
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "name", sshKeyName),
2245
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "public_key", dataSourceAccountSSHKey),
23-
testAccCheckScalewayAccountSSHKeyExists("data.scaleway_account_ssh_key.stg"),
46+
testAccCheckScalewayAccountSSHKeyExists(tt, "data.scaleway_account_ssh_key.stg"),
2447
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "name", sshKeyName),
2548
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "public_key", dataSourceAccountSSHKey),
26-
),
27-
},
49+
)},
2850
},
2951
})
3052
}
31-
32-
const dataSourceAccountSSHKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCtMlb2pIlcCC3Exui1z77NfFWtlf59P/L38OwfHCKnxgHZfcjWZZEOdJPxeA6/iRCUrJo+mUKOedNJTCR9Wgg5zhUM1dd/fiyCS+STLf9fzoA1evPEEIHp0iobIpVQ0pGIdNqipL2n8BcV7f1oC2AkELCUp4gogkeUkKtK71DtsjGJQJBRlg01U2gTAnU0Q3kf5wxhCIELke9J3eblTpvdnNonqpXsQuy+InpT51NDtMbBdgkwNsgw6wDxI64NY92mEXO7PK/uhSAmxIM9a4evLhgFxSr8vFNTwqq5fbSyDeeQwHG23U0CmhM0tOwLuJAbQotWrfYsLCpinnBb+sp [email protected]"
33-
34-
func testAccDataSourceScalewayAccountSSHKeyConfig(name string) string {
35-
return `
36-
resource "scaleway_account_ssh_key" "main" {
37-
name = "` + name + `"
38-
public_key = "` + dataSourceAccountSSHKey + `"
39-
}
40-
41-
data "scaleway_account_ssh_key" "prod" {
42-
name = "${scaleway_account_ssh_key.main.name}"
43-
}
44-
45-
data "scaleway_account_ssh_key" "stg" {
46-
ssh_key_id = "${scaleway_account_ssh_key.main.id}"
47-
}`
48-
}

scaleway/resource_account_ssh_key_test.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ func testSweepAccountSSHKey(region string) error {
4343
return nil
4444
}
4545

46-
func TestAccScalewayAccountSSHKey(t *testing.T) {
46+
func TestAccScalewayAccountSSHKey_basic(t *testing.T) {
4747
name := newRandomName("ssh-key")
4848
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX [email protected]"
49+
tt := NewTestTools(t)
50+
defer tt.Cleanup()
4951

5052
resource.ParallelTest(t, resource.TestCase{
51-
PreCheck: func() { testAccPreCheck(t) },
52-
Providers: testAccProviders,
53-
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy,
53+
PreCheck: func() { testAccPreCheck(t) },
54+
ProviderFactories: tt.ProviderFactories,
55+
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy(tt),
5456
Steps: []resource.TestStep{
5557
{
5658
Config: `
@@ -60,7 +62,7 @@ func TestAccScalewayAccountSSHKey(t *testing.T) {
6062
}
6163
`,
6264
Check: resource.ComposeTestCheckFunc(
63-
testAccCheckScalewayAccountSSHKeyExists("scaleway_account_ssh_key.main"),
65+
testAccCheckScalewayAccountSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
6466
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name),
6567
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
6668
),
@@ -73,7 +75,7 @@ func TestAccScalewayAccountSSHKey(t *testing.T) {
7375
}
7476
`,
7577
Check: resource.ComposeTestCheckFunc(
76-
testAccCheckScalewayAccountSSHKeyExists("scaleway_account_ssh_key.main"),
78+
testAccCheckScalewayAccountSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
7779
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name+"-updated"),
7880
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
7981
),
@@ -85,11 +87,13 @@ func TestAccScalewayAccountSSHKey(t *testing.T) {
8587
func TestAccScalewayAccountSSHKey_WithNewLine(t *testing.T) {
8688
name := newRandomName("ssh-key")
8789
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup [email protected]"
90+
tt := NewTestTools(t)
91+
defer tt.Cleanup()
8892

8993
resource.ParallelTest(t, resource.TestCase{
90-
PreCheck: func() { testAccPreCheck(t) },
91-
Providers: testAccProviders,
92-
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy,
94+
PreCheck: func() { testAccPreCheck(t) },
95+
ProviderFactories: tt.ProviderFactories,
96+
CheckDestroy: testAccCheckScalewayAccountSSHKeyDestroy(tt),
9397
Steps: []resource.TestStep{
9498
{
9599
Config: `
@@ -99,7 +103,7 @@ func TestAccScalewayAccountSSHKey_WithNewLine(t *testing.T) {
99103
}
100104
`,
101105
Check: resource.ComposeTestCheckFunc(
102-
testAccCheckScalewayAccountSSHKeyExists("scaleway_account_ssh_key.main"),
106+
testAccCheckScalewayAccountSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
103107
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name),
104108
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
105109
),
@@ -108,40 +112,42 @@ func TestAccScalewayAccountSSHKey_WithNewLine(t *testing.T) {
108112
})
109113
}
110114

111-
func testAccCheckScalewayAccountSSHKeyDestroy(s *terraform.State) error {
112-
for _, rs := range s.RootModule().Resources {
113-
if rs.Type != "scaleway_account_ssh_key" {
114-
continue
115-
}
115+
func testAccCheckScalewayAccountSSHKeyDestroy(tt *TestTools) resource.TestCheckFunc {
116+
return func(state *terraform.State) error {
117+
for _, rs := range state.RootModule().Resources {
118+
if rs.Type != "scaleway_account_ssh_key" {
119+
continue
120+
}
116121

117-
accountAPI := accountAPI(testAccProvider.Meta())
122+
accountAPI := accountAPI(tt.Meta)
118123

119-
_, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{
120-
SSHKeyID: rs.Primary.ID,
121-
})
124+
_, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{
125+
SSHKeyID: rs.Primary.ID,
126+
})
122127

123-
// If no error resource still exist
124-
if err == nil {
125-
return fmt.Errorf("SSH key (%s) still exists", rs.Primary.ID)
126-
}
128+
// If no error resource still exist
129+
if err == nil {
130+
return fmt.Errorf("SSH key (%s) still exists", rs.Primary.ID)
131+
}
127132

128-
// Unexpected api error we return it
129-
if !is404Error(err) {
130-
return err
133+
// Unexpected api error we return it
134+
if !is404Error(err) {
135+
return err
136+
}
131137
}
132-
}
133138

134-
return nil
139+
return nil
140+
}
135141
}
136142

137-
func testAccCheckScalewayAccountSSHKeyExists(n string) resource.TestCheckFunc {
143+
func testAccCheckScalewayAccountSSHKeyExists(tt *TestTools, n string) resource.TestCheckFunc {
138144
return func(s *terraform.State) error {
139145
rs, ok := s.RootModule().Resources[n]
140146
if !ok {
141147
return fmt.Errorf("resource not found: %s", n)
142148
}
143149

144-
accountAPI := accountAPI(testAccProvider.Meta())
150+
accountAPI := accountAPI(tt.Meta)
145151

146152
_, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{
147153
SSHKeyID: rs.Primary.ID,

0 commit comments

Comments
 (0)