Skip to content

Commit

Permalink
Add unit tests for cloudinit generation (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoflorido committed Jul 10, 2024
1 parent 7af836b commit 7d278a5
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 9 deletions.
62 changes: 59 additions & 3 deletions pkg/cloudinit/controlplane_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"))
}
64 changes: 61 additions & 3 deletions pkg/cloudinit/controlplane_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"))
}
65 changes: 62 additions & 3 deletions pkg/cloudinit/worker_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"))
}

0 comments on commit 7d278a5

Please sign in to comment.