Skip to content

Commit

Permalink
test: separate unit tests from integration suite for cleaner practice (
Browse files Browse the repository at this point in the history
…#473)

* separate unit tests from integration tests and edit the makefiles accordingly

* address PR comment
  • Loading branch information
ykim-1 committed Mar 20, 2024
1 parent dc4f129 commit 18f3764
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 66 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ citest: lint test

testunit:
go test -v $(PACKAGES) $(ARGS)
cd test && make testunit

testint:
cd test && make test
cd test && make testint

testcov-func:
@go test -v -coverprofile="coverage.txt" . > /dev/null 2>&1
Expand Down
9 changes: 7 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
.PHONY: test
.PHONY: testint

test:
testint:
@LINODE_FIXTURE_MODE="play" \
LINODE_TOKEN="awesometokenawesometokenawesometoken" \
LINODE_API_VERSION="v4beta" \
GO111MODULE="on" \
go test -v ./integration $(ARGS)

.PHONY: testunit

testunit:
go test -v ./unit/...

.PHONY: smoketest

smoketest:
Expand Down
5 changes: 0 additions & 5 deletions test/integration/integration_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
"github.com/linode/linodego"
"github.com/linode/linodego/internal/testutil"
"golang.org/x/oauth2"
"k8s.io/client-go/transport"
)
Expand Down Expand Up @@ -153,10 +152,6 @@ func createTestClient(t *testing.T, fixturesYaml string) (*linodego.Client, func
return &c, recordStopper
}

func createMockClient(t *testing.T) *linodego.Client {
return testutil.CreateMockClient(t, linodego.NewClient)
}

// transportRecordWrapper returns a tranport.WrapperFunc which provides the test
// recorder as an http.RoundTripper.
func transportRecorderWrapper(t *testing.T, fixtureYaml string) (transport.WrapperFunc, func()) {
Expand Down
27 changes: 0 additions & 27 deletions test/integration/lke_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"reflect"
"testing"

"github.com/jarcoal/httpmock"
"github.com/linode/linodego"
k8scondition "github.com/linode/linodego/k8s/pkg/condition"
)
Expand Down Expand Up @@ -219,32 +218,6 @@ func TestLKEVersion_GetMissing(t *testing.T) {
}
}

func TestLKECluster_Regenerate(t *testing.T) {
client := createMockClient(t)

requestData := linodego.LKEClusterRegenerateOptions{
KubeConfig: true,
ServiceToken: false,
}

httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "clusters/1234/regenerate"),
mockRequestBodyValidate(t, requestData, nil))

if _, err := client.RegenerateLKECluster(context.Background(), 1234, requestData); err != nil {
t.Fatal(err)
}
}

func TestLKECluster_DeleteServiceToken(t *testing.T) {
client := createMockClient(t)

httpmock.RegisterRegexpResponder("DELETE", mockRequestURL(t, "clusters/1234/servicetoken"), httpmock.NewStringResponder(200, "{}"))

if err := client.DeleteLKEClusterServiceToken(context.Background(), 1234); err != nil {
t.Fatal(err)
}
}

func TestLKEVersion_GetFound(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestLKEVersion_GetFound")
defer teardown()
Expand Down
13 changes: 0 additions & 13 deletions test/integration/util_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package integration

import (
"regexp"
"strconv"
"testing"
"time"

"github.com/linode/linodego/internal/testutil"

"github.com/jarcoal/httpmock"
)

func assertDateSet(t *testing.T, compared *time.Time) {
Expand All @@ -18,14 +13,6 @@ func assertDateSet(t *testing.T, compared *time.Time) {
}
}

func mockRequestBodyValidate(t *testing.T, expected interface{}, response interface{}) httpmock.Responder {
return testutil.MockRequestBodyValidate(t, expected, response)
}

func mockRequestURL(t *testing.T, path string) *regexp.Regexp {
return testutil.MockRequestURL(path)
}

func assertSliceContains[T comparable](t *testing.T, slice []T, target T) {
for _, v := range slice {
if v == target {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand Down
35 changes: 35 additions & 0 deletions test/unit/lke_clusters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package unit

import (
"context"
"testing"

"github.com/jarcoal/httpmock"
"github.com/linode/linodego"
)

func TestLKECluster_Regenerate(t *testing.T) {
client := createMockClient(t)

requestData := linodego.LKEClusterRegenerateOptions{
KubeConfig: true,
ServiceToken: false,
}

httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "clusters/1234/regenerate"),
mockRequestBodyValidate(t, requestData, nil))

if _, err := client.RegenerateLKECluster(context.Background(), 1234, requestData); err != nil {
t.Fatal(err)
}
}

func TestLKECluster_DeleteServiceToken(t *testing.T) {
client := createMockClient(t)

httpmock.RegisterRegexpResponder("DELETE", mockRequestURL(t, "clusters/1234/servicetoken"), httpmock.NewStringResponder(200, "{}"))

if err := client.DeleteLKEClusterServiceToken(context.Background(), 1234); err != nil {
t.Fatal(err)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand All @@ -7,16 +7,6 @@ import (
"github.com/jarcoal/httpmock"
)

func TestObjectStorage_Get_Transfer(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestObjectStorage_transfer")
defer teardown()

_, err := client.GetObjectStorageTransfer(context.Background())
if err != nil {
t.Errorf("unable to get object storage transfer data : %s", err)
}
}

func TestObjectStorage_Cancel(t *testing.T) {
client := createMockClient(t)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package unit

import (
"context"
Expand Down
22 changes: 22 additions & 0 deletions test/unit/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package unit

import (
"github.com/linode/linodego"
"github.com/linode/linodego/internal/testutil"
"regexp"
"testing"

"github.com/jarcoal/httpmock"
)

func mockRequestBodyValidate(t *testing.T, expected interface{}, response interface{}) httpmock.Responder {
return testutil.MockRequestBodyValidate(t, expected, response)
}

func mockRequestURL(t *testing.T, path string) *regexp.Regexp {
return testutil.MockRequestURL(path)
}

func createMockClient(t *testing.T) *linodego.Client {
return testutil.CreateMockClient(t, linodego.NewClient)
}

0 comments on commit 18f3764

Please sign in to comment.