Skip to content

Commit

Permalink
Implement GetVersionInfo client API method (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 authored Jan 9, 2023
1 parent 3c44a4b commit 8e39357
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
28 changes: 28 additions & 0 deletions service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,31 @@ func (c *Client) reconnectHandler() {
c.sendError(fmt.Errorf("failed to re-connect: %w", err))
}
}

func (c *Client) GetVersionInfo() (VersionInfo, error) {
if c.httpClient == nil {
return VersionInfo{}, fmt.Errorf("http client is not initialized")
}

req, err := http.NewRequest("GET", c.cfg.httpURL+"/version", nil)
if err != nil {
return VersionInfo{}, fmt.Errorf("failed to build request: %w", err)
}

resp, err := c.httpClient.Do(req)
if err != nil {
return VersionInfo{}, fmt.Errorf("http request failed: %w", err)
}
defer resp.Body.Close()

var info VersionInfo
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return VersionInfo{}, fmt.Errorf("decoding http response failed: %w", err)
}

if resp.StatusCode != http.StatusOK {
return VersionInfo{}, fmt.Errorf("request failed with status %s", resp.Status)
}

return info, nil
}
35 changes: 35 additions & 0 deletions service/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net"
"runtime"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -600,3 +601,37 @@ func TestClientConcurrency(t *testing.T) {
require.Equal(t, n-1, int(nErrors))
})
}

func TestClientGetVersionInfo(t *testing.T) {
th := SetupTestHelper(t, nil)
defer th.Teardown()

c, err := NewClient(ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey,
})
require.NoError(t, err)
require.NotNil(t, c)
defer c.Close()

t.Run("success", func(t *testing.T) {
buildHash = "432dad0"
buildDate = "2022-05-12 09:05"
buildVersion = "v0.1.0"
defer func() {
buildHash = ""
buildDate = ""
buildVersion = ""
}()

info, err := c.GetVersionInfo()
require.NoError(t, err)
require.NotEmpty(t, info)
require.Equal(t, VersionInfo{
BuildHash: buildHash,
BuildDate: buildDate,
BuildVersion: buildVersion,
GoVersion: runtime.Version(),
}, info)
})
}
8 changes: 4 additions & 4 deletions service/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ var (
buildDate string
)

type versionInfo struct {
type VersionInfo struct {
BuildDate string `json:"buildDate"`
BuildVersion string `json:"buildVersion"`
BuildHash string `json:"buildHash"`
GoVersion string `json:"goVersion"`
}

func getVersionInfo() versionInfo {
return versionInfo{
func getVersionInfo() VersionInfo {
return VersionInfo{
BuildDate: buildDate,
BuildVersion: buildVersion,
BuildHash: buildHash,
GoVersion: runtime.Version(),
}
}

func (v versionInfo) logFields() []mlog.Field {
func (v VersionInfo) logFields() []mlog.Field {
return []mlog.Field{
mlog.String("buildDate", v.BuildDate),
mlog.String("buildVersion", v.BuildVersion),
Expand Down
8 changes: 4 additions & 4 deletions service/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func TestGetVersion(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
defer resp.Body.Close()
var info versionInfo
var info VersionInfo
err = json.NewDecoder(resp.Body).Decode(&info)
require.NoError(t, err)
require.Equal(t, versionInfo{
require.Equal(t, VersionInfo{
BuildHash: buildHash,
BuildDate: buildDate,
BuildVersion: buildVersion,
Expand All @@ -53,10 +53,10 @@ func TestGetVersion(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
defer resp.Body.Close()
var info versionInfo
var info VersionInfo
err = json.NewDecoder(resp.Body).Decode(&info)
require.NoError(t, err)
require.Equal(t, versionInfo{
require.Equal(t, VersionInfo{
GoVersion: goVersion,
}, info)
})
Expand Down

0 comments on commit 8e39357

Please sign in to comment.