Skip to content

Commit

Permalink
add IsReaperUp func and update test setup with reaper readiness check
Browse files Browse the repository at this point in the history
  • Loading branch information
jsanda committed Oct 29, 2020
1 parent b5106cc commit e9912b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
27 changes: 23 additions & 4 deletions reaper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ func NewReaperClient(baseURL string) (ReaperClient, error) {
return newClient(baseURL)
}

func (c *Client) IsReaperUp(ctx context.Context) (bool, error) {
rel := &url.URL{Path: "/ping"}
u := c.BaseURL.ResolveReference(rel)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return false, err
}

if resp, err := c.doRequest(ctx, req, nil); err == nil {
return resp.StatusCode == http.StatusNoContent, nil
} else {
return false, err
}
}

func (c *Client) GetClusterNames(ctx context.Context) ([]string, error) {
rel := &url.URL{Path: "/cluster"}
u := c.BaseURL.ResolveReference(rel)
Expand All @@ -61,7 +76,7 @@ func (c *Client) GetClusterNames(ctx context.Context) ([]string, error) {
//req.Header.Set("User-Agent", c.UserAgent)

clusterNames := []string{}
_, err = c.do(ctx, req, &clusterNames)
_, err = c.doJsonRequest(ctx, req, &clusterNames)

if err != nil {
return nil, fmt.Errorf("failed to get cluster names: %w", err)
Expand All @@ -79,7 +94,7 @@ func (c *Client) GetCluster(ctx context.Context, name string) (*Cluster, error)
}

clusterState := &clusterStatus{}
resp, err := c.do(ctx, req, clusterState)
resp, err := c.doJsonRequest(ctx, req, clusterState)

if err != nil {
fmt.Printf("response: %+v", resp)
Expand Down Expand Up @@ -189,7 +204,7 @@ func (c *Client) DeleteCluster(ctx context.Context, cluster string) error {
return err
}

_, err = c.do(ctx, req, nil)
_, err = c.doJsonRequest(ctx, req, nil)

// TODO check response status code

Expand All @@ -200,8 +215,12 @@ func (c *Client) DeleteCluster(ctx context.Context, cluster string) error {
return nil
}

func (c *Client) do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
func (c *Client) doJsonRequest(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
req.Header.Set("Accept", "application/json")
return c.doRequest(ctx, req, v)
}

func (c *Client) doRequest(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
req.WithContext(ctx)

resp, err := c.httpClient.Do(req)
Expand Down
18 changes: 18 additions & 0 deletions reaper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reaper
import (
"context"
"testing"
"time"

"github.com/jsanda/reaper-client-go/testenv"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -44,6 +45,23 @@ func TestClient(t *testing.T) {
t.Fatalf("cluster-1 readiness check failed: %s", err)
}

isUp := false
for i := 0; i < 10; i++ {
t.Log("checking if reaper is ready")
if isUp, err = client.IsReaperUp(context.Background()); err == nil {
if isUp {
t.Log("reaper is ready!")
break
}
} else {
t.Logf("reaper readiness check failed: %s", err)
}
time.Sleep(6 * time.Second)
}
if !isUp {
t.Fatalf("reaper readiness check timed out")
}

if err = testenv.AddCluster(t,"cluster-1", "cluster-1-node-0"); err != nil {
t.Fatalf("failed to add cluster-1: %s", err)
}
Expand Down

0 comments on commit e9912b2

Please sign in to comment.