Skip to content

Commit

Permalink
Tests for incoming sbom with no components
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaize Kaye committed Aug 13, 2024
1 parent efd88c0 commit ffddab4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
65 changes: 65 additions & 0 deletions internal/handler/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,68 @@ func Test_processFactsFromSBOM(t *testing.T) {
})
}
}

func Test_processFactsFromSBOMWithNoComponents(t *testing.T) {
type args struct {
bom *[]cdx.Component
environmentId int
source string
}

testResponse, err := ioutil.ReadFile("./testassets/testSbomPayloadNoComponents.json")
if err != nil {
t.Fatalf("Could not open file")
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
t.Errorf("Expected to request '/fixedvalue', got: %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
w.Write(testResponse)
}))
defer server.Close()

bom := new(cdx.BOM)
resp, err := http.Get(server.URL)
if err != nil {
panic(err)
}
decoder := cdx.NewBOMDecoder(resp.Body, cdx.BOMFileFormatJSON)
if err = decoder.Decode(bom); err != nil {
panic(err)
}

tests := []struct {
name string
args args
want []lagoonclient.AddFactInput
}{
{
name: "sbom.cdx.json",
args: args{
bom: bom.Components,
environmentId: 3,
source: "syft",
},
want: []lagoonclient.AddFactInput{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := processFactsFromSBOM(slog.Default(), tt.args.bom, tt.args.environmentId, tt.args.source)
if len(got) != len(tt.want) {
t.Errorf("processFactsFromSBOM() returned %d results, want %d", len(got), len(tt.want))
}
for i := range tt.want {
if got[i].Environment != tt.want[i].Environment ||
got[i].Name != tt.want[i].Name ||
got[i].Value != tt.want[i].Value ||
got[i].Source != tt.want[i].Source ||
got[i].Description != tt.want[i].Description ||
got[i].KeyFact != tt.want[i].KeyFact {
t.Errorf("processFactsFromSBOM()[%d] = %v, want %v", i, got[i], tt.want[i])
}
}
})
}
}
21 changes: 21 additions & 0 deletions internal/handler/testassets/testSbomPayloadNoComponents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"bomFormat": "CycloneDX",
"specVersion": "1.3",
"serialNumber": "urn:uuid:db9b54af-f4ea-4043-9f53-b0f1b4485d4d",
"version": 1,
"metadata": {
"timestamp": "2022-01-12T10:16:55Z",
"tools": [
{
"vendor": "anchore",
"name": "syft",
"version": "0.35.1"
}
],
"component": {
"type": "container",
"name": "uselagoon/php-8.1-cli-drupal",
"version": "sha256:b364c41e9c6bf5dea414e3a382f8088883265a7ad48bfecc83c6ff2f75998d10"
}
}
}

0 comments on commit ffddab4

Please sign in to comment.