-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_test.go
112 lines (101 loc) · 3.05 KB
/
resource_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package vervet_test
import (
"context"
"testing"
"time"
qt "github.com/frankban/quicktest"
. "github.com/snyk/vervet/v5"
"github.com/snyk/vervet/v5/testdata"
)
func TestResource(t *testing.T) {
c := qt.New(t)
eps, err := LoadResourceVersions(testdata.Path("resources/_examples/hello-world"))
c.Assert(err, qt.IsNil)
c.Assert(eps.Versions(), qt.DeepEquals, VersionSlice{{
Date: time.Date(2021, time.June, 1, 0, 0, 0, 0, time.UTC),
Stability: StabilityExperimental,
}, {
Date: time.Date(2021, time.June, 7, 0, 0, 0, 0, time.UTC),
Stability: StabilityExperimental,
}, {
Date: time.Date(2021, time.June, 13, 0, 0, 0, 0, time.UTC),
Stability: StabilityBeta,
}})
for _, v := range eps.Versions() {
e, err := eps.At(v.String())
c.Assert(err, qt.IsNil)
c.Assert(e.Validate(context.Background()), qt.IsNil)
c.Assert(e.Version, qt.DeepEquals, v)
}
}
func TestVersionRangesHelloWorld(t *testing.T) {
c := qt.New(t)
eps, err := LoadResourceVersions(testdata.Path("resources/_examples/hello-world"))
c.Assert(err, qt.IsNil)
tests := []struct {
query, match string
}{{
query: "2021-07-01~experimental",
match: "2021-06-13~beta",
}, {
query: "2021-07-01~beta",
match: "2021-06-13~beta",
}, {
query: "2021-06-08~experimental",
match: "2021-06-07~experimental",
}}
for _, t := range tests {
e, err := eps.At(t.query)
c.Assert(err, qt.IsNil)
c.Assert(e.Version.String(), qt.Equals, t.match)
}
}
func TestVersionRangesProjects(t *testing.T) {
c := qt.New(t)
eps, err := LoadResourceVersions(testdata.Path("resources/projects"))
c.Assert(err, qt.IsNil)
c.Assert(eps.Versions(), qt.HasLen, 2)
tests := []struct {
query, match, err string
}{{
query: "2021-07-01~experimental",
match: "2021-06-04~experimental",
}, {
query: "2021-07-01~beta",
err: `no matching version`,
}, {
query: "2021-07-01",
err: `no matching version`,
}}
for i, t := range tests {
c.Logf("test#%d: %#v", i, t)
e, err := eps.At(t.query)
if t.err != "" {
c.Assert(err, qt.ErrorMatches, t.err)
} else {
c.Assert(err, qt.IsNil)
c.Assert(e.Version.String(), qt.Equals, t.match)
}
}
}
func TestIsExtensionNotFound(t *testing.T) {
c := qt.New(t)
eps, err := LoadResourceVersions(testdata.Path("resources/_examples/hello-world"))
c.Assert(err, qt.IsNil)
resource, err := eps.At("2021-06-04~experimental")
c.Assert(err, qt.IsNil)
_, err = ExtensionString(resource.ExtensionProps, ExtSnykApiVersion)
c.Assert(IsExtensionNotFound(err), qt.IsTrue)
_, err = ExtensionString(resource.ExtensionProps, "some-bogus-value")
c.Assert(IsExtensionNotFound(err), qt.IsTrue)
_, err = ExtensionString(resource.ExtensionProps, ExtSnykApiStability)
c.Assert(IsExtensionNotFound(err), qt.IsFalse)
}
func TestLoadResourceVersionsWithDuplicateSpecs(t *testing.T) {
c := qt.New(t)
resourceVersions, err := LoadResourceVersions(testdata.Path("duplicate-specs"))
dirPath := testdata.Path("duplicate-specs")
c.Assert(resourceVersions, qt.IsNil)
c.Assert(err, qt.IsNotNil)
c.Assert(err, qt.ErrorMatches, "duplicate spec found in "+dirPath+"/2022-08-31")
}