|
| 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 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "github.com/go-logr/logr" |
| 10 | + "net/http" |
| 11 | + |
| 12 | + bootv1alpha1 "github.com/ironcore-dev/boot-operator/api/v1alpha1" |
| 13 | + |
| 14 | + . "github.com/onsi/ginkgo/v2" |
| 15 | + . "github.com/onsi/gomega" |
| 16 | + corev1 "k8s.io/api/core/v1" |
| 17 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | +) |
| 19 | + |
| 20 | +type httpBootResponse struct { |
| 21 | + ClientIPs string `json:"ClientIPs"` |
| 22 | + UKIURL string `json:"UKIURL"` |
| 23 | + SystemUUID string `json:"SystemUUID,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +var _ = Describe("BootServer", func() { |
| 27 | + Context("/httpboot endpoint", func() { |
| 28 | + It("delivers default httpboot data when no HTTPBootConfig matches the client IP", func() { |
| 29 | + resp, err := http.Get(testServerURL + "/httpboot") |
| 30 | + Expect(err).NotTo(HaveOccurred()) |
| 31 | + defer func() { |
| 32 | + _ = resp.Body.Close() |
| 33 | + }() |
| 34 | + |
| 35 | + Expect(resp.StatusCode).To(Equal(http.StatusOK)) |
| 36 | + Expect(resp.Header.Get("Content-Type")).To(Equal("application/json")) |
| 37 | + |
| 38 | + var body httpBootResponse |
| 39 | + Expect(json.NewDecoder(resp.Body).Decode(&body)).To(Succeed()) |
| 40 | + |
| 41 | + By("returning the default UKI URL") |
| 42 | + Expect(body.UKIURL).To(Equal(defaultUKIURL)) |
| 43 | + |
| 44 | + By("including the recorded client IPs") |
| 45 | + Expect(body.ClientIPs).NotTo(BeEmpty()) |
| 46 | + |
| 47 | + By("not setting a SystemUUID in the default case") |
| 48 | + Expect(body.SystemUUID).To(SatisfyAny(BeEmpty(), Equal(""))) |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + It("converts valid Butane YAML to JSON", func() { |
| 53 | + butaneYAML := []byte(` |
| 54 | +variant: fcos |
| 55 | +version: 1.5.0 |
| 56 | +systemd: |
| 57 | + units: |
| 58 | + - name: test.service |
| 59 | + enabled: true |
| 60 | +`) |
| 61 | + |
| 62 | + jsonData, err := renderIgnition(butaneYAML) |
| 63 | + Expect(err).ToNot(HaveOccurred()) |
| 64 | + Expect(jsonData).ToNot(BeEmpty()) |
| 65 | + Expect(string(jsonData)).To(ContainSubstring(`"systemd"`)) |
| 66 | + }) |
| 67 | + |
| 68 | + It("returns an error for invalid YAML", func() { |
| 69 | + bad := []byte("this ::: is not yaml") |
| 70 | + _, err := renderIgnition(bad) |
| 71 | + Expect(err).To(HaveOccurred()) |
| 72 | + }) |
| 73 | + |
| 74 | + Context("Verify the SetStatusCondition method", func() { |
| 75 | + |
| 76 | + var testLog = logr.Discard() |
| 77 | + |
| 78 | + It("returns an error for unknown condition type", func() { |
| 79 | + cfg := &bootv1alpha1.IPXEBootConfig{ |
| 80 | + ObjectMeta: v1.ObjectMeta{ |
| 81 | + Name: "unknown-cond", |
| 82 | + Namespace: "default", |
| 83 | + }, |
| 84 | + } |
| 85 | + Expect(k8sClient.Create(context.Background(), cfg)).To(Succeed()) |
| 86 | + |
| 87 | + err := SetStatusCondition( |
| 88 | + context.Background(), |
| 89 | + k8sClient, |
| 90 | + testLog, |
| 91 | + cfg, |
| 92 | + "DoesNotExist", |
| 93 | + ) |
| 94 | + |
| 95 | + Expect(err).To(HaveOccurred()) |
| 96 | + Expect(err.Error()).To(ContainSubstring("condition type DoesNotExist not found")) |
| 97 | + }) |
| 98 | + |
| 99 | + It("returns an error for unsupported resource types", func() { |
| 100 | + secret := &corev1.Secret{ |
| 101 | + ObjectMeta: v1.ObjectMeta{ |
| 102 | + Name: "bad-type", |
| 103 | + Namespace: "default", |
| 104 | + }, |
| 105 | + } |
| 106 | + _ = k8sClient.Create(context.Background(), secret) |
| 107 | + |
| 108 | + err := SetStatusCondition( |
| 109 | + context.Background(), |
| 110 | + k8sClient, |
| 111 | + testLog, |
| 112 | + secret, |
| 113 | + "IgnitionDataFetched", |
| 114 | + ) |
| 115 | + |
| 116 | + Expect(err).To(HaveOccurred()) |
| 117 | + Expect(err.Error()).To(ContainSubstring("unsupported resource type")) |
| 118 | + }) |
| 119 | + }) |
| 120 | +}) |
0 commit comments