|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMigrateGithubActionsOrganizationSecretStateV0toV1(t *testing.T) { |
| 11 | + // Secret without destroy_on_drift should get default value |
| 12 | + oldAttributes := map[string]string{ |
| 13 | + "id": "test-secret", |
| 14 | + "secret_name": "test-secret", |
| 15 | + "visibility": "private", |
| 16 | + "created_at": "2023-01-01T00:00:00Z", |
| 17 | + "updated_at": "2023-01-01T00:00:00Z", |
| 18 | + "plaintext_value": "secret-value", |
| 19 | + } |
| 20 | + |
| 21 | + newState, err := migrateGithubActionsOrganizationSecretStateV0toV1(&terraform.InstanceState{ |
| 22 | + ID: "test-secret", |
| 23 | + Attributes: oldAttributes, |
| 24 | + }) |
| 25 | + if err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + expectedAttributes := map[string]string{ |
| 30 | + "id": "test-secret", |
| 31 | + "secret_name": "test-secret", |
| 32 | + "visibility": "private", |
| 33 | + "created_at": "2023-01-01T00:00:00Z", |
| 34 | + "updated_at": "2023-01-01T00:00:00Z", |
| 35 | + "plaintext_value": "secret-value", |
| 36 | + "destroy_on_drift": "true", |
| 37 | + } |
| 38 | + if !reflect.DeepEqual(newState.Attributes, expectedAttributes) { |
| 39 | + t.Fatalf("Expected attributes:\n%#v\n\nGiven:\n%#v\n", |
| 40 | + expectedAttributes, newState.Attributes) |
| 41 | + } |
| 42 | + |
| 43 | + // Secret with existing destroy_on_drift should be preserved |
| 44 | + oldAttributesWithDrift := map[string]string{ |
| 45 | + "id": "test-secret", |
| 46 | + "secret_name": "test-secret", |
| 47 | + "visibility": "private", |
| 48 | + "destroy_on_drift": "false", |
| 49 | + } |
| 50 | + |
| 51 | + newState2, err := migrateGithubActionsOrganizationSecretStateV0toV1(&terraform.InstanceState{ |
| 52 | + ID: "test-secret", |
| 53 | + Attributes: oldAttributesWithDrift, |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + expectedAttributesWithDrift := map[string]string{ |
| 60 | + "id": "test-secret", |
| 61 | + "secret_name": "test-secret", |
| 62 | + "visibility": "private", |
| 63 | + "destroy_on_drift": "false", |
| 64 | + } |
| 65 | + if !reflect.DeepEqual(newState2.Attributes, expectedAttributesWithDrift) { |
| 66 | + t.Fatalf("Expected attributes:\n%#v\n\nGiven:\n%#v\n", |
| 67 | + expectedAttributesWithDrift, newState2.Attributes) |
| 68 | + } |
| 69 | +} |
0 commit comments