diff options
Diffstat (limited to 'go/internal/gitlabnet')
-rw-r--r-- | go/internal/gitlabnet/healthcheck/client.go | 54 | ||||
-rw-r--r-- | go/internal/gitlabnet/healthcheck/client_test.go | 48 |
2 files changed, 102 insertions, 0 deletions
diff --git a/go/internal/gitlabnet/healthcheck/client.go b/go/internal/gitlabnet/healthcheck/client.go new file mode 100644 index 0000000..288b150 --- /dev/null +++ b/go/internal/gitlabnet/healthcheck/client.go @@ -0,0 +1,54 @@ +package healthcheck + +import ( + "fmt" + "net/http" + + "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet" +) + +const ( + checkPath = "/check" +) + +type Client struct { + config *config.Config + client *gitlabnet.GitlabClient +} + +type Response struct { + APIVersion string `json:"api_version"` + GitlabVersion string `json:"gitlab_version"` + GitlabRevision string `json:"gitlab_rev"` + Redis bool `json:"redis"` +} + +func NewClient(config *config.Config) (*Client, error) { + client, err := gitlabnet.GetClient(config) + if err != nil { + return nil, fmt.Errorf("Error creating http client: %v", err) + } + + return &Client{config: config, client: client}, nil +} + +func (c *Client) Check() (*Response, error) { + resp, err := c.client.Get(checkPath) + if err != nil { + return nil, err + } + + defer resp.Body.Close() + + return parse(resp) +} + +func parse(hr *http.Response) (*Response, error) { + response := &Response{} + if err := gitlabnet.ParseJSON(hr, response); err != nil { + return nil, err + } + + return response, nil +} diff --git a/go/internal/gitlabnet/healthcheck/client_test.go b/go/internal/gitlabnet/healthcheck/client_test.go new file mode 100644 index 0000000..d32e6f4 --- /dev/null +++ b/go/internal/gitlabnet/healthcheck/client_test.go @@ -0,0 +1,48 @@ +package healthcheck + +import ( + "encoding/json" + "net/http" + "testing" + + "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/testserver" + + "github.com/stretchr/testify/require" +) + +var ( + requests = []testserver.TestRequestHandler{ + { + Path: "/api/v4/internal/check", + Handler: func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(testResponse) + }, + }, + } + + testResponse = &Response{ + APIVersion: "v4", + GitlabVersion: "v12.0.0-ee", + GitlabRevision: "3b13818e8330f68625d80d9bf5d8049c41fbe197", + Redis: true, + } +) + +func TestCheck(t *testing.T) { + client, cleanup := setup(t) + defer cleanup() + + result, err := client.Check() + require.NoError(t, err) + require.Equal(t, testResponse, result) +} + +func setup(t *testing.T) (*Client, func()) { + url, cleanup := testserver.StartSocketHttpServer(t, requests) + + client, err := NewClient(&config.Config{GitlabUrl: url}) + require.NoError(t, err) + + return client, cleanup +} |