11package render
22
33import (
4+ "fmt"
5+ "io/fs"
46 "io/ioutil"
57 "os"
8+ "path"
9+ "strings"
610 "testing"
711
812 configv1 "github.com/openshift/api/config/v1"
13+ "github.com/openshift/cluster-cloud-controller-manager-operator/pkg/cloud/aws"
914 "github.com/stretchr/testify/assert"
1015 corev1 "k8s.io/api/core/v1"
1116 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17+ "sigs.k8s.io/controller-runtime/pkg/client"
1218)
1319
1420const (
@@ -21,6 +27,13 @@ status:
2127 platform: AWS
2228 platformStatus:
2329 type: AWS
30+ `
31+ infraMissingPlatform = `apiVersion: config.openshift.io/v1
32+ kind: Infrastructure
33+ metadata:
34+ name: cluster
35+ spec: {}
36+ status: {}
2437`
2538 imagesConfigMap = `apiVersion: v1
2639kind: ConfigMap
@@ -85,28 +98,23 @@ func TestReadAssets(t *testing.T) {
8598 infra : matchingInfra ,
8699 imagesContent : imagesConfigMap ,
87100 imagesConfigMap : matchingConfigMap ,
88- },
89- {
90- name : "Infrastructure not located" ,
91- expectError : "open not_found: no such file or directory" ,
92- },
93- {
94- name : "ImagesConfigMap not located" ,
95- infraContent : infra ,
96- expectError : "open not_found: no such file or directory" ,
97- },
98- {
99- name : "Bad images config map file content" ,
100- infraContent : "BAD" ,
101- expectError : "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1.Infrastructure" ,
102- },
103- {
104- name : "Bad infrastructure file content" ,
105- infraContent : infra ,
106- imagesContent : "BAD" ,
107- expectError : "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1.ConfigMap" ,
108- },
109- }
101+ }, {
102+ name : "Infrastructure not located" ,
103+ expectError : "open not_found: no such file or directory" ,
104+ }, {
105+ name : "ImagesConfigMap not located" ,
106+ infraContent : infra ,
107+ expectError : "open not_found: no such file or directory" ,
108+ }, {
109+ name : "Bad images config map file content" ,
110+ infraContent : "BAD" ,
111+ expectError : "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1.Infrastructure" ,
112+ }, {
113+ name : "Bad infrastructure file content" ,
114+ infraContent : infra ,
115+ imagesContent : "BAD" ,
116+ expectError : "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1.ConfigMap" ,
117+ }}
110118
111119 infraPath := "infra.yaml"
112120 configPath := "imagesConfigMap.yaml"
@@ -150,3 +158,135 @@ func TestReadAssets(t *testing.T) {
150158 })
151159 }
152160}
161+
162+ func TestRenderRun (t * testing.T ) {
163+ tc := []struct {
164+ name string
165+ infraContent string
166+ imagesContent string
167+ expectObjects []client.Object
168+ expectError string
169+ }{{
170+ name : "Unmarshal both infrastructure and images with no issue" ,
171+ infraContent : infra ,
172+ imagesContent : imagesConfigMap ,
173+ expectObjects : aws .GetBootstrapResources (),
174+ }, {
175+ name : "Infrastructure not populated" ,
176+ infraContent : infraMissingPlatform ,
177+ imagesContent : imagesConfigMap ,
178+ expectError : "platform status is not pupulated on infrastructure" ,
179+ }, {
180+ name : "ImagesConfigMap not located" ,
181+ infraContent : infra ,
182+ imagesContent : "BAD" ,
183+ expectError : "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1.ConfigMap" ,
184+ }}
185+
186+ infraPath := "infra.yaml"
187+ configPath := "imagesConfigMap.yaml"
188+
189+ for _ , tc := range tc {
190+ t .Run (tc .name , func (t * testing.T ) {
191+ imagesFile := "not_found"
192+ infrastructureFile := "not_found"
193+ destination , err := ioutil .TempDir ("" , "test" )
194+ assert .NoError (t , err )
195+
196+ if tc .imagesContent != "" {
197+ file , err := ioutil .TempFile (os .TempDir (), configPath )
198+ path := file .Name ()
199+ assert .NoError (t , err )
200+ defer file .Close ()
201+
202+ _ , err = file .WriteString (tc .imagesContent )
203+ assert .NoError (t , err )
204+ imagesFile = path
205+ }
206+
207+ if tc .infraContent != "" {
208+ file , err := ioutil .TempFile (os .TempDir (), infraPath )
209+ path := file .Name ()
210+ assert .NoError (t , err )
211+ defer file .Close ()
212+
213+ _ , err = file .WriteString (tc .infraContent )
214+ assert .NoError (t , err )
215+ infrastructureFile = path
216+ }
217+
218+ r := New ("" , infrastructureFile , imagesFile )
219+ err = r .Run (destination )
220+ if tc .expectError != "" {
221+ assert .EqualError (t , err , tc .expectError )
222+ return
223+ }
224+ assert .NoError (t , err )
225+
226+ // Assert all files were written to bootstrap dir
227+ files , err := ioutil .ReadDir (path .Join (destination , bootstrapPrefix ))
228+ assert .NoError (t , err )
229+ assert .Len (t , files , len (tc .expectObjects ))
230+ })
231+ }
232+ }
233+
234+ func TestWriteAssets (t * testing.T ) {
235+ tc := []struct {
236+ name string
237+ destination string
238+ preCreateMode fs.FileMode
239+ objects []client.Object
240+ expectErr string
241+ }{{
242+ name : "Writing file finished with success" ,
243+ destination : "test" ,
244+ objects : []client.Object {matchingInfra , matchingConfigMap },
245+ }, {
246+ name : "Fail to write into /dev/null" ,
247+ objects : []client.Object {matchingInfra , matchingConfigMap },
248+ expectErr : "mkdir /dev/null: not a directory" ,
249+ }, {
250+ name : "Fail to write into /dev/null" ,
251+ objects : []client.Object {matchingInfra , matchingConfigMap },
252+ destination : "bad_permissions" ,
253+ preCreateMode : 0444 ,
254+ expectErr : "permission denied" ,
255+ }}
256+
257+ for _ , tc := range tc {
258+ t .Run (tc .name , func (t * testing.T ) {
259+ destination := "/dev/null"
260+
261+ if tc .destination != "" {
262+ dirPath , err := ioutil .TempDir ("" , tc .destination )
263+ assert .NoError (t , err )
264+ destination = dirPath
265+ if tc .preCreateMode != 0 {
266+ os .MkdirAll (path .Join (destination , bootstrapPrefix ), tc .preCreateMode )
267+ assert .NoError (t , err )
268+ }
269+ }
270+
271+ err := writeAssets (destination , tc .objects )
272+ if tc .expectErr != "" {
273+ assert .Contains (t , err .Error (), tc .expectErr )
274+ return
275+ }
276+ assert .NoError (t , err )
277+
278+ // Assert all files were written to bootstrap dir
279+ files , err := ioutil .ReadDir (path .Join (destination , bootstrapPrefix ))
280+ assert .NoError (t , err )
281+ assert .Len (t , files , len (tc .objects ))
282+
283+ names := []string {}
284+ for _ , file := range files {
285+ names = append (names , file .Name ())
286+ }
287+ for _ , res := range tc .objects {
288+ assert .Contains (t , names , fmt .Sprintf ("%s-%s.yaml" , strings .ToLower (res .GetObjectKind ().GroupVersionKind ().Kind ), res .GetName ()))
289+ }
290+ })
291+ }
292+ }
0 commit comments