Skip to content

Commit

Permalink
Split TestReconcileBootstrapData_Formats into CloudConfig and Ignitio…
Browse files Browse the repository at this point in the history
…n variants, add format to createBootstrapSecret helper function
  • Loading branch information
rybnico authored and wikkyk committed Jan 16, 2025
1 parent e5c1dff commit 9beeb31
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
49 changes: 30 additions & 19 deletions internal/service/vmservice/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1alpha1 "github.com/ionos-cloud/cluster-api-provider-proxmox/api/v1alpha1"
"github.com/ionos-cloud/cluster-api-provider-proxmox/internal/inject"
Expand Down Expand Up @@ -72,7 +71,7 @@ func TestReconcileBootstrapData_NoNetworkConfig_UpdateStatus(t *testing.T) {
machineScope.SetVirtualMachine(vm)
machineScope.ProxmoxMachine.Status.IPAddresses = map[string]infrav1alpha1.IPAddress{infrav1alpha1.DefaultNetworkDevice: {IPV4: "10.10.10.10"}}
createIP4AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "10.10.10.10")
createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)

requeue, err := reconcileBootstrapData(context.Background(), machineScope)
require.NoError(t, err)
Expand Down Expand Up @@ -100,7 +99,7 @@ func TestReconcileBootstrapData_UpdateStatus(t *testing.T) {
machineScope.ProxmoxMachine.Status.IPAddresses = map[string]infrav1alpha1.IPAddress{infrav1alpha1.DefaultNetworkDevice: {IPV4: "10.10.10.10"}, "net1": {IPV4: "10.100.10.10"}}
createIP4AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "10.10.10.10")
createIP4AddressResource(t, kubeClient, machineScope, "net1", "10.100.10.10")
createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)
getISOInjector = func(_ *proxmox.VirtualMachine, _ []byte, _, _ cloudinit.Renderer) isoInjector {
return FakeISOInjector{}
}
Expand All @@ -119,7 +118,7 @@ func TestReconcileBootstrapData_BadInjector(t *testing.T) {
machineScope.SetVirtualMachine(vm)
machineScope.ProxmoxMachine.Status.IPAddresses = map[string]infrav1alpha1.IPAddress{infrav1alpha1.DefaultNetworkDevice: {IPV4: "10.10.10.10"}}
createIP4AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "10.10.10.10")
createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)

getISOInjector = func(_ *proxmox.VirtualMachine, _ []byte, _, _ cloudinit.Renderer) isoInjector {
return FakeISOInjector{Error: errors.New("bad FakeISOInjector")}
Expand Down Expand Up @@ -336,7 +335,7 @@ func TestReconcileBootstrapData_DualStack(t *testing.T) {
createIP4AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "10.10.10.10")
createIP6AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "2001:db8::2")

createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)
getISOInjector = func(_ *proxmox.VirtualMachine, _ []byte, _, _ cloudinit.Renderer) isoInjector {
return FakeISOInjector{}
}
Expand Down Expand Up @@ -388,7 +387,7 @@ func TestReconcileBootstrapData_DualStack_AdditionalDevices(t *testing.T) {
createIP6AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "2001:db8::2")
createIP4AddressResource(t, kubeClient, machineScope, "net1", "10.0.0.10")
createIP6AddressResource(t, kubeClient, machineScope, "net1", "2001:db8::9")
createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)
getISOInjector = func(_ *proxmox.VirtualMachine, _ []byte, _, _ cloudinit.Renderer) isoInjector {
return FakeISOInjector{}
}
Expand Down Expand Up @@ -433,7 +432,7 @@ func TestReconcileBootstrapData_VirtualDevices_VRF(t *testing.T) {
createIP4AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "10.10.10.10")
createIP4AddressResource(t, kubeClient, machineScope, "net1", "10.100.10.10")

createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)
getISOInjector = func(_ *proxmox.VirtualMachine, _ []byte, _, _ cloudinit.Renderer) isoInjector {
return FakeISOInjector{}
}
Expand Down Expand Up @@ -479,7 +478,7 @@ func TestReconcileBootstrapDataMissingNetworkConfig(t *testing.T) {
machineScope.SetVirtualMachine(vm)

machineScope.ProxmoxMachine.Status.IPAddresses = map[string]infrav1alpha1.IPAddress{infrav1alpha1.DefaultNetworkDevice: {IPV4: "10.10.10.10"}}
createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)

requeue, err := reconcileBootstrapData(context.Background(), machineScope)
require.Error(t, err)
Expand All @@ -489,16 +488,17 @@ func TestReconcileBootstrapDataMissingNetworkConfig(t *testing.T) {
require.True(t, conditions.GetReason(machineScope.ProxmoxMachine, infrav1alpha1.VMProvisionedCondition) == infrav1alpha1.WaitingForStaticIPAllocationReason)
}

