-
Notifications
You must be signed in to change notification settings - Fork 4
Add Test Framework for BootServer #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||||||||||||||||||||
| // SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors | ||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| package server | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||
| "testing" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "github.com/go-logr/logr" | ||||||||||||||||||||||||||||||||
| bootv1alpha1 "github.com/ironcore-dev/boot-operator/api/v1alpha1" | ||||||||||||||||||||||||||||||||
| . "github.com/onsi/ginkgo/v2" | ||||||||||||||||||||||||||||||||
| . "github.com/onsi/gomega" | ||||||||||||||||||||||||||||||||
| corev1 "k8s.io/api/core/v1" | ||||||||||||||||||||||||||||||||
| "k8s.io/apimachinery/pkg/runtime" | ||||||||||||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||||||||||||||||||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| var ( | ||||||||||||||||||||||||||||||||
| testServerAddr = ":30003" | ||||||||||||||||||||||||||||||||
| testServerURL = "http://localhost:30003" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defaultUKIURL = "https://example.com/default.efi" | ||||||||||||||||||||||||||||||||
| ipxeServiceURL = "http://localhost:30004" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| k8sClient client.Client | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func TestBootServer(t *testing.T) { | ||||||||||||||||||||||||||||||||
| RegisterFailHandler(Fail) | ||||||||||||||||||||||||||||||||
| RunSpecs(t, "Boot Server Suite") | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| var _ = BeforeSuite(func() { | ||||||||||||||||||||||||||||||||
| scheme := runtime.NewScheme() | ||||||||||||||||||||||||||||||||
| Expect(corev1.AddToScheme(scheme)).To(Succeed()) | ||||||||||||||||||||||||||||||||
| Expect(bootv1alpha1.AddToScheme(scheme)).To(Succeed()) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| k8sClient = fake.NewClientBuilder(). | ||||||||||||||||||||||||||||||||
| WithScheme(scheme). | ||||||||||||||||||||||||||||||||
| Build() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||||
| defer GinkgoRecover() | ||||||||||||||||||||||||||||||||
| RunBootServer(testServerAddr, ipxeServiceURL, k8sClient, logr.Discard(), defaultUKIURL) | ||||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fake client lacks field indexer for The fake client built with You need to configure the fake client with the index using 🔧 Proposed fix k8sClient = fake.NewClientBuilder().
WithScheme(scheme).
+ WithIndex(&bootv1alpha1.HTTPBootConfig{}, bootv1alpha1.NetworkIdentifierIndexKey, func(obj client.Object) []string {
+ config := obj.(*bootv1alpha1.HTTPBootConfig)
+ return config.Spec.NetworkIdentifiers
+ }).
Build()📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Eventually(func() error { | ||||||||||||||||||||||||||||||||
| _, err := http.Get(testServerURL + "/httpboot") | ||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||
| }, "5s", "200ms").Should(Succeed()) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| // SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| bootv1alpha1 "github.com/ironcore-dev/boot-operator/api/v1alpha1" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| corev1 "k8s.io/api/core/v1" | ||
| v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| type httpBootResponse struct { | ||
| ClientIPs string `json:"ClientIPs"` | ||
| UKIURL string `json:"UKIURL"` | ||
| SystemUUID string `json:"SystemUUID,omitempty"` | ||
| } | ||
|
|
||
| var _ = Describe("BootServer", func() { | ||
| Context("/httpboot endpoint", func() { | ||
| It("delivers default httpboot data when no HTTPBootConfig matches the client IP", func() { | ||
| resp, err := http.Get(testServerURL + "/httpboot") | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| defer func() { | ||
| _ = resp.Body.Close() | ||
| }() | ||
|
|
||
| Expect(resp.StatusCode).To(Equal(http.StatusOK)) | ||
| Expect(resp.Header.Get("Content-Type")).To(Equal("application/json")) | ||
|
|
||
| var body httpBootResponse | ||
| Expect(json.NewDecoder(resp.Body).Decode(&body)).To(Succeed()) | ||
|
|
||
| By("returning the default UKI URL") | ||
| Expect(body.UKIURL).To(Equal(defaultUKIURL)) | ||
|
|
||
| By("including the recorded client IPs") | ||
| Expect(body.ClientIPs).NotTo(BeEmpty()) | ||
|
|
||
| By("not setting a SystemUUID in the default case") | ||
| Expect(body.SystemUUID).To(SatisfyAny(BeEmpty(), Equal(""))) | ||
| }) | ||
| }) | ||
|
|
||
| It("converts valid Butane YAML to JSON", func() { | ||
| butaneYAML := []byte(` | ||
| variant: fcos | ||
| version: 1.5.0 | ||
| systemd: | ||
| units: | ||
| - name: test.service | ||
| enabled: true | ||
| `) | ||
|
|
||
| jsonData, err := renderIgnition(butaneYAML) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(jsonData).ToNot(BeEmpty()) | ||
| Expect(string(jsonData)).To(ContainSubstring(`"systemd"`)) | ||
| }) | ||
|
|
||
| It("returns an error for invalid YAML", func() { | ||
| bad := []byte("this ::: is not yaml") | ||
| _, err := renderIgnition(bad) | ||
| Expect(err).To(HaveOccurred()) | ||
| }) | ||
|
|
||
| Context("Verify the SetStatusCondition method", func() { | ||
|
|
||
| var testLog = logr.Discard() | ||
|
|
||
| It("returns an error for unknown condition type", func() { | ||
| cfg := &bootv1alpha1.IPXEBootConfig{ | ||
| ObjectMeta: v1.ObjectMeta{ | ||
| Name: "unknown-cond", | ||
| Namespace: "default", | ||
| }, | ||
| } | ||
| Expect(k8sClient.Create(context.Background(), cfg)).To(Succeed()) | ||
|
|
||
| err := SetStatusCondition( | ||
| context.Background(), | ||
| k8sClient, | ||
| testLog, | ||
| cfg, | ||
| "DoesNotExist", | ||
| ) | ||
|
|
||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("condition type DoesNotExist not found")) | ||
| }) | ||
|
|
||
| It("returns an error for unsupported resource types", func() { | ||
| secret := &corev1.Secret{ | ||
| ObjectMeta: v1.ObjectMeta{ | ||
| Name: "bad-type", | ||
| Namespace: "default", | ||
| }, | ||
| } | ||
| Expect(k8sClient.Create(context.Background(), secret)).To(Succeed()) | ||
|
|
||
| err := SetStatusCondition( | ||
| context.Background(), | ||
| k8sClient, | ||
| testLog, | ||
| secret, | ||
| "IgnitionDataFetched", | ||
| ) | ||
|
|
||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("unsupported resource type")) | ||
| }) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.