@@ -3,67 +3,53 @@ package github
33import (
44 "reflect"
55 "testing"
6-
7- "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
86)
97
10- func TestMigrateGithubActionsSecretStateV0toV1 (t * testing.T ) {
11- // Secret without destroy_on_drift should get default value
12- oldAttributes := map [string ]string {
8+ func testResourceGithubActionsSecretInstanceStateDataV0 () map [string ]any {
9+ return map [string ]any {
1310 "id" : "test-secret" ,
1411 "repository" : "test-repo" ,
1512 "secret_name" : "test-secret" ,
1613 "created_at" : "2023-01-01T00:00:00Z" ,
1714 "updated_at" : "2023-01-01T00:00:00Z" ,
1815 "plaintext_value" : "secret-value" ,
1916 }
17+ }
2018
21- newState , err := migrateGithubActionsSecretStateV0toV1 (& 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- "repository" : "test-repo" ,
32- "secret_name" : "test-secret" ,
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 \n Given:\n %#v\n " ,
40- expectedAttributes , newState .Attributes )
41- }
19+ func testResourceGithubActionsSecretInstanceStateDataV0_WithDrift () map [string ]any {
20+ v0 := testResourceGithubActionsSecretInstanceStateDataV0 ()
21+ v0 ["destroy_on_drift" ] = false
22+ return v0
23+ }
4224
43- // Secret with existing destroy_on_drift should be preserved
44- oldAttributesWithDrift := map [string ]string {
45- "id" : "test-secret" ,
46- "repository" : "test-repo" ,
47- "secret_name" : "test-secret" ,
48- "destroy_on_drift" : "false" ,
49- }
25+ func testResourceGithubActionsSecretInstanceStateDataV1 () map [string ]any {
26+ v0 := testResourceGithubActionsSecretInstanceStateDataV0 ()
27+ v0 ["destroy_on_drift" ] = true
28+ return v0
29+ }
5030
51- newState2 , err := migrateGithubActionsSecretStateV0toV1 (& terraform.InstanceState {
52- ID : "test-secret" ,
53- Attributes : oldAttributesWithDrift ,
31+ func TestGithub_MigrateActionsSecretStateV0toV1 (t * testing.T ) {
32+ t .Run ("without destroy_on_drift" , func (t * testing.T ) {
33+ expected := testResourceGithubActionsSecretInstanceStateDataV1 ()
34+ actual , err := resourceGithubActionsSecretInstanceStateUpgradeV0 (t .Context (), testResourceGithubActionsSecretInstanceStateDataV0 (), nil )
35+ if err != nil {
36+ t .Fatalf ("error migrating state: %s" , err )
37+ }
38+
39+ if ! reflect .DeepEqual (expected , actual ) {
40+ t .Fatalf ("\n \n expected:\n \n %#v\n \n got:\n \n %#v\n \n " , expected , actual )
41+ }
5442 })
55- if err != nil {
56- t .Fatal (err )
57- }
5843
59- expectedAttributesWithDrift := map [string ]string {
60- "id" : "test-secret" ,
61- "repository" : "test-repo" ,
62- "secret_name" : "test-secret" ,
63- "destroy_on_drift" : "false" ,
64- }
65- if ! reflect .DeepEqual (newState2 .Attributes , expectedAttributesWithDrift ) {
66- t .Fatalf ("Expected attributes:\n %#v\n \n Given:\n %#v\n " ,
67- expectedAttributesWithDrift , newState2 .Attributes )
68- }
44+ t .Run ("with destroy_on_drift" , func (t * testing.T ) {
45+ expected := testResourceGithubActionsSecretInstanceStateDataV0_WithDrift ()
46+ actual , err := resourceGithubActionsSecretInstanceStateUpgradeV0 (t .Context (), testResourceGithubActionsSecretInstanceStateDataV0_WithDrift (), nil )
47+ if err != nil {
48+ t .Fatalf ("error migrating state: %s" , err )
49+ }
50+
51+ if ! reflect .DeepEqual (expected , actual ) {
52+ t .Fatalf ("\n \n expected:\n \n %#v\n \n got:\n \n %#v\n \n " , expected , actual )
53+ }
54+ })
6955}
0 commit comments