Skip to content

Commit 37e157a

Browse files
committed
addressing comments and reverting test_helper.go
1 parent 441cb68 commit 37e157a

File tree

8 files changed

+103
-232
lines changed

8 files changed

+103
-232
lines changed

cmd/manager/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func main() { // nolint: gocyclo
7777
webhookPort int
7878
enforceFirstBoot bool
7979
enforcePowerOff bool
80-
ignitionConfigPath string
80+
discoveryIgnitionPath string
8181
serverResyncInterval time.Duration
8282
maintenanceResyncInterval time.Duration
8383
powerPollingInterval time.Duration
@@ -116,7 +116,7 @@ func main() { // nolint: gocyclo
116116
"Defines the duration which the bmc waits before reconciling again when bmc has been reset.")
117117
flag.DurationVar(&maintenanceResyncInterval, "maintenance-resync-interval", 2*time.Minute,
118118
"Defines the interval at which the CRD performing maintenance is polled during server maintenance task.")
119-
flag.StringVar(&ignitionConfigPath, "ignition-config-path", "/etc/metal-operator/ignition-template.yaml",
119+
flag.StringVar(&discoveryIgnitionPath, "discovery-ignition-path", "/etc/metal-operator/ignition-template.yaml",
120120
"Path to the ignition template file.")
121121
flag.StringVar(&registryURL, "registry-url", "", "The URL of the registry.")
122122
flag.StringVar(&registryProtocol, "registry-protocol", "http", "The protocol to use for the registry.")
@@ -354,7 +354,7 @@ func main() { // nolint: gocyclo
354354
EnforcePowerOff: enforcePowerOff,
355355
MaxConcurrentReconciles: serverMaxConcurrentReconciles,
356356
Conditions: conditionutils.NewAccessor(conditionutils.AccessorOptions{}),
357-
IgnitionConfigPath: ignitionConfigPath,
357+
DiscoveryIgnitionPath: discoveryIgnitionPath,
358358
BMCOptions: bmc.Options{
359359
BasicAuth: true,
360360
PowerPollingInterval: powerPollingInterval,

internal/controller/server_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type ServerReconciler struct {
9292
DiscoveryTimeout time.Duration
9393
MaxConcurrentReconciles int
9494
Conditions *conditionutils.Accessor
95-
IgnitionConfigPath string
95+
DiscoveryIgnitionPath string
9696
}
9797

9898
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=bmcs,verbs=get;list;watch
@@ -717,11 +717,11 @@ func (r *ServerReconciler) generateDefaultIgnitionDataForServer(flags string, ss
717717

718718
// Load ignition template from file
719719
ignitionData, err := ignition.GenerateIgnitionDataFromFile(
720-
r.IgnitionConfigPath,
720+
r.DiscoveryIgnitionPath,
721721
config,
722722
)
723723
if err != nil {
724-
return nil, fmt.Errorf("failed to generate ignition data from file %s: %w", r.IgnitionConfigPath, err)
724+
return nil, fmt.Errorf("failed to generate ignition data from file %s: %w", r.DiscoveryIgnitionPath, err)
725725
}
726726

727727
return ignitionData, nil

internal/controller/server_controller_test.go

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -923,22 +923,20 @@ passwd:
923923

924924
tmpFile, err := os.CreateTemp("", "ignition-template-*.yaml")
925925
Expect(err).NotTo(HaveOccurred())
926-
defer func() {
927-
Expect(os.Remove(tmpFile.Name())).To(Succeed())
928-
}()
926+
DeferCleanup(os.Remove, tmpFile.Name())
929927

930928
_, err = tmpFile.WriteString(customTemplate)
931929
Expect(err).NotTo(HaveOccurred())
932930
Expect(tmpFile.Close()).To(Succeed())
933931

934932
By("Creating a ServerReconciler with file path")
935933
reconciler := &ServerReconciler{
936-
Client: k8sClient,
937-
Scheme: k8sClient.Scheme(),
938-
RegistryURL: registryURL,
939-
ManagerNamespace: ns.Name,
940-
ProbeImage: "foo:latest",
941-
IgnitionConfigPath: tmpFile.Name(),
934+
Client: k8sClient,
935+
Scheme: k8sClient.Scheme(),
936+
RegistryURL: registryURL,
937+
ManagerNamespace: ns.Name,
938+
ProbeImage: "foo:latest",
939+
DiscoveryIgnitionPath: tmpFile.Name(),
942940
}
943941

944942
By("Generating ignition data with custom file")
@@ -961,12 +959,12 @@ passwd:
961959
It("Should fallback with error when file is missing", func(ctx SpecContext) {
962960
By("Creating a ServerReconciler with non-existent file path")
963961
reconciler := &ServerReconciler{
964-
Client: k8sClient,
965-
Scheme: k8sClient.Scheme(),
966-
RegistryURL: registryURL,
967-
ManagerNamespace: ns.Name,
968-
ProbeImage: "foo:latest",
969-
IgnitionConfigPath: "/nonexistent/path/ignition.yaml",
962+
Client: k8sClient,
963+
Scheme: k8sClient.Scheme(),
964+
RegistryURL: registryURL,
965+
ManagerNamespace: ns.Name,
966+
ProbeImage: "foo:latest",
967+
DiscoveryIgnitionPath: "/nonexistent/path/ignition.yaml",
970968
}
971969

972970
By("Generating ignition data (should fail)")
@@ -989,22 +987,20 @@ systemd:
989987

990988
tmpFile, err := os.CreateTemp("", "invalid-template-*.yaml")
991989
Expect(err).NotTo(HaveOccurred())
992-
defer func() {
993-
Expect(os.Remove(tmpFile.Name())).To(Succeed())
994-
}()
990+
DeferCleanup(os.Remove, tmpFile.Name())
995991

996992
_, err = tmpFile.WriteString(invalidTemplate)
997993
Expect(err).NotTo(HaveOccurred())
998994
Expect(tmpFile.Close()).To(Succeed())
999995

1000996
By("Creating a ServerReconciler with invalid template file")
1001997
reconciler := &ServerReconciler{
1002-
Client: k8sClient,
1003-
Scheme: k8sClient.Scheme(),
1004-
RegistryURL: registryURL,
1005-
ManagerNamespace: ns.Name,
1006-
ProbeImage: "foo:latest",
1007-
IgnitionConfigPath: tmpFile.Name(),
998+
Client: k8sClient,
999+
Scheme: k8sClient.Scheme(),
1000+
RegistryURL: registryURL,
1001+
ManagerNamespace: ns.Name,
1002+
ProbeImage: "foo:latest",
1003+
DiscoveryIgnitionPath: tmpFile.Name(),
10081004
}
10091005

10101006
By("Generating ignition data (should fail)")
@@ -1040,22 +1036,20 @@ passwd:
10401036

10411037
tmpFile, err := os.CreateTemp("", "default-ignition-*.yaml")
10421038
Expect(err).NotTo(HaveOccurred())
1043-
defer func() {
1044-
Expect(os.Remove(tmpFile.Name())).To(Succeed())
1045-
}()
1039+
DeferCleanup(os.Remove, tmpFile.Name())
10461040

10471041
_, err = tmpFile.WriteString(defaultTemplate)
10481042
Expect(err).NotTo(HaveOccurred())
10491043
Expect(tmpFile.Close()).To(Succeed())
10501044

10511045
By("Creating a ServerReconciler with default file path")
10521046
reconciler := &ServerReconciler{
1053-
Client: k8sClient,
1054-
Scheme: k8sClient.Scheme(),
1055-
RegistryURL: registryURL,
1056-
ManagerNamespace: ns.Name,
1057-
ProbeImage: "foo:latest",
1058-
IgnitionConfigPath: tmpFile.Name(),
1047+
Client: k8sClient,
1048+
Scheme: k8sClient.Scheme(),
1049+
RegistryURL: registryURL,
1050+
ManagerNamespace: ns.Name,
1051+
ProbeImage: "foo:latest",
1052+
DiscoveryIgnitionPath: tmpFile.Name(),
10591053
}
10601054

10611055
By("Generating ignition data (should use default template)")
@@ -1098,23 +1092,21 @@ passwd:
10981092

10991093
tmpFile, err := os.CreateTemp("", "e2e-ignition-*.yaml")
11001094
Expect(err).NotTo(HaveOccurred())
1101-
defer func() {
1102-
Expect(os.Remove(tmpFile.Name())).To(Succeed())
1103-
}()
1095+
DeferCleanup(os.Remove, tmpFile.Name())
11041096

11051097
_, err = tmpFile.WriteString(customTemplate)
11061098
Expect(err).NotTo(HaveOccurred())
11071099
Expect(tmpFile.Close()).To(Succeed())
11081100

11091101
By("Creating a reconciler with custom ignition template path")
11101102
customReconciler := &ServerReconciler{
1111-
Client: k8sClient,
1112-
Scheme: k8sClient.Scheme(),
1113-
RegistryURL: registryURL,
1114-
ManagerNamespace: ns.Name,
1115-
ProbeImage: "custom-probe:v1.0.0",
1116-
Insecure: true,
1117-
IgnitionConfigPath: tmpFile.Name(),
1103+
Client: k8sClient,
1104+
Scheme: k8sClient.Scheme(),
1105+
RegistryURL: registryURL,
1106+
ManagerNamespace: ns.Name,
1107+
ProbeImage: "custom-probe:v1.0.0",
1108+
Insecure: true,
1109+
DiscoveryIgnitionPath: tmpFile.Name(),
11181110
}
11191111

11201112
By("Generating ignition data with custom template")

internal/controller/suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +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
196-
IgnitionConfigPath: filepath.Join("..", "..", "config", "manager", "ignition-template.yaml"),
195+
DiscoveryTimeout: time.Second, // Force timeout to be quick for tests
196+
DiscoveryIgnitionPath: filepath.Join("..", "..", "config", "manager", "ignition-template.yaml"),
197197
}).SetupWithManager(k8sManager)).To(Succeed())
198198

199199
Expect((&ServerClaimReconciler{

internal/controller/test_helper.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package controller
5+
6+
import (
7+
. "github.com/onsi/ginkgo/v2" // nolint: staticcheck
8+
. "github.com/onsi/gomega" // nolint: staticcheck
9+
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega" // nolint: staticcheck
10+
11+
metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1"
12+
)
13+
14+
// EnsureCleanState ensures that all ServerClaims and cluster scoped objects are removed from the API server.
15+
func EnsureCleanState() {
16+
GinkgoHelper()
17+
18+
Eventually(func(g Gomega) error {
19+
bmcList := &metalv1alpha1.BMCList{}
20+
g.Eventually(ObjectList(bmcList)).Should(HaveField("Items", HaveLen(0)))
21+
22+
bmcSecretList := &metalv1alpha1.BMCSecretList{}
23+
g.Eventually(ObjectList(bmcSecretList)).Should(HaveField("Items", HaveLen(0)))
24+
25+
claimList := &metalv1alpha1.ServerClaimList{}
26+
g.Eventually(ObjectList(claimList)).Should(HaveField("Items", HaveLen(0)))
27+
28+
bmcSettingsList := &metalv1alpha1.BMCSettingsList{}
29+
g.Eventually(ObjectList(bmcSettingsList)).Should(HaveField("Items", HaveLen(0)))
30+
31+
bmcVersionSetList := &metalv1alpha1.BMCVersionSetList{}
32+
g.Eventually(ObjectList(bmcVersionSetList)).Should(HaveField("Items", HaveLen(0)))
33+
34+
bmcVersionList := &metalv1alpha1.BMCVersionList{}
35+
g.Eventually(ObjectList(bmcVersionList)).Should(HaveField("Items", HaveLen(0)))
36+
37+
biosSettingsSetList := &metalv1alpha1.BIOSSettingsSetList{}
38+
g.Eventually(ObjectList(biosSettingsSetList)).Should(HaveField("Items", HaveLen(0)))
39+
40+
biosSettings := &metalv1alpha1.BIOSSettingsList{}
41+
g.Eventually(ObjectList(biosSettings)).Should(HaveField("Items", HaveLen(0)))
42+
43+
maintenanceList := &metalv1alpha1.ServerMaintenanceList{}
44+
g.Eventually(ObjectList(maintenanceList)).Should(HaveField("Items", HaveLen(0)))
45+
46+
serverList := &metalv1alpha1.ServerList{}
47+
g.Eventually(ObjectList(serverList)).Should(HaveField("Items", HaveLen(0)))
48+
49+
return nil
50+
}).Should(Succeed())
51+
}

0 commit comments

Comments
 (0)