Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao committed May 3, 2024
1 parent bf58a61 commit 2529eca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
25 changes: 25 additions & 0 deletions cmd/troubleshoot/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,28 @@ spec:
})
}
}

func Test_loadDuplicatedBundleSpecs(t *testing.T) {
spec := testutils.ServeFromFilePath(t, `
apiVersion: troubleshoot.sh/v1beta2
kind: SupportBundle
metadata:
name: sb
spec:
analyzers:
- clusterVersion: {}
hostCollectors:
- cpu: {}
hostAnalyzers:
- cpu: {}
`)
args := []string{spec, spec}

ctx := context.Background()
client := testclient.NewSimpleClientset()
sb, _, err := loadSpecs(ctx, args, client)
require.NoError(t, err)
assert.Len(t, sb.Spec.Analyzers, 1)
assert.Len(t, sb.Spec.HostCollectors, 1)
assert.Len(t, sb.Spec.HostAnalyzers, 1)
}
21 changes: 10 additions & 11 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,22 @@ func DedupAnalyzers(allAnalyzers []*troubleshootv1beta2.Analyze) []*troubleshoot
}

func DedupHostAnalyzers(allAnalyzers []*troubleshootv1beta2.HostAnalyze) []*troubleshootv1beta2.HostAnalyze {
uniqueAnalyzers := make(map[string]bool)
finalAnalyzers := []*troubleshootv1beta2.HostAnalyze{}
seen := make(map[string]bool)
out := []*troubleshootv1beta2.HostAnalyze{}

for _, analyzer := range allAnalyzers {
data, err := json.Marshal(analyzer)
if err != nil {
// return analyzer as is if for whatever reason it can't be marshalled into json
finalAnalyzers = append(finalAnalyzers, analyzer)
} else {
stringData := string(data)
if _, value := uniqueAnalyzers[stringData]; !value {
uniqueAnalyzers[stringData] = true
finalAnalyzers = append(finalAnalyzers, analyzer)
}
out = append(out, analyzer)
continue
}
key := string(data)
if _, ok := seen[key]; !ok {
out = append(out, analyzer)
seen[key] = true
}
}
return finalAnalyzers
return out
}

func stripRedactedLines(yaml []byte) []byte {
Expand Down
21 changes: 10 additions & 11 deletions pkg/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,20 @@ func CollectRemote(c *troubleshootv1beta2.RemoteCollector, additionalRedactors *
}

func DedupHostCollectors(allCollectors []*troubleshootv1beta2.HostCollect) []*troubleshootv1beta2.HostCollect {
uniqueCollectors := make(map[string]bool)
finalCollectors := []*troubleshootv1beta2.HostCollect{}
seen := make(map[string]bool)
out := []*troubleshootv1beta2.HostCollect{}

for _, collector := range allCollectors {
data, err := json.Marshal(collector)
if err != nil {
// return collector as is if for whatever reason it can't be marshalled into json
finalCollectors = append(finalCollectors, collector)
} else {
stringData := string(data)
if _, value := uniqueCollectors[stringData]; !value {
uniqueCollectors[stringData] = true
finalCollectors = append(finalCollectors, collector)
}
out = append(out, collector)
continue
}
key := string(data)
if _, ok := seen[key]; !ok {
out = append(out, collector)
seen[key] = true
}
}
return finalCollectors
return out
}

0 comments on commit 2529eca

Please sign in to comment.