-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package server | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
hardikdr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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" | ||
hardikdr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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() | ||
|
|
||
| errCh := make(chan error, 1) | ||
| testLog := logr.Discard() | ||
| go func() { | ||
| defer GinkgoRecover() | ||
| errCh <- RunBootServer(testServerAddr, ipxeServiceURL, k8sClient, testLog, defaultUKIURL) | ||
| }() | ||
|
|
||
| Eventually(func() error { | ||
| select { | ||
| case err := <-errCh: | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return fmt.Errorf("boot server exited unexpectedly without error") | ||
| default: | ||
| } | ||
|
|
||
| resp, err := http.Get(testServerURL + "/httpboot") | ||
| if resp != nil { | ||
| _ = resp.Body.Close() | ||
| } | ||
| return err | ||
| }, "5s", "200ms").Should(Succeed()) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| }, | ||
| } | ||
| _ = k8sClient.Create(context.Background(), secret) | ||
|
|
||
| err := SetStatusCondition( | ||
| context.Background(), | ||
| k8sClient, | ||
| testLog, | ||
| secret, | ||
| "IgnitionDataFetched", | ||
| ) | ||
|
|
||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("unsupported resource type")) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.