From 7d278a5434616a03176211186b13140bc013e696 Mon Sep 17 00:00:00 2001 From: Mateo Florido <32885896+mateoflorido@users.noreply.github.com> Date: Wed, 10 Jul 2024 09:18:14 -0500 Subject: [PATCH] Add unit tests for `cloudinit` generation (#16) --- pkg/cloudinit/controlplane_init_test.go | 62 +++++++++++++++++++++-- pkg/cloudinit/controlplane_join_test.go | 64 ++++++++++++++++++++++-- pkg/cloudinit/worker_join_test.go | 65 +++++++++++++++++++++++-- 3 files changed, 182 insertions(+), 9 deletions(-) diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index 5ad86d68..33bc5d6b 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -40,12 +40,18 @@ func TestNewInitControlPlane(t *testing.T) { Owner: "root:root", }}, ConfigFileContents: "### config file ###", - MicroclusterAddress: ":2380", + MicroclusterAddress: "10.0.0.10", }, + Token: "test-token", + K8sdProxyDaemonSet: "test-daemonset", }) - // TODO: add tests for expected files and commands - g.Expect(err).To(BeNil()) + g.Expect(err).ToNot(HaveOccurred()) + + // Verify the boot commands. + g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) + + // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", "prerun1", @@ -60,4 +66,54 @@ func TestNewInitControlPlane(t *testing.T) { "postrun1", "postrun2", })) + + // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_init.go file. + g.Expect(config.WriteFiles).To(ConsistOf( + HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/bootstrap.sh"), + HaveField("Path", "/capi/scripts/load-images.sh"), + HaveField("Path", "/capi/scripts/join-cluster.sh"), + HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), + HaveField("Path", "/capi/scripts/deploy-manifests.sh"), + HaveField("Path", "/capi/scripts/configure-token.sh"), + HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), + HaveField("Path", "/capi/etc/config.yaml"), + HaveField("Path", "/capi/etc/microcluster-address"), + HaveField("Path", "/capi/etc/token"), + HaveField("Path", "/capi/etc/snap-track"), + HaveField("Path", "/capi/manifests/00-k8sd-proxy.yaml"), + HaveField("Path", "/tmp/file"), + ), "Some /capi/scripts files are missing") +} + +func TestNewInitControlPlaneInvalidVersionError(t *testing.T) { + g := NewWithT(t) + + _, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "invalid-version", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + }}) + + g.Expect(err).To(HaveOccurred()) +} + +func TestNewInitControlPlaneAirGapped(t *testing.T) { + g := NewWithT(t) + + config, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + AirGapped: true, + }}) + + g.Expect(err).NotTo(HaveOccurred()) + + // Verify the run commands is missing install.sh script. + g.Expect(config.RunCommands).NotTo(ContainElement("/capi/scripts/install.sh")) } diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index 4126fc0d..c3cc8663 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -24,12 +24,17 @@ func TestNewJoinControlPlane(t *testing.T) { Owner: "root:root", }}, ConfigFileContents: "### config file ###", - MicroclusterAddress: ":2380", + MicroclusterAddress: "10.0.0.11", }, + JoinToken: "test-token", }) - // TODO: add tests for expected files and commands - g.Expect(err).To(BeNil()) + g.Expect(err).NotTo(HaveOccurred()) + + // Verify the boot commands. + g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) + + // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", "prerun1", @@ -42,4 +47,57 @@ func TestNewJoinControlPlane(t *testing.T) { "postrun1", "postrun2", })) + + // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_join.go file. + g.Expect(config.WriteFiles).To(ConsistOf( + HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/bootstrap.sh"), + HaveField("Path", "/capi/scripts/load-images.sh"), + HaveField("Path", "/capi/scripts/join-cluster.sh"), + HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), + HaveField("Path", "/capi/scripts/deploy-manifests.sh"), + HaveField("Path", "/capi/scripts/configure-token.sh"), + HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), + HaveField("Path", "/capi/etc/config.yaml"), + HaveField("Path", "/capi/etc/microcluster-address"), + HaveField("Path", "/capi/etc/join-token"), + HaveField("Path", "/capi/etc/snap-track"), + HaveField("Path", "/tmp/file"), + ), "Some /capi/scripts files are missing") +} + +func TestNewJoinControlPlaneInvalidVersionError(t *testing.T) { + g := NewWithT(t) + + _, err := cloudinit.NewJoinControlPlane(cloudinit.JoinControlPlaneInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "invalid-version", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + }, + JoinToken: "test-token", + }) + + g.Expect(err).To(HaveOccurred()) +} + +func TestNewJoinControlPlaneAirGapped(t *testing.T) { + g := NewWithT(t) + + config, err := cloudinit.NewJoinControlPlane(cloudinit.JoinControlPlaneInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + AirGapped: true, + }, + JoinToken: "test-token", + }) + + g.Expect(err).NotTo(HaveOccurred()) + + // Verify the run commands is missing install.sh script. + g.Expect(config.RunCommands).NotTo(ContainElement("/capi/scripts/install.sh")) } diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index c504b7a2..20bc849b 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -24,12 +24,18 @@ func TestNewJoinWorker(t *testing.T) { Owner: "root:root", }}, ConfigFileContents: "### config file ###", - MicroclusterAddress: ":2380", + MicroclusterAddress: "10.0.0.10", + MicroclusterPort: 8080, }, + JoinToken: "test-token", }) - // TODO: add tests for expected files and commands - g.Expect(err).To(BeNil()) + g.Expect(err).NotTo(HaveOccurred()) + + // Verify the boot commands. + g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) + + // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", "prerun1", @@ -41,4 +47,57 @@ func TestNewJoinWorker(t *testing.T) { "postrun1", "postrun2", })) + + // NOTE (mateoflorido): Keep this test in sync with the expected paths in the worker_join.go file. + g.Expect(config.WriteFiles).To(ConsistOf( + HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/bootstrap.sh"), + HaveField("Path", "/capi/scripts/load-images.sh"), + HaveField("Path", "/capi/scripts/join-cluster.sh"), + HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), + HaveField("Path", "/capi/scripts/deploy-manifests.sh"), + HaveField("Path", "/capi/scripts/configure-token.sh"), + HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), + HaveField("Path", "/capi/etc/config.yaml"), + HaveField("Path", "/capi/etc/microcluster-address"), + HaveField("Path", "/capi/etc/join-token"), + HaveField("Path", "/capi/etc/snap-track"), + HaveField("Path", "/tmp/file"), + ), "Some /capi/scripts files are missing") +} + +func TestNewJoinWorkerInvalidVersionError(t *testing.T) { + g := NewWithT(t) + + _, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "invalid-version", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + }, + JoinToken: "test-token", + }) + + g.Expect(err).To(HaveOccurred()) +} + +func TestNewJoinWorkerAirGapped(t *testing.T) { + g := NewWithT(t) + + config, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + AirGapped: true, + }, + JoinToken: "test-token", + }) + + g.Expect(err).NotTo(HaveOccurred()) + + // Verify the run commands is missing install.sh script. + g.Expect(config.RunCommands).NotTo(ContainElement("/capi/scripts/install.sh")) }