Skip to content

Commit 836fd2e

Browse files
committed
update tests
1 parent 95d8cae commit 836fd2e

File tree

2 files changed

+73
-37
lines changed

2 files changed

+73
-37
lines changed

internal/controller/server_controller_test.go

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/http"
1111
"os"
12+
"path/filepath"
1213
"time"
1314

1415
"golang.org/x/crypto/bcrypt"
@@ -193,37 +194,16 @@ var _ = Describe("Server Controller", func() {
193194
err = bcrypt.CompareHashAndPassword([]byte(passwordHash), sshSecret.Data[SSHKeyPairSecretPasswordKeyName])
194195
Expect(err).ToNot(HaveOccurred(), "passwordHash should match the expected password")
195196

196-
// Create a temporary ignition template file for testing
197-
tmpIgnitionFile, err := os.CreateTemp("", "ignition-test-*.yaml")
198-
Expect(err).NotTo(HaveOccurred())
199-
defer func() {
200-
Expect(os.Remove(tmpIgnitionFile.Name())).To(Succeed())
201-
}()
202-
203-
defaultTemplate := `variant: fcos
204-
version: "1.3.0"
205-
systemd:
206-
units:
207-
- name: metalprobe.service
208-
enabled: true
209-
contents: |-
210-
[Service]
211-
ExecStart=/usr/bin/docker run {{.Image}} {{.Flags}}
212-
passwd:
213-
users:
214-
- name: metal
215-
password_hash: {{.PasswordHash}}
216-
ssh_authorized_keys: [ {{.SSHPublicKey}} ]`
217-
_, err = tmpIgnitionFile.WriteString(defaultTemplate)
218-
Expect(err).NotTo(HaveOccurred())
219-
Expect(tmpIgnitionFile.Close()).To(Succeed())
220-
221-
ignitionData, err := ignition.GenerateIgnitionDataFromFile(tmpIgnitionFile.Name(), ignition.Config{
222-
Image: "foo:latest",
223-
Flags: "--registry-url=http://localhost:30000 --server-uuid=38947555-7742-3448-3784-823347823834",
224-
SSHPublicKey: string(sshSecret.Data[SSHKeyPairSecretPublicKeyName]),
225-
PasswordHash: passwordHash,
226-
})
197+
// Generate expected ignition data using the same template file the controller uses
198+
ignitionData, err := ignition.GenerateIgnitionDataFromFile(
199+
filepath.Join("..", "..", "config", "manager", "ignition-template.yaml"),
200+
ignition.Config{
201+
Image: "foo:latest",
202+
Flags: "--registry-url=http://localhost:30000 --server-uuid=38947555-7742-3448-3784-823347823834",
203+
SSHPublicKey: string(sshSecret.Data[SSHKeyPairSecretPublicKeyName]),
204+
PasswordHash: passwordHash,
205+
},
206+
)
227207
Expect(err).NotTo(HaveOccurred())
228208

229209
Eventually(Object(ignitionSecret)).Should(SatisfyAll(
@@ -885,11 +865,6 @@ passwd:
885865
Expect(k8sClient.Create(ctx, server)).To(Succeed())
886866
})
887867

888-
AfterEach(func(ctx SpecContext) {
889-
Expect(k8sClient.Delete(ctx, server)).Should(Succeed())
890-
Expect(k8sClient.Delete(ctx, bmcSecret)).Should(Succeed())
891-
})
892-
893868
It("Should use custom file template when available", func(ctx SpecContext) {
894869
By("Creating a custom ignition template file")
895870
customTemplate := `variant: fcos
@@ -1068,6 +1043,66 @@ passwd:
10681043
Expect(ignitionStr).To(ContainSubstring("metalprobe.service"))
10691044
Expect(ignitionStr).To(ContainSubstring("name: metal"))
10701045
})
1046+
1047+
It("Should create server with custom ignition template file end-to-end", func(ctx SpecContext) {
1048+
By("Creating a custom ignition template file")
1049+
customTemplate := `variant: fcos
1050+
version: "1.5.0"
1051+
systemd:
1052+
units:
1053+
- name: e2e-custom-probe.service
1054+
enabled: true
1055+
contents: |-
1056+
[Unit]
1057+
Description=E2E Custom Probe Service
1058+
[Service]
1059+
Restart=always
1060+
ExecStart=/usr/bin/docker run --name e2e-probe {{.Image}} {{.Flags}}
1061+
[Install]
1062+
WantedBy=multi-user.target
1063+
passwd:
1064+
users:
1065+
- name: e2e-user
1066+
password_hash: {{.PasswordHash}}
1067+
ssh_authorized_keys: [ {{.SSHPublicKey}} ]`
1068+
1069+
tmpFile, err := os.CreateTemp("", "e2e-ignition-*.yaml")
1070+
Expect(err).NotTo(HaveOccurred())
1071+
defer func() {
1072+
Expect(os.Remove(tmpFile.Name())).To(Succeed())
1073+
}()
1074+
1075+
_, err = tmpFile.WriteString(customTemplate)
1076+
Expect(err).NotTo(HaveOccurred())
1077+
Expect(tmpFile.Close()).To(Succeed())
1078+
1079+
By("Creating a reconciler with custom ignition template path")
1080+
customReconciler := &ServerReconciler{
1081+
Client: k8sClient,
1082+
Scheme: k8sClient.Scheme(),
1083+
RegistryURL: registryURL,
1084+
ManagerNamespace: ns.Name,
1085+
ProbeImage: "custom-probe:v1.0.0",
1086+
Insecure: true,
1087+
IgnitionConfigPath: tmpFile.Name(),
1088+
}
1089+
1090+
By("Generating ignition data with custom template")
1091+
ignitionData, err := customReconciler.generateDefaultIgnitionDataForServer(
1092+
"--registry-url=http://localhost:30000 --server-uuid=e2e-test-12345",
1093+
[]byte("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC [email protected]"),
1094+
[]byte("testpassword"),
1095+
)
1096+
Expect(err).NotTo(HaveOccurred())
1097+
Expect(ignitionData).NotTo(BeEmpty())
1098+
1099+
ignitionStr := string(ignitionData)
1100+
Expect(ignitionStr).To(ContainSubstring("version: \"1.5.0\""), "Should use custom template version")
1101+
Expect(ignitionStr).To(ContainSubstring("e2e-custom-probe.service"), "Should use custom service name")
1102+
Expect(ignitionStr).To(ContainSubstring("E2E Custom Probe Service"), "Should use custom description")
1103+
Expect(ignitionStr).To(ContainSubstring("e2e-user"), "Should use custom username")
1104+
Expect(ignitionStr).To(ContainSubstring("custom-probe:v1.0.0"), "Should include custom probe image")
1105+
})
10711106
})
10721107
})
10731108

internal/controller/suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ func SetupTest(redfishMockServers []netip.AddrPort) *corev1.Namespace {
192192
PowerPollingTimeout: 200 * time.Millisecond,
193193
BasicAuth: true,
194194
},
195-
DiscoveryTimeout: time.Second, // Force timeout to be quick for tests
195+
DiscoveryTimeout: time.Second, // Force timeout to be quick for tests
196+
IgnitionConfigPath: filepath.Join("..", "..", "config", "manager", "ignition-template.yaml"),
196197
}).SetupWithManager(k8sManager)).To(Succeed())
197198

198199
Expect((&ServerClaimReconciler{

0 commit comments

Comments
 (0)