|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +// TestEtagDiffSuppressFunction tests that the etag diff suppress function |
| 10 | +// always returns true, suppressing all etag differences |
| 11 | +func TestEtagDiffSuppressFunction(t *testing.T) { |
| 12 | + repositoryResource := resourceGithubRepository() |
| 13 | + etagField := repositoryResource.Schema["etag"] |
| 14 | + |
| 15 | + if etagField == nil { |
| 16 | + t.Fatal("etag field not found in repository schema") |
| 17 | + } |
| 18 | + |
| 19 | + if etagField.DiffSuppressFunc == nil { |
| 20 | + t.Fatal("etag should have DiffSuppressFunc") |
| 21 | + } |
| 22 | + |
| 23 | + if !etagField.DiffSuppressOnRefresh { |
| 24 | + t.Fatal("etag should have DiffSuppressOnRefresh enabled") |
| 25 | + } |
| 26 | + |
| 27 | + testCases := []struct { |
| 28 | + name string |
| 29 | + key string |
| 30 | + old string |
| 31 | + new string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "different etag values", |
| 35 | + key: "etag", |
| 36 | + old: `"abc123"`, |
| 37 | + new: `"def456"`, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "empty to non-empty etag", |
| 41 | + key: "etag", |
| 42 | + old: "", |
| 43 | + new: `"abc123"`, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "non-empty to empty etag", |
| 47 | + key: "etag", |
| 48 | + old: `"abc123"`, |
| 49 | + new: "", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "same etag values", |
| 53 | + key: "etag", |
| 54 | + old: `"abc123"`, |
| 55 | + new: `"abc123"`, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tc := range testCases { |
| 60 | + t.Run(tc.name, func(t *testing.T) { |
| 61 | + d := schema.TestResourceDataRaw(t, repositoryResource.Schema, map[string]interface{}{ |
| 62 | + "name": "test-repo", |
| 63 | + }) |
| 64 | + |
| 65 | + result := etagField.DiffSuppressFunc(tc.key, tc.old, tc.new, d) |
| 66 | + if !result { |
| 67 | + t.Errorf("DiffSuppressFunc should always return true for etag field") |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// TestEtagSchemaConsistency ensure DiffSuppressFunc and DiffSuppressOnRefresh are consistently applied |
| 74 | +func TestEtagSchemaConsistency(t *testing.T) { |
| 75 | + resourcesWithEtag := map[string]*schema.Resource{ |
| 76 | + "github_repository": resourceGithubRepository(), |
| 77 | + "github_branch": resourceGithubBranch(), |
| 78 | + "github_branch_default": resourceGithubBranchDefault(), |
| 79 | + "github_issue_label": resourceGithubIssueLabel(), |
| 80 | + "github_repository_webhook": resourceGithubRepositoryWebhook(), |
| 81 | + "github_repository_deployment_branch_policy": resourceGithubRepositoryDeploymentBranchPolicy(), |
| 82 | + "github_repository_project": resourceGithubRepositoryProject(), |
| 83 | + } |
| 84 | + |
| 85 | + for resourceName, resource := range resourcesWithEtag { |
| 86 | + t.Run(resourceName, func(t *testing.T) { |
| 87 | + etagField, exists := resource.Schema["etag"] |
| 88 | + if !exists { |
| 89 | + t.Errorf("Resource %s should have etag field", resourceName) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // Verify etag is optional and computed |
| 94 | + if !etagField.Optional { |
| 95 | + t.Errorf("etag should be optional in %s", resourceName) |
| 96 | + } |
| 97 | + if !etagField.Computed { |
| 98 | + t.Errorf("etag should be computed in %s", resourceName) |
| 99 | + } |
| 100 | + |
| 101 | + // Verify etag has DiffSuppressFunc |
| 102 | + if etagField.DiffSuppressFunc == nil { |
| 103 | + t.Errorf("etag should have DiffSuppressFunc in %s", resourceName) |
| 104 | + } |
| 105 | + |
| 106 | + // Verify DiffSuppressOnRefresh is enabled |
| 107 | + if !etagField.DiffSuppressOnRefresh { |
| 108 | + t.Errorf("etag should have DiffSuppressOnRefresh enabled in %s", resourceName) |
| 109 | + } |
| 110 | + |
| 111 | + // Verify the DiffSuppressFunc always returns true |
| 112 | + if etagField.DiffSuppressFunc != nil { |
| 113 | + d := schema.TestResourceDataRaw(t, resource.Schema, map[string]interface{}{}) |
| 114 | + result := etagField.DiffSuppressFunc("etag", "old", "new", d) |
| 115 | + if !result { |
| 116 | + t.Errorf("DiffSuppressFunc should return true in %s", resourceName) |
| 117 | + } |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments