Skip to content

Commit 11ccab8

Browse files
unit tests
1 parent 1cdb2af commit 11ccab8

File tree

2 files changed

+513
-0
lines changed

2 files changed

+513
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
routev1 "github.com/openshift/api/route/v1"
8+
routefake "github.com/openshift/client-go/route/clientset/versioned/fake"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
corev1 "k8s.io/api/core/v1"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/runtime"
14+
"k8s.io/client-go/kubernetes/fake"
15+
)
16+
17+
func TestGetInternalRegistryHostnames(t *testing.T) {
18+
t.Parallel()
19+
20+
testCases := []struct {
21+
name string
22+
services []runtime.Object
23+
routes []runtime.Object
24+
expectedHostnames []string
25+
errExpected bool
26+
}{
27+
{
28+
name: "Registry with service and route",
29+
services: []runtime.Object{
30+
&corev1.Service{
31+
ObjectMeta: metav1.ObjectMeta{
32+
Name: "image-registry",
33+
Namespace: "openshift-image-registry",
34+
},
35+
Spec: corev1.ServiceSpec{
36+
Ports: []corev1.ServicePort{
37+
{Port: 5000},
38+
},
39+
},
40+
},
41+
},
42+
routes: []runtime.Object{
43+
&routev1.Route{
44+
ObjectMeta: metav1.ObjectMeta{
45+
Name: "default-route",
46+
Namespace: "openshift-image-registry",
47+
},
48+
Spec: routev1.RouteSpec{
49+
Host: "default-route-openshift-image-registry.apps.example.com",
50+
},
51+
},
52+
},
53+
expectedHostnames: []string{
54+
"image-registry.openshift-image-registry.svc:5000",
55+
"default-route-openshift-image-registry.apps.example.com",
56+
},
57+
},
58+
{
59+
name: "Registry with service without port",
60+
services: []runtime.Object{
61+
&corev1.Service{
62+
ObjectMeta: metav1.ObjectMeta{
63+
Name: "image-registry",
64+
Namespace: "openshift-image-registry",
65+
},
66+
Spec: corev1.ServiceSpec{
67+
Ports: []corev1.ServicePort{},
68+
},
69+
},
70+
},
71+
routes: []runtime.Object{},
72+
expectedHostnames: []string{
73+
"image-registry.openshift-image-registry.svc",
74+
},
75+
},
76+
{
77+
name: "Registry not deployed (empty namespace)",
78+
services: []runtime.Object{},
79+
routes: []runtime.Object{},
80+
expectedHostnames: []string{},
81+
},
82+
{
83+
name: "Multiple services and routes",
84+
services: []runtime.Object{
85+
&corev1.Service{
86+
ObjectMeta: metav1.ObjectMeta{
87+
Name: "image-registry",
88+
Namespace: "openshift-image-registry",
89+
},
90+
Spec: corev1.ServiceSpec{
91+
Ports: []corev1.ServicePort{
92+
{Port: 5000},
93+
},
94+
},
95+
},
96+
&corev1.Service{
97+
ObjectMeta: metav1.ObjectMeta{
98+
Name: "image-registry-internal",
99+
Namespace: "openshift-image-registry",
100+
},
101+
Spec: corev1.ServiceSpec{
102+
Ports: []corev1.ServicePort{
103+
{Port: 5001},
104+
},
105+
},
106+
},
107+
},
108+
routes: []runtime.Object{
109+
&routev1.Route{
110+
ObjectMeta: metav1.ObjectMeta{
111+
Name: "external-route",
112+
Namespace: "openshift-image-registry",
113+
},
114+
Spec: routev1.RouteSpec{
115+
Host: "external-route.apps.example.com",
116+
},
117+
},
118+
},
119+
expectedHostnames: []string{
120+
"image-registry.openshift-image-registry.svc:5000",
121+
"image-registry-internal.openshift-image-registry.svc:5001",
122+
"external-route.apps.example.com",
123+
},
124+
},
125+
}
126+
127+
for _, testCase := range testCases {
128+
testCase := testCase
129+
t.Run(testCase.name, func(t *testing.T) {
130+
t.Parallel()
131+
132+
kubeclient := fake.NewSimpleClientset(testCase.services...)
133+
routeclient := routefake.NewSimpleClientset(testCase.routes...)
134+
135+
hostnames, err := GetInternalRegistryHostnames(context.TODO(), kubeclient, routeclient)
136+
137+
if testCase.errExpected {
138+
assert.Error(t, err)
139+
return
140+
}
141+
142+
require.NoError(t, err)
143+
assert.ElementsMatch(t, testCase.expectedHostnames, hostnames)
144+
})
145+
}
146+
}
147+
148+
func TestIsOpenShiftRegistry(t *testing.T) {
149+
t.Parallel()
150+
151+
// Set up a standard registry environment
152+
services := []runtime.Object{
153+
&corev1.Service{
154+
ObjectMeta: metav1.ObjectMeta{
155+
Name: "image-registry",
156+
Namespace: "openshift-image-registry",
157+
},
158+
Spec: corev1.ServiceSpec{
159+
Ports: []corev1.ServicePort{
160+
{Port: 5000},
161+
},
162+
},
163+
},
164+
}
165+
166+
routes := []runtime.Object{
167+
&routev1.Route{
168+
ObjectMeta: metav1.ObjectMeta{
169+
Name: "default-route",
170+
Namespace: "openshift-image-registry",
171+
},
172+
Spec: routev1.RouteSpec{
173+
Host: "default-route-openshift-image-registry.apps.example.com",
174+
},
175+
},
176+
}
177+
178+
testCases := []struct {
179+
name string
180+
imageRef string
181+
isInternal bool
182+
errExpected bool
183+
}{
184+
{
185+
name: "Internal registry via service hostname",
186+
imageRef: "image-registry.openshift-image-registry.svc:5000/openshift/custom-image:latest",
187+
isInternal: true,
188+
},
189+
{
190+
name: "Internal registry via route hostname",
191+
imageRef: "default-route-openshift-image-registry.apps.example.com/openshift/custom-image:latest",
192+
isInternal: true,
193+
},
194+
{
195+
name: "External registry (Quay)",
196+
imageRef: "quay.io/openshift/custom-image:latest",
197+
isInternal: false,
198+
},
199+
{
200+
name: "External registry (Docker Hub)",
201+
imageRef: "docker.io/library/nginx:latest",
202+
isInternal: false,
203+
},
204+
{
205+
name: "External private registry",
206+
imageRef: "my-private-registry.example.com:5000/app/image:v1",
207+
isInternal: false,
208+
},
209+
}
210+
211+
for _, testCase := range testCases {
212+
testCase := testCase
213+
t.Run(testCase.name, func(t *testing.T) {
214+
t.Parallel()
215+
216+
kubeclient := fake.NewSimpleClientset(services...)
217+
routeclient := routefake.NewSimpleClientset(routes...)
218+
219+
isInternal, err := IsOpenShiftRegistry(context.TODO(), testCase.imageRef, kubeclient, routeclient)
220+
221+
if testCase.errExpected {
222+
assert.Error(t, err)
223+
return
224+
}
225+
226+
require.NoError(t, err)
227+
assert.Equal(t, testCase.isInternal, isInternal)
228+
})
229+
}
230+
}
231+
232+
func TestIsOpenShiftRegistryWhenRegistryNotDeployed(t *testing.T) {
233+
t.Parallel()
234+
235+
// Empty clients - simulating registry not deployed
236+
kubeclient := fake.NewSimpleClientset()
237+
routeclient := routefake.NewSimpleClientset()
238+
239+
testCases := []struct {
240+
name string
241+
imageRef string
242+
}{
243+
{
244+
name: "Image that would be internal if registry existed",
245+
imageRef: "image-registry.openshift-image-registry.svc:5000/openshift/custom-image:latest",
246+
},
247+
{
248+
name: "External registry image",
249+
imageRef: "quay.io/openshift/custom-image:latest",
250+
},
251+
}
252+
253+
for _, testCase := range testCases {
254+
testCase := testCase
255+
t.Run(testCase.name, func(t *testing.T) {
256+
t.Parallel()
257+
258+
isInternal, err := IsOpenShiftRegistry(context.TODO(), testCase.imageRef, kubeclient, routeclient)
259+
260+
// Should not error when registry is not deployed
261+
require.NoError(t, err)
262+
263+
// Should return false (not internal) when registry not deployed
264+
assert.False(t, isInternal, "Should treat as external registry when OpenShift registry is not deployed")
265+
})
266+
}
267+
}

0 commit comments

Comments
 (0)