@@ -9,10 +9,13 @@ import (
99 "testing"
1010 "time"
1111
12+ "github.com/replicatedhq/troubleshoot/internal/testutils"
13+ troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1214 "github.com/replicatedhq/troubleshoot/pkg/httputil"
1315 "github.com/replicatedhq/troubleshoot/pkg/loader"
1416 "github.com/stretchr/testify/assert"
1517 "github.com/stretchr/testify/require"
18+ testclient "k8s.io/client-go/kubernetes/fake"
1619)
1720
1821var orig = `
@@ -90,3 +93,186 @@ func Test_loadSupportBundleSpecsFromURIs_TimeoutError(t *testing.T) {
9093
9194 assert .JSONEq (t , string (beforeJSON ), string (afterJSON ))
9295}
96+
97+ func Test_loadSupportBundleSpecs (t * testing.T ) {
98+ tests := []struct {
99+ name string
100+ args []string
101+ expected troubleshootv1beta2.SupportBundleSpec
102+ }{
103+ {
104+ name : "empty collectors array in spec, default collectors added" ,
105+ args : []string {testutils .ServeFromFilePath (t , `
106+ apiVersion: troubleshoot.sh/v1beta2
107+ kind: SupportBundle
108+ spec:
109+ collectors: []
110+ ` )},
111+ expected : troubleshootv1beta2.SupportBundleSpec {
112+ Collectors : []* troubleshootv1beta2.Collect {
113+ {
114+ ClusterInfo : & troubleshootv1beta2.ClusterInfo {},
115+ },
116+ {
117+ ClusterResources : & troubleshootv1beta2.ClusterResources {},
118+ },
119+ },
120+ },
121+ },
122+ {
123+ name : "no collectors defined in spec, default collectors added" ,
124+ args : []string {testutils .ServeFromFilePath (t , `
125+ apiVersion: troubleshoot.sh/v1beta2
126+ kind: SupportBundle
127+ spec:
128+ analyzers: []
129+ ` )},
130+ expected : troubleshootv1beta2.SupportBundleSpec {
131+ Collectors : []* troubleshootv1beta2.Collect {
132+ {
133+ ClusterInfo : & troubleshootv1beta2.ClusterInfo {},
134+ },
135+ {
136+ ClusterResources : & troubleshootv1beta2.ClusterResources {},
137+ },
138+ },
139+ Analyzers : []* troubleshootv1beta2.Analyze {},
140+ },
141+ },
142+ {
143+ name : "collectors present but defaults missing, default collectors added" ,
144+ args : []string {testutils .ServeFromFilePath (t , `
145+ apiVersion: troubleshoot.sh/v1beta2
146+ kind: SupportBundle
147+ spec:
148+ collectors:
149+ - logs: {}
150+ ` )},
151+ expected : troubleshootv1beta2.SupportBundleSpec {
152+ Collectors : []* troubleshootv1beta2.Collect {
153+ {
154+ Logs : & troubleshootv1beta2.Logs {},
155+ },
156+ {
157+ ClusterInfo : & troubleshootv1beta2.ClusterInfo {},
158+ },
159+ {
160+ ClusterResources : & troubleshootv1beta2.ClusterResources {},
161+ },
162+ },
163+ },
164+ },
165+ {
166+ name : "empty support bundle spec adds default collectors" ,
167+ args : []string {testutils .ServeFromFilePath (t , `
168+ apiVersion: troubleshoot.sh/v1beta2
169+ kind: SupportBundle
170+ ` )},
171+ expected : troubleshootv1beta2.SupportBundleSpec {
172+ Collectors : []* troubleshootv1beta2.Collect {
173+ {
174+ ClusterInfo : & troubleshootv1beta2.ClusterInfo {},
175+ },
176+ {
177+ ClusterResources : & troubleshootv1beta2.ClusterResources {},
178+ },
179+ },
180+ },
181+ },
182+ {
183+ name : "sb spec with host collectors does not add default collectors" ,
184+ args : []string {testutils .ServeFromFilePath (t , `
185+ apiVersion: troubleshoot.sh/v1beta2
186+ kind: SupportBundle
187+ spec:
188+ hostCollectors:
189+ - cpu: {}
190+ ` )},
191+ expected : troubleshootv1beta2.SupportBundleSpec {
192+ HostCollectors : []* troubleshootv1beta2.HostCollect {
193+ {
194+ CPU : & troubleshootv1beta2.CPU {},
195+ },
196+ },
197+ },
198+ },
199+ {
200+ name : "host collector spec with collectors does not add default collectors" ,
201+ args : []string {testutils .ServeFromFilePath (t , `
202+ apiVersion: troubleshoot.sh/v1beta2
203+ kind: HostCollector
204+ spec:
205+ collectors:
206+ - cpu: {}
207+ ` )},
208+ expected : troubleshootv1beta2.SupportBundleSpec {
209+ HostCollectors : []* troubleshootv1beta2.HostCollect {
210+ {
211+ CPU : & troubleshootv1beta2.CPU {},
212+ },
213+ },
214+ },
215+ },
216+ {
217+ name : "sb spec with host and in-cluster collectors adds default collectors" ,
218+ args : []string {testutils .ServeFromFilePath (t , `
219+ apiVersion: troubleshoot.sh/v1beta2
220+ kind: SupportBundle
221+ spec:
222+ hostCollectors:
223+ - cpu: {}
224+ collectors:
225+ - logs: {}
226+ ` )},
227+ expected : troubleshootv1beta2.SupportBundleSpec {
228+ HostCollectors : []* troubleshootv1beta2.HostCollect {
229+ {
230+ CPU : & troubleshootv1beta2.CPU {},
231+ },
232+ },
233+ Collectors : []* troubleshootv1beta2.Collect {
234+ {
235+ Logs : & troubleshootv1beta2.Logs {},
236+ },
237+ {
238+ ClusterInfo : & troubleshootv1beta2.ClusterInfo {},
239+ },
240+ {
241+ ClusterResources : & troubleshootv1beta2.ClusterResources {},
242+ },
243+ },
244+ },
245+ },
246+ {
247+ name : "sb spec with host collectors and empty in-cluster collectors does not default collectors" ,
248+ args : []string {testutils .ServeFromFilePath (t , `
249+ apiVersion: troubleshoot.sh/v1beta2
250+ kind: SupportBundle
251+ spec:
252+ hostCollectors:
253+ - cpu: {}
254+ collectors: []
255+ ` )},
256+ expected : troubleshootv1beta2.SupportBundleSpec {
257+ HostCollectors : []* troubleshootv1beta2.HostCollect {
258+ {
259+ CPU : & troubleshootv1beta2.CPU {},
260+ },
261+ },
262+ Collectors : []* troubleshootv1beta2.Collect {},
263+ },
264+ },
265+ }
266+
267+ for _ , test := range tests {
268+ t .Run (test .name , func (t * testing.T ) {
269+ ctx := context .Background ()
270+ client := testclient .NewSimpleClientset ()
271+
272+ sb , _ , err := loadSpecs (ctx , test .args , client )
273+ require .NoError (t , err )
274+
275+ assert .Equal (t , test .expected , sb .Spec )
276+ })
277+ }
278+ }
0 commit comments