From 478d9ce07a9877625e7dc1ecbf3995b917ba1f7b Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Sat, 6 Apr 2024 13:33:20 -0600 Subject: [PATCH] Handle nil values in Hardware spec when templating: If a value used in the template contract is nil, handle properly. Signed-off-by: Jacob Weinstock --- docs/Template.md | 2 +- internal/deprecated/workflow/reconciler.go | 16 ++++++++++++---- internal/deprecated/workflow/reconciler_test.go | 5 +++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/docs/Template.md b/docs/Template.md index 3eda55744..bce0b10fe 100644 --- a/docs/Template.md +++ b/docs/Template.md @@ -66,7 +66,7 @@ There are a number of built in functions that Go provides and that can be used i | Function Name | Description | Use | Examples | | ------------- | ----------- | ------- | -------- | -| `contains` | Contains returns a bool for whether `substr` is within `s`. | `{{ contains "HELLO" "H" }}` | `contains ` | +| `contains` | contains returns a bool for whether `substr` is within `s`. | `{{ contains "HELLO" "H" }}` | `contains ` | | `hasPrefix` | hasPrefix returns a bool for whether the string s begins with prefix. | `{{ hasPrefix "HELLO" "HE" }}` | `hasPrefix ` | | `hasSuffix` | hasSuffix returns a bool for whether the string s ends with suffix. | `{{ hasPrefix "HELLO" "HE" }}` | `hasSuffix ` | | `formatPartition` | formatPartition formats a device path with partition for the specific device type. Supported devices: `/dev/nvme`, `/dev/sd`, `/dev/vd`, `/dev/xvd`, `/dev/hd`. | `{{ formatPartition ( index .Hardware.Disks 0 ) 2 }}` | `formatPartition("/dev/nvme0n1", 0) -> /dev/nvme0n1p1`, `formatPartition("/dev/sda", 1) -> /dev/sda1` | diff --git a/internal/deprecated/workflow/reconciler.go b/internal/deprecated/workflow/reconciler.go index 72dc5f02f..005de573d 100644 --- a/internal/deprecated/workflow/reconciler.go +++ b/internal/deprecated/workflow/reconciler.go @@ -140,10 +140,18 @@ func toTemplateHardwareData(hardware v1alpha1.Hardware) templateHardwareData { for _, disk := range hardware.Spec.Disks { contract.Disks = append(contract.Disks, disk.Device) } - contract.Interfaces = hardware.Spec.Interfaces - contract.UserData = ptr.StringValue(hardware.Spec.UserData) - contract.Metadata = *hardware.Spec.Metadata - contract.VendorData = ptr.StringValue(hardware.Spec.VendorData) + if len(hardware.Spec.Interfaces) > 0 { + contract.Interfaces = hardware.Spec.Interfaces + } + if hardware.Spec.UserData != nil { + contract.UserData = ptr.StringValue(hardware.Spec.UserData) + } + if hardware.Spec.Metadata != nil { + contract.Metadata = *hardware.Spec.Metadata + } + if hardware.Spec.VendorData != nil { + contract.VendorData = ptr.StringValue(hardware.Spec.VendorData) + } return contract } diff --git a/internal/deprecated/workflow/reconciler_test.go b/internal/deprecated/workflow/reconciler_test.go index f88105d3a..a386d3db1 100644 --- a/internal/deprecated/workflow/reconciler_test.go +++ b/internal/deprecated/workflow/reconciler_test.go @@ -3,6 +3,7 @@ package workflow import ( "context" "errors" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -347,7 +348,7 @@ tasks: }, }, }, - wantErr: errors.New("parsing yaml data: yaml: line 2: found character that cannot start any token"), + wantErr: errors.New("found character that cannot start any token"), }, { name: "MissingTemplate", @@ -821,7 +822,7 @@ tasks: if gotErr != nil { if tc.wantErr == nil { t.Errorf(`Got unexpected error: %v"`, gotErr) - } else if gotErr.Error() != tc.wantErr.Error() { + } else if !strings.Contains(gotErr.Error(), tc.wantErr.Error()) { t.Errorf(`Got unexpected error: got "%v" wanted "%v"`, gotErr, tc.wantErr) } return