From c4c04bc72e128fba1cd25534fcec8882f2323fd6 Mon Sep 17 00:00:00 2001 From: Gerard Nguyen Date: Wed, 13 Nov 2024 13:39:34 +1100 Subject: [PATCH] * add unit test * reorder in Preflight as well --- pkg/collect/collector_test.go | 81 +++++++++++++++++++++++++++++++++++ pkg/preflight/collect.go | 3 ++ 2 files changed, 84 insertions(+) diff --git a/pkg/collect/collector_test.go b/pkg/collect/collector_test.go index eb3f33fb1..9362d4b39 100644 --- a/pkg/collect/collector_test.go +++ b/pkg/collect/collector_test.go @@ -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) + }) + } +} diff --git a/pkg/preflight/collect.go b/pkg/preflight/collect.go index 115c8bf35..d6ffeefc2 100644 --- a/pkg/preflight/collect.go +++ b/pkg/preflight/collect.go @@ -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()))