@@ -4,41 +4,25 @@ import (
4
4
"context"
5
5
"errors"
6
6
"io"
7
- "os"
8
7
"testing"
9
8
10
- "github.com/aquasecurity/fanal/extractor"
11
-
12
9
"github.com/stretchr/testify/assert"
10
+
11
+ "github.com/aquasecurity/fanal/extractor"
12
+ "github.com/aquasecurity/fanal/extractor/image"
13
13
)
14
14
15
15
type mockDockerExtractor struct {
16
- saveLocalImage func (ctx context.Context , imageName string ) (io.Reader , error )
17
- extractFromFile func (ctx context.Context , r io.Reader , filenames []string ) (extractor.FileMap , error )
18
- extract func (ctx context.Context , imageName string , filenames []string ) (extractor.FileMap , error )
16
+ extract func (ctx context.Context , imageRef image.Reference , transports , filenames []string ) (extractor.FileMap , error )
19
17
}
20
18
21
- func (mde mockDockerExtractor ) Extract (ctx context.Context , imageName string , filenames []string ) (extractor.FileMap , error ) {
19
+ func (mde mockDockerExtractor ) Extract (ctx context.Context , imageRef image. Reference , transports , filenames []string ) (extractor.FileMap , error ) {
22
20
if mde .extract != nil {
23
- return mde .extract (ctx , imageName , filenames )
24
- }
25
- return extractor.FileMap {}, nil
26
- }
27
-
28
- func (mde mockDockerExtractor ) ExtractFromFile (ctx context.Context , r io.Reader , filenames []string ) (extractor.FileMap , error ) {
29
- if mde .extractFromFile != nil {
30
- return mde .extractFromFile (ctx , r , filenames )
21
+ return mde .extract (ctx , imageRef , transports , filenames )
31
22
}
32
23
return extractor.FileMap {}, nil
33
24
}
34
25
35
- func (mde mockDockerExtractor ) SaveLocalImage (ctx context.Context , imageName string ) (io.Reader , error ) {
36
- if mde .saveLocalImage != nil {
37
- return mde .saveLocalImage (ctx , imageName )
38
- }
39
- return nil , nil
40
- }
41
-
42
26
func (mde mockDockerExtractor ) ExtractFiles (layer io.Reader , filenames []string ) (extractor.FileMap , extractor.OPQDirs , error ) {
43
27
panic ("implement me" )
44
28
}
@@ -55,32 +39,14 @@ func (m mockOSAnalyzer) RequiredFiles() []string {
55
39
56
40
func TestConfig_Analyze (t * testing.T ) {
57
41
testCases := []struct {
58
- name string
59
- saveLocalImageFunc func (ctx context.Context , imageName string ) (io.Reader , error )
60
- extractFunc func (ctx context.Context , imageName string , filenames []string ) (extractor.FileMap , error )
61
- extractFromFileFunc func (ctx context.Context , r io.Reader , filenames []string ) (maps extractor.FileMap , e error )
62
- expectedError error
63
- expectedFileMap extractor.FileMap
42
+ name string
43
+ extractFunc func (ctx context.Context , imageRef image.Reference , transports , filenames []string ) (extractor.FileMap , error )
44
+ expectedError error
45
+ expectedFileMap extractor.FileMap
64
46
}{
65
- {
66
- name : "happy path with docker installed and image found" ,
67
- extractFromFileFunc : func (ctx context.Context , r io.Reader , filenames []string ) (maps extractor.FileMap , e error ) {
68
- return extractor.FileMap {
69
- "file1" : []byte {0x1 , 0x2 , 0x3 },
70
- "file2" : []byte {0x4 , 0x5 , 0x6 },
71
- }, nil
72
- },
73
- expectedFileMap : extractor.FileMap {
74
- "file1" : []byte {0x1 , 0x2 , 0x3 },
75
- "file2" : []byte {0x4 , 0x5 , 0x6 },
76
- },
77
- },
78
47
{
79
48
name : "happy path with no docker installed or no image found" ,
80
- saveLocalImageFunc : func (ctx context.Context , imageName string ) (reader io.Reader , e error ) {
81
- return nil , errors .New ("couldn't save local image" )
82
- },
83
- extractFunc : func (ctx context.Context , imageName string , filenames []string ) (maps extractor.FileMap , e error ) {
49
+ extractFunc : func (ctx context.Context , imageRef image.Reference , transports , filenames []string ) (maps extractor.FileMap , e error ) {
84
50
return extractor.FileMap {
85
51
"file1" : []byte {0x1 , 0x2 , 0x3 },
86
52
"file2" : []byte {0x4 , 0x5 , 0x6 },
@@ -97,9 +63,7 @@ func TestConfig_Analyze(t *testing.T) {
97
63
RegisterOSAnalyzer (mockOSAnalyzer {})
98
64
99
65
ac := Config {Extractor : mockDockerExtractor {
100
- extractFromFile : tc .extractFromFileFunc ,
101
- extract : tc .extractFunc ,
102
- saveLocalImage : tc .saveLocalImageFunc ,
66
+ extract : tc .extractFunc ,
103
67
}}
104
68
fm , err := ac .Analyze (context .TODO (), "fooimage" )
105
69
assert .Equal (t , tc .expectedError , err , tc .name )
@@ -112,11 +76,11 @@ func TestConfig_Analyze(t *testing.T) {
112
76
113
77
func TestConfig_AnalyzeFile (t * testing.T ) {
114
78
testCases := []struct {
115
- name string
116
- extractFromFileFunc func (ctx context.Context , r io. Reader , filenames []string ) (fileMap extractor.FileMap , err error )
117
- inputFile string
118
- expectedError error
119
- expectedFileMap extractor.FileMap
79
+ name string
80
+ extractFunc func (ctx context.Context , imageReference image. Reference , transports , filenames []string ) (extractor.FileMap , error )
81
+ inputFile string
82
+ expectedError error
83
+ expectedFileMap extractor.FileMap
120
84
}{
121
85
{
122
86
name : "happy path, valid tar.gz file" ,
@@ -130,8 +94,8 @@ func TestConfig_AnalyzeFile(t *testing.T) {
130
94
},
131
95
{
132
96
name : "sad path, valid file but ExtractFromFile fails" ,
133
- expectedError : errors .New ("failed to extract files from tar : extract from file failed" ),
134
- extractFromFileFunc : func (ctx context.Context , r io. Reader , filenames []string ) (fileMap extractor.FileMap , err error ) {
97
+ expectedError : errors .New ("failed to extract files: extract from file failed" ),
98
+ extractFunc : func (ctx context.Context , imageRef image. Reference , transports , filenames []string ) (fileMap extractor.FileMap , err error ) {
135
99
return nil , errors .New ("extract from file failed" )
136
100
},
137
101
},
@@ -140,13 +104,11 @@ func TestConfig_AnalyzeFile(t *testing.T) {
140
104
for _ , tc := range testCases {
141
105
ac := Config {
142
106
Extractor : mockDockerExtractor {
143
- extractFromFile : tc .extractFromFileFunc ,
107
+ extract : tc .extractFunc ,
144
108
},
145
109
}
146
110
147
- f , _ := os .Open (tc .inputFile )
148
- defer f .Close ()
149
- fm , err := ac .AnalyzeFile (context .TODO (), f )
111
+ fm , err := ac .AnalyzeFile (context .Background (), tc .inputFile )
150
112
switch {
151
113
case tc .expectedError != nil :
152
114
assert .Equal (t , tc .expectedError .Error (), err .Error (), tc .name )
0 commit comments