diff --git a/cloud/services/loadbalancers.go b/cloud/services/loadbalancers.go index a281ed7bf..c194a82a1 100644 --- a/cloud/services/loadbalancers.go +++ b/cloud/services/loadbalancers.go @@ -31,7 +31,11 @@ func CreateNodeBalancer(ctx context.Context, clusterScope *scope.ClusterScope, l Label: NBLabel, Tags: tags, } - linodeNBs, err := clusterScope.LinodeClient.ListNodeBalancers(ctx, linodego.NewListOptions(1, listFilter.String())) + filter, err := listFilter.String() + if err != nil { + return nil, err + } + linodeNBs, err := clusterScope.LinodeClient.ListNodeBalancers(ctx, linodego.NewListOptions(1, filter)) if err != nil { logger.Info("Failed to list NodeBalancers", "error", err.Error()) diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index 74fa36b6d..e8957888e 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -248,7 +248,11 @@ func (r *LinodeMachineReconciler) reconcileCreate( Label: machineScope.LinodeMachine.Name, Tags: tags, } - linodeInstances, err := machineScope.LinodeClient.ListInstances(ctx, linodego.NewListOptions(1, listFilter.String())) + filter, err := listFilter.String() + if err != nil { + return nil, err + } + linodeInstances, err := machineScope.LinodeClient.ListInstances(ctx, linodego.NewListOptions(1, filter)) if err != nil { logger.Error(err, "Failed to list Linode machine instances") diff --git a/controller/linodevpc_controller_helpers.go b/controller/linodevpc_controller_helpers.go index 99eaf3552..e382bf33e 100644 --- a/controller/linodevpc_controller_helpers.go +++ b/controller/linodevpc_controller_helpers.go @@ -48,7 +48,11 @@ func (r *LinodeVPCReconciler) reconcileVPC(ctx context.Context, vpcScope *scope. Label: createConfig.Label, Tags: nil, } - if vpcs, err := vpcScope.LinodeClient.ListVPCs(ctx, linodego.NewListOptions(1, listFilter.String())); err != nil { + filter, err := listFilter.String() + if err != nil { + return err + } + if vpcs, err := vpcScope.LinodeClient.ListVPCs(ctx, linodego.NewListOptions(1, filter)); err != nil { logger.Error(err, "Failed to list VPCs") return err diff --git a/util/filter.go b/util/filter.go index 509f6dfe9..77aa972b3 100644 --- a/util/filter.go +++ b/util/filter.go @@ -35,11 +35,11 @@ func (f Filter) MarshalJSON() ([]byte, error) { // String returns the string representation of the encoded value from // [Filter.MarshalJSON]. -func (f Filter) String() string { +func (f Filter) String() (string, error) { p, err := f.MarshalJSON() if err != nil { - panic("this should not have failed") + return "", err } - return string(p) + return string(p), nil } diff --git a/util/filter_test.go b/util/filter_test.go new file mode 100644 index 000000000..c2d03c295 --- /dev/null +++ b/util/filter_test.go @@ -0,0 +1,54 @@ +package util + +import "testing" + +func TestString(t *testing.T) { + t.Parallel() + tests := []struct { + name string + filter Filter + expectErr bool + }{{ + name: "success ID", + filter: Filter{ + ID: Pointer(123), + Label: "", + Tags: nil, + }, + expectErr: false, + }, { + name: "success label", + filter: Filter{ + ID: nil, + Label: "test", + Tags: nil, + }, + expectErr: false, + }, { + name: "success tags", + filter: Filter{ + ID: nil, + Label: "", + Tags: []string{"testtag"}, + }, + expectErr: false, + }, { + name: "failure unmarshal", + filter: Filter{ + ID: nil, + Label: "", + Tags: []string{}, + }, + expectErr: true, + }} + for _, tt := range tests { + testcase := tt + t.Run(testcase.name, func(t *testing.T) { + t.Parallel() + _, err := testcase.filter.String() + if testcase.expectErr && err != nil { + t.Error("expected err but got nil") + } + }) + } +} diff --git a/version/version_test.go b/version/version_test.go new file mode 100644 index 000000000..28581d2cb --- /dev/null +++ b/version/version_test.go @@ -0,0 +1,11 @@ +package version + +import "testing" + +func TestVersion(t *testing.T) { + t.Parallel() + vers := GetVersion() + if vers != "dev" { + t.Errorf("unset version should be dev, got %s", vers) + } +}