Skip to content

Commit 9699701

Browse files
committed
Improve tests for the bootserver
1 parent 901b4e7 commit 9699701

File tree

5 files changed

+108
-8
lines changed

5 files changed

+108
-8
lines changed

api/v1alpha1/constants.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
package v1alpha1
55

66
const (
7-
DefaultIgnitionKey = "ignition" // Key for accessing Ignition configuration data within a Kubernetes Secret object.
8-
DefaultIPXEScriptKey = "ipxe-script" // Key for accessing iPXE script data within the iPXE-specific Secret object.
9-
SystemUUIDIndexKey = "spec.systemUUID" // Field to index resources by their system UUID.
10-
SystemIPIndexKey = "spec.systemIPs" // Field to index resources by their system IP addresses.
11-
DefaultFormatKey = "format" // Key for determining the format of the data stored in a Secret, such as fcos or plain-ignition.
12-
FCOSFormat = "fcos" // Specifies the format value used for Fedora CoreOS specific configurations.
7+
DefaultIgnitionKey = "ignition" // Key for accessing Ignition configuration data within a Kubernetes Secret object.
8+
DefaultIPXEScriptKey = "ipxe-script" // Key for accessing iPXE script data within the iPXE-specific Secret object.
9+
SystemUUIDIndexKey = "spec.systemUUID" // Field to index resources by their system UUID.
10+
SystemIPIndexKey = "spec.systemIPs" // Field to index resources by their system IP addresses.
11+
NetworkIdentifierIndexKey = "spec.networkIdentifiers" // Field to index resources by their network identifiers (IP addresses and MAC addresses).
12+
DefaultFormatKey = "format" // Key for determining the format of the data stored in a Secret, such as fcos or plain-ignition.
13+
FCOSFormat = "fcos" // Specifies the format value used for Fedora CoreOS specific configurations.
1314
)

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func IndexHTTPBootConfigByNetworkIDs(ctx context.Context, mgr ctrl.Manager) erro
354354
return mgr.GetFieldIndexer().IndexField(
355355
ctx,
356356
&bootv1alpha1.HTTPBootConfig{},
357-
bootv1alpha1.SystemIPIndexKey,
357+
bootv1alpha1.NetworkIdentifierIndexKey,
358358
func(Obj client.Object) []string {
359359
HTTPBootConfig := Obj.(*bootv1alpha1.HTTPBootConfig)
360360
return HTTPBootConfig.Spec.NetworkIdentifiers

server/bootserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func handleHTTPBoot(w http.ResponseWriter, r *http.Request, k8sClient client.Cli
393393

394394
var httpBootConfigs bootv1alpha1.HTTPBootConfigList
395395
for _, ip := range clientIPs {
396-
if err := k8sClient.List(ctx, &httpBootConfigs, client.MatchingFields{bootv1alpha1.SystemIPIndexKey: ip}); err != nil {
396+
if err := k8sClient.List(ctx, &httpBootConfigs, client.MatchingFields{bootv1alpha1.NetworkIdentifierIndexKey: ip}); err != nil {
397397
log.Info("Failed to list HTTPBootConfig for IP", "IP", ip, "error", err)
398398
continue
399399
}

server/bootserver_suit_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package server_test
5+
6+
import (
7+
"net/http"
8+
"testing"
9+
10+
"github.com/go-logr/logr"
11+
bootv1alpha1 "github.com/ironcore-dev/boot-operator/api/v1alpha1"
12+
"github.com/ironcore-dev/boot-operator/server"
13+
. "github.com/onsi/ginkgo/v2"
14+
. "github.com/onsi/gomega"
15+
corev1 "k8s.io/api/core/v1"
16+
"k8s.io/apimachinery/pkg/runtime"
17+
"sigs.k8s.io/controller-runtime/pkg/client"
18+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
19+
)
20+
21+
var (
22+
testServerAddr = ":30003"
23+
testServerURL = "http://localhost:30003"
24+
25+
defaultUKIURL = "https://example.com/default.efi"
26+
ipxeServiceURL = "http://localhost:30004"
27+
28+
k8sClient client.Client
29+
)
30+
31+
func TestBootServer(t *testing.T) {
32+
RegisterFailHandler(Fail)
33+
RunSpecs(t, "Boot Server Suite")
34+
}
35+
36+
var _ = BeforeSuite(func() {
37+
scheme := runtime.NewScheme()
38+
Expect(corev1.AddToScheme(scheme)).To(Succeed())
39+
Expect(bootv1alpha1.AddToScheme(scheme)).To(Succeed())
40+
41+
k8sClient = fake.NewClientBuilder().
42+
WithScheme(scheme).
43+
Build()
44+
45+
go func() {
46+
defer GinkgoRecover()
47+
server.RunBootServer(testServerAddr, ipxeServiceURL, k8sClient, logr.Discard(), defaultUKIURL)
48+
}()
49+
50+
Eventually(func() error {
51+
_, err := http.Get(testServerURL + "/httpboot")
52+
return err
53+
}, "5s", "200ms").Should(Succeed())
54+
})

server/bootserver_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package server_test
5+
6+
import (
7+
"encoding/json"
8+
"net/http"
9+
10+
. "github.com/onsi/ginkgo/v2"
11+
. "github.com/onsi/gomega"
12+
)
13+
14+
type httpBootResponse struct {
15+
ClientIPs string `json:"ClientIPs"`
16+
UKIURL string `json:"UKIURL"`
17+
SystemUUID string `json:"SystemUUID,omitempty"`
18+
}
19+
20+
var _ = Describe("BootServer", func() {
21+
Context("/httpboot endpoint", func() {
22+
It("delivers default httpboot data when no HTTPBootConfig matches the client IP", func() {
23+
resp, err := http.Get(testServerURL + "/httpboot")
24+
Expect(err).NotTo(HaveOccurred())
25+
defer func() {
26+
_ = resp.Body.Close()
27+
}()
28+
29+
Expect(resp.StatusCode).To(Equal(http.StatusOK))
30+
Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
31+
32+
var body httpBootResponse
33+
Expect(json.NewDecoder(resp.Body).Decode(&body)).To(Succeed())
34+
35+
By("returning the default UKI URL")
36+
Expect(body.UKIURL).To(Equal(defaultUKIURL))
37+
38+
By("including the recorded client IPs")
39+
Expect(body.ClientIPs).NotTo(BeEmpty())
40+
41+
By("not setting a SystemUUID in the default case")
42+
Expect(body.SystemUUID).To(SatisfyAny(BeEmpty(), Equal("")))
43+
})
44+
})
45+
})

0 commit comments

Comments
 (0)