-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmutate_test.go
188 lines (178 loc) · 5.73 KB
/
mutate_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package webhook
import (
"context"
"testing"
corev1 "k8s.io/api/core/v1"
"github.com/indeedeng-alpha/harbor-container-webhook/internal/config"
"github.com/stretchr/testify/require"
)
func TestPodContainerProxier_rewriteImage(t *testing.T) {
transformers, err := MakeTransformers([]config.ProxyRule{
{
Name: "docker.io proxy cache except ubuntu",
Matches: []string{"^docker.io"},
Excludes: []string{"^docker.io/(library/)?ubuntu:.*$"},
Replace: "harbor.example.com/dockerhub-proxy",
},
{
Name: "quay.io proxy cache",
Matches: []string{"^quay.io"},
Replace: "harbor.example.com/quay-proxy",
},
{
Name: "docker.io proxy cache but only ubuntu",
Matches: []string{"^docker.io/(library/)?ubuntu"},
Replace: "harbor.example.com/ubuntu-proxy",
},
{
Name: "docker.io proxy cache with imagePullSecret change",
Matches: []string{"^docker.io/(library/)?ubuntu"},
Replace: "harbor.example.com/ubuntu-proxy",
},
}, nil)
require.NoError(t, err)
proxier := PodContainerProxier{
Transformers: transformers,
}
type testcase struct {
name string
image string
platform string
os string
expected string
}
tests := []testcase{
{
name: "an image from quay should be rewritten",
image: "quay.io/bitnami/sealed-secrets-controller:latest",
os: "linux",
platform: "amd64",
expected: "harbor.example.com/quay-proxy/bitnami/sealed-secrets-controller:latest",
},
{
name: "an image from quay without a tag should be rewritten",
image: "quay.io/bitnami/sealed-secrets-controller",
os: "linux",
platform: "amd64",
expected: "harbor.example.com/quay-proxy/bitnami/sealed-secrets-controller:latest",
},
{
name: "an image from docker.io with ubuntu should be rewritten to the ubuntu proxy",
image: "docker.io/library/ubuntu:latest",
os: "linux",
platform: "amd64",
expected: "harbor.example.com/ubuntu-proxy/library/ubuntu:latest",
},
{
name: "a bare ubuntu image from docker.io should be rewritten to the ubuntu proxy",
image: "ubuntu",
os: "linux",
platform: "amd64",
expected: "harbor.example.com/ubuntu-proxy/library/ubuntu:latest",
},
{
name: "an image from docker.io should be rewritten",
image: "docker.io/library/centos:latest",
os: "linux",
platform: "amd64",
expected: "harbor.example.com/dockerhub-proxy/library/centos:latest",
},
{
name: "a bare image from docker.io should be rewritten",
image: "centos",
os: "linux",
platform: "amd64",
expected: "harbor.example.com/dockerhub-proxy/library/centos:latest",
},
{
name: "an image from gcr should not be rewritten",
image: "k8s.gcr.io/kubernetes",
os: "linux",
platform: "amd64",
expected: "k8s.gcr.io/kubernetes",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rewritten, err := proxier.rewriteImage(context.TODO(), tc.image)
require.NoError(t, err)
require.Equal(t, tc.expected, rewritten)
})
}
}
func TestPodContainerProxier_updateImagePullSecretsWithReplaceEnabled(t *testing.T) {
transformers, err := MakeTransformers([]config.ProxyRule{
{
Name: "docker.io proxy cache with imagePullSecrets change",
Matches: []string{"^docker.io"},
Replace: "harbor.example.com/dockerhub-proxy",
ReplaceImagePullSecrets: true,
AuthSecretName: "secret-test",
},
}, nil)
require.NoError(t, err)
proxier := PodContainerProxier{
Transformers: transformers,
}
type testcase struct {
name string
imagePullSecrets []corev1.LocalObjectReference
expected []corev1.LocalObjectReference
}
tests := []testcase{
{
name: "imagePullSecrets is empty, replacement is expected and secret name should be added",
imagePullSecrets: []corev1.LocalObjectReference{},
expected: []corev1.LocalObjectReference{{Name: "secret-test"}},
},
{
name: "imagePullSecrets has a secret, replacement is expected and secret name should be added",
imagePullSecrets: []corev1.LocalObjectReference{{Name: "mysecret"}},
expected: []corev1.LocalObjectReference{{Name: "mysecret"}, {Name: "secret-test"}},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
newImagePullSecrets, err := proxier.updateImagePullSecrets("pod-test", tc.imagePullSecrets)
require.NoError(t, err)
require.Equal(t, tc.expected, newImagePullSecrets)
})
}
}
func TestPodContainerProxier_updateImagePullSecretsWithReplaceDinabled(t *testing.T) {
transformers, err := MakeTransformers([]config.ProxyRule{
{
Name: "docker.io proxy cache without imagePullSecrets change",
Matches: []string{"^docker.io"},
Replace: "harbor.example.com/dockerhub-proxy",
},
}, nil)
require.NoError(t, err)
proxier := PodContainerProxier{
Transformers: transformers,
}
type testcase struct {
name string
imagePullSecrets []corev1.LocalObjectReference
expected []corev1.LocalObjectReference
}
tests := []testcase{
{
name: "imagePullSecrets is empty, replacement is not expected",
imagePullSecrets: []corev1.LocalObjectReference{},
expected: []corev1.LocalObjectReference{},
},
{
name: "imagePullSecrets has a secret, replacement is not expected",
imagePullSecrets: []corev1.LocalObjectReference{{Name: "mysecret"}},
expected: []corev1.LocalObjectReference{{Name: "mysecret"}},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
newImagePullSecrets, err := proxier.updateImagePullSecrets("pod-test", tc.imagePullSecrets)
require.NoError(t, err)
require.Equal(t, tc.expected, newImagePullSecrets)
})
}
}