Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[testing] - add more coverage #194

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.idea
bin/*
kind-logs-*
cover.out
cover.out*
kubeconfig*
.devbox/*
docs/book
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion cloud/services/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
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

Check warning on line 36 in cloud/services/loadbalancers.go

View check run for this annotation

Codecov / codecov/patch

cloud/services/loadbalancers.go#L34-L36

Added lines #L34 - L36 were not covered by tests
}
linodeNBs, err := clusterScope.LinodeClient.ListNodeBalancers(ctx, linodego.NewListOptions(1, filter))

Check warning on line 38 in cloud/services/loadbalancers.go

View check run for this annotation

Codecov / codecov/patch

cloud/services/loadbalancers.go#L38

Added line #L38 was not covered by tests
if err != nil {
logger.Info("Failed to list NodeBalancers", "error", err.Error())

Expand Down
6 changes: 5 additions & 1 deletion controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@
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

Check warning on line 253 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L251-L253

Added lines #L251 - L253 were not covered by tests
}
linodeInstances, err := machineScope.LinodeClient.ListInstances(ctx, linodego.NewListOptions(1, filter))

Check warning on line 255 in controller/linodemachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controller/linodemachine_controller.go#L255

Added line #L255 was not covered by tests
if err != nil {
logger.Error(err, "Failed to list Linode machine instances")

Expand Down
6 changes: 5 additions & 1 deletion controller/linodevpc_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
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

Check warning on line 51 in controller/linodevpc_controller_helpers.go

View check run for this annotation

Codecov / codecov/patch

controller/linodevpc_controller_helpers.go#L49-L51

Added lines #L49 - L51 were not covered by tests
}
if vpcs, err := vpcScope.LinodeClient.ListVPCs(ctx, linodego.NewListOptions(1, filter)); err != nil {

Check warning on line 53 in controller/linodevpc_controller_helpers.go

View check run for this annotation

Codecov / codecov/patch

controller/linodevpc_controller_helpers.go#L53

Added line #L53 was not covered by tests
logger.Error(err, "Failed to list VPCs")

return err
Expand Down
6 changes: 3 additions & 3 deletions util/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@

// 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

Check warning on line 41 in util/filter.go

View check run for this annotation

Codecov / codecov/patch

util/filter.go#L41

Added line #L41 was not covered by tests
}

return string(p)
return string(p), nil
}
54 changes: 54 additions & 0 deletions util/filter_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
})
}
}
51 changes: 51 additions & 0 deletions util/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
})
}
}
11 changes: 11 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}