|
| 1 | +package openvulnerabilityexchange |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/kubescape/storage/pkg/apis/softwarecomposition" |
| 8 | +) |
| 9 | + |
| 10 | +func TestValidate_RequiresAuthorAndStatements(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + name string |
| 13 | + obj *softwarecomposition.OpenVulnerabilityExchangeContainer |
| 14 | + wantPaths map[string]int |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "empty spec is rejected on both fields", |
| 18 | + obj: &softwarecomposition.OpenVulnerabilityExchangeContainer{}, |
| 19 | + wantPaths: map[string]int{ |
| 20 | + "spec.author": 1, |
| 21 | + "spec.statements": 1, |
| 22 | + }, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "missing statements only", |
| 26 | + obj: &softwarecomposition.OpenVulnerabilityExchangeContainer{ |
| 27 | + Spec: softwarecomposition.VEX{ |
| 28 | + Metadata: softwarecomposition.Metadata{Author: "kubescape.io"}, |
| 29 | + }, |
| 30 | + }, |
| 31 | + wantPaths: map[string]int{ |
| 32 | + "spec.statements": 1, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "missing author only", |
| 37 | + obj: &softwarecomposition.OpenVulnerabilityExchangeContainer{ |
| 38 | + Spec: softwarecomposition.VEX{ |
| 39 | + Statements: []softwarecomposition.Statement{{}}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + wantPaths: map[string]int{ |
| 43 | + "spec.author": 1, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "valid spec has no errors", |
| 48 | + obj: &softwarecomposition.OpenVulnerabilityExchangeContainer{ |
| 49 | + Spec: softwarecomposition.VEX{ |
| 50 | + Metadata: softwarecomposition.Metadata{Author: "kubescape.io"}, |
| 51 | + Statements: []softwarecomposition.Statement{{}}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + wantPaths: map[string]int{}, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + s := OpenVulnerabilityExchangeContainerStrategy{} |
| 59 | + for _, tt := range cases { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + errs := s.Validate(context.TODO(), tt.obj) |
| 62 | + got := map[string]int{} |
| 63 | + for _, e := range errs { |
| 64 | + got[e.Field]++ |
| 65 | + } |
| 66 | + if len(got) != len(tt.wantPaths) { |
| 67 | + t.Fatalf("got paths %v, want %v", got, tt.wantPaths) |
| 68 | + } |
| 69 | + for path, count := range tt.wantPaths { |
| 70 | + if got[path] != count { |
| 71 | + t.Errorf("path %s: got %d errors, want %d", path, got[path], count) |
| 72 | + } |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments