From a4862754b8e4d40e20fcb272d84f700e14cd93a1 Mon Sep 17 00:00:00 2001 From: Damyan Yordanov Date: Tue, 7 Jan 2025 15:06:05 +0100 Subject: [PATCH] Dynamically create test configuration data Do not check in test configuration that small, go for dynamic generation instead --- .reuse/dep5 | 1 - README.md | 6 +- plugins/pxeboot/malformed_pxeboot_config.yaml | 2 - plugins/pxeboot/plugin_test.go | 125 ++++++++++++++---- 4 files changed, 100 insertions(+), 34 deletions(-) delete mode 100644 plugins/pxeboot/malformed_pxeboot_config.yaml diff --git a/.reuse/dep5 b/.reuse/dep5 index 2408c88..81c5ca6 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -17,7 +17,6 @@ Files: go.mod go.sum hack/* - plugins/pxeboot/*.yaml Copyright: 2024 SAP SE or an SAP affiliate company and IronCore contributors License: MIT diff --git a/README.md b/README.md index a502107..18f454b 100644 --- a/README.md +++ b/README.md @@ -90,9 +90,7 @@ subnetLabel: subnet=dhcp The Metal plugin acts as a connection link between DHCP and the IronCore metal stack. It creates an `EndPoint` object for each machine with leased IP address. Those endpoints are then consumed by the metal operator, who then creates the corresponding `Machine` objects. ### Configuration -The metal configuration consists of an inventory list. Currently, there are two different ways to provide an inventory list: either by specifying a MAC address filter or by providing the inventory list explicitly. If both a static list and a filter are specified in the `metal_config.yaml`, the static list gets a precedence, so the filter will be ignored. - -Providing an explicit static inventory list in `metal_config.yaml` goes as follows: +The metal configuration consists of an inventory list. Currently, there are two different ways to provide an inventory list: either by specifying a MAC address filter or by providing the inventory list explicitly. If both a static list and a filter are specified in the `metal_config.yaml`, the static list gets a precedence, so the filter will be ignored. Providing an explicit static inventory list in `metal_config.yaml` goes as follows: ```yaml hosts: - name: server-01 @@ -123,7 +121,7 @@ The PXEBoot plugin implements an (i)PXE network boot. When configured properly, the PXEBoot plugin will [break the PXE chainloading loop](https://ipxe.org/howto/dhcpd#pxe_chainloading). In such a way legacy PXE clients will be handed out an iPXE environment, whereas iPXE clients (classified based on the user class for [IPv6](https://datatracker.ietf.org/doc/html/rfc8415#section-21.15) and [IPv4](https://www.rfc-editor.org/rfc/rfc3004.html#section-4)) will get the HTTP PXE boot script. ### Configuration -Two parameters shall be passed as strings specified in the `pxeboot_config.yaml` an TFTP address to an iPXE environment and an HTTP(s) boot script address. +A TFTP address to an iPXE environment and an HTTP(s) boot script address shall be specified. Providing those in the `pxeboot_config.yaml` goes as follows: ````yaml tftpServer: tftp://[2001:db8::1]/ipxe/x86_64/ipxe diff --git a/plugins/pxeboot/malformed_pxeboot_config.yaml b/plugins/pxeboot/malformed_pxeboot_config.yaml deleted file mode 100644 index e263641..0000000 --- a/plugins/pxeboot/malformed_pxeboot_config.yaml +++ /dev/null @@ -1,2 +0,0 @@ -tftpServer: tftp://1.2.3.4/ -ipxeServer: httpfoo://www.example.com \ No newline at end of file diff --git a/plugins/pxeboot/plugin_test.go b/plugins/pxeboot/plugin_test.go index 58bca29..81c8eb9 100644 --- a/plugins/pxeboot/plugin_test.go +++ b/plugins/pxeboot/plugin_test.go @@ -6,11 +6,16 @@ package pxeboot import ( "net" "net/url" + "os" "testing" "github.com/insomniacslk/dhcp/dhcpv4" + "github.com/insomniacslk/dhcp/dhcpv6" "github.com/insomniacslk/dhcp/iana" + + "github.com/ironcore-dev/fedhcp/internal/api" + "gopkg.in/yaml.v3" ) const ( @@ -20,23 +25,65 @@ const ( var ( numberOptsBootFileURL int - pxebootFilePath = []string{"pxeboot_config.yaml"} + tempConfigFilePattern = "*-pxeboot_config.yaml" + validConfig = &api.PxebootConfig{ + TFTPServer: tftpPath, + IPXEServer: ipxePath, + } ) -func Init4() { - _, err := setup4(pxebootFilePath...) +func createTempConfig(config api.PxebootConfig, tempDir string) (string, error) { + configData, err := yaml.Marshal(config) + if err != nil { + return "", err + } + + file, err := os.CreateTemp(tempDir, tempConfigFilePattern) if err != nil { - log.Fatal(err) + return "", err } + defer func() { + _ = file.Close() + }() + + configFile := file.Name() + + err = os.WriteFile(configFile, configData, 0644) + if err != nil { + return "", err + } + + return configFile, nil } -func Init6(numOptBoot int) { +func Init4(config api.PxebootConfig, tempDir string) error { + configFile, err := createTempConfig(config, tempDir) + if err != nil { + return err + } + + _, err = setup4(configFile) + if err != nil { + return err + } + + return nil +} + +func Init6(config api.PxebootConfig, tempDir string, numOptBoot int) error { numberOptsBootFileURL = numOptBoot - _, err := setup6(pxebootFilePath...) + configFile, err := createTempConfig(config, tempDir) + if err != nil { + return err + } + + _, err = setup6(configFile) if err != nil { - log.Fatal(err) + return err } + + return err } /* parametrization */ @@ -54,12 +101,14 @@ func TestWrongNumberArgs(t *testing.T) { } func TestWrongArgs(t *testing.T) { - malformedTFTPPath := []string{"tftp://1.2.3.4/", "foo://1.2.3.4/boot.efi"} - malformedIPXEPath := []string{"httpfoo://www.example.com", "https:/1.2.3"} - malformedPxebootFilePath := []string{"malformed_pxeboot_config.yaml"} - + malformedTFTPPath := []string{"tftp://example.com", "tftp:/example.com/boot.efi", "foo://example.com/boot.efi"} for _, wrongTFTP := range malformedTFTPPath { - _, err := setup4(malformedPxebootFilePath...) + config := &api.PxebootConfig{ + TFTPServer: wrongTFTP, + IPXEServer: ipxePath, + } + tempDir := t.TempDir() + err := Init4(*config, tempDir) if err == nil { t.Fatalf("no error occurred when providing wrong TFTP path %s, but it should have", wrongTFTP) } @@ -73,7 +122,7 @@ func TestWrongArgs(t *testing.T) { t.Fatalf("IPXE boot file was set when providing wrong TFTP path %s, but it should be empty", wrongTFTP) } - _, err = setup6(malformedPxebootFilePath...) + err = Init6(*config, tempDir, 0) if err == nil { t.Fatalf("no error occurred when providing wrong TFTP path %s, but it should have", wrongTFTP) } @@ -85,8 +134,18 @@ func TestWrongArgs(t *testing.T) { } } + malformedIPXEPath := []string{"https://example.com", "http:/www.example.com/boot.ipxe", "foo://example.com/boot.ipxe"} for _, wrongIPXE := range malformedIPXEPath { - _, err := setup4(malformedPxebootFilePath...) + config := &api.PxebootConfig{ + TFTPServer: tftpPath, + IPXEServer: wrongIPXE, + } + tempDir := t.TempDir() + err := Init4(*config, tempDir) + if err == nil { + t.Fatalf("no error occurred when providing wrong IPXE path %s, but it should have", wrongIPXE) + } + err = Init4(*config, tempDir) if err == nil { t.Fatalf("no error occurred when providing wrong IPXE path %s, but it should have", wrongIPXE) } @@ -100,7 +159,7 @@ func TestWrongArgs(t *testing.T) { t.Fatalf("IPXE boot file was set when providing wrong IPXE path %s, but it should be empty", wrongIPXE) } - _, err = setup6(malformedPxebootFilePath...) + err = Init6(*config, tempDir, 0) if err == nil { t.Fatalf("no error occurred when providing wrong IPXE path %s, but it should have", wrongIPXE) } @@ -116,7 +175,8 @@ func TestWrongArgs(t *testing.T) { /* IPv6 */ func TestPXERequested6(t *testing.T) { - Init6(1) + tempDir := t.TempDir() + _ = Init6(*validConfig, tempDir, 1) req, err := dhcpv6.NewMessage() if err != nil { @@ -158,7 +218,8 @@ func TestPXERequested6(t *testing.T) { } func TestTFTPRequested6(t *testing.T) { - Init6(1) + tempDir := t.TempDir() + _ = Init6(*validConfig, tempDir, 1) req, err := dhcpv6.NewMessage() if err != nil { @@ -195,7 +256,8 @@ func TestTFTPRequested6(t *testing.T) { } func TestWrongPXERequested6(t *testing.T) { - Init6(0) + tempDir := t.TempDir() + _ = Init6(*validConfig, tempDir, 0) req, err := dhcpv6.NewMessage() if err != nil { @@ -232,7 +294,8 @@ func TestWrongPXERequested6(t *testing.T) { } func TestWrongTFTPRequested6(t *testing.T) { - Init6(0) + tempDir := t.TempDir() + _ = Init6(*validConfig, tempDir, 0) req, err := dhcpv6.NewMessage() if err != nil { @@ -264,7 +327,8 @@ func TestWrongTFTPRequested6(t *testing.T) { } func TestPXENotRequested6(t *testing.T) { - Init6(0) + tempDir := t.TempDir() + _ = Init6(*validConfig, tempDir, 0) req, err := dhcpv6.NewMessage() if err != nil { @@ -294,7 +358,8 @@ func TestPXENotRequested6(t *testing.T) { } func TestTFTPNotRequested6(t *testing.T) { - Init6(0) + tempDir := t.TempDir() + _ = Init6(*validConfig, tempDir, 0) req, err := dhcpv6.NewMessage() if err != nil { @@ -326,7 +391,8 @@ func TestTFTPNotRequested6(t *testing.T) { /* IPV4 */ func TestPXERequested4(t *testing.T) { - Init4() + tempDir := t.TempDir() + _ = Init4(*validConfig, tempDir) req, err := dhcpv4.NewDiscovery(net.HardwareAddr{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, @@ -360,7 +426,8 @@ func TestPXERequested4(t *testing.T) { } func TestTFTPRequested4(t *testing.T) { - Init4() + tempDir := t.TempDir() + _ = Init4(*validConfig, tempDir) req, err := dhcpv4.NewDiscovery(net.HardwareAddr{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, @@ -401,7 +468,8 @@ func TestTFTPRequested4(t *testing.T) { } func TestPXENotRequested4(t *testing.T) { - Init4() + tempDir := t.TempDir() + _ = Init4(*validConfig, tempDir) req, err := dhcpv4.NewDiscovery(net.HardwareAddr{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, @@ -432,7 +500,8 @@ func TestPXENotRequested4(t *testing.T) { } func TestTFTPNotRequested4(t *testing.T) { - Init4() + tempDir := t.TempDir() + _ = Init4(*validConfig, tempDir) req, err := dhcpv4.NewDiscovery(net.HardwareAddr{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, @@ -467,7 +536,8 @@ func TestTFTPNotRequested4(t *testing.T) { } func TestWrongPXERequested4(t *testing.T) { - Init4() + tempDir := t.TempDir() + _ = Init4(*validConfig, tempDir) req, err := dhcpv4.NewDiscovery(net.HardwareAddr{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, @@ -501,7 +571,8 @@ func TestWrongPXERequested4(t *testing.T) { } func TestWrongTFTPRequested4(t *testing.T) { - Init4() + tempDir := t.TempDir() + _ = Init4(*validConfig, tempDir) req, err := dhcpv4.NewDiscovery(net.HardwareAddr{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},