Skip to content

Commit dba164b

Browse files
committed
* use set for unique uri
* add unit tests
1 parent 9ed9749 commit dba164b

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

internal/specs/specs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func LoadAdditionalSpecFromURIs(ctx context.Context, kinds *loader.TroubleshootK
370370
klog.Info("No additional URIs found in all specs")
371371
return
372372
}
373-
for _, uri := range uris {
373+
for uri := range uris {
374374
rawSpec, err := downloadFromHttpURL(ctx, uri, nil)
375375
if err != nil {
376376
klog.Warningf("failed to download spec from URI %q: %v", uri, err)

internal/specs/specs_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,39 @@ spec:
224224
require.NoError(t, err)
225225
require.Len(t, specs.HostCollectorsV1Beta2, 1)
226226
}
227+
228+
func TestLoadAdditionalSpecFromURIs(t *testing.T) {
229+
m := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
230+
w.Write([]byte(`apiVersion: troubleshoot.sh/v1beta2
231+
apiVersion: troubleshoot.sh/v1beta2
232+
kind: Preflight
233+
metadata:
234+
name: preflight-2
235+
spec:
236+
collectors:
237+
- ceph: {}
238+
`))
239+
}))
240+
defer m.Close()
241+
kinds := loader.NewTroubleshootKinds()
242+
kinds.PreflightsV1Beta2 = []troubleshootv1beta2.Preflight{
243+
{
244+
ObjectMeta: metav1.ObjectMeta{
245+
Name: "preflight-1",
246+
},
247+
Spec: troubleshootv1beta2.PreflightSpec{
248+
Uri: m.URL,
249+
Collectors: []*troubleshootv1beta2.Collect{
250+
{
251+
DNS: &troubleshootv1beta2.DNS{},
252+
},
253+
},
254+
},
255+
},
256+
}
257+
258+
LoadAdditionalSpecFromURIs(context.Background(), kinds)
259+
require.Len(t, kinds.PreflightsV1Beta2, 2)
260+
require.Len(t, kinds.PreflightsV1Beta2[0].Spec.Collectors, 1) // ceph
261+
require.Len(t, kinds.PreflightsV1Beta2[1].Spec.Collectors, 1) // dns
262+
}

pkg/loader/loader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ func (kinds *TroubleshootKinds) ToYaml() (string, error) {
133133

134134
// GetURIs dynamically extracts all the URIs from the troubleshoot kinds
135135
// .Spec.Uri field
136-
func (kinds *TroubleshootKinds) GetURIs() []string {
137-
uris := []string{}
136+
func (kinds *TroubleshootKinds) GetURIs() map[string]bool {
137+
uris := map[string]bool{}
138138
obj := reflect.ValueOf(*kinds)
139139

140140
for i := 0; i < obj.NumField(); i++ {
@@ -154,7 +154,7 @@ func (kinds *TroubleshootKinds) GetURIs() []string {
154154
continue
155155
}
156156

157-
uris = append(uris, uriField.String())
157+
uris[uriField.String()] = true
158158
}
159159
}
160160

pkg/loader/loader_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,33 @@ func TestLoadingEmptySpec(t *testing.T) {
632632
},
633633
}, kinds)
634634
}
635+
636+
func TestGetURIs(t *testing.T) {
637+
kinds := NewTroubleshootKinds()
638+
kinds.SupportBundlesV1Beta2 = []troubleshootv1beta2.SupportBundle{
639+
{
640+
Spec: troubleshootv1beta2.SupportBundleSpec{
641+
Uri: "https://supportbundle.com",
642+
},
643+
},
644+
}
645+
kinds.PreflightsV1Beta2 = []troubleshootv1beta2.Preflight{
646+
{
647+
Spec: troubleshootv1beta2.PreflightSpec{
648+
Uri: "https://preflight.com",
649+
},
650+
},
651+
}
652+
kinds.CollectorsV1Beta2 = []troubleshootv1beta2.Collector{
653+
{
654+
Spec: troubleshootv1beta2.CollectorSpec{
655+
Uri: "https://supportbundle.com",
656+
},
657+
},
658+
}
659+
uris := kinds.GetURIs()
660+
assert.Equal(t, map[string]bool{
661+
"https://supportbundle.com": true,
662+
"https://preflight.com": true,
663+
}, uris)
664+
}

0 commit comments

Comments
 (0)