@@ -21,35 +21,45 @@ func TestClient_NeedsUpdate(t *testing.T) {
21
21
timeNextUpdateDay2 := time .Date (2019 , 10 , 2 , 0 , 0 , 0 , 0 , time .UTC )
22
22
23
23
tests := []struct {
24
- name string
25
- skip bool
26
- metadata metadata.Metadata
27
- want bool
28
- wantErr string
24
+ name string
25
+ skip bool
26
+ dbFileExists bool
27
+ metadata metadata.Metadata
28
+ want bool
29
+ wantErr string
29
30
}{
30
31
{
31
- name : "happy path" ,
32
+ name : "happy path" ,
33
+ dbFileExists : true ,
32
34
metadata : metadata.Metadata {
33
35
Version : db .SchemaVersion ,
34
36
NextUpdate : timeNextUpdateDay1 ,
35
37
},
36
38
want : true ,
37
39
},
38
40
{
39
- name : "happy path for first run" ,
40
- metadata : metadata.Metadata {},
41
- want : true ,
41
+ name : "happy path for first run" ,
42
+ dbFileExists : true ,
43
+ metadata : metadata.Metadata {},
44
+ want : true ,
42
45
},
43
46
{
44
- name : "happy path with old schema version" ,
47
+ name : "happy path for first run without trivy.db" ,
48
+ dbFileExists : false ,
49
+ want : true ,
50
+ },
51
+ {
52
+ name : "happy path with old schema version" ,
53
+ dbFileExists : true ,
45
54
metadata : metadata.Metadata {
46
55
Version : 0 ,
47
56
NextUpdate : timeNextUpdateDay1 ,
48
57
},
49
58
want : true ,
50
59
},
51
60
{
52
- name : "happy path with --skip-update" ,
61
+ name : "happy path with --skip-update" ,
62
+ dbFileExists : true ,
53
63
metadata : metadata.Metadata {
54
64
Version : db .SchemaVersion ,
55
65
NextUpdate : timeNextUpdateDay1 ,
@@ -58,15 +68,17 @@ func TestClient_NeedsUpdate(t *testing.T) {
58
68
want : false ,
59
69
},
60
70
{
61
- name : "skip downloading DB" ,
71
+ name : "skip downloading DB" ,
72
+ dbFileExists : true ,
62
73
metadata : metadata.Metadata {
63
74
Version : db .SchemaVersion ,
64
75
NextUpdate : timeNextUpdateDay2 ,
65
76
},
66
77
want : false ,
67
78
},
68
79
{
69
- name : "newer schema version" ,
80
+ name : "newer schema version" ,
81
+ dbFileExists : true ,
70
82
metadata : metadata.Metadata {
71
83
Version : db .SchemaVersion + 1 ,
72
84
NextUpdate : timeNextUpdateDay2 ,
@@ -75,13 +87,21 @@ func TestClient_NeedsUpdate(t *testing.T) {
75
87
db .SchemaVersion + 1 , db .SchemaVersion ),
76
88
},
77
89
{
78
- name : "--skip-update on the first run" ,
79
- metadata : metadata.Metadata {},
80
- skip : true ,
81
- wantErr : "--skip-update cannot be specified on the first run" ,
90
+ name : "--skip-update without trivy.db on the first run" ,
91
+ dbFileExists : false ,
92
+ skip : true ,
93
+ wantErr : "--skip-update cannot be specified on the first run" ,
94
+ },
95
+ {
96
+ name : "--skip-update without metadata.json on the first run" ,
97
+ dbFileExists : true ,
98
+ metadata : metadata.Metadata {},
99
+ skip : true ,
100
+ wantErr : "--skip-update cannot be specified on the first run" ,
82
101
},
83
102
{
84
- name : "--skip-update with different schema version" ,
103
+ name : "--skip-update with different schema version" ,
104
+ dbFileExists : true ,
85
105
metadata : metadata.Metadata {
86
106
Version : 0 ,
87
107
NextUpdate : timeNextUpdateDay1 ,
@@ -91,7 +111,8 @@ func TestClient_NeedsUpdate(t *testing.T) {
91
111
0 , db .SchemaVersion ),
92
112
},
93
113
{
94
- name : "happy with old DownloadedAt" ,
114
+ name : "happy with old DownloadedAt" ,
115
+ dbFileExists : true ,
95
116
metadata : metadata.Metadata {
96
117
Version : db .SchemaVersion ,
97
118
NextUpdate : timeNextUpdateDay1 ,
@@ -100,7 +121,8 @@ func TestClient_NeedsUpdate(t *testing.T) {
100
121
want : true ,
101
122
},
102
123
{
103
- name : "skip downloading DB with recent DownloadedAt" ,
124
+ name : "skip downloading DB with recent DownloadedAt" ,
125
+ dbFileExists : true ,
104
126
metadata : metadata.Metadata {
105
127
Version : db .SchemaVersion ,
106
128
NextUpdate : timeNextUpdateDay1 ,
@@ -119,6 +141,11 @@ func TestClient_NeedsUpdate(t *testing.T) {
119
141
require .NoError (t , err )
120
142
}
121
143
144
+ if tt .dbFileExists {
145
+ err := db .Init (dbDir )
146
+ require .NoError (t , err )
147
+ }
148
+
122
149
// Set a fake time
123
150
ctx := clock .With (context .Background (), time .Date (2019 , 10 , 1 , 0 , 0 , 0 , 0 , time .UTC ))
124
151
@@ -138,7 +165,6 @@ func TestClient_NeedsUpdate(t *testing.T) {
138
165
}
139
166
140
167
func TestClient_Download (t * testing.T ) {
141
-
142
168
tests := []struct {
143
169
name string
144
170
input string
0 commit comments