Skip to content

Commit b5b435c

Browse files
committed
update tests
1 parent f8de4dd commit b5b435c

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"
@@ -194,37 +195,16 @@ var _ = Describe("Server Controller", func() {
194195
err = bcrypt.CompareHashAndPassword([]byte(passwordHash), sshSecret.Data[SSHKeyPairSecretPasswordKeyName])
195196
Expect(err).ToNot(HaveOccurred(), "passwordHash should match the expected password")
196197

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

230210
Eventually(Object(ignitionSecret)).Should(SatisfyAll(
@@ -892,11 +872,6 @@ passwd:
892872
Expect(k8sClient.Create(ctx, server)).To(Succeed())
893873
})
894874

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

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)