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

refactor: reuse mockutils package #462

Merged
merged 2 commits into from
Jun 18, 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
69 changes: 43 additions & 26 deletions hcloud/action_waiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutils"
)

func TestWaitFor(t *testing.T) {
RunMockedTestCases(t,
[]MockedTestCase{
{
Name: "succeed",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200,
`{
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "running", "progress": 0 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "success", "progress": 100 }
],
"meta": { "pagination": { "page": 1 }}
}`},
}`,
},
},
Run: func(env testEnv) {
actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}}
Expand All @@ -46,12 +52,14 @@ func TestWaitFor(t *testing.T) {
},
{
Name: "fail with unknown action",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200,
`{
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [],
"meta": { "pagination": { "page": 1 }}
}`},
}`,
},
},
Run: func(env testEnv) {
actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}}
Expand All @@ -74,8 +82,9 @@ func TestWaitFor(t *testing.T) {
},
{
Name: "fail with api error",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 503, ""},
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 503},
},
Run: func(env testEnv) {
actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}}
Expand All @@ -94,33 +103,40 @@ func TestWaitForFunc(t *testing.T) {
[]MockedTestCase{
{
Name: "succeed",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "running", "progress": 40 },
{ "id": 1509772238, "status": "running", "progress": 0 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "running", "progress": 60 },
{ "id": 1509772238, "status": "running", "progress": 50 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "success", "progress": 100 },
{ "id": 1509772238, "status": "running", "progress": 75 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772238, "status": "error", "progress": 75,
"error": {
Expand All @@ -130,7 +146,8 @@ func TestWaitForFunc(t *testing.T) {
}
],
"meta": { "pagination": { "page": 1 }}
}`},
}`,
},
},
Run: func(env testEnv) {
actions := []*Action{
Expand Down
58 changes: 5 additions & 53 deletions hcloud/mocked_test.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,27 @@
package hcloud

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutils"
)

type MockedTestCase struct {
Name string
WantRequests []MockedRequest
WantRequests []mockutils.Request
Run func(env testEnv)
}

type MockedRequest struct {
Method string
Path string
WantRequestBodyFunc func(t *testing.T, r *http.Request, body []byte)

Status int
Body string
}

func RunMockedTestCases(t *testing.T, testCases []MockedTestCase) {
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
env := newTestEnvWithServer(httptest.NewServer(MockedRequestHandler(t, testCase.WantRequests)), nil)
testServer := httptest.NewServer(mockutils.Handler(t, testCase.WantRequests))

env := newTestEnvWithServer(testServer, nil)
defer env.Teardown()

testCase.Run(env)
})
}
}

func MockedRequestHandler(t *testing.T, requests []MockedRequest) http.HandlerFunc {
index := 0
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if testing.Verbose() {
t.Logf("request %d: %s %s\n", index, r.Method, r.URL.Path)
}

if index >= len(requests) {
t.Fatalf("received unknown request %d", index)
}

response := requests[index]
assert.Equal(t, response.Method, r.Method)
assert.Equal(t, response.Path, r.RequestURI)

if response.WantRequestBodyFunc != nil {
buffer, err := io.ReadAll(r.Body)
defer func() {
if err := r.Body.Close(); err != nil {
t.Fatal(err)
}
}()
if err != nil {
t.Fatal(err)
}
response.WantRequestBodyFunc(t, r, buffer)
}

w.WriteHeader(response.Status)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(response.Body))
if err != nil {
t.Fatal(err)
}

index++
})
}
Loading