|
7 | 7 | "testing" |
8 | 8 |
|
9 | 9 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
10 | 11 | ) |
11 | 12 |
|
12 | 13 | func TestAccGithubActionsOrganizationSecret(t *testing.T) { |
@@ -184,4 +185,108 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) { |
184 | 185 | testCase(t, organization) |
185 | 186 | }) |
186 | 187 | }) |
| 188 | + |
| 189 | + // Unit tests for drift detection behavior |
| 190 | + t.Run("destroyOnDrift false clears sensitive values instead of recreating", func(t *testing.T) { |
| 191 | + originalTimestamp := "2023-01-01T00:00:00Z" |
| 192 | + newTimestamp := "2023-01-02T00:00:00Z" |
| 193 | + |
| 194 | + d := schema.TestResourceDataRaw(t, resourceGithubActionsOrganizationSecret().Schema, map[string]interface{}{ |
| 195 | + "secret_name": "test-secret", |
| 196 | + "plaintext_value": "original-value", |
| 197 | + "encrypted_value": "original-encrypted", |
| 198 | + "visibility": "private", |
| 199 | + "destroy_on_drift": false, |
| 200 | + "updated_at": originalTimestamp, |
| 201 | + }) |
| 202 | + d.SetId("test-secret") |
| 203 | + |
| 204 | + // Simulate drift detection logic when destroy_on_drift is false |
| 205 | + destroyOnDrift := d.Get("destroy_on_drift").(bool) |
| 206 | + storedUpdatedAt, hasStoredUpdatedAt := d.GetOk("updated_at") |
| 207 | + |
| 208 | + if hasStoredUpdatedAt && storedUpdatedAt != newTimestamp { |
| 209 | + if destroyOnDrift { |
| 210 | + // Would clear ID for recreation |
| 211 | + d.SetId("") |
| 212 | + } else { |
| 213 | + // Should clear sensitive values to trigger update |
| 214 | + d.Set("encrypted_value", "") |
| 215 | + d.Set("plaintext_value", "") |
| 216 | + } |
| 217 | + d.Set("updated_at", newTimestamp) |
| 218 | + } |
| 219 | + |
| 220 | + // Should NOT have cleared the ID when destroy_on_drift=false |
| 221 | + if d.Id() == "" { |
| 222 | + t.Error("Expected ID to be preserved when destroy_on_drift=false, but it was cleared") |
| 223 | + } |
| 224 | + |
| 225 | + // Should have cleared sensitive values to trigger update plan |
| 226 | + if plaintextValue := d.Get("plaintext_value").(string); plaintextValue != "" { |
| 227 | + t.Errorf("Expected plaintext_value to be cleared for update plan, got %s", plaintextValue) |
| 228 | + } |
| 229 | + |
| 230 | + if encryptedValue := d.Get("encrypted_value").(string); encryptedValue != "" { |
| 231 | + t.Errorf("Expected encrypted_value to be cleared for update plan, got %s", encryptedValue) |
| 232 | + } |
| 233 | + |
| 234 | + // Should have updated the timestamp |
| 235 | + if updatedAt := d.Get("updated_at").(string); updatedAt != newTimestamp { |
| 236 | + t.Errorf("Expected timestamp to be updated to %s, got %s", newTimestamp, updatedAt) |
| 237 | + } |
| 238 | + }) |
| 239 | + |
| 240 | + t.Run("destroyOnDrift true still recreates resource on drift", func(t *testing.T) { |
| 241 | + originalTimestamp := "2023-01-01T00:00:00Z" |
| 242 | + newTimestamp := "2023-01-02T00:00:00Z" |
| 243 | + |
| 244 | + d := schema.TestResourceDataRaw(t, resourceGithubActionsOrganizationSecret().Schema, map[string]interface{}{ |
| 245 | + "secret_name": "test-secret", |
| 246 | + "plaintext_value": "original-value", |
| 247 | + "visibility": "private", |
| 248 | + "destroy_on_drift": true, // Explicitly set to true |
| 249 | + "updated_at": originalTimestamp, |
| 250 | + }) |
| 251 | + d.SetId("test-secret") |
| 252 | + |
| 253 | + // Simulate drift detection logic when destroy_on_drift is true |
| 254 | + destroyOnDrift := d.Get("destroy_on_drift").(bool) |
| 255 | + storedUpdatedAt, hasStoredUpdatedAt := d.GetOk("updated_at") |
| 256 | + |
| 257 | + if hasStoredUpdatedAt && storedUpdatedAt != newTimestamp { |
| 258 | + if destroyOnDrift { |
| 259 | + // Should clear ID for recreation (original behavior) |
| 260 | + d.SetId("") |
| 261 | + return // Exit early like the real function would |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + // Should have cleared the ID for recreation when destroy_on_drift=true |
| 266 | + if d.Id() != "" { |
| 267 | + t.Error("Expected ID to be cleared for recreation when destroy_on_drift=true, but it was preserved") |
| 268 | + } |
| 269 | + }) |
| 270 | + |
| 271 | + t.Run("destroy_on_drift field defaults", func(t *testing.T) { |
| 272 | + // Test that destroy_on_drift defaults to true for backward compatibility |
| 273 | + schema := resourceGithubActionsOrganizationSecret().Schema["destroy_on_drift"] |
| 274 | + if schema.Default != true { |
| 275 | + t.Error("destroy_on_drift should default to true for backward compatibility") |
| 276 | + } |
| 277 | + }) |
| 278 | + |
| 279 | + t.Run("default destroy_on_drift is true", func(t *testing.T) { |
| 280 | + d := schema.TestResourceDataRaw(t, resourceGithubActionsOrganizationSecret().Schema, map[string]interface{}{ |
| 281 | + "secret_name": "test-secret", |
| 282 | + "plaintext_value": "test-value", |
| 283 | + "visibility": "private", |
| 284 | + // destroy_on_drift not set, should default to true |
| 285 | + }) |
| 286 | + |
| 287 | + destroyOnDrift := d.Get("destroy_on_drift").(bool) |
| 288 | + if !destroyOnDrift { |
| 289 | + t.Error("Expected destroy_on_drift to default to true") |
| 290 | + } |
| 291 | + }) |
187 | 292 | } |
0 commit comments