|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
@@ -71,6 +72,60 @@ func TestEtagDiffSuppressFunction(t *testing.T) { |
71 | 72 | } |
72 | 73 | } |
73 | 74 |
|
| 75 | +// TestEtagComputedOnlyRejectsSuppress verifies that the SDK rejects |
| 76 | +// DiffSuppressFunc on Computed-only fields, proving Optional is required. |
| 77 | +func TestEtagComputedOnlyRejectsSuppress(t *testing.T) { |
| 78 | + p := &schema.Provider{ |
| 79 | + ResourcesMap: map[string]*schema.Resource{ |
| 80 | + "test": { |
| 81 | + Schema: map[string]*schema.Schema{ |
| 82 | + "etag": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + DiffSuppressFunc: func(k, o, n string, d *schema.ResourceData) bool { |
| 86 | + return true |
| 87 | + }, |
| 88 | + DiffSuppressOnRefresh: true, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + err := p.InternalValidate() |
| 95 | + if err == nil { |
| 96 | + t.Fatal("expected SDK to reject DiffSuppressFunc on Computed-only field") |
| 97 | + } |
| 98 | + if !strings.Contains(err.Error(), "DiffSuppressFunc") { |
| 99 | + t.Fatalf("unexpected error: %s", err) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// TestEtagOptionalComputedAcceptsSuppress verifies that the SDK accepts |
| 104 | +// DiffSuppressFunc on Optional+Computed fields. |
| 105 | +func TestEtagOptionalComputedAcceptsSuppress(t *testing.T) { |
| 106 | + p := &schema.Provider{ |
| 107 | + ResourcesMap: map[string]*schema.Resource{ |
| 108 | + "test": { |
| 109 | + Schema: map[string]*schema.Schema{ |
| 110 | + "etag": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Optional: true, |
| 113 | + Computed: true, |
| 114 | + DiffSuppressFunc: func(k, o, n string, d *schema.ResourceData) bool { |
| 115 | + return true |
| 116 | + }, |
| 117 | + DiffSuppressOnRefresh: true, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + err := p.InternalValidate() |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("Optional+Computed with DiffSuppressFunc should be valid, got: %s", err) |
| 126 | + } |
| 127 | +} |
| 128 | + |
74 | 129 | // TestEtagSchemaConsistency ensure DiffSuppressFunc and DiffSuppressOnRefresh are consistently applied. |
75 | 130 | func TestEtagSchemaConsistency(t *testing.T) { |
76 | 131 | resourcesWithEtag := map[string]*schema.Resource{ |
@@ -136,6 +191,16 @@ func TestEtagSchemaConsistency(t *testing.T) { |
136 | 191 | t.Errorf("DiffSuppressFunc should return true in %s", resourceName) |
137 | 192 | } |
138 | 193 | } |
| 194 | + |
| 195 | + // Verify the schema passes SDK internal validation |
| 196 | + p := &schema.Provider{ |
| 197 | + ResourcesMap: map[string]*schema.Resource{ |
| 198 | + resourceName: resource, |
| 199 | + }, |
| 200 | + } |
| 201 | + if err := p.InternalValidate(); err != nil { |
| 202 | + t.Errorf("SDK validation failed for %s: %s", resourceName, err) |
| 203 | + } |
139 | 204 | }) |
140 | 205 | } |
141 | 206 | } |
0 commit comments