@@ -5,11 +5,13 @@ package integration
5
5
import (
6
6
"context"
7
7
"fmt"
8
+ "io"
8
9
"io/ioutil"
9
10
"os"
10
11
"testing"
11
12
"time"
12
13
14
+ "github.com/aquasecurity/fanal/analyzer"
13
15
_ "github.com/aquasecurity/fanal/analyzer/command/apk"
14
16
_ "github.com/aquasecurity/fanal/analyzer/library/bundler"
15
17
_ "github.com/aquasecurity/fanal/analyzer/library/cargo"
@@ -27,8 +29,6 @@ import (
27
29
_ "github.com/aquasecurity/fanal/analyzer/pkg/apk"
28
30
_ "github.com/aquasecurity/fanal/analyzer/pkg/dpkg"
29
31
_ "github.com/aquasecurity/fanal/analyzer/pkg/rpm"
30
-
31
- "github.com/aquasecurity/fanal/analyzer"
32
32
"github.com/aquasecurity/fanal/cache"
33
33
"github.com/aquasecurity/fanal/extractor/docker"
34
34
"github.com/aquasecurity/fanal/types"
@@ -46,37 +46,37 @@ type testCase struct {
46
46
47
47
var testCases = []testCase {
48
48
{
49
- name : "happy path, alpine:3.10" ,
49
+ name : "happy path alpine:3.10" ,
50
50
imageName : "alpine:3.10" ,
51
51
imageFile : "testdata/fixtures/alpine-310.tar.gz" ,
52
52
},
53
53
{
54
- name : "happy path, amazonlinux:2" ,
54
+ name : "happy path amazonlinux:2" ,
55
55
imageName : "amazonlinux:2" ,
56
56
imageFile : "testdata/fixtures/amazon-2.tar.gz" ,
57
57
},
58
58
{
59
- name : "happy path, debian:buster" ,
59
+ name : "happy path debian:buster" ,
60
60
imageName : "debian:buster" ,
61
61
imageFile : "testdata/fixtures/debian-buster.tar.gz" ,
62
62
},
63
63
{
64
- name : "happy path, photon:1.0" ,
64
+ name : "happy path photon:1.0" ,
65
65
imageName : "photon:1.0-20190823" ,
66
66
imageFile : "testdata/fixtures/photon-10.tar.gz" ,
67
67
},
68
68
{
69
- name : "happy path, registry.redhat.io/ubi7" ,
69
+ name : "happy path registry.redhat.io/ubi7" ,
70
70
imageName : "registry.redhat.io/ubi7" ,
71
71
imageFile : "testdata/fixtures/ubi-7.tar.gz" ,
72
72
},
73
73
{
74
- name : "happy path, opensuse leap 15.1" ,
74
+ name : "happy path opensuse leap 15.1" ,
75
75
imageName : "opensuse/leap:latest" ,
76
76
imageFile : "testdata/fixtures/opensuse-leap-151.tar.gz" ,
77
77
},
78
78
{
79
- name : "happy path, vulnimage with lock files" ,
79
+ name : "happy path vulnimage with lock files" ,
80
80
imageName : "knqyf263/vuln-image:1.2.3" ,
81
81
imageFile : "testdata/fixtures/vulnimage.tar.gz" ,
82
82
},
@@ -103,50 +103,62 @@ func runChecksBench(b *testing.B, ctx context.Context, imageName string, ac anal
103
103
for i := 0 ; i < b .N ; i ++ {
104
104
run (b , ctx , imageName , ac )
105
105
if c != nil {
106
- c .Clear ()
106
+ _ = c .Clear ()
107
107
}
108
108
}
109
109
}
110
110
111
- func BenchmarkFanal_Library_DockerMode_WithoutCache (b * testing.B ) {
112
- benchCache , _ := ioutil .TempDir ("" , "BenchmarkFanal_Library_DockerMode_WithoutCache_*" )
113
- defer os .RemoveAll (benchCache )
114
-
111
+ func BenchmarkDockerMode_WithoutCache (b * testing.B ) {
115
112
for _ , tc := range testCases {
116
- ctx , imageName , c , cli , ac := setup ( b , tc , benchCache )
113
+ tc := tc
117
114
b .Run (tc .name , func (b * testing.B ) {
115
+ benchCache , err := ioutil .TempDir ("" , "DockerMode_WithoutCache_" )
116
+ require .NoError (b , err )
117
+ defer os .RemoveAll (benchCache )
118
+
119
+ ctx , imageName , c , cli , ac := setup (b , tc , benchCache )
120
+
118
121
b .ReportAllocs ()
119
122
b .ResetTimer ()
120
123
runChecksBench (b , ctx , imageName , ac , c )
121
124
b .StopTimer ()
122
- })
123
125
124
- teardown (b , ctx , imageName , cli )
126
+ teardown (b , ctx , tc .imageName , imageName , cli )
127
+ })
125
128
}
126
129
}
127
130
128
- func BenchmarkFanal_Library_DockerMode_WithCache (b * testing.B ) {
129
- benchCache , _ := ioutil .TempDir ("" , "BenchmarkFanal_Library_DockerMode_WithCache_*" )
130
- defer os .RemoveAll (benchCache )
131
-
131
+ func BenchmarkDockerMode_WithCache (b * testing.B ) {
132
132
for _ , tc := range testCases {
133
- ctx , imageName , _ , cli , ac := setup (b , tc , benchCache )
134
- // run once to generate cache
135
- run (b , ctx , imageName , ac )
136
-
133
+ tc := tc
137
134
b .Run (tc .name , func (b * testing.B ) {
135
+ benchCache , err := ioutil .TempDir ("" , "DockerMode_WithCache_" )
136
+ require .NoError (b , err )
137
+ defer os .RemoveAll (benchCache )
138
+
139
+ ctx , imageName , _ , cli , ac := setup (b , tc , benchCache )
140
+ // run once to generate cache
141
+ run (b , ctx , imageName , ac )
142
+
138
143
b .ReportAllocs ()
139
144
b .ResetTimer ()
140
145
runChecksBench (b , ctx , imageName , ac , nil )
141
146
b .StopTimer ()
147
+
148
+ teardown (b , ctx , tc .imageName , imageName , cli )
142
149
})
143
150
144
- teardown (b , ctx , imageName , cli )
145
151
}
146
152
}
147
153
148
- func teardown (b * testing.B , ctx context.Context , imageName string , cli * client.Client ) {
149
- _ , err := cli .ImageRemove (ctx , imageName , dtypes.ImageRemoveOptions {
154
+ func teardown (b * testing.B , ctx context.Context , originalImageName , imageName string , cli * client.Client ) {
155
+ _ , err := cli .ImageRemove (ctx , originalImageName , dtypes.ImageRemoveOptions {
156
+ Force : true ,
157
+ PruneChildren : true ,
158
+ })
159
+ assert .NoError (b , err )
160
+
161
+ _ , err = cli .ImageRemove (ctx , imageName , dtypes.ImageRemoveOptions {
150
162
Force : true ,
151
163
PruneChildren : true ,
152
164
})
@@ -165,15 +177,24 @@ func setup(b *testing.B, tc testCase, cacheDir string) (context.Context, string,
165
177
cli , err := client .NewClientWithOpts (client .FromEnv )
166
178
require .NoError (b , err , tc .name )
167
179
168
- testfile , err := os .Open (tc .imageFile )
180
+ // ensure image doesnt already exists
181
+ _ , _ = cli .ImageRemove (ctx , tc .imageName , dtypes.ImageRemoveOptions {
182
+ Force : true ,
183
+ PruneChildren : true ,
184
+ })
185
+
186
+ testFile , err := os .Open (tc .imageFile )
169
187
require .NoError (b , err )
170
188
171
189
// load image into docker engine
172
- _ , err = cli .ImageLoad (ctx , testfile , true )
190
+ resp , err : = cli .ImageLoad (ctx , testFile , false )
173
191
require .NoError (b , err , tc .name )
174
192
193
+ // ensure an image has finished being loaded.
194
+ io .Copy (ioutil .Discard , resp .Body )
195
+ require .NoError (b , resp .Body .Close ())
196
+
175
197
imageName := fmt .Sprintf ("%s-%s" , tc .imageName , nextRandom ())
176
- fmt .Println (imageName )
177
198
178
199
// tag our image to something unique
179
200
err = cli .ImageTag (ctx , tc .imageName , imageName )
0 commit comments