@@ -18,7 +18,8 @@ import (
1818 testclient "k8s.io/client-go/kubernetes/fake"
1919)
2020
21- var orig = `
21+ func templSpec () string {
22+ return `
2223apiVersion: troubleshoot.sh/v1beta2
2324kind: SupportBundle
2425metadata:
3031 name: kube-root-ca.crt
3132 namespace: default
3233`
34+ }
3335
3436func Test_loadSupportBundleSpecsFromURIs (t * testing.T ) {
3537 // Run a webserver to serve the spec
4547 }))
4648 defer srv .Close ()
4749
48- orig := strings .ReplaceAll (orig , "$MY_URI" , srv .URL )
50+ orig := strings .ReplaceAll (templSpec () , "$MY_URI" , srv .URL )
4951
5052 ctx := context .Background ()
5153 kinds , err := loader .LoadSpecs (ctx , loader.LoadOptions {RawSpec : orig })
5759 err = loadSupportBundleSpecsFromURIs (ctx , kinds )
5860 require .NoError (t , err )
5961
60- require .Len (t , kinds .SupportBundlesV1Beta2 , 2 )
61- assert .NotNil (t , kinds .SupportBundlesV1Beta2 [1 ].Spec .Collectors [0 ].ClusterInfo )
62+ require .Len (t , kinds .SupportBundlesV1Beta2 , 1 )
63+ assert .NotNil (t , kinds .SupportBundlesV1Beta2 [0 ].Spec .Collectors [0 ].ClusterInfo )
64+ }
65+
66+ func Test_loadMultipleSupportBundleSpecsWithNoURIs (t * testing.T ) {
67+ ctx := context .Background ()
68+ client := testclient .NewSimpleClientset ()
69+ specs := []string {testutils .ServeFromFilePath (t , `
70+ apiVersion: troubleshoot.sh/v1beta2
71+ kind: SupportBundle
72+ metadata:
73+ name: sb-1
74+ spec:
75+ collectors:
76+ - clusterInfo:{}` ), testutils .ServeFromFilePath (t , `
77+ apiVersion: troubleshoot.sh/v1beta2
78+ kind: SupportBundle
79+ metadata:
80+ name: sb-2
81+ spec:
82+ collectors:
83+ - clusterInfo: {}` )}
84+
85+ sb , _ , err := loadSpecs (ctx , specs , client )
86+ require .NoError (t , err )
87+ require .Len (t , sb .Spec .Collectors , 2 )
88+ }
89+
90+ func Test_loadMultipleSupportBundleSpecsWithURIs (t * testing.T ) {
91+ ctx := context .Background ()
92+ client := testclient .NewSimpleClientset ()
93+
94+ specFile := testutils .ServeFromFilePath (t , `
95+ apiVersion: troubleshoot.sh/v1beta2
96+ kind: SupportBundle
97+ metadata:
98+ name: sb-file
99+ spec:
100+ collectors:
101+ - logs:
102+ name: podlogs/kotsadm
103+ selector:
104+ - app=kotsadm
105+ ` )
106+
107+ // Run a webserver to serve the spec
108+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
109+ w .Write ([]byte (`
110+ apiVersion: troubleshoot.sh/v1beta2
111+ kind: SupportBundle
112+ metadata:
113+ name: sb-uri
114+ spec:
115+ collectors:
116+ - clusterInfo: {}` ))
117+ }))
118+ defer srv .Close ()
119+
120+ orig := strings .ReplaceAll (templSpec (), "$MY_URI" , srv .URL )
121+ specUri := testutils .ServeFromFilePath (t , orig )
122+ specs := []string {specFile , specUri }
123+
124+ sb , _ , err := loadSpecs (ctx , specs , client )
125+ require .NoError (t , err )
126+ assert .NotNil (t , sb .Spec .Collectors [0 ].Logs )
127+ assert .Nil (t , sb .Spec .Collectors [1 ].ConfigMap ) // original spec gone
128+ assert .NotNil (t , sb .Spec .Collectors [1 ].ClusterInfo ) // new spec from URI
62129}
63130
64131func Test_loadSupportBundleSpecsFromURIs_TimeoutError (t * testing.T ) {
@@ -69,7 +136,7 @@ func Test_loadSupportBundleSpecsFromURIs_TimeoutError(t *testing.T) {
69136 ctx := context .Background ()
70137
71138 kinds , err := loader .LoadSpecs (ctx , loader.LoadOptions {
72- RawSpec : strings .ReplaceAll (orig , "$MY_URI" , srv .URL ),
139+ RawSpec : strings .ReplaceAll (templSpec () , "$MY_URI" , srv .URL ),
73140 })
74141 require .NoError (t , err )
75142
0 commit comments