Skip to content

Commit

Permalink
disck client pases home dir query
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidNix committed Aug 18, 2023
1 parent 5373895 commit b21f439
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 6 additions & 1 deletion internal/healthcheck/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewClient(client *http.Client) *Client {

// DiskUsage returns disk usage statistics or an error if unable to obtain.
// Do not include the port in the host.
func (c Client) DiskUsage(ctx context.Context, host string) (DiskUsageResponse, error) {
func (c Client) DiskUsage(ctx context.Context, host, homeDir string) (DiskUsageResponse, error) {
var diskResp DiskUsageResponse
u, err := url.Parse(host)
if err != nil {
Expand All @@ -37,6 +37,11 @@ func (c Client) DiskUsage(ctx context.Context, host string) (DiskUsageResponse,
if err != nil {
return diskResp, fmt.Errorf("new request: %w", err)
}

q := req.URL.Query()
q.Set("dir", homeDir)
req.URL.RawQuery = q.Encode()

resp, err := c.httpDo(req)
if err != nil {
return diskResp, fmt.Errorf("http do: %w", err)
Expand Down
18 changes: 11 additions & 7 deletions internal/healthcheck/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func TestClient_DiskUsage(t *testing.T) {
httpClient = &http.Client{}
)

const host = "http://10.1.1.1"
const (
host = "http://10.1.1.1"
homeDir = "/home/test"
)

t.Run("happy path", func(t *testing.T) {
client := NewClient(httpClient)
Expand All @@ -32,8 +35,9 @@ func TestClient_DiskUsage(t *testing.T) {
}

client.httpDo = func(req *http.Request) (*http.Response, error) {
require.Equal(t, "http://10.1.1.1:1251/disk", req.URL.String())
require.Equal(t, "http://10.1.1.1:1251/disk?dir=%2Fhome%2Ftest", req.URL.String())
require.Equal(t, "GET", req.Method)
require.Equal(t, homeDir, req.URL.Query().Get("dir"))

b, err := json.Marshal(want)
if err != nil {
Expand All @@ -42,7 +46,7 @@ func TestClient_DiskUsage(t *testing.T) {
return &http.Response{Body: io.NopCloser(bytes.NewReader(b))}, nil
}

got, err := client.DiskUsage(ctx, host)
got, err := client.DiskUsage(ctx, host, homeDir)

require.NoError(t, err)
require.Equal(t, want, got)
Expand All @@ -53,7 +57,7 @@ func TestClient_DiskUsage(t *testing.T) {
client.httpDo = func(req *http.Request) (*http.Response, error) {
return nil, errors.New("boom")
}
_, err := client.DiskUsage(ctx, host)
_, err := client.DiskUsage(ctx, host, "")

require.Error(t, err)
require.EqualError(t, err, "http do: boom")
Expand All @@ -73,7 +77,7 @@ func TestClient_DiskUsage(t *testing.T) {
}, nil
}

_, err := client.DiskUsage(ctx, host)
_, err := client.DiskUsage(ctx, host, "")

require.Error(t, err)
require.EqualError(t, err, "something bad happened")
Expand All @@ -88,7 +92,7 @@ func TestClient_DiskUsage(t *testing.T) {
}, nil
}

_, err := client.DiskUsage(ctx, host)
_, err := client.DiskUsage(ctx, host, "")

require.Error(t, err)
require.EqualError(t, err, "malformed json: unexpected EOF")
Expand All @@ -103,7 +107,7 @@ func TestClient_DiskUsage(t *testing.T) {
}, nil
}

_, err := client.DiskUsage(ctx, host)
_, err := client.DiskUsage(ctx, host, "")

require.Error(t, err)
require.EqualError(t, err, "invalid response: 0 free bytes")
Expand Down

0 comments on commit b21f439

Please sign in to comment.