Skip to content

Commit b717be3

Browse files
committed
Update tests to use terraform-plugin-testing and fix broken tests
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent bc460d1 commit b717be3

2 files changed

Lines changed: 68 additions & 36 deletions

File tree

github/resource_github_emu_group_mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func resourceGithubEMUGroupMappingCreate(ctx context.Context, d *schema.Resource
125125
return diag.FromErr(err)
126126
}
127127

128-
tflog.Trace(ctx, "Resource created or updated successfully", map[string]any{
128+
tflog.Trace(ctx, "Resource created successfully", map[string]any{
129129
"resource_id": d.Id(),
130130
})
131131

github/resource_github_emu_group_mapping_test.go

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ import (
66
"strconv"
77
"testing"
88

9+
"github.com/hashicorp/terraform-plugin-testing/compare"
910
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1011
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
13+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
14+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
1115
"github.com/hashicorp/terraform-plugin-testing/terraform"
16+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1217
)
1318

1419
func TestAccGithubEMUGroupMapping(t *testing.T) {
1520
groupID := testAccConf.testEnterpriseEMUGroupId
1621
if groupID == 0 {
1722
t.Skip("Skipping EMU group mapping tests because testEnterpriseEMUGroupId is not set")
1823
}
19-
t.Run("creates and manages EMU group mapping", func(t *testing.T) {
24+
t.Run("creates_without_error", func(t *testing.T) {
2025
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
2126
teamName := fmt.Sprintf("%steam-emu-%s", testResourcePrefix, randomID)
2227

@@ -27,17 +32,18 @@ func TestAccGithubEMUGroupMapping(t *testing.T) {
2732
Steps: []resource.TestStep{
2833
{
2934
Config: testAccGithubEMUGroupMappingConfig(teamName, groupID),
30-
Check: resource.ComposeTestCheckFunc(
31-
resource.TestCheckResourceAttrSet("github_emu_group_mapping.test", "group_id"),
32-
resource.TestCheckResourceAttr("github_emu_group_mapping.test", "team_slug", teamName),
33-
resource.TestCheckResourceAttrSet("github_emu_group_mapping.test", "etag"),
34-
),
35+
ConfigStateChecks: []statecheck.StateCheck{
36+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("group_id"), knownvalue.NotNull()),
37+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("etag"), knownvalue.NotNull()),
38+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("team_id"), knownvalue.NotNull()),
39+
statecheck.CompareValuePairs("github_emu_group_mapping.test", tfjsonpath.New("team_slug"), "github_team.test", tfjsonpath.New("slug"), compare.ValuesSame()),
40+
},
3541
},
3642
},
3743
})
3844
})
3945

40-
t.Run("imports EMU group mapping", func(t *testing.T) {
46+
t.Run("imports_without_error", func(t *testing.T) {
4147
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
4248
teamName := fmt.Sprintf("%steam-emu-%s", testResourcePrefix, randomID)
4349
rn := "github_emu_group_mapping.test"
@@ -51,15 +57,16 @@ func TestAccGithubEMUGroupMapping(t *testing.T) {
5157
Config: testAccGithubEMUGroupMappingConfig(teamName, groupID),
5258
},
5359
{
54-
ResourceName: rn,
55-
ImportState: true,
56-
ImportStateIdFunc: testAccGithubEMUGroupMappingImportStateIdFunc(rn),
57-
ImportStateVerify: true,
60+
ResourceName: rn,
61+
ImportState: true,
62+
ImportStateIdFunc: testAccGithubEMUGroupMappingImportStateIdFunc(rn),
63+
ImportStateVerify: true,
64+
ImportStateVerifyIgnore: []string{"etag"},
5865
},
5966
},
6067
})
6168
})
62-
t.Run("imports EMU group mapping with multiple teams", func(t *testing.T) {
69+
t.Run("imports_multiple_teams_without_error", func(t *testing.T) {
6370
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
6471
teamName := fmt.Sprintf("%steam1-emu-%s", testResourcePrefix, randomID)
6572
teamName2 := fmt.Sprintf("%steam2-emu-%s", testResourcePrefix, randomID)
@@ -92,43 +99,55 @@ func TestAccGithubEMUGroupMapping(t *testing.T) {
9299
Config: config,
93100
},
94101
{
95-
ResourceName: rn,
96-
ImportState: true,
97-
ImportStateIdFunc: testAccGithubEMUGroupMappingImportStateIdFunc(rn),
98-
ImportStateVerify: true,
102+
ResourceName: rn,
103+
ImportState: true,
104+
ImportStateIdFunc: testAccGithubEMUGroupMappingImportStateIdFunc(rn),
105+
ImportStateVerify: true,
106+
ImportStateVerifyIgnore: []string{"etag"},
99107
},
100108
},
101109
})
102110
})
103111

