Skip to content

Commit

Permalink
Added Unit tests for cloud/scope/machine.go (#180)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: Khaja Omer <komer@akamai.com>
  • Loading branch information
komer3 and komer3 authored Mar 12, 2024
1 parent 7179513 commit 0256f43
Show file tree
Hide file tree
Showing 5 changed files with 558 additions and 12 deletions.
8 changes: 1 addition & 7 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (

"github.com/linode/linodego"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

infrav1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
Expand All @@ -37,10 +35,6 @@ type ClusterScopeParams struct {
LinodeCluster *infrav1alpha1.LinodeCluster
}

// var patchNewHelper = patch.NewHelper

type patchHelper func(obj client.Object, crClient client.Client) (*patch.Helper, error)

func validateClusterScopeParams(params ClusterScopeParams) error {
if params.Cluster == nil {
return errors.New("cluster is required when creating a ClusterScope")
Expand All @@ -54,7 +48,7 @@ func validateClusterScopeParams(params ClusterScopeParams) error {

// NewClusterScope creates a new Scope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewClusterScope(ctx context.Context, apiKey string, params ClusterScopeParams, patchNewHelper patchHelper) (*ClusterScope, error) {
func NewClusterScope(ctx context.Context, apiKey string, params ClusterScopeParams, patchNewHelper patchNewHelper) (*ClusterScope, error) {
if err := validateClusterScopeParams(params); err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions cloud/scope/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"github.com/linode/linodego"
"golang.org/x/oauth2"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/linode/cluster-api-provider-linode/version"
)

type patchNewHelper func(obj client.Object, crClient client.Client) (*patch.Helper, error)

func createLinodeClient(apiKey string) (*linodego.Client, error) {
if apiKey == "" {
return nil, errors.New("missing Linode API key")
Expand Down
9 changes: 4 additions & 5 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Expand All @@ -25,9 +24,9 @@ type MachineScopeParams struct {
}

type MachineScope struct {
client client.Client
client k8sClient

PatchHelper *patch.Helper
PatchHelper PatchHelper
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeClient *linodego.Client
Expand All @@ -52,7 +51,7 @@ func validateMachineScopeParams(params MachineScopeParams) error {
return nil
}

func NewMachineScope(ctx context.Context, apiKey string, params MachineScopeParams) (*MachineScope, error) {
func NewMachineScope(ctx context.Context, apiKey string, params MachineScopeParams, patchNewHelper patchNewHelper) (*MachineScope, error) {
if err := validateMachineScopeParams(params); err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,7 +88,7 @@ func NewMachineScope(ctx context.Context, apiKey string, params MachineScopePara
return nil, fmt.Errorf("failed to create linode client: %w", err)
}

helper, err := patch.NewHelper(params.LinodeMachine, params.Client)
helper, err := patchNewHelper(params.LinodeMachine, params.Client)
if err != nil {
return nil, fmt.Errorf("failed to init patch helper: %w", err)
}
Expand Down
Loading

0 comments on commit 0256f43

Please sign in to comment.