Skip to content

Commit 50c9d81

Browse files
committed
test
1 parent 732c6a0 commit 50c9d81

6 files changed

Lines changed: 281 additions & 7 deletions

File tree

cli/cage/audit/aggregator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
ecrtypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
7-
"github.com/loilo-inc/canarycage/cli/color"
7+
"github.com/loilo-inc/canarycage/logger"
88
)
99

1010
type aggregater struct {
@@ -132,7 +132,7 @@ func (a *aggregater) filterCvesBySeverity(severity ecrtypes.FindingSeverity) []e
132132
type severityPrinter struct {
133133
noColor bool
134134
severity ecrtypes.FindingSeverity
135-
color color.Color
135+
color logger.Color
136136
}
137137

138138
func (s *severityPrinter) Sprintf(format string, a ...any) string {

cli/cage/audit/aggregator_test.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,147 @@ func TestSeverityPrinter_BSprintf(t *testing.T) {
7171
want := "\x1b[1m\x1b[35mtest critical\x1b[0m\x1b[0m"
7272
assert.Equal(t, want, got)
7373
}
74+
75+
func TestNewAggregater(t *testing.T) {
76+
agg := NewAggregater()
77+
assert.NotNil(t, agg)
78+
assert.NotNil(t, agg.cves)
79+
assert.NotNil(t, agg.cveToSeverity)
80+
assert.NotNil(t, agg.summaries)
81+
assert.Equal(t, 0, len(agg.cves))
82+
assert.Equal(t, 0, len(agg.cveToSeverity))
83+
assert.Equal(t, 0, len(agg.summaries))
84+
}
85+
86+
func TestAggregater_Add(t *testing.T) {
87+
tests := []struct {
88+
name string
89+
scanResult *ScanResult
90+
wantStatus string
91+
wantCVECount int
92+
}{
93+
{
94+
name: "add result with error",
95+
scanResult: &ScanResult{
96+
Err: assert.AnError,
97+
ImageInfo: &ImageInfo{},
98+
},
99+
wantStatus: "ERROR",
100+
wantCVECount: 0,
101+
},
102+
{
103+
name: "add result with nil findings",
104+
scanResult: &ScanResult{
105+
ImageScanFindings: nil,
106+
ImageInfo: &ImageInfo{},
107+
},
108+
wantStatus: "N/A",
109+
wantCVECount: 0,
110+
},
111+
{
112+
name: "add result with findings",
113+
scanResult: &ScanResult{
114+
ImageInfo: &ImageInfo{
115+
ContainerName: "test-container",
116+
},
117+
ImageScanFindings: &ecrtypes.ImageScanFindings{
118+
Findings: []ecrtypes.ImageScanFinding{
119+
{
120+
Name: stringPtr("CVE-2021-1234"),
121+
Severity: ecrtypes.FindingSeverityCritical,
122+
},
123+
{
124+
Name: stringPtr("CVE-2021-5678"),
125+
Severity: ecrtypes.FindingSeverityHigh,
126+
},
127+
},
128+
},
129+
},
130+
wantStatus: "VULNERABLE",
131+
wantCVECount: 2,
132+
},
133+
}
134+
135+
for _, tt := range tests {
136+
t.Run(tt.name, func(t *testing.T) {
137+
agg := NewAggregater()
138+
agg.Add(tt.scanResult)
139+
assert.Equal(t, 1, len(agg.summaries))
140+
assert.Equal(t, tt.wantStatus, agg.summaries[tt.scanResult.ImageInfo.ContainerName][0].Status)
141+
assert.Equal(t, tt.wantCVECount, len(agg.cves))
142+
})
143+
144+
}
145+
}
146+
147+
func TestAggregater_SummarizeTotal(t *testing.T) {
148+
agg := NewAggregater()
149+
agg.cves = map[string]ecrtypes.ImageScanFinding{
150+
"CVE-2021-1": {Name: stringPtr("CVE-2021-1"), Severity: ecrtypes.FindingSeverityCritical},
151+
"CVE-2021-2": {Name: stringPtr("CVE-2021-2"), Severity: ecrtypes.FindingSeverityHigh},
152+
"CVE-2021-3": {Name: stringPtr("CVE-2021-3"), Severity: ecrtypes.FindingSeverityMedium},
153+
"CVE-2021-4": {Name: stringPtr("CVE-2021-4"), Severity: ecrtypes.FindingSeverityLow},
154+
"CVE-2021-5": {Name: stringPtr("CVE-2021-5"), Severity: ecrtypes.FindingSeverityInformational},
155+
}
156+
agg.cveToSeverity = map[string]string{
157+
"CVE-2021-1": string(ecrtypes.FindingSeverityCritical),
158+
"CVE-2021-2": string(ecrtypes.FindingSeverityHigh),
159+
"CVE-2021-3": string(ecrtypes.FindingSeverityMedium),
160+
"CVE-2021-4": string(ecrtypes.FindingSeverityLow),
161+
"CVE-2021-5": string(ecrtypes.FindingSeverityInformational),
162+
}
163+
164+
result := agg.SummarizeTotal()
165+
assert.Equal(t, int32(1), result.CriticalCount)
166+
assert.Equal(t, int32(1), result.HighCount)
167+
assert.Equal(t, int32(1), result.MediumCount)
168+
assert.Equal(t, int32(1), result.LowCount)
169+
assert.Equal(t, int32(1), result.InfoCount)
170+
assert.Equal(t, int32(5), result.TotalCount)
171+
assert.Equal(t, ecrtypes.FindingSeverityCritical, result.HighestSeverity)
172+
}
173+
174+
func TestAggregater_FilterCvesBySeverity(t *testing.T) {
175+
agg := NewAggregater()
176+
agg.cves = map[string]ecrtypes.ImageScanFinding{
177+
"CVE-2021-1": {Name: stringPtr("CVE-2021-1"), Severity: ecrtypes.FindingSeverityCritical},
178+
"CVE-2021-2": {Name: stringPtr("CVE-2021-2"), Severity: ecrtypes.FindingSeverityHigh},
179+
"CVE-2021-3": {Name: stringPtr("CVE-2021-3"), Severity: ecrtypes.FindingSeverityCritical},
180+
}
181+
agg.cveToSeverity = map[string]string{
182+
"CVE-2021-1": string(ecrtypes.FindingSeverityCritical),
183+
"CVE-2021-2": string(ecrtypes.FindingSeverityHigh),
184+
"CVE-2021-3": string(ecrtypes.FindingSeverityCritical),
185+
}
186+
187+
critical := agg.CriticalCves()
188+
assert.Equal(t, 2, len(critical))
189+
190+
high := agg.HighCves()
191+
assert.Equal(t, 1, len(high))
192+
193+
medium := agg.MediumCves()
194+
assert.Equal(t, 0, len(medium))
195+
}
196+
197+
func TestAggregateResult_SeverityCounts(t *testing.T) {
198+
result := &AggregateResult{
199+
CriticalCount: 1,
200+
HighCount: 2,
201+
MediumCount: 3,
202+
LowCount: 4,
203+
InfoCount: 5,
204+
}
205+
206+
counts := result.SeverityCounts()
207+
assert.Equal(t, 5, len(counts))
208+
assert.Equal(t, 5, counts[0].Count)
209+
assert.Equal(t, 4, counts[1].Count)
210+
assert.Equal(t, 3, counts[2].Count)
211+
assert.Equal(t, 2, counts[3].Count)
212+
assert.Equal(t, 1, counts[4].Count)
213+
}
214+
215+
func stringPtr(s string) *string {
216+
return &s
217+
}

cli/cage/audit/printer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
ecrtypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
8-
"github.com/loilo-inc/canarycage/cli/color"
98
"github.com/loilo-inc/canarycage/logger"
109
)
1110

@@ -44,9 +43,9 @@ func (p *Printer) Print(result []*ScanResult) {
4443
p.logImageScanFindings("HIGH", agg.HighCves())
4544
p.logImageScanFindings("MEDIUM", agg.MediumCves())
4645
total := agg.TotalCVECount()
47-
chalk := color.Color{NoColor: p.NoColor}
46+
color := logger.Color{NoColor: p.NoColor}
4847
if total == 0 {
49-
p.Logger.Printf("%s\n", chalk.Greenf("No CVEs found"))
48+
p.Logger.Printf("%s\n", color.Greenf("No CVEs found"))
5049
return
5150
}
5251
summary := agg.SummarizeTotal()

cli/cage/audit/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type ScanResultSummary struct {
5858
}
5959

6060
func summaryScanResult(result *ScanResult) *ScanResultSummary {
61-
var status string
61+
var status = "OK"
6262
var critical, high, medium, low, info int32
6363
findings := result.ImageScanFindings
6464
for _, f := range findings.Findings {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package color
1+
package logger
22

33
import "fmt"
44

logger/color_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package logger
2+
3+
import "testing"
4+
5+
func TestColor_Redf(t *testing.T) {
6+
c := &Color{NoColor: false}
7+
result := c.Redf("test %s", "message")
8+
expected := "\033[31mtest message\033[0m"
9+
if result != expected {
10+
t.Errorf("expected %q, got %q", expected, result)
11+
}
12+
13+
c.NoColor = true
14+
result = c.Redf("test %s", "message")
15+
expected = "test %s"
16+
if result != expected {
17+
t.Errorf("expected %q, got %q", expected, result)
18+
}
19+
}
20+
21+
func TestColor_Greenf(t *testing.T) {
22+
c := &Color{NoColor: false}
23+
result := c.Greenf("test %s", "message")
24+
expected := "\033[32mtest message\033[0m"
25+
if result != expected {
26+
t.Errorf("expected %q, got %q", expected, result)
27+
}
28+
29+
c.NoColor = true
30+
result = c.Greenf("test %s", "message")
31+
expected = "test %s"
32+
if result != expected {
33+
t.Errorf("expected %q, got %q", expected, result)
34+
}
35+
}
36+
37+
func TestColor_Yellowf(t *testing.T) {
38+
c := &Color{NoColor: false}
39+
result := c.Yellowf("test %s", "message")
40+
expected := "\033[33mtest message\033[0m"
41+
if result != expected {
42+
t.Errorf("expected %q, got %q", expected, result)
43+
}
44+
45+
c.NoColor = true
46+
result = c.Yellowf("test %s", "message")
47+
expected = "test %s"
48+
if result != expected {
49+
t.Errorf("expected %q, got %q", expected, result)
50+
}
51+
}
52+
53+
func TestColor_Bluef(t *testing.T) {
54+
c := &Color{NoColor: false}
55+
result := c.Bluef("test %s", "message")
56+
expected := "\033[34mtest message\033[0m"
57+
if result != expected {
58+
t.Errorf("expected %q, got %q", expected, result)
59+
}
60+
61+
c.NoColor = true
62+
result = c.Bluef("test %s", "message")
63+
expected = "test %s"
64+
if result != expected {
65+
t.Errorf("expected %q, got %q", expected, result)
66+
}
67+
}
68+
69+
func TestColor_Magentaf(t *testing.T) {
70+
c := &Color{NoColor: false}
71+
result := c.Magentaf("test %s", "message")
72+
expected := "\033[35mtest message\033[0m"
73+
if result != expected {
74+
t.Errorf("expected %q, got %q", expected, result)
75+
}
76+
77+
c.NoColor = true
78+
result = c.Magentaf("test %s", "message")
79+
expected = "test %s"
80+
if result != expected {
81+
t.Errorf("expected %q, got %q", expected, result)
82+
}
83+
}
84+
85+
func TestColor_Cyanf(t *testing.T) {
86+
c := &Color{NoColor: false}
87+
result := c.Cyanf("test %s", "message")
88+
expected := "\033[36mtest message\033[0m"
89+
if result != expected {
90+
t.Errorf("expected %q, got %q", expected, result)
91+
}
92+
93+
c.NoColor = true
94+
result = c.Cyanf("test %s", "message")
95+
expected = "test %s"
96+
if result != expected {
97+
t.Errorf("expected %q, got %q", expected, result)
98+
}
99+
}
100+
101+
func TestColor_Whitef(t *testing.T) {
102+
c := &Color{NoColor: false}
103+
result := c.Whitef("test %s", "message")
104+
expected := "\033[37mtest message\033[0m"
105+
if result != expected {
106+
t.Errorf("expected %q, got %q", expected, result)
107+
}
108+
109+
c.NoColor = true
110+
result = c.Whitef("test %s", "message")
111+
expected = "test %s"
112+
if result != expected {
113+
t.Errorf("expected %q, got %q", expected, result)
114+
}
115+
}
116+
117+
func TestColor_Boldf(t *testing.T) {
118+
c := &Color{NoColor: false}
119+
result := c.Boldf("test %s", "message")
120+
expected := "\033[1mtest message\033[0m"
121+
if result != expected {
122+
t.Errorf("expected %q, got %q", expected, result)
123+
}
124+
125+
c.NoColor = true
126+
result = c.Boldf("test %s", "message")
127+
expected = "test %s"
128+
if result != expected {
129+
t.Errorf("expected %q, got %q", expected, result)
130+
}
131+
}

0 commit comments

Comments
 (0)