@@ -8,33 +8,91 @@ import (
8
8
"github.com/aws/aws-sdk-go-v2/aws"
9
9
"github.com/aws/aws-sdk-go-v2/service/ecr"
10
10
awstypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
11
+ "github.com/stretchr/testify/require"
11
12
12
13
"github.com/aquasecurity/trivy/pkg/fanal/types"
13
14
)
14
15
16
+ type testECRClient interface {
17
+ Options () ecr.Options
18
+ }
19
+
15
20
func TestCheckOptions (t * testing.T ) {
16
21
var tests = map [string ]struct {
17
- domain string
18
- wantErr error
22
+ domain string
23
+ expectedRegion string
24
+ wantErr error
19
25
}{
20
26
"InvalidURL" : {
21
27
domain : "alpine:3.9" ,
22
28
wantErr : types .InvalidURLPattern ,
23
29
},
24
30
"NoOption" : {
25
- domain : "xxx.ecr.ap-northeast-1.amazonaws.com" ,
31
+ domain : "xxx.dkr.ecr.ap-northeast-1.amazonaws.com" ,
32
+ expectedRegion : "ap-northeast-1" ,
33
+ },
34
+ "region-1" : {
35
+ domain : "xxx.dkr.ecr.region-1.amazonaws.com" ,
36
+ expectedRegion : "region-1" ,
37
+ },
38
+ "region-2" : {
39
+ domain : "xxx.dkr.ecr.region-2.amazonaws.com" ,
40
+ expectedRegion : "region-2" ,
41
+ },
42
+ "fips-region-1" : {
43
+ domain : "xxx.dkr.ecr-fips.fips-region.amazonaws.com" ,
44
+ expectedRegion : "fips-region" ,
45
+ },
46
+ "cn-region-1" : {
47
+ domain : "xxx.dkr.ecr.region-1.amazonaws.com.cn" ,
48
+ expectedRegion : "region-1" ,
49
+ },
50
+ "cn-region-2" : {
51
+ domain : "xxx.dkr.ecr.region-2.amazonaws.com.cn" ,
52
+ expectedRegion : "region-2" ,
53
+ },
54
+ "sc2s-region-1" : {
55
+ domain : "xxx.dkr.ecr.sc2s-region.sc2s.sgov.gov" ,
56
+ expectedRegion : "sc2s-region" ,
57
+ },
58
+ "c2s-region-1" : {
59
+ domain : "xxx.dkr.ecr.c2s-region.c2s.ic.gov" ,
60
+ expectedRegion : "c2s-region" ,
61
+ },
62
+ "invalid-ecr" : {
63
+ domain : "xxx.dkrecr.region-1.amazonaws.com" ,
64
+ wantErr : types .InvalidURLPattern ,
65
+ },
66
+ "invalid-fips" : {
67
+ domain : "xxx.dkr.ecrfips.fips-region.amazonaws.com" ,
68
+ wantErr : types .InvalidURLPattern ,
69
+ },
70
+ "invalid-cn" : {
71
+ domain : "xxx.dkr.ecr.region-2.amazonaws.cn" ,
72
+ wantErr : types .InvalidURLPattern ,
73
+ },
74
+ "invalid-sc2s" : {
75
+ domain : "xxx.dkr.ecr.sc2s-region.sc2s.sgov" ,
76
+ wantErr : types .InvalidURLPattern ,
77
+ },
78
+ "invalid-cs2" : {
79
+ domain : "xxx.dkr.ecr.c2s-region.c2s.ic" ,
80
+ wantErr : types .InvalidURLPattern ,
26
81
},
27
82
}
28
83
29
84
for testname , v := range tests {
30
85
a := & ECR {}
31
- err := a .CheckOptions (v .domain , types.RegistryOptions {})
86
+ ecrClient , err := a .CheckOptions (v .domain , types.RegistryOptions {})
32
87
if err != nil {
33
88
if ! errors .Is (err , v .wantErr ) {
34
89
t .Errorf ("[%s]\n expected error based on %v\n actual : %v" , testname , v .wantErr , err )
35
90
}
36
91
continue
37
92
}
93
+
94
+ client := (ecrClient .(* ECRClient )).Client .(testECRClient )
95
+ require .Equal (t , v .expectedRegion , client .Options ().Region )
38
96
}
39
97
}
40
98
@@ -82,7 +140,7 @@ func TestECRGetCredential(t *testing.T) {
82
140
}
83
141
84
142
for i , c := range cases {
85
- e := ECR {
143
+ e := ECRClient {
86
144
Client : mockedECR {Resp : c .Resp },
87
145
}
88
146
username , password , err := e .GetCredential (context .Background ())
0 commit comments