Skip to content

Commit c770155

Browse files
author
Shreya2005-2005
committed
fixes #355
1 parent 72c61d4 commit c770155

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

pkg/registry/softwarecomposition/openvulnerabilityexchange/strategy.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@ func (OpenVulnerabilityExchangeContainerStrategy) PrepareForCreate(_ context.Con
5959
func (OpenVulnerabilityExchangeContainerStrategy) PrepareForUpdate(_ context.Context, _, _ runtime.Object) {
6060
}
6161

62-
func (OpenVulnerabilityExchangeContainerStrategy) Validate(_ context.Context, _ runtime.Object) field.ErrorList {
63-
return field.ErrorList{}
62+
func (OpenVulnerabilityExchangeContainerStrategy) Validate(_ context.Context, obj runtime.Object) field.ErrorList {
63+
vexContainer := obj.(*softwarecomposition.OpenVulnerabilityExchangeContainer)
64+
var allErrors field.ErrorList
65+
if vexContainer.Spec.Author == "" {
66+
allErrors = append(allErrors, field.Required(field.NewPath("spec", "author"), "must not be empty"))
67+
}
68+
if len(vexContainer.Spec.Statements) == 0 {
69+
allErrors = append(allErrors, field.Required(field.NewPath("spec", "statements"), "must contain at least one statement"))
70+
}
71+
return allErrors
6472
}
6573

6674
// WarningsOnCreate returns warnings for the creation of the given object.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)