Skip to content

Commit

Permalink
Dynamically create test configuration data
Browse files Browse the repository at this point in the history
Do not check in test configuration that small, go for dynamic generation
instead
  • Loading branch information
damyan committed Jan 7, 2025
1 parent 6067ab6 commit 274a51d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 37 deletions.
1 change: 0 additions & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions plugins/pxeboot/malformed_pxeboot_config.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion plugins/pxeboot/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func loadConfig(args ...string) (*api.PxebootConfig, error) {
return nil, fmt.Errorf("invalid configuration: %v", err)
}

log.Debugf("Reading ipam config file %s", path)
log.Debugf("Reading pxeboot config file %s", path)
configData, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %v", err)
Expand Down
125 changes: 98 additions & 27 deletions plugins/pxeboot/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 */
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down
2 changes: 0 additions & 2 deletions plugins/pxeboot/pxeboot_config.yaml

This file was deleted.

0 comments on commit 274a51d

Please sign in to comment.