Skip to content

Commit

Permalink
* add unit test
Browse files Browse the repository at this point in the history
* reorder in Preflight as well
  • Loading branch information
nvanthao committed Nov 13, 2024
1 parent b069b1f commit c4c04bc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
81 changes: 81 additions & 0 deletions pkg/collect/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,84 @@ func TestCollector_DedupCollectors(t *testing.T) {
})
}
}
func TestEnsureCopyLast(t *testing.T) {
tests := []struct {
name string
allCollectors []Collector
want []Collector
}{
{
name: "no copy collectors",
allCollectors: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
},
},
{
name: "only copy collectors",
allCollectors: []Collector{
&CollectCopy{},
&CollectCopy{},
},
want: []Collector{
&CollectCopy{},
&CollectCopy{},
},
},
{
name: "mixed collectors",
allCollectors: []Collector{
&CollectClusterInfo{},
&CollectCopy{},
&CollectClusterResources{},
&CollectCopy{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
},
{
name: "copy collectors at the beginning",
allCollectors: []Collector{
&CollectCopy{},
&CollectCopy{},
&CollectClusterInfo{},
&CollectClusterResources{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
},
{
name: "copy collectors at the end",
allCollectors: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EnsureCopyLast(tt.allCollectors)
assert.Equal(t, tt.want, got)
})
}
}
3 changes: 3 additions & 0 deletions pkg/preflight/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ func CollectWithContext(ctx context.Context, opts CollectOpts, p *troubleshootv1
return collectResult, collect.ErrInsufficientPermissionsToRun
}

// move Copy Collectors if any to the end of the execution list
allCollectors = collect.EnsureCopyLast(allCollectors)

for i, collector := range allCollectors {
_, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, collector.Title())
span.SetAttributes(attribute.String("type", reflect.TypeOf(collector).String()))
Expand Down

0 comments on commit c4c04bc

Please sign in to comment.