Skip to content
Closed
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
11 changes: 2 additions & 9 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import (
"net/url"
)

// HTTPClient interface holds the required Post method
// to send FleetLock requests.
type HTTPClient interface {
// Do send a `body` payload to the URL.
Do(*http.Request) (*http.Response, error)
}

// Payload is the content to send
// to the FleetLock server.
type Payload struct {
Expand All @@ -40,7 +33,7 @@ type Client struct {
URL string
group string
id string
http HTTPClient
http *http.Client
}

func (c *Client) generateRequest(endpoint string) (*http.Request, error) {
Expand Down Expand Up @@ -129,7 +122,7 @@ func (c *Client) UnlockIfHeld() error {
}

// New builds a Fleet-Lock client.
func New(URL, group, ID string, c HTTPClient) (*Client, error) {
func New(URL, group, ID string, c *http.Client) (*Client, error) {
if _, err := url.ParseRequestURI(URL); err != nil {
return nil, fmt.Errorf("parsing URL: %w", err)
}
Expand Down
30 changes: 18 additions & 12 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"github.com/flatcar-linux/fleetlock/pkg/client"
)

type httpClient struct {
type mockRoundTripper struct {
resp *http.Response
r *http.Request
doErr error
}

func (h *httpClient) Do(req *http.Request) (*http.Response, error) {
h.r = req
return h.resp, h.doErr
func (r *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
r.r = req
return r.resp, r.doErr
}

func TestBadURL(t *testing.T) {
Expand Down Expand Up @@ -61,27 +61,30 @@ func TestRecursiveLock(t *testing.T) {
expErr: errors.New("unexpected status code: 100"),
},
{
expErr: errors.New("doing the request: connection refused"),
expErr: errors.New("doing the request: Post \"http://1.2.3.4/v1/pre-reboot\": connection refused"),
doErr: errors.New("connection refused"),
},
} {
h := &httpClient{
h := http.DefaultClient
tr := &mockRoundTripper{
resp: &http.Response{
StatusCode: test.statusCode,
Body: test.body,
},
doErr: test.doErr,
}

h.Transport = tr

c, _ := client.New("http://1.2.3.4", "default", "1234", h)

err := c.RecursiveLock()
if err != nil && err.Error() != test.expErr.Error() {
t.Fatalf("should have %v for err, got: %v", test.expErr, err)
}

if h.r.URL.String() != expURL {
t.Fatalf("should have %s for URL, got: %s", expURL, h.r.URL.String())
if tr.r.URL.String() != expURL {
t.Fatalf("should have %s for URL, got: %s", expURL, tr.r.URL.String())
}
}
}
Expand Down Expand Up @@ -114,27 +117,30 @@ func TestUnlockIfHeld(t *testing.T) {
expErr: errors.New("unexpected status code: 100"),
},
{
expErr: errors.New("doing the request: connection refused"),
expErr: errors.New("doing the request: Post \"http://1.2.3.4/v1/steady-state\": connection refused"),
doErr: errors.New("connection refused"),
},
} {
h := &httpClient{
h := http.DefaultClient
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultClient is just &Client{}, so you can just initialize everything at one go as http.Client{Transport: ...}.

tr := &mockRoundTripper{
resp: &http.Response{
StatusCode: test.statusCode,
Body: test.body,
},
doErr: test.doErr,
}

h.Transport = tr

c, _ := client.New("http://1.2.3.4", "default", "1234", h)

err := c.UnlockIfHeld()
if err != nil && err.Error() != test.expErr.Error() {
t.Fatalf("should have %v for err, got: %v", test.expErr, err)
}

if h.r.URL.String() != expURL {
t.Fatalf("should have %s for URL, got: %s", expURL, h.r.URL.String())
if tr.r.URL.String() != expURL {
t.Fatalf("should have %s for URL, got: %s", expURL, tr.r.URL.String())
}
}
}