diff --git a/.gitignore b/.gitignore index dbd2a29c3..6b9366061 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ .idea bin/* kind-logs-* -cover.out +cover.out* kubeconfig* .devbox/* docs/book diff --git a/Makefile b/Makefile index 98fef737d..f2b8639ac 100644 --- a/Makefile +++ b/Makefile @@ -140,7 +140,9 @@ docs: .PHONY: test test: generate fmt vet envtest ## Run tests. - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(CACHE_BIN) -p path)" go test -race -timeout 60s ./... -coverprofile cover.out + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(CACHE_BIN) -p path)" go test -race -timeout 60s `go list ./... | grep -v ./mock` -coverprofile cover.out.tmp + grep -v "zz_generated.deepcopy.go" cover.out.tmp > cover.out + rm cover.out.tmp .PHONY: e2etest e2etest: generate local-deploy chainsaw diff --git a/cloud/services/loadbalancers.go b/cloud/services/loadbalancers.go index 106283a34..42a5f10a5 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 298f2e827..125e93980 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 c7e70d33d..c2e4fc5c3 100644 --- a/controller/linodevpc_controller_helpers.go +++ b/controller/linodevpc_controller_helpers.go @@ -46,7 +46,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/go.mod b/go.mod index 47084be35..9df5511a9 100644 --- a/go.mod +++ b/go.mod @@ -68,7 +68,7 @@ require ( golang.org/x/tools v0.17.0 // indirect gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.31.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index 2db0a0e0f..ec0f1e992 100644 --- a/go.sum +++ b/go.sum @@ -254,8 +254,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 h1: google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13/go.mod h1:KSqppvjFjtoCI+KGd4PELB0qLNxdJHRGqRI09mB6pQA= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 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/util/helpers_test.go b/util/helpers_test.go new file mode 100644 index 000000000..1be231969 --- /dev/null +++ b/util/helpers_test.go @@ -0,0 +1,51 @@ +package util + +import ( + "errors" + "testing" + + "github.com/linode/linodego" +) + +func TestIgnoreLinodeAPIError(t *testing.T) { + t.Parallel() + tests := []struct { + name string + err error + code int + shouldFilter bool + }{{ + name: "Not Linode API error", + err: errors.New("foo"), + code: 0, + shouldFilter: false, + }, { + name: "Ignore not found Linode API error", + err: linodego.Error{ + Response: nil, + Code: 400, + Message: "not found", + }, + code: 400, + shouldFilter: true, + }, { + name: "Don't ignore not found Linode API error", + err: linodego.Error{ + Response: nil, + Code: 400, + Message: "not found", + }, + code: 500, + shouldFilter: false, + }} + for _, tt := range tests { + testcase := tt + t.Run(testcase.name, func(t *testing.T) { + t.Parallel() + err := IgnoreLinodeAPIError(testcase.err, testcase.code) + if testcase.shouldFilter && 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) + } +}