-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
79 lines (63 loc) · 1.95 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package metalgo
import (
"fmt"
"net"
"net/http"
"testing"
"time"
"github.com/emicklei/go-restful/v3"
"github.com/metal-stack/metal-go/api/client/machine"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/httperrors"
"github.com/stretchr/testify/require"
)
var ws *restful.WebService
func init() {
ws = new(restful.WebService)
ws.
Path("/v1/machine").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON).
Route(ws.POST("/allocate").To(func(request *restful.Request, response *restful.Response) {}).
Reads(models.V1MachineAllocateRequest{}).
Returns(http.StatusOK, "OK", models.V1MachineResponse{}).
Returns(http.StatusNotFound, "No free machine for allocation found", httperrors.HTTPErrorResponse{}).
Returns(http.StatusUnprocessableEntity, "Unprocessable Entity", httperrors.HTTPErrorResponse{}))
restful.DefaultContainer.Add(ws)
}
func TestMachineCreate(t *testing.T) {
// given
machineID := "machineID"
metalMachine := &models.V1MachineResponse{
ID: &machineID,
}
ws.Routes()[0].Function = func(req *restful.Request, resp *restful.Response) {
_ = resp.WriteEntity(metalMachine)
}
server, client := startServerAndGetDriver(t)
defer func() {
_ = server.Close()
}()
// when
resp, err := client.Machine().AllocateMachine(machine.NewAllocateMachineParams().WithBody(&models.V1MachineAllocateRequest{
UUID: machineID,
}), nil)
// then
require.NoError(t, err)
require.NotNil(t, resp) //nolint:testifylint
require.Equal(t, metalMachine, resp.Payload)
}
func startServerAndGetDriver(t *testing.T) (*http.Server, Client) {
//nolint:gosec
listener, _ := net.Listen("tcp", ":0")
server := &http.Server{ReadHeaderTimeout: time.Minute}
go func() {
_ = server.Serve(listener)
}()
time.Sleep(10 * time.Millisecond)
port := listener.Addr().(*net.TCPAddr).Port
addr := fmt.Sprintf("http://localhost:%d", port)
client, err := NewDriver(addr, "", "")
require.NoError(t, err)
return server, client
}