Skip to content

Commit

Permalink
bug: fix nil check in host collector filter (#1653)
Browse files Browse the repository at this point in the history
* add nil check in filter host collector
  • Loading branch information
nvanthao authored Oct 18, 2024
1 parent 77fd7da commit 289102f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/supportbundle/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ func collectHost(ctx context.Context, filteredCollectors []FilteredCollector, op
if err != nil {
span.SetStatus(codes.Error, err.Error())
opts.ProgressChan <- fmt.Sprintf("[%s] Error: %v", collector.Title(), err)
return errors.Wrap(err, "failed to run host collector")
}

// Send progress event: completed successfully
Expand Down Expand Up @@ -332,13 +331,14 @@ func filterHostCollectors(ctx context.Context, collectSpecs []*troubleshootv1bet

for _, desiredCollector := range collectSpecs {
collector, ok := collect.GetHostCollector(desiredCollector, bundlePath)
_, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, collector.Title())
span.SetAttributes(attribute.String("type", reflect.TypeOf(collector).String()))

if !ok {
return nil, collect.ErrHostCollectorNotFound
opts.ProgressChan <- "Host collector not found"
continue
}

_, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, collector.Title())
span.SetAttributes(attribute.String("type", reflect.TypeOf(collector).String()))

isExcluded, _ := collector.IsExcluded()
if isExcluded {
opts.ProgressChan <- fmt.Sprintf("[%s] Excluding host collector", collector.Title())
Expand Down
45 changes: 45 additions & 0 deletions pkg/supportbundle/collect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package supportbundle

import (
"context"
"testing"

v1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace/noop"
)

func Test_filterHostCollectors(t *testing.T) {
otel.SetTracerProvider(noop.NewTracerProvider())

testCases := []struct {
name string
collectSpecs []*v1beta2.HostCollect
bundlePath string
opts SupportBundleCreateOpts
expectedResult []FilteredCollector
expectedError error
}{
{
name: "nil host collectors spec",
collectSpecs: []*v1beta2.HostCollect{},
bundlePath: "/tmp",
opts: SupportBundleCreateOpts{
ProgressChan: make(chan interface{}, 10),
},
expectedResult: []FilteredCollector{},
expectedError: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
filtered, err := filterHostCollectors(context.TODO(), tc.collectSpecs, tc.bundlePath, tc.opts)
if err != tc.expectedError {
t.Fatalf("expected error %v, got %v", tc.expectedError, err)
}
if len(filtered) != len(tc.expectedResult) {
t.Fatalf("expected %d filtered collectors, got %d", len(tc.expectedResult), len(filtered))
}
})
}
}

0 comments on commit 289102f

Please sign in to comment.