@@ -2,89 +2,197 @@ package argocd
22
33import (
44 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+ "reflect"
56 "testing"
67)
78
8- func testResourceArgoCDProjectStateDataV0 () map [string ]interface {} {
9- return map [string ]interface {}{
10- "spec" : []map [string ]interface {}{
11- {
12- "orphaned_resources" : map [string ]bool {"warn" : true },
9+ func orphanedResourcesSchemaSetFuncV1 () schema.SchemaSetFunc {
10+ return schema .HashResource (& schema.Resource {
11+ Schema : map [string ]* schema.Schema {
12+ "warn" : {
13+ Type : schema .TypeBool ,
14+ Optional : true ,
15+ },
16+ "ignore" : {
17+ Type : schema .TypeSet ,
18+ Optional : true ,
19+ Elem : & schema.Resource {
20+ Schema : map [string ]* schema.Schema {
21+ "group" : {
22+ Type : schema .TypeString ,
23+ ValidateFunc : validateGroupName ,
24+ Optional : true ,
25+ },
26+ "kind" : {
27+ Type : schema .TypeString ,
28+ Optional : true ,
29+ },
30+ "name" : {
31+ Type : schema .TypeString ,
32+ Optional : true ,
33+ },
34+ },
35+ },
1336 },
1437 },
15- }
38+ })
1639}
1740
18- func testResourceArgoCDProjectStateDataV1 () map [string ]interface {} {
19- newOrphanedResources := schema .NewSet (
20- schema .HashResource (& schema.Resource {
21- Schema : map [string ]* schema.Schema {
22- "warn" : {
23- Type : schema .TypeBool ,
24- Optional : true ,
41+ func TestResourceArgoCDProjectStateUpgradeV0 (t * testing.T ) {
42+ type projectStateUpgradeTestCases []struct {
43+ name string
44+ expectedState map [string ]interface {}
45+ sourceState map [string ]interface {}
46+ }
47+
48+ cases := projectStateUpgradeTestCases {
49+ {
50+ name : "source_<_v0.5.0_with_warn" ,
51+ sourceState : map [string ]interface {}{
52+ "spec" : []map [string ]interface {}{
53+ {
54+ "orphaned_resources" : map [string ]bool {"warn" : true },
55+ },
2556 },
26- "ignore" : {
27- Type : schema .TypeSet ,
28- Optional : true ,
29- Elem : & schema.Resource {
30- Schema : map [string ]* schema.Schema {
31- "group" : {
32- Type : schema .TypeString ,
33- ValidateFunc : validateGroupName ,
34- Optional : true ,
35- },
36- "kind" : {
37- Type : schema .TypeString ,
38- Optional : true ,
39- },
40- "name" : {
41- Type : schema .TypeString ,
42- Optional : true ,
43- },
44- },
57+ },
58+ expectedState : map [string ]interface {}{
59+ "spec" : []map [string ]interface {}{
60+ {
61+ "orphaned_resources" : schema .NewSet (
62+ orphanedResourcesSchemaSetFuncV1 (),
63+ []interface {}{map [string ]interface {}{"warn" : true }},
64+ ),
65+ },
66+ },
67+ },
68+ },
69+ {
70+ name : "source_<_v0.5.0_without_orphaned_resources" ,
71+ sourceState : map [string ]interface {}{
72+ "spec" : []map [string ]interface {}{
73+ {
74+ "source_repos" : []string {"*" },
4575 },
4676 },
4777 },
48- }),
49- []interface {}{map [string ]interface {}{"warn" : true }},
50- )
51- return map [string ]interface {}{
52- "spec" : []map [string ]interface {}{
53- {
54- "orphaned_resources" : newOrphanedResources ,
78+ expectedState : map [string ]interface {}{
79+ "spec" : []map [string ]interface {}{
80+ {
81+ "source_repos" : []string {"*" },
82+ },
83+ },
5584 },
5685 },
57- }
58- }
59-
60- func TestResourceArgoCDProjectStateUpgradeV0 (t * testing.T ) {
61- cases := []struct {
62- name string
63- expected map [string ]interface {}
64- sourceState map [string ]interface {}
65- }{
6686 {
67- "source < v0.5.0" ,
68- testResourceArgoCDProjectStateDataV1 (),
69- testResourceArgoCDProjectStateDataV0 (),
87+ name : "source_<_v0.5.0_with_empty_orphaned_resources" ,
88+ sourceState : map [string ]interface {}{
89+ "spec" : []map [string ]interface {}{
90+ {
91+ "orphaned_resources" : map [string ]bool {},
92+ },
93+ },
94+ },
95+ expectedState : map [string ]interface {}{
96+ "spec" : []map [string ]interface {}{
97+ {
98+ "orphaned_resources" : schema .NewSet (
99+ orphanedResourcesSchemaSetFuncV1 (),
100+ []interface {}{map [string ]interface {}{"warn" : false }},
101+ ),
102+ },
103+ },
104+ },
105+ },
106+ {
107+ name : "source_<_v1.1.0_>=_0.4.8_with_warn" ,
108+ sourceState : map [string ]interface {}{
109+ "spec" : []map [string ]interface {}{
110+ {
111+ "orphaned_resources" : schema .NewSet (
112+ orphanedResourcesSchemaSetFuncV1 (),
113+ []interface {}{map [string ]interface {}{"warn" : true }},
114+ ),
115+ "source_repos" : []string {"*" },
116+ },
117+ },
118+ },
119+ expectedState : map [string ]interface {}{
120+ "spec" : []map [string ]interface {}{
121+ {
122+ "orphaned_resources" : schema .NewSet (
123+ orphanedResourcesSchemaSetFuncV1 (),
124+ []interface {}{map [string ]interface {}{"warn" : true }},
125+ ),
126+ "source_repos" : []string {"*" },
127+ },
128+ },
129+ },
70130 },
71131 {
72- "source < v1.1.0, >= v0.5.0" ,
73- testResourceArgoCDProjectStateDataV1 (),
74- testResourceArgoCDProjectStateDataV1 (),
132+ name : "source_<_v1.1.1_without_orphaned_resources" ,
133+ sourceState : map [string ]interface {}{
134+ "spec" : []map [string ]interface {}{
135+ {
136+ "source_repos" : []string {"*" },
137+ },
138+ },
139+ },
140+ expectedState : map [string ]interface {}{
141+ "spec" : []map [string ]interface {}{
142+ {
143+ "source_repos" : []string {"*" },
144+ },
145+ },
146+ },
75147 },
76148 }
149+
77150 for _ , tc := range cases {
78151 t .Run (tc .name , func (t * testing.T ) {
79- _actual , err := resourceArgoCDProjectStateUpgradeV0 (tc .sourceState , nil )
152+ actualState , err := resourceArgoCDProjectStateUpgradeV0 (tc .sourceState , nil )
80153 if err != nil {
81154 t .Fatalf ("error migrating state: %s" , err )
82155 }
83- expected := tc .expected ["spec" ].([]map [string ]interface {})[0 ]["orphaned_resources" ].(* schema.Set )
84- actual := _actual ["spec" ].([]map [string ]interface {})[0 ]["orphaned_resources" ].(* schema.Set )
85- if ! expected .HashEqual (actual ) {
86- t .Fatalf ("\n \n expected:\n \n %#v\n \n got:\n \n %#v\n \n " , expected , actual )
156+ if ! reflect .DeepEqual (actualState , tc .expectedState ) {
157+ if expectedSet , ok := tc .expectedState ["spec" ].([]map [string ]interface {})[0 ]["orphaned_resources" ]; ok {
158+
159+ actualSet := actualState ["spec" ].([]map [string ]interface {})[0 ]["orphaned_resources" ].(* schema.Set )
160+
161+ if ! expectedSet .(* schema.Set ).HashEqual (actualSet ) {
162+ t .Fatalf ("\n \n expected:\n \n %#v\n \n got:\n \n %#v\n \n " , expectedSet , actualSet )
163+ }
164+ // Cannot DeepEqual a pointer reference
165+ for k , _ := range tc .expectedState ["spec" ].([]map [string ]interface {})[0 ] {
166+ av := actualState ["spec" ].([]map [string ]interface {})[0 ][k ]
167+ ev := tc .expectedState ["spec" ].([]map [string ]interface {})[0 ][k ]
168+ if k != "orphaned_resources" && ! reflect .DeepEqual (av , ev ) {
169+ t .Fatalf ("\n \n [maps] expected:\n \n %#v\n \n got:\n \n %#v\n \n " , tc .expectedState , actualState )
170+ }
171+ }
172+ for k , av := range actualState ["spec" ].([]map [string ]interface {})[0 ] {
173+ ev := tc .expectedState ["spec" ].([]map [string ]interface {})[0 ][k ]
174+ if k != "orphaned_resources" && ! reflect .DeepEqual (av , ev ) {
175+ t .Fatalf ("\n \n [maps] expected:\n \n %#v\n \n got:\n \n %#v\n \n " , tc .expectedState , actualState )
176+ }
177+ }
178+ } else {
179+ // Cannot DeepEqual a pointer reference
180+ for k , _ := range tc .expectedState ["spec" ].([]map [string ]interface {})[0 ] {
181+ av := actualState ["spec" ].([]map [string ]interface {})[0 ][k ]
182+ ev := tc .expectedState ["spec" ].([]map [string ]interface {})[0 ][k ]
183+ if k != "orphaned_resources" && ! reflect .DeepEqual (av , ev ) {
184+ t .Fatalf ("\n \n [maps without set] expected:\n \n %#v\n \n got:\n \n %#v\n \n " , tc .expectedState , actualState )
185+ }
186+ }
187+ for k , av := range actualState ["spec" ].([]map [string ]interface {})[0 ] {
188+ ev := tc .expectedState ["spec" ].([]map [string ]interface {})[0 ][k ]
189+ if k != "orphaned_resources" && ! reflect .DeepEqual (av , ev ) {
190+ t .Fatalf ("\n \n [maps] expected:\n \n %#v\n \n got:\n \n %#v\n \n " , tc .expectedState , actualState )
191+ }
192+ }
193+ }
87194 }
195+
88196 })
89197 }
90198}
0 commit comments