Skip to content

Commit

Permalink
Refactor cloudInit files testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoflorido committed Jul 8, 2024
1 parent 13de91e commit 732d522
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 177 deletions.
80 changes: 17 additions & 63 deletions pkg/cloudinit/controlplane_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package cloudinit_test

import (
"os"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -68,70 +67,25 @@ func TestNewInitControlPlane(t *testing.T) {
"postrun2",
}))

// Define the expected files to write with their content, path, permissions, and owner.
expectedWriteFiles := []cloudinit.File{
{
Path: "/tmp/file",
Content: "test file",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/config.yaml",
Content: "### config file ###",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/microcluster-address",
Content: "10.0.0.10:2380",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/snap-track",
Content: "1.30-classic/stable",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/token",
Content: "test-token",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/manifests/00-k8sd-proxy.yaml",
Content: "test-daemonset",
Permissions: "0400",
Owner: "root:root",
},
}

scriptFiles := map[string]string{
"./scripts/install.sh": "/capi/scripts/install.sh",
"./scripts/bootstrap.sh": "/capi/scripts/bootstrap.sh",
"./scripts/load-images.sh": "/capi/scripts/load-images.sh",
"./scripts/join-cluster.sh": "/capi/scripts/join-cluster.sh",
"./scripts/wait-apiserver-ready.sh": "/capi/scripts/wait-apiserver-ready.sh",
"./scripts/deploy-manifests.sh": "/capi/scripts/deploy-manifests.sh",
"./scripts/configure-token.sh": "/capi/scripts/configure-token.sh",
"./scripts/create-sentinel-bootstrap.sh": "/capi/scripts/create-sentinel-bootstrap.sh",
}

// Read the content of each script file and append it to the expected write files.
for relativePath, scriptPath := range scriptFiles {
content, err := os.ReadFile(relativePath)
g.Expect(err).NotTo(HaveOccurred())
expectedWriteFiles = append(expectedWriteFiles, cloudinit.File{
Path: scriptPath,
Content: string(content),
Permissions: "0500",
Owner: "root:root",
})
// NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_init.go file.
expectedPaths := []interface{}{
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"),
}

g.Expect(config.WriteFiles).To(ConsistOf(expectedWriteFiles))
g.Expect(config.WriteFiles).To(ConsistOf(expectedPaths...), "Some /capi/scripts files are missing")
}

func TestNewInitControlPlaneInvalidVersionError(t *testing.T) {
Expand Down
73 changes: 16 additions & 57 deletions pkg/cloudinit/controlplane_join_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cloudinit_test

import (
"os"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -49,64 +48,24 @@ func TestNewJoinControlPlane(t *testing.T) {
"postrun2",
}))

// Define the expected extra files with their content, path, permissions, and owner.
expectedWriteFiles := []cloudinit.File{
{
Path: "/tmp/file",
Content: "test file",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/config.yaml",
Content: "### config file ###",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/snap-track",
Content: "1.30-classic/stable",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/microcluster-address",
Content: "10.0.0.11:2380",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/join-token",
Content: "test-token",
Permissions: "0400",
Owner: "root:root",
},
}

scriptFiles := map[string]string{
"./scripts/install.sh": "/capi/scripts/install.sh",
"./scripts/bootstrap.sh": "/capi/scripts/bootstrap.sh",
"./scripts/load-images.sh": "/capi/scripts/load-images.sh",
"./scripts/join-cluster.sh": "/capi/scripts/join-cluster.sh",
"./scripts/wait-apiserver-ready.sh": "/capi/scripts/wait-apiserver-ready.sh",
"./scripts/deploy-manifests.sh": "/capi/scripts/deploy-manifests.sh",
"./scripts/configure-token.sh": "/capi/scripts/configure-token.sh",
"./scripts/create-sentinel-bootstrap.sh": "/capi/scripts/create-sentinel-bootstrap.sh",
}

// Read the content of each script file and append it to the expected write files.
for relativePath, scriptPath := range scriptFiles {
content, err := os.ReadFile(relativePath)
g.Expect(err).NotTo(HaveOccurred())
expectedWriteFiles = append(expectedWriteFiles, cloudinit.File{
Path: scriptPath,
Content: string(content),
Permissions: "0500",
Owner: "root:root",
})
// NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_join.go file.
expectedPaths := []interface{}{
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"),
}

g.Expect(config.WriteFiles).To(ConsistOf(expectedWriteFiles))
g.Expect(config.WriteFiles).To(ConsistOf(expectedPaths...), "Some /capi/scripts files are missing")
}

func TestNewJoinControlPlaneInvalidVersionError(t *testing.T) {
Expand Down
73 changes: 16 additions & 57 deletions pkg/cloudinit/worker_join_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cloudinit_test

import (
"os"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -49,64 +48,24 @@ func TestNewJoinWorker(t *testing.T) {
"postrun2",
}))

// Define the expected files to write with their content, path, permissions, and owner.
expectedWriteFiles := []cloudinit.File{
{
Path: "/tmp/file",
Content: "test file",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/config.yaml",
Content: "### config file ###",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/microcluster-address",
Content: "10.0.0.10:8080",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/snap-track",
Content: "1.30-classic/stable",
Permissions: "0400",
Owner: "root:root",
},
{
Path: "/capi/etc/join-token",
Content: "test-token",
Permissions: "0400",
Owner: "root:root",
},
}

scriptFiles := map[string]string{
"./scripts/install.sh": "/capi/scripts/install.sh",
"./scripts/bootstrap.sh": "/capi/scripts/bootstrap.sh",
"./scripts/load-images.sh": "/capi/scripts/load-images.sh",
"./scripts/join-cluster.sh": "/capi/scripts/join-cluster.sh",
"./scripts/wait-apiserver-ready.sh": "/capi/scripts/wait-apiserver-ready.sh",
"./scripts/deploy-manifests.sh": "/capi/scripts/deploy-manifests.sh",
"./scripts/configure-token.sh": "/capi/scripts/configure-token.sh",
"./scripts/create-sentinel-bootstrap.sh": "/capi/scripts/create-sentinel-bootstrap.sh",
}

// Read the content of each script file and append it to the expected write files.
for relativePath, scriptPath := range scriptFiles {
content, err := os.ReadFile(relativePath)
g.Expect(err).NotTo(HaveOccurred())
expectedWriteFiles = append(expectedWriteFiles, cloudinit.File{
Path: scriptPath,
Content: string(content),
Permissions: "0500",
Owner: "root:root",
})
// NOTE (mateoflorido): Keep this test in sync with the expected paths in the worker_join.go file.
expectedPaths := []interface{}{
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"),
}

g.Expect(config.WriteFiles).To(ConsistOf(expectedWriteFiles))
g.Expect(config.WriteFiles).To(ConsistOf(expectedPaths...), "Some /capi/scripts files are missing")
}

func TestNewJoinWorkerInvalidVersionError(t *testing.T) {
Expand Down

0 comments on commit 732d522

Please sign in to comment.