104-
t.Run("handles team slug update by recreating", func(t *testing.T) {
112+
t.Run("updates_team_slug_without_error", func(t *testing.T) {
105113
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
106114
teamName1 := fmt.Sprintf("%steam-emu-%s", testResourcePrefix, randomID)
107115
teamName2 := fmt.Sprintf("%s-upd", teamName1)
108116

117+
compareIDSame := statecheck.CompareValue(compare.ValuesSame())
109118
resource.Test(t, resource.TestCase{
110119
PreCheck: func() { skipUnlessEMUEnterprise(t) },
111120
ProviderFactories: providerFactories,
112121
CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy,
113122
Steps: []resource.TestStep{
114123
{
115124
Config: testAccGithubEMUGroupMappingConfig(teamName1, groupID),
116-
Check: resource.ComposeTestCheckFunc(
117-
resource.TestCheckResourceAttr("github_emu_group_mapping.test", "team_slug", teamName1),
118-
),
125+
ConfigStateChecks: []statecheck.StateCheck{
126+
compareIDSame.AddStateValue("github_emu_group_mapping.test", tfjsonpath.New("id")),
127+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("group_id"), knownvalue.NotNull()),
128+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("etag"), knownvalue.NotNull()),
129+
statecheck.CompareValuePairs("github_emu_group_mapping.test", tfjsonpath.New("team_slug"), "github_team.test", tfjsonpath.New("slug"), compare.ValuesSame()),
130+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("team_id"), knownvalue.NotNull()),
131+
},
119132
},
120133
{
121134
Config: testAccGithubEMUGroupMappingConfig(teamName2, groupID),
122-
Check: resource.ComposeTestCheckFunc(
123-
resource.TestCheckResourceAttr("github_emu_group_mapping.test", "team_slug", teamName2),
124-
),
135+
ConfigPlanChecks: resource.ConfigPlanChecks{
136+
PreApply: []plancheck.PlanCheck{
137+
plancheck.ExpectUnknownValue("github_emu_group_mapping.test", tfjsonpath.New("team_slug")),
138+
plancheck.ExpectResourceAction("github_emu_group_mapping.test", plancheck.ResourceActionUpdate),
139+
},
140+
},
141+
ConfigStateChecks: []statecheck.StateCheck{
142+
compareIDSame.AddStateValue("github_emu_group_mapping.test", tfjsonpath.New("id")),
143+
statecheck.CompareValuePairs("github_emu_group_mapping.test", tfjsonpath.New("team_slug"), "github_team.test", tfjsonpath.New("slug"), compare.ValuesSame()),
144+
},
125145
},
126146
},
127147
})
128148
})
129149

130-
t.Run("forces new when switching to different team", func(t *testing.T) {
131-
t.Skip("Skipping this test because we don't have terraform-plugin-testing available yet.")
150+
t.Run("recreates_when_switching_to_different_team_without_error", func(t *testing.T) {
132151
randomID := acctest.RandString(5)
133152
teamName1 := fmt.Sprintf("%semu1-%s", testResourcePrefix, randomID)
134153
teamName2 := fmt.Sprintf("%semu2-%s", testResourcePrefix, randomID)
@@ -155,18 +174,27 @@ resource "github_emu_group_mapping" "test" {
155174
Steps: []resource.TestStep{
156175
{
157176
Config: fmt.Sprintf(config, teamName1, teamName2, "test1", groupID),
158-
Check: resource.ComposeTestCheckFunc(
159-
resource.TestCheckResourceAttr("github_emu_group_mapping.test", "team_slug", teamName1),
160-
),
177+
ConfigStateChecks: []statecheck.StateCheck{
178+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("group_id"), knownvalue.NotNull()),
179+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("team_id"), knownvalue.NotNull()),
180+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("etag"), knownvalue.NotNull()),
181+
statecheck.CompareValuePairs("github_emu_group_mapping.test", tfjsonpath.New("team_slug"), "github_team.test1", tfjsonpath.New("slug"), compare.ValuesSame()),
182+
},
161183
},
162184
{
163185
Config: fmt.Sprintf(config, teamName1, teamName2, "test2", groupID),
164-
// ConfigPlanChecks: resource.ConfigPlanChecks{
165-
// PreApply: []plancheck.PlanCheck{
166-
// plancheckExpectKnownValues("github_emu_group_mapping.test", "team_slug", teamName2),
167-
// plancheck.ExpectResourceAction("github_emu_group_mapping.test", plancheck.ResourceActionDestroyBeforeCreate), // Verify that ForceNew is triggered
168-
// },
169-
// },
186+
ConfigPlanChecks: resource.ConfigPlanChecks{
187+
PreApply: []plancheck.PlanCheck{
188+
plancheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("team_slug"), knownvalue.StringExact(teamName2)),
189+
plancheck.ExpectResourceAction("github_emu_group_mapping.test", plancheck.ResourceActionReplace), // Verify that ForceNew is triggered
190+
},
191+
},
192+
ConfigStateChecks: []statecheck.StateCheck{
193+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("group_id"), knownvalue.NotNull()),
194+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("team_id"), knownvalue.NotNull()),
195+
statecheck.ExpectKnownValue("github_emu_group_mapping.test", tfjsonpath.New("etag"), knownvalue.NotNull()),
196+
statecheck.CompareValuePairs("github_emu_group_mapping.test", tfjsonpath.New("team_slug"), "github_team.test2", tfjsonpath.New("slug"), compare.ValuesSame()),
197+
},
170198
},
171199
},
172200
})
@@ -212,7 +240,11 @@ func testAccGithubEMUGroupMappingImportStateIdFunc(resourceName string) resource
212240
if !ok {
213241
return "", fmt.Errorf("not found: %s", resourceName)
214242
}
215-
return buildTwoPartID(rs.Primary.Attributes["group_id"], rs.Primary.Attributes["team_slug"]), nil
243+
resourceID, err := buildID(rs.Primary.Attributes["group_id"], rs.Primary.Attributes["team_slug"])
244+
if err != nil {
245+
return "", err
246+
}
247+
return resourceID, nil
216248
}
217249
}
218250

0 commit comments

Comments
 (0)