Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
wip: refactor unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Gozdek <[email protected]>
  • Loading branch information
invidian committed Jan 12, 2021
1 parent 40a5dce commit 55956ee
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 1,039 deletions.
16 changes: 16 additions & 0 deletions controllers/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ func (mrc *machineReconcileContext) ensureHardware() error {
}

func (mrc *machineReconcileContext) hardwareForMachine() (*tinkv1alpha1.Hardware, error) {
alreadySelectedHardwareSelector := []string{
fmt.Sprintf("%s=%s", HardwareOwnerNameLabel, mrc.tinkerbellMachine.Name),
fmt.Sprintf("%s=%s", HardwareOwnerNamespaceLabel, mrc.tinkerbellMachine.Namespace),
}

alreadySelectedHardware, err := nextHardware(mrc.ctx, mrc.client, alreadySelectedHardwareSelector)
if err != nil {
return nil, fmt.Errorf("checking if hardware has already been selected: %w", err)
}

// If we already selected Hardware but we failed to commit this information into TinkerbellMachine object,
// this allows to pick up the process from where we left.
if alreadySelectedHardware != nil {
return alreadySelectedHardware, nil
}

extraSelectors := []string{}

if util.IsControlPlaneMachine(mrc.machine) {
Expand Down
13 changes: 11 additions & 2 deletions controllers/tinkerbellcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,16 @@ const (
)

func nextAvailableHardware(ctx context.Context, k8sClient client.Client, extraSelectors []string) (*tinkv1alpha1.Hardware, error) { //nolint:lll
return nextHardware(ctx, k8sClient, append(extraSelectors, fmt.Sprintf("!%s", HardwareOwnerNameLabel)))
hardware, err := nextHardware(ctx, k8sClient, append(extraSelectors, fmt.Sprintf("!%s", HardwareOwnerNameLabel)))
if err != nil {
return nil, fmt.Errorf("getting next Hardware object: %w", err)
}

if hardware == nil {
return nil, fmt.Errorf("no hardware available")
}

return hardware, nil
}

func nextHardware(ctx context.Context, k8sClient client.Client, selectors []string) (*tinkv1alpha1.Hardware, error) { //nolint:lll
Expand All @@ -169,7 +178,7 @@ func nextHardware(ctx context.Context, k8sClient client.Client, selectors []stri
}

if len(availableHardwares.Items) == 0 {
return nil, fmt.Errorf("no hardware available")
return nil, nil
}

return &availableHardwares.Items[0], nil
Expand Down
Loading

0 comments on commit 55956ee

Please sign in to comment.