Skip to content

Commit 76e4f2d

Browse files
committed
Improves tests
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 582462b commit 76e4f2d

1 file changed

Lines changed: 110 additions & 28 deletions

File tree

github/resource_github_repository_vulnerability_alerts_test.go

Lines changed: 110 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
99
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
11+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
1112
"github.com/hashicorp/terraform-plugin-testing/statecheck"
1213
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1314
)
1415

1516
func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
16-
t.Run("enables_vulnerability_alerts_without_error", func(t *testing.T) {
17+
t.Run("creates_vulnerability_alerts_as_enabled_without_error", func(t *testing.T) {
1718
randomID := acctest.RandString(5)
1819
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
1920

@@ -46,7 +47,7 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
4647
})
4748
})
4849

49-
t.Run("disables_vulnerability_alerts_without_error", func(t *testing.T) {
50+
t.Run("creates_vulnerability_alerts_as_disabled_without_error", func(t *testing.T) {
5051
randomID := acctest.RandString(5)
5152
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
5253

@@ -77,11 +78,9 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
7778
})
7879
})
7980

80-
t.Run("updates_vulnerability_alerts_from_disabled_to_enabled", func(t *testing.T) {
81+
t.Run("updates_vulnerability_alerts_without_error", func(t *testing.T) {
8182
randomID := acctest.RandString(5)
8283
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
83-
enabled := false
84-
updatedEnabled := true
8584

8685
config := `
8786
resource "github_repository" "test" {
@@ -103,13 +102,19 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
103102
ProviderFactories: providerFactories,
104103
Steps: []resource.TestStep{
105104
{
106-
Config: fmt.Sprintf(config, repoName, enabled),
105+
Config: fmt.Sprintf(config, repoName, false),
107106
ConfigStateChecks: []statecheck.StateCheck{
108107
compareValuesDiffer.AddStateValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled")),
109108
},
110109
},
111110
{
112-
Config: fmt.Sprintf(config, repoName, updatedEnabled),
111+
Config: fmt.Sprintf(config, repoName, true),
112+
ConfigStateChecks: []statecheck.StateCheck{
113+
compareValuesDiffer.AddStateValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled")),
114+
},
115+
},
116+
{
117+
Config: fmt.Sprintf(config, repoName, false),
113118
ConfigStateChecks: []statecheck.StateCheck{
114119
compareValuesDiffer.AddStateValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled")),
115120
},
@@ -118,13 +123,11 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
118123
})
119124
})
120125

121-
t.Run("updates_vulnerability_alerts_from_enabled_to_disabled", func(t *testing.T) {
126+
t.Run("imports_vulnerability_alerts_without_error", func(t *testing.T) {
122127
randomID := acctest.RandString(5)
123128
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
124-
enabled := true
125-
updatedEnabled := false
126129

127-
config := `
130+
config := fmt.Sprintf(`
128131
resource "github_repository" "test" {
129132
name = "%s"
130133
visibility = "private"
@@ -133,33 +136,31 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
133136
134137
resource "github_repository_vulnerability_alerts" "test" {
135138
repository = github_repository.test.name
136-
enabled = %t
139+
enabled = true
137140
}
138-
`
139-
140-
compareValuesDiffer := statecheck.CompareValue(compare.ValuesDiffer())
141+
`, repoName)
141142

142143
resource.Test(t, resource.TestCase{
143144
PreCheck: func() { skipUnauthenticated(t) },
144145
ProviderFactories: providerFactories,
145146
Steps: []resource.TestStep{
146147
{
147-
Config: fmt.Sprintf(config, repoName, enabled),
148+
Config: config,
148149
ConfigStateChecks: []statecheck.StateCheck{
149-
compareValuesDiffer.AddStateValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled")),
150+
statecheck.ExpectKnownValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()),
150151
},
151152
},
152153
{
153-
Config: fmt.Sprintf(config, repoName, updatedEnabled),
154-
ConfigStateChecks: []statecheck.StateCheck{
155-
compareValuesDiffer.AddStateValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled")),
156-
},
154+
ResourceName: "github_repository_vulnerability_alerts.test",
155+
ImportState: true,
156+
ImportStateId: repoName,
157+
ImportStateVerify: true,
157158
},
158159
},
159160
})
160161
})
161162

162-
t.Run("imports_vulnerability_alerts_without_error", func(t *testing.T) {
163+
t.Run("creates_with_defaults_without_error", func(t *testing.T) {
163164
randomID := acctest.RandString(5)
164165
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
165166

@@ -171,8 +172,7 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
171172
}
172173
173174
resource "github_repository_vulnerability_alerts" "test" {
174-
repository = github_repository.test.name
175-
enabled = true
175+
repository = github_repository.test.name
176176
}
177177
`, repoName)
178178

@@ -183,13 +183,95 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
183183
{
184184
Config: config,
185185
ConfigStateChecks: []statecheck.StateCheck{
186-
statecheck.ExpectKnownValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()),
186+
statecheck.ExpectKnownValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled"), knownvalue.Bool(true)),
187+
},
188+
},
189+
},
190+
})
191+
})
192+
193+
t.Run("destroys_from_archived_repository_without_error", func(t *testing.T) {
194+
randomID := acctest.RandString(5)
195+
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
196+
197+
withAlertsConfig := `
198+
resource "github_repository" "test" {
199+
name = "%s"
200+
visibility = "private"
201+
auto_init = true
202+
archived = %t
203+
}
204+
205+
resource "github_repository_vulnerability_alerts" "test" {
206+
repository = github_repository.test.name
207+
enabled = true
208+
}
209+
`
210+
211+
archivedOnlyConfig := fmt.Sprintf(`
212+
resource "github_repository" "test" {
213+
name = "%s"
214+
visibility = "private"
215+
auto_init = true
216+
archived = true
217+
}
218+
`, repoName)
219+
220+
resource.Test(t, resource.TestCase{
221+
PreCheck: func() { skipUnauthenticated(t) },
222+
ProviderFactories: providerFactories,
223+
Steps: []resource.TestStep{
224+
{
225+
Config: fmt.Sprintf(withAlertsConfig, repoName, false),
226+
ConfigStateChecks: []statecheck.StateCheck{
227+
statecheck.ExpectKnownValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled"), knownvalue.Bool(true)),
228+
},
229+
},
230+
{
231+
Config: fmt.Sprintf(withAlertsConfig, repoName, true),
232+
ExpectNonEmptyPlan: true,
233+
ConfigStateChecks: []statecheck.StateCheck{
234+
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("archived"), knownvalue.Bool(true)),
187235
},
188236
},
189237
{
190-
ResourceName: "github_repository_vulnerability_alerts.test",
191-
ImportState: true,
192-
ImportStateId: repoName,
238+
Config: archivedOnlyConfig,
239+
ConfigPlanChecks: resource.ConfigPlanChecks{
240+
PreApply: []plancheck.PlanCheck{
241+
plancheck.ExpectResourceAction("github_repository_vulnerability_alerts.test", plancheck.ResourceActionDestroy),
242+
},
243+
},
244+
},
245+
},
246+
})
247+
})
248+
249+
t.Run("creates_on_public_repo_without_error", func(t *testing.T) {
250+
randomID := acctest.RandString(5)
251+
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
252+
253+
config := fmt.Sprintf(`
254+
resource "github_repository" "test" {
255+
name = "%s"
256+
visibility = "public"
257+
auto_init = true
258+
}
259+
260+
resource "github_repository_vulnerability_alerts" "test" {
261+
repository = github_repository.test.name
262+
enabled = true
263+
}
264+
`, repoName)
265+
266+
resource.Test(t, resource.TestCase{
267+
PreCheck: func() { skipUnauthenticated(t); skipIfEMUEnterprise(t) },
268+
ProviderFactories: providerFactories,
269+
Steps: []resource.TestStep{
270+
{
271+
Config: config,
272+
ConfigStateChecks: []statecheck.StateCheck{
273+
statecheck.ExpectKnownValue("github_repository_vulnerability_alerts.test", tfjsonpath.New("enabled"), knownvalue.Bool(true)),
274+
},
193275
},
194276
},
195277
})

0 commit comments

Comments
 (0)