@@ -5,8 +5,13 @@ import (
55 "regexp"
66 "testing"
77
8+ "github.com/hashicorp/terraform-plugin-testing/compare"
89 "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
910 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+ "github.com/hashicorp/terraform-plugin-testing/knownvalue"
12+ "github.com/hashicorp/terraform-plugin-testing/plancheck"
13+ "github.com/hashicorp/terraform-plugin-testing/statecheck"
14+ "github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1015)
1116
1217func TestAccGithubRepositoryAutolinkReference (t * testing.T ) {
@@ -61,6 +66,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
6166 resource .TestCheckResourceAttr (
6267 "github_repository_autolink_reference.autolink_default" , "is_alphanumeric" , "true" ,
6368 ),
69+ resource .TestCheckResourceAttrSet (
70+ "github_repository_autolink_reference.autolink_default" , "repository_id" ,
71+ ),
6472 // autolink_alphanumeric
6573 resource .TestCheckResourceAttr (
6674 "github_repository_autolink_reference.autolink_alphanumeric" , "key_prefix" , "TEST2-" ,
@@ -71,6 +79,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
7179 resource .TestCheckResourceAttr (
7280 "github_repository_autolink_reference.autolink_alphanumeric" , "is_alphanumeric" , "true" ,
7381 ),
82+ resource .TestCheckResourceAttrSet (
83+ "github_repository_autolink_reference.autolink_alphanumeric" , "repository_id" ,
84+ ),
7485 // autolink_numeric
7586 resource .TestCheckResourceAttr (
7687 "github_repository_autolink_reference.autolink_numeric" , "key_prefix" , "TEST3-" ,
@@ -81,6 +92,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
8192 resource .TestCheckResourceAttr (
8293 "github_repository_autolink_reference.autolink_numeric" , "is_alphanumeric" , "false" ,
8394 ),
95+ resource .TestCheckResourceAttrSet (
96+ "github_repository_autolink_reference.autolink_numeric" , "repository_id" ,
97+ ),
8498 // autolink_with_port
8599 resource .TestCheckResourceAttr (
86100 "github_repository_autolink_reference.autolink_with_port" , "key_prefix" , "TEST4-" ,
@@ -91,6 +105,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
91105 resource .TestCheckResourceAttr (
92106 "github_repository_autolink_reference.autolink_with_port" , "is_alphanumeric" , "true" ,
93107 ),
108+ resource .TestCheckResourceAttrSet (
109+ "github_repository_autolink_reference.autolink_with_port" , "repository_id" ,
110+ ),
94111 )
95112
96113 resource .Test (t , resource.TestCase {
@@ -156,6 +173,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
156173 resource .TestCheckResourceAttr (
157174 "github_repository_autolink_reference.autolink_default" , "is_alphanumeric" , "true" ,
158175 ),
176+ resource .TestCheckResourceAttrSet (
177+ "github_repository_autolink_reference.autolink_default" , "repository_id" ,
178+ ),
159179 // autolink_alphanumeric
160180 resource .TestCheckResourceAttr (
161181 "github_repository_autolink_reference.autolink_alphanumeric" , "key_prefix" , "TEST2-" ,
@@ -166,6 +186,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
166186 resource .TestCheckResourceAttr (
167187 "github_repository_autolink_reference.autolink_alphanumeric" , "is_alphanumeric" , "true" ,
168188 ),
189+ resource .TestCheckResourceAttrSet (
190+ "github_repository_autolink_reference.autolink_alphanumeric" , "repository_id" ,
191+ ),
169192 // autolink_numeric
170193 resource .TestCheckResourceAttr (
171194 "github_repository_autolink_reference.autolink_numeric" , "key_prefix" , "TEST3-" ,
@@ -176,6 +199,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
176199 resource .TestCheckResourceAttr (
177200 "github_repository_autolink_reference.autolink_numeric" , "is_alphanumeric" , "false" ,
178201 ),
202+ resource .TestCheckResourceAttrSet (
203+ "github_repository_autolink_reference.autolink_numeric" , "repository_id" ,
204+ ),
179205 // autolink_with_port
180206 resource .TestCheckResourceAttr (
181207 "github_repository_autolink_reference.autolink_with_port" , "key_prefix" , "TEST4-" ,
@@ -186,6 +212,9 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
186212 resource .TestCheckResourceAttr (
187213 "github_repository_autolink_reference.autolink_with_port" , "is_alphanumeric" , "true" ,
188214 ),
215+ resource .TestCheckResourceAttrSet (
216+ "github_repository_autolink_reference.autolink_with_port" , "repository_id" ,
217+ ),
189218 )
190219
191220 resource .Test (t , resource.TestCase {
@@ -296,4 +325,48 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) {
296325 },
297326 })
298327 })
328+ t .Run ("should not recreate autolink reference when repository is renamed" , func (t * testing.T ) {
329+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
330+ repoName := fmt .Sprintf ("%srepo-rename-%s" , testResourcePrefix , randomID )
331+ repoNameRenamed := fmt .Sprintf ("%srepo-renamed-%s" , testResourcePrefix , randomID )
332+ const configStr = `
333+ resource "github_repository" "test" {
334+ name = "%s"
335+ description = "Test autolink creation"
336+ }
337+
338+ resource "github_repository_autolink_reference" "autolink_default" {
339+ repository = github_repository.test.name
340+
341+ key_prefix = "TEST1-"
342+ target_url_template = "https://example.com/TEST-<num>"
343+ }
344+ `
345+
346+ repoIdChangeCheck := statecheck .CompareValue (compare .ValuesSame ())
347+ resource .Test (t , resource.TestCase {
348+ PreCheck : func () { skipUnauthenticated (t ) },
349+ ProviderFactories : providerFactories ,
350+ Steps : []resource.TestStep {
351+ {
352+ Config : fmt .Sprintf (configStr , repoName ),
353+ ConfigStateChecks : []statecheck.StateCheck {
354+ repoIdChangeCheck .AddStateValue ("github_repository_autolink_reference.autolink_default" , tfjsonpath .New ("repository_id" )),
355+ },
356+ },
357+ {
358+ Config : fmt .Sprintf (configStr , repoNameRenamed ),
359+ ConfigPlanChecks : resource.ConfigPlanChecks {
360+ PreApply : []plancheck.PlanCheck {
361+ plancheck .ExpectResourceAction ("github_repository_autolink_reference.autolink_default" , plancheck .ResourceActionUpdate ),
362+ },
363+ },
364+ ConfigStateChecks : []statecheck.StateCheck {
365+ repoIdChangeCheck .AddStateValue ("github_repository_autolink_reference.autolink_default" , tfjsonpath .New ("repository_id" )),
366+ statecheck .ExpectKnownValue ("github_repository_autolink_reference.autolink_default" , tfjsonpath .New ("repository" ), knownvalue .StringExact (repoNameRenamed )),
367+ },
368+ },
369+ },
370+ })
371+ })
299372}
0 commit comments