From ffddab4c1660fef16426d2738293a367478d40d7 Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Tue, 13 Aug 2024 21:08:07 +1200 Subject: [PATCH] Tests for incoming sbom with no components --- internal/handler/main_test.go | 65 +++++++++++++++++++ .../testSbomPayloadNoComponents.json | 21 ++++++ 2 files changed, 86 insertions(+) create mode 100644 internal/handler/testassets/testSbomPayloadNoComponents.json diff --git a/internal/handler/main_test.go b/internal/handler/main_test.go index f23bed8..2fa92f4 100644 --- a/internal/handler/main_test.go +++ b/internal/handler/main_test.go @@ -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]) + } + } + }) + } +} diff --git a/internal/handler/testassets/testSbomPayloadNoComponents.json b/internal/handler/testassets/testSbomPayloadNoComponents.json new file mode 100644 index 0000000..f22fd23 --- /dev/null +++ b/internal/handler/testassets/testSbomPayloadNoComponents.json @@ -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" + } + } +}