From 5ea09516eff6f8a5d5e37cb6439a566f1eb27808 Mon Sep 17 00:00:00 2001 From: cbrom Date: Fri, 24 Nov 2023 00:04:45 +0300 Subject: [PATCH] tests added for ScanResultReport.Validate function Signed-off-by: cbrom --- core/pkg/containerscan/containerscan_test.go | 83 ++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/core/pkg/containerscan/containerscan_test.go b/core/pkg/containerscan/containerscan_test.go index fcf428b8..3eaf4324 100644 --- a/core/pkg/containerscan/containerscan_test.go +++ b/core/pkg/containerscan/containerscan_test.go @@ -68,6 +68,89 @@ func TestGetByPkgNameSuccess(t *testing.T) { } +func TestValidate(t *testing.T) { + tests := []struct { + name string + in ScanResultReport + expected bool + }{ + { + name: "empty report should return false", + in: ScanResultReport{}, + expected: false, + }, + { + name: "report with empty CustomerGUID should return false", + in: ScanResultReport{ + CustomerGUID: "", + ImgHash: "aaa", + ImgTag: "bbb", + Timestamp: 1, + }, + expected: false, + }, + { + name: "report with empty ImgHash and ImgTag should return false", + in: ScanResultReport{ + CustomerGUID: "aaa", + ImgHash: "", + ImgTag: "", + Timestamp: 1, + }, + expected: false, + }, + { + name: "report with empty ImageHash and non-empty ImgTag should return true", + in: ScanResultReport{ + CustomerGUID: "aaa", + ImgHash: "", + ImgTag: "bbb", + Timestamp: 1, + }, + expected: true, + }, + { + name: "report with non-empty ImageHash and empty ImgTag should return true", + in: ScanResultReport{ + CustomerGUID: "aaa", + ImgHash: "bbb", + ImgTag: "", + Timestamp: 1, + }, + expected: true, + }, + { + name: "report with non-empty ImageHash and non-empty ImgTag should return true", + in: ScanResultReport{ + CustomerGUID: "aaa", + ImgHash: "bbb", + ImgTag: "ccc", + Timestamp: 1, + }, + expected: true, + }, + { + name: "report with Timestamp <= 0 should return false", + in: ScanResultReport{ + CustomerGUID: "aaa", + ImgHash: "bbb", + ImgTag: "ccc", + Timestamp: 0, + }, + expected: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + res := test.in.Validate() + if test.expected != res { + t.Errorf("wrong validation status: %v", res) + } + }) + } +} + func TestCalculateFixed(t *testing.T) { tests := []struct { name string