func TestReconcileBootstrapData_Formats(t *testing.T) {
func TestReconcileBootstrapData_Format_CloudConfig(t *testing.T) {
machineScope, _, kubeClient := setupReconcilerTest(t)

vm := newVMWithNets("virtio=A6:23:64:4D:84:CB,bridge=vmbr0")
vm.VirtualMachineConfig.SMBios1 = biosUUID
machineScope.SetVirtualMachine(vm)
machineScope.ProxmoxMachine.Status.IPAddresses = map[string]infrav1alpha1.IPAddress{infrav1alpha1.DefaultNetworkDevice: {IPV4: "10.10.10.10"}}
createIP4AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "10.10.10.10")
createBootstrapSecret(t, kubeClient, machineScope)
createBootstrapSecret(t, kubeClient, machineScope, cloudinit.FormatCloudConfig)
machineScope.SetVirtualMachine(vm)

getISOInjector = func(_ *proxmox.VirtualMachine, _ []byte, _, _ cloudinit.Renderer) isoInjector {
return FakeISOInjector{}
}
Expand All @@ -514,21 +514,32 @@ func TestReconcileBootstrapData_Formats(t *testing.T) {
require.Equal(t, cloudinit.FormatCloudConfig, ptr.Deref(format, ""))
require.Equal(t, []byte("data"), data)
require.Nil(t, err)
}

// test explicitly setting format to ignition
var dataSecret corev1.Secret
err = kubeClient.Get(context.Background(), client.ObjectKey{Namespace: machineScope.Machine.Namespace, Name: ptr.Deref(machineScope.Machine.Spec.Bootstrap.DataSecretName, "")}, &dataSecret)
require.NoError(t, err)
dataSecret.Data["format"] = []byte(ignition.FormatIgnition)
require.NoError(t, kubeClient.Update(context.Background(), &dataSecret))
func TestReconcileBootstrapData_Format_Ignition(t *testing.T) {
machineScope, _, kubeClient := setupReconcilerTest(t)

vm := newVMWithNets("virtio=A6:23:64:4D:84:CB,bridge=vmbr0")
vm.VirtualMachineConfig.SMBios1 = biosUUID
machineScope.SetVirtualMachine(vm)
machineScope.ProxmoxMachine.Status.IPAddresses = map[string]infrav1alpha1.IPAddress{infrav1alpha1.DefaultNetworkDevice: {IPV4: "10.10.10.10"}}
createIP4AddressResource(t, kubeClient, machineScope, infrav1alpha1.DefaultNetworkDevice, "10.10.10.10")
createBootstrapSecret(t, kubeClient, machineScope, ignition.FormatIgnition)
machineScope.SetVirtualMachine(vm)

getIgnitionISOInjector = func(_ *proxmox.VirtualMachine, _ cloudinit.Renderer, _ *ignition.Enricher) isoInjector {
return FakeIgnitionISOInjector{}
}
t.Cleanup(func() { getISOInjector = defaultISOInjector })

_, err = reconcileBootstrapData(context.Background(), machineScope)
requeue, err := reconcileBootstrapData(context.Background(), machineScope)
require.NoError(t, err)
require.False(t, requeue)
require.True(t, *machineScope.ProxmoxMachine.Status.BootstrapDataProvided)

data, format, err = getBootstrapData(context.Background(), machineScope)
data, format, err := getBootstrapData(context.Background(), machineScope)
require.Equal(t, ignition.FormatIgnition, ptr.Deref(format, ""))
require.Equal(t, []byte("data"), data)
require.Equal(t, []byte("{\"ignition\":{\"version\":\"2.3.0\"}}"), data)
require.Nil(t, err)
}

Expand Down
30 changes: 25 additions & 5 deletions internal/service/vmservice/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (

infrav1alpha1 "github.com/ionos-cloud/cluster-api-provider-proxmox/api/v1alpha1"
"github.com/ionos-cloud/cluster-api-provider-proxmox/internal/inject"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/cloudinit"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/ignition"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/kubernetes/ipam"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/proxmox/proxmoxtest"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
Expand All @@ -51,6 +53,14 @@ func (f FakeISOInjector) Inject(_ context.Context, _ inject.BootstrapDataFormat)
return f.Error
}

type FakeIgnitionISOInjector struct {
Error error
}

func (f FakeIgnitionISOInjector) Inject(_ context.Context, _ inject.BootstrapDataFormat) error {
return f.Error
}

// setupReconcilerTest initializes a MachineScope with a mock Proxmox client and a fake controller-runtime client.
func setupReconcilerTest(t *testing.T) (*scope.MachineScope, *proxmoxtest.MockClient, client.Client) {
cluster := &clusterv1.Cluster{
Expand Down Expand Up @@ -209,17 +219,27 @@ func createIPPools(t *testing.T, c client.Client, machineScope *scope.MachineSco
}
}

func createBootstrapSecret(t *testing.T, c client.Client, machineScope *scope.MachineScope) {
func createBootstrapSecret(t *testing.T, c client.Client, machineScope *scope.MachineScope, format string) {
machineScope.Machine.Spec.Bootstrap.DataSecretName = ptr.To(machineScope.Name())
data := map[string][]byte{}
switch format {
case cloudinit.FormatCloudConfig:
data = map[string][]byte{
"value": []byte("data"),
"format": []byte("cloud-config"),
}
case ignition.FormatIgnition:
data = map[string][]byte{
"value": []byte("{\"ignition\":{\"version\":\"2.3.0\"}}"),
"format": []byte("ignition"),
}
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: machineScope.Name(),
Namespace: machineScope.Namespace(),
},
Data: map[string][]byte{
"value": []byte("data"),
"format": []byte("cloud-config"),
},
Data: data,
}
require.NoError(t, c.Create(context.Background(), secret))
}
Expand Down

0 comments on commit 9beeb31

Please sign in to comment.