|
| 1 | +package preflight |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 8 | + "github.com/replicatedhq/troubleshoot/pkg/collect" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "k8s.io/client-go/kubernetes/fake" |
| 12 | + "k8s.io/client-go/rest" |
| 13 | +) |
| 14 | + |
| 15 | +// TestCollectWithContext_ClusterResourcesFirst verifies that clusterResources |
| 16 | +// collector runs first, even when it's not first in the spec. |
| 17 | +func TestCollectWithContext_ClusterResourcesFirst(t *testing.T) { |
| 18 | + // Create a preflight spec with collectors in a specific order |
| 19 | + // where clusterResources is NOT first |
| 20 | + preflight := &troubleshootv1beta2.Preflight{ |
| 21 | + Spec: troubleshootv1beta2.PreflightSpec{ |
| 22 | + Collectors: []*troubleshootv1beta2.Collect{ |
| 23 | + { |
| 24 | + Data: &troubleshootv1beta2.Data{ |
| 25 | + CollectorMeta: troubleshootv1beta2.CollectorMeta{ |
| 26 | + CollectorName: "test-data", |
| 27 | + }, |
| 28 | + Name: "test.json", |
| 29 | + Data: `{"test": "data"}`, |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + ClusterResources: &troubleshootv1beta2.ClusterResources{}, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + // Use a fake Kubernetes client to avoid network calls |
| 40 | + fakeClient := fake.NewSimpleClientset() |
| 41 | + restConfig := &rest.Config{ |
| 42 | + Host: "https://fake-host", |
| 43 | + } |
| 44 | + |
| 45 | + opts := CollectOpts{ |
| 46 | + Namespace: "default", |
| 47 | + KubernetesRestConfig: restConfig, |
| 48 | + ProgressChan: make(chan interface{}, 100), |
| 49 | + BundlePath: t.TempDir(), |
| 50 | + IgnorePermissionErrors: true, // Ignore RBAC errors in tests |
| 51 | + } |
| 52 | + |
| 53 | + // Manually test the ordering logic by simulating what CollectWithContext does |
| 54 | + collectSpecs := make([]*troubleshootv1beta2.Collect, 0) |
| 55 | + if preflight.Spec.Collectors != nil { |
| 56 | + collectSpecs = append(collectSpecs, preflight.Spec.Collectors...) |
| 57 | + } |
| 58 | + collectSpecs = collect.EnsureCollectorInList( |
| 59 | + collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}}, |
| 60 | + ) |
| 61 | + collectSpecs = collect.EnsureCollectorInList( |
| 62 | + collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}}, |
| 63 | + ) |
| 64 | + collectSpecs = collect.DedupCollectors(collectSpecs) |
| 65 | + collectSpecs = collect.EnsureClusterResourcesFirst(collectSpecs) |
| 66 | + |
| 67 | + // Verify clusterResources is first in the specs |
| 68 | + require.NotEmpty(t, collectSpecs, "should have collectors") |
| 69 | + require.NotNil(t, collectSpecs[0].ClusterResources, "first collector should be clusterResources") |
| 70 | + |
| 71 | + // Now simulate the map grouping and order preservation |
| 72 | + allCollectorsMap := make(map[reflect.Type][]collect.Collector) |
| 73 | + collectorTypeOrder := make([]reflect.Type, 0) |
| 74 | + |
| 75 | + for _, desiredCollector := range collectSpecs { |
| 76 | + if collectorInterface, ok := collect.GetCollector(desiredCollector, opts.BundlePath, opts.Namespace, opts.KubernetesRestConfig, fakeClient, nil); ok { |
| 77 | + if collector, ok := collectorInterface.(collect.Collector); ok { |
| 78 | + // Skip RBAC check for this unit test |
| 79 | + collectorType := reflect.TypeOf(collector) |
| 80 | + if _, exists := allCollectorsMap[collectorType]; !exists { |
| 81 | + collectorTypeOrder = append(collectorTypeOrder, collectorType) |
| 82 | + } |
| 83 | + allCollectorsMap[collectorType] = append(allCollectorsMap[collectorType], collector) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Verify that clusterResources type is first in the order |
| 89 | + require.NotEmpty(t, collectorTypeOrder, "should have collector types") |
| 90 | + |
| 91 | + // Find the clusterResources type by checking the actual collectors |
| 92 | + var clusterResourcesType reflect.Type |
| 93 | + for collectorType, collectors := range allCollectorsMap { |
| 94 | + if len(collectors) > 0 { |
| 95 | + if _, ok := collectors[0].(*collect.CollectClusterResources); ok { |
| 96 | + clusterResourcesType = collectorType |
| 97 | + break |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + require.NotNil(t, clusterResourcesType, "should find clusterResources type") |
| 102 | + assert.Equal(t, clusterResourcesType, collectorTypeOrder[0], "clusterResources type should be first in collectorTypeOrder") |
| 103 | +} |
| 104 | + |
| 105 | +// TestCollectWithContext_PreservesOrderAfterClusterResources verifies that |
| 106 | +// after clusterResources, other collectors maintain their relative order. |
| 107 | +func TestCollectWithContext_PreservesOrderAfterClusterResources(t *testing.T) { |
| 108 | + // Create a preflight spec with multiple collectors in a specific order |
| 109 | + preflight := &troubleshootv1beta2.Preflight{ |
| 110 | + Spec: troubleshootv1beta2.PreflightSpec{ |
| 111 | + Collectors: []*troubleshootv1beta2.Collect{ |
| 112 | + { |
| 113 | + Data: &troubleshootv1beta2.Data{ |
| 114 | + CollectorMeta: troubleshootv1beta2.CollectorMeta{ |
| 115 | + CollectorName: "data-first", |
| 116 | + }, |
| 117 | + Name: "first.json", |
| 118 | + Data: `{"first": "data"}`, |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + Secret: &troubleshootv1beta2.Secret{ |
| 123 | + CollectorMeta: troubleshootv1beta2.CollectorMeta{ |
| 124 | + CollectorName: "secret-second", |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + // Use a fake Kubernetes client |
| 133 | + fakeClient := fake.NewSimpleClientset() |
| 134 | + restConfig := &rest.Config{ |
| 135 | + Host: "https://fake-host", |
| 136 | + } |
| 137 | + |
| 138 | + opts := CollectOpts{ |
| 139 | + Namespace: "default", |
| 140 | + KubernetesRestConfig: restConfig, |
| 141 | + ProgressChan: make(chan interface{}, 100), |
| 142 | + BundlePath: t.TempDir(), |
| 143 | + IgnorePermissionErrors: true, |
| 144 | + } |
| 145 | + |
| 146 | + // Simulate the ordering logic |
| 147 | + collectSpecs := make([]*troubleshootv1beta2.Collect, 0) |
| 148 | + if preflight.Spec.Collectors != nil { |
| 149 | + collectSpecs = append(collectSpecs, preflight.Spec.Collectors...) |
| 150 | + } |
| 151 | + collectSpecs = collect.EnsureCollectorInList( |
| 152 | + collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}}, |
| 153 | + ) |
| 154 | + collectSpecs = collect.EnsureCollectorInList( |
| 155 | + collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}}, |
| 156 | + ) |
| 157 | + collectSpecs = collect.DedupCollectors(collectSpecs) |
| 158 | + collectSpecs = collect.EnsureClusterResourcesFirst(collectSpecs) |
| 159 | + |
| 160 | + // Group collectors by type and track order |
| 161 | + allCollectorsMap := make(map[reflect.Type][]collect.Collector) |
| 162 | + collectorTypeOrder := make([]reflect.Type, 0) |
| 163 | + |
| 164 | + for _, desiredCollector := range collectSpecs { |
| 165 | + if collectorInterface, ok := collect.GetCollector(desiredCollector, opts.BundlePath, opts.Namespace, opts.KubernetesRestConfig, fakeClient, nil); ok { |
| 166 | + if collector, ok := collectorInterface.(collect.Collector); ok { |
| 167 | + collectorType := reflect.TypeOf(collector) |
| 168 | + if _, exists := allCollectorsMap[collectorType]; !exists { |
| 169 | + collectorTypeOrder = append(collectorTypeOrder, collectorType) |
| 170 | + } |
| 171 | + allCollectorsMap[collectorType] = append(allCollectorsMap[collectorType], collector) |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Verify clusterResources is first |
| 177 | + require.NotEmpty(t, collectorTypeOrder, "should have collector types") |
| 178 | + |
| 179 | + // Find the actual types from the collectors |
| 180 | + var clusterResourcesType, dataType, secretType reflect.Type |
| 181 | + for collectorType, collectors := range allCollectorsMap { |
| 182 | + if len(collectors) > 0 { |
| 183 | + switch collectors[0].(type) { |
| 184 | + case *collect.CollectClusterResources: |
| 185 | + clusterResourcesType = collectorType |
| 186 | + case *collect.CollectData: |
| 187 | + dataType = collectorType |
| 188 | + case *collect.CollectSecret: |
| 189 | + secretType = collectorType |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + require.NotNil(t, clusterResourcesType, "should find clusterResources type") |
| 195 | + assert.Equal(t, clusterResourcesType, collectorTypeOrder[0], "clusterResources should be first") |
| 196 | + |
| 197 | + dataIndex := -1 |
| 198 | + secretIndex := -1 |
| 199 | + for i, ct := range collectorTypeOrder { |
| 200 | + if ct == dataType { |
| 201 | + dataIndex = i |
| 202 | + } |
| 203 | + if ct == secretType { |
| 204 | + secretIndex = i |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if dataIndex >= 0 && secretIndex >= 0 { |
| 209 | + assert.Less(t, dataIndex, secretIndex, "data collectors should come before secret collectors, preserving relative order") |
| 210 | + } |
| 211 | +} |
0 commit comments