@@ -3,6 +3,7 @@ package sops
33import (
44 "fmt"
55 "os"
6+ "reflect"
67 "testing"
78
89 "github.com/hashicorp/terraform/helper/resource"
@@ -26,6 +27,9 @@ func TestDataSourceSopsFile_basic(t *testing.T) {
2627 Config : config ,
2728 Check : resource .ComposeTestCheckFunc (
2829 resource .TestCheckResourceAttr ("data.sops_file.test_basic" , "data.hello" , "world" ),
30+ resource .TestCheckResourceAttr ("data.sops_file.test_basic" , "data.integer" , "0" ),
31+ resource .TestCheckResourceAttr ("data.sops_file.test_basic" , "data.float" , "0.2" ),
32+ resource .TestCheckResourceAttr ("data.sops_file.test_basic" , "data.bool" , "true" ),
2933 ),
3034 },
3135 },
@@ -81,3 +85,133 @@ func TestDataSourceSopsFile_raw(t *testing.T) {
8185 },
8286 })
8387}
88+
89+ const configTestDataSourceSopsFile_simplelist = `
90+ data "sops_file" "test_list" {
91+ source_file = "%s/test-fixtures/simple-list.yaml"
92+ }`
93+
94+ func TestDataSourceSopsFile_simplelist (t * testing.T ) {
95+ wd , err := os .Getwd ()
96+ if err != nil {
97+ t .Fatal (err )
98+ }
99+ config := fmt .Sprintf (configTestDataSourceSopsFile_simplelist , wd )
100+ resource .UnitTest (t , resource.TestCase {
101+ Providers : testAccProviders ,
102+ Steps : []resource.TestStep {
103+ {
104+ Config : config ,
105+ Check : resource .ComposeTestCheckFunc (
106+ resource .TestCheckResourceAttr ("data.sops_file.test_list" , "data.a_list.0" , "val1" ),
107+ resource .TestCheckResourceAttr ("data.sops_file.test_list" , "data.a_list.1" , "val2" ),
108+ ),
109+ },
110+ },
111+ })
112+ }
113+
114+ const configTestDataSourceSopsFile_complexlist = `
115+ data "sops_file" "test_list" {
116+ source_file = "%s/test-fixtures/complex-list.yaml"
117+ }`
118+
119+ func TestDataSourceSopsFile_complexlist (t * testing.T ) {
120+ wd , err := os .Getwd ()
121+ if err != nil {
122+ t .Fatal (err )
123+ }
124+ config := fmt .Sprintf (configTestDataSourceSopsFile_complexlist , wd )
125+ resource .UnitTest (t , resource.TestCase {
126+ Providers : testAccProviders ,
127+ Steps : []resource.TestStep {
128+ {
129+ Config : config ,
130+ Check : resource .ComposeTestCheckFunc (
131+ resource .TestCheckResourceAttr ("data.sops_file.test_list" , "data.a_list.0.name" , "foo" ),
132+ resource .TestCheckResourceAttr ("data.sops_file.test_list" , "data.a_list.0.index" , "0" ),
133+ resource .TestCheckResourceAttr ("data.sops_file.test_list" , "data.a_list.1.name" , "bar" ),
134+ resource .TestCheckResourceAttr ("data.sops_file.test_list" , "data.a_list.1.index" , "1" ),
135+ ),
136+ },
137+ },
138+ })
139+ }
140+
141+ func TestFlattening (t * testing.T ) {
142+ tc := []struct {
143+ name string
144+ input map [string ]interface {}
145+ expected map [string ]string
146+ }{
147+ {
148+ name : "all data types become strings" ,
149+ input : map [string ]interface {}{
150+ "a_string" : "foo" ,
151+ "an_integer" : 12 ,
152+ "a_bool" : true ,
153+ "a_float" : 1.1 ,
154+ },
155+ expected : map [string ]string {
156+ "a_string" : "foo" ,
157+ "an_integer" : "12" ,
158+ "a_bool" : "true" ,
159+ "a_float" : "1.1" ,
160+ },
161+ },
162+ {
163+ name : "dicts are unnested" ,
164+ input : map [string ]interface {}{
165+ "a_dict" : map [string ]interface {}{"foo" : "bar" },
166+ },
167+ expected : map [string ]string {
168+ "a_dict.foo" : "bar" ,
169+ },
170+ },
171+ {
172+ name : "lists are unpacked with index keys" ,
173+ input : map [string ]interface {}{
174+ "a_list" : []interface {}{1 , 2 },
175+ },
176+ expected : map [string ]string {
177+ "a_list.0" : "1" ,
178+ "a_list.1" : "2" ,
179+ },
180+ },
181+ {
182+ name : "deep nesting" ,
183+ /*
184+ This test corresponds to this yaml structure:
185+ foo:
186+ - a: 1
187+ b:
188+ c:
189+ - d: 2
190+ */
191+ input : map [string ]interface {}{
192+ "foo" : []interface {}{
193+ map [string ]interface {}{
194+ "a" : 1 ,
195+ "b" : map [string ]interface {}{
196+ "c" : []interface {}{
197+ map [string ]interface {}{"d" : 2 },
198+ },
199+ },
200+ },
201+ },
202+ },
203+ expected : map [string ]string {
204+ "foo.0.a" : "1" ,
205+ "foo.0.b.c.0.d" : "2" ,
206+ },
207+ },
208+ }
209+ for _ , c := range tc {
210+ t .Run (c .name , func (t * testing.T ) {
211+ output := flatten (c .input )
212+ if ! reflect .DeepEqual (c .expected , output ) {
213+ t .Errorf ("Unexpected flattening output, expected %v, got %v" , c .expected , output )
214+ }
215+ })
216+ }
217+ }
0 commit comments