Skip to content

Commit 1114902

Browse files
author
yunju.lly
authored
refactor: keep support bundle concat logic to be consistent with Preflight concat (#1002)
* refactor: keep support bundle concat logic to be consistent with Preflight * test: add tests for support bundle spec concat function
1 parent 4fca6af commit 1114902

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
8282

8383
// Defining `v` below will render using `v` in reference to Viper unusable.
8484
// Therefore refactoring `v` to `val` will make sure we can still use it.
85-
for i, val := range arg {
85+
for _, val := range arg {
8686

8787
collectorContent, err := supportbundle.LoadSupportBundleSpec(val)
8888
if err != nil {
@@ -98,11 +98,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
9898
return errors.Wrap(err, "failed to parse support bundle spec")
9999
}
100100

101-
if i == 0 {
102-
mainBundle = supportBundle
103-
} else {
104-
mainBundle = supportbundle.ConcatSpec(mainBundle, supportBundle)
105-
}
101+
mainBundle = supportbundle.ConcatSpec(mainBundle, supportBundle)
106102

107103
parsedRedactors, err := supportbundle.ParseRedactorsFromDocs(multidocs)
108104
if err != nil {

pkg/supportbundle/supportbundle.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,21 @@ func AnalyzeSupportBundle(spec *troubleshootv1beta2.SupportBundleSpec, tmpDir st
233233
return analyzeResults, nil
234234
}
235235

236-
// the intention with these appends is to swap them out at a later date with more specific handlers for merging the spec fields
236+
// ConcatSpec the intention with these appends is to swap them out at a later date with more specific handlers for merging the spec fields
237237
func ConcatSpec(target *troubleshootv1beta2.SupportBundle, source *troubleshootv1beta2.SupportBundle) *troubleshootv1beta2.SupportBundle {
238-
newBundle := target.DeepCopy()
239-
newBundle.Spec.Collectors = append(target.Spec.Collectors, source.Spec.Collectors...)
240-
newBundle.Spec.AfterCollection = append(target.Spec.AfterCollection, source.Spec.AfterCollection...)
241-
newBundle.Spec.HostCollectors = append(target.Spec.HostCollectors, source.Spec.HostCollectors...)
242-
newBundle.Spec.HostAnalyzers = append(target.Spec.HostAnalyzers, source.Spec.HostAnalyzers...)
243-
newBundle.Spec.Analyzers = append(target.Spec.Analyzers, source.Spec.Analyzers...)
238+
if source == nil {
239+
return target
240+
}
241+
var newBundle *troubleshootv1beta2.SupportBundle
242+
if target == nil {
243+
newBundle = source
244+
} else {
245+
newBundle = target.DeepCopy()
246+
newBundle.Spec.Collectors = append(target.Spec.Collectors, source.Spec.Collectors...)
247+
newBundle.Spec.AfterCollection = append(target.Spec.AfterCollection, source.Spec.AfterCollection...)
248+
newBundle.Spec.HostCollectors = append(target.Spec.HostCollectors, source.Spec.HostCollectors...)
249+
newBundle.Spec.HostAnalyzers = append(target.Spec.HostAnalyzers, source.Spec.HostAnalyzers...)
250+
newBundle.Spec.Analyzers = append(target.Spec.Analyzers, source.Spec.Analyzers...)
251+
}
244252
return newBundle
245253
}

pkg/supportbundle/supportbundle_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package supportbundle
33
import (
44
"reflect"
55
"testing"
6+
7+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
68
)
79

810
func Test_LoadAndConcatSpec(t *testing.T) {
@@ -44,3 +46,27 @@ func Test_LoadAndConcatSpec(t *testing.T) {
4446
}
4547

4648
}
49+
50+
func Test_LoadAndConcatSpec_WithNil(t *testing.T) {
51+
var bundle *troubleshootv1beta2.SupportBundle
52+
// both function arguments are nil
53+
bundle4 := ConcatSpec(bundle, bundle)
54+
if reflect.DeepEqual(bundle4, (*troubleshootv1beta2.SupportBundle)(nil)) == false {
55+
t.Error("concatenating nil pointer with nil pointer has error.")
56+
}
57+
58+
fulldoc, _ := LoadSupportBundleSpec("test/completebundle.yaml")
59+
bundle, _ = ParseSupportBundleFromDoc(fulldoc)
60+
61+
// targetBundle is nil pointer
62+
bundle5 := ConcatSpec((*troubleshootv1beta2.SupportBundle)(nil), bundle)
63+
if reflect.DeepEqual(bundle5, bundle) == false {
64+
t.Error("concatenating targetBundle of nil pointer has error.")
65+
}
66+
67+
// sourceBundle is nil pointer
68+
bundle6 := ConcatSpec(bundle, (*troubleshootv1beta2.SupportBundle)(nil))
69+
if reflect.DeepEqual(bundle6, bundle) == false {
70+
t.Error("concatenating sourceBundle of nil pointer has error.")
71+
}
72+
}

0 commit comments

Comments
 (0)