Skip to content

Commit 6d47b72

Browse files
committed
test/e2e/in-memory: enable unit tests
Signed-off-by: Stefan Büringer [email protected] Co-authored-by: Fabrizio Pandini [email protected]
1 parent 74b5c8a commit 6d47b72

File tree

5 files changed

+114
-20
lines changed

5 files changed

+114
-20
lines changed

scripts/ci-test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ echo -e "\n*** Testing Cluster API Provider Docker ***\n"
3131
# Docker provider
3232
make test-docker-infrastructure-junit
3333

34+
echo -e "\n*** Testing Cluster API Provider In-Memory ***\n"
35+
# Docker provider
36+
make test-in-memory-infrastructure-junit
37+
3438
echo -e "\n*** Testing Cluster API Runtime SDK test extension ***\n"
3539
# Test Extension
3640
make test-test-extension-junit

test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,14 @@ func TestReconcileNormalEtcd(t *testing.T) {
314314
manager := cmanager.New(scheme)
315315

316316
host := "127.0.0.1"
317-
wcmux := server.NewWorkloadClustersMux(manager, host)
318-
_, err := wcmux.InitWorkloadClusterListener(klog.KObj(cluster).String())
317+
wcmux, err := server.NewWorkloadClustersMux(manager, host, server.CustomPorts{
318+
// NOTE: make sure to use ports different than other tests, so we can run tests in parallel
319+
MinPort: server.DefaultMinPort + 1000,
320+
MaxPort: server.DefaultMinPort + 1099,
321+
DebugPort: server.DefaultDebugPort,
322+
})
323+
g.Expect(err).ToNot(HaveOccurred())
324+
_, err = wcmux.InitWorkloadClusterListener(klog.KObj(cluster).String())
319325
g.Expect(err).ToNot(HaveOccurred())
320326

321327
r := InMemoryMachineReconciler{
@@ -436,8 +442,14 @@ func TestReconcileNormalApiServer(t *testing.T) {
436442
manager := cmanager.New(scheme)
437443

438444
host := "127.0.0.1"
439-
wcmux := server.NewWorkloadClustersMux(manager, host)
440-
_, err := wcmux.InitWorkloadClusterListener(klog.KObj(cluster).String())
445+
wcmux, err := server.NewWorkloadClustersMux(manager, host, server.CustomPorts{
446+
// NOTE: make sure to use ports different than other tests, so we can run tests in parallel
447+
MinPort: server.DefaultMinPort + 1100,
448+
MaxPort: server.DefaultMinPort + 1299,
449+
DebugPort: server.DefaultDebugPort,
450+
})
451+
g.Expect(err).ToNot(HaveOccurred())
452+
_, err = wcmux.InitWorkloadClusterListener(klog.KObj(cluster).String())
441453
g.Expect(err).ToNot(HaveOccurred())
442454

443455
r := InMemoryMachineReconciler{

test/infrastructure/inmemory/internal/server/mux.go

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,52 @@ import (
4444
)
4545

4646
const (
47-
debugPort = 19000
47+
// DefaultDebugPort default debug port of the workload clusters mux.
48+
DefaultDebugPort = 19000
4849

4950
// This range allows for 4k clusters, which is 4 times the goal we have in mind for
5051
// the first iteration of stress tests.
5152

52-
minPort = 20000
53-
maxPort = 24000
53+
// DefaultMinPort default min port of the workload clusters mux.
54+
DefaultMinPort = 20000
55+
// DefaultMaxPort default max port of the workload clusters mux.
56+
DefaultMaxPort = 24000
5457
)
5558

59+
// WorkloadClustersMuxOption define an option for the WorkloadClustersMux creation.
60+
type WorkloadClustersMuxOption interface {
61+
Apply(*WorkloadClustersMuxOptions)
62+
}
63+
64+
// WorkloadClustersMuxOptions are options for the workload clusters mux.
65+
type WorkloadClustersMuxOptions struct {
66+
MinPort int
67+
MaxPort int
68+
DebugPort int
69+
}
70+
71+
// ApplyOptions applies WorkloadClustersMuxOption to the current WorkloadClustersMuxOptions.
72+
func (o *WorkloadClustersMuxOptions) ApplyOptions(opts []WorkloadClustersMuxOption) *WorkloadClustersMuxOptions {
73+
for _, opt := range opts {
74+
opt.Apply(o)
75+
}
76+
return o
77+
}
78+
79+
// CustomPorts allows to customize the ports used by the workload clusters mux.
80+
type CustomPorts struct {
81+
MinPort int
82+
MaxPort int
83+
DebugPort int
84+
}
85+
86+
// Apply applies this configuration to the given WorkloadClustersMuxOptions.
87+
func (c CustomPorts) Apply(options *WorkloadClustersMuxOptions) {
88+
options.MinPort = c.MinPort
89+
options.MaxPort = c.MaxPort
90+
options.DebugPort = c.DebugPort
91+
}
92+
5693
// WorkloadClustersMux implements a server that handles requests for multiple workload clusters.
5794
// Each workload clusters will get its own listener, serving on a dedicated port, eg.
5895
// wkl-cluster-1 >> :20000, wkl-cluster-2 >> :20001 etc.
@@ -77,12 +114,19 @@ type WorkloadClustersMux struct {
77114
}
78115

79116
// NewWorkloadClustersMux returns a WorkloadClustersMux that handles requests for multiple workload clusters.
80-
func NewWorkloadClustersMux(manager cmanager.Manager, host string) *WorkloadClustersMux {
117+
func NewWorkloadClustersMux(manager cmanager.Manager, host string, opts ...WorkloadClustersMuxOption) (*WorkloadClustersMux, error) {
118+
options := WorkloadClustersMuxOptions{
119+
MinPort: DefaultMinPort,
120+
MaxPort: DefaultMaxPort,
121+
DebugPort: DefaultDebugPort,
122+
}
123+
options.ApplyOptions(opts)
124+
81125
m := &WorkloadClustersMux{
82126
host: host,
83-
minPort: minPort,
84-
maxPort: maxPort,
85-
portIndex: minPort,
127+
minPort: options.MinPort,
128+
maxPort: options.MaxPort,
129+
portIndex: options.MinPort,
86130
manager: manager,
87131
workloadClusterListeners: map[string]*WorkloadClusterListener{},
88132
workloadClusterNameByHost: map[string]string{},
@@ -107,10 +151,13 @@ func NewWorkloadClustersMux(manager cmanager.Manager, host string) *WorkloadClus
107151
m.debugServer = http.Server{
108152
Handler: api.NewDebugHandler(manager, m.log, m),
109153
}
110-
l, _ := net.Listen("tcp", net.JoinHostPort(host, fmt.Sprintf("%d", debugPort)))
154+
l, err := net.Listen("tcp", net.JoinHostPort(host, fmt.Sprintf("%d", options.DebugPort)))
155+
if err != nil {
156+
return nil, errors.Wrapf(err, "failed to create listener for workload cluster mux")
157+
}
111158
go func() { _ = m.debugServer.Serve(l) }()
112159

113-
return m
160+
return m, nil
114161
}
115162

116163
// mixedHandler returns an handler that can serve either API server calls or etcd calls.

test/infrastructure/inmemory/internal/server/mux_test.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ func init() {
6060
}
6161

6262
func TestMux(t *testing.T) {
63+
t.Parallel()
6364
g := NewWithT(t)
6465

6566
manager := cmanager.New(scheme)
6667

6768
wcl := "workload-cluster"
6869
host := "127.0.0.1" //nolint:goconst
69-
wcmux := NewWorkloadClustersMux(manager, host)
70+
wcmux, err := NewWorkloadClustersMux(manager, host, CustomPorts{
71+
// NOTE: make sure to use ports different than other tests, so we can run tests in parallel
72+
MinPort: DefaultMinPort,
73+
MaxPort: DefaultMinPort + 99,
74+
DebugPort: DefaultDebugPort,
75+
})
76+
g.Expect(err).ToNot(HaveOccurred())
7077

7178
listener, err := wcmux.InitWorkloadClusterListener(wcl)
7279
g.Expect(err).ToNot(HaveOccurred())
@@ -115,9 +122,15 @@ func TestMux(t *testing.T) {
115122
}
116123

117124
func TestAPI_corev1_CRUD(t *testing.T) {
125+
t.Parallel()
118126
g := NewWithT(t)
119127

120-
wcmux, c := setupWorkloadClusterListener(g)
128+
wcmux, c := setupWorkloadClusterListener(g, CustomPorts{
129+
// NOTE: make sure to use ports different than other tests, so we can run tests in parallel
130+
MinPort: DefaultMinPort + 100,
131+
MaxPort: DefaultMinPort + 199,
132+
DebugPort: DefaultDebugPort + 1,
133+
})
121134

122135
// create
123136

@@ -171,9 +184,15 @@ func TestAPI_corev1_CRUD(t *testing.T) {
171184
}
172185

173186
func TestAPI_rbacv1_CRUD(t *testing.T) {
187+
t.Parallel()
174188
g := NewWithT(t)
175189

176-
wcmux, c := setupWorkloadClusterListener(g)
190+
wcmux, c := setupWorkloadClusterListener(g, CustomPorts{
191+
// NOTE: make sure to use ports different than other tests, so we can run tests in parallel
192+
MinPort: DefaultMinPort + 200,
193+
MaxPort: DefaultMinPort + 299,
194+
DebugPort: DefaultDebugPort + 2,
195+
})
177196

178197
// create
179198

@@ -214,12 +233,19 @@ func TestAPI_rbacv1_CRUD(t *testing.T) {
214233
}
215234

216235
func TestAPI_PortForward(t *testing.T) {
236+
t.Parallel()
217237
g := NewWithT(t)
218238
manager := cmanager.New(scheme)
219239

220240
// TODO: deduplicate this setup code with the test above
221241
host := "127.0.0.1"
222-
wcmux := NewWorkloadClustersMux(manager, host)
242+
wcmux, err := NewWorkloadClustersMux(manager, host, CustomPorts{
243+
// NOTE: make sure to use ports different than other tests, so we can run tests in parallel
244+
MinPort: DefaultMinPort + 300,
245+
MaxPort: DefaultMinPort + 399,
246+
DebugPort: DefaultDebugPort + 3,
247+
})
248+
g.Expect(err).ToNot(HaveOccurred())
223249

224250
// InfraCluster controller >> when "creating the load balancer"
225251
wcl1 := "workload-cluster1"
@@ -341,11 +367,12 @@ func TestAPI_PortForward(t *testing.T) {
341367
g.Expect(err).ToNot(HaveOccurred())
342368
}
343369

344-
func setupWorkloadClusterListener(g Gomega) (*WorkloadClustersMux, client.Client) {
370+
func setupWorkloadClusterListener(g Gomega, ports CustomPorts) (*WorkloadClustersMux, client.Client) {
345371
manager := cmanager.New(scheme)
346372

347373
host := "127.0.0.1"
348-
wcmux := NewWorkloadClustersMux(manager, host)
374+
wcmux, err := NewWorkloadClustersMux(manager, host, ports)
375+
g.Expect(err).ToNot(HaveOccurred())
349376

350377
// InfraCluster controller >> when "creating the load balancer"
351378
wcl1 := "workload-cluster1"

test/infrastructure/inmemory/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
257257

258258
// Start an http server
259259
podIP := os.Getenv("POD_IP")
260-
apiServerMux := server.NewWorkloadClustersMux(cloudMgr, podIP)
260+
apiServerMux, err := server.NewWorkloadClustersMux(cloudMgr, podIP)
261+
if err != nil {
262+
setupLog.Error(err, "unable to create workload clusters mux")
263+
os.Exit(1)
264+
}
261265

262266
// Setup reconcilers
263267
if err := (&controllers.InMemoryClusterReconciler{

0 commit comments

Comments
 (0)