From 8e39357582f88075428ea07599326799686b3384 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Mon, 9 Jan 2023 07:58:56 -0600 Subject: [PATCH] Implement GetVersionInfo client API method (#84) --- service/client.go | 28 ++++++++++++++++++++++++++++ service/client_test.go | 35 +++++++++++++++++++++++++++++++++++ service/version.go | 8 ++++---- service/version_test.go | 8 ++++---- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/service/client.go b/service/client.go index 6970bb1..1d86e7c 100644 --- a/service/client.go +++ b/service/client.go @@ -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 +} diff --git a/service/client_test.go b/service/client_test.go index 85259bf..b6c4899 100644 --- a/service/client_test.go +++ b/service/client_test.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "net" + "runtime" "sync" "sync/atomic" "testing" @@ -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) + }) +} diff --git a/service/version.go b/service/version.go index ffc6e6d..7b55537 100644 --- a/service/version.go +++ b/service/version.go @@ -17,15 +17,15 @@ 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, @@ -33,7 +33,7 @@ func getVersionInfo() versionInfo { } } -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), diff --git a/service/version_test.go b/service/version_test.go index 0c1f543..64186a5 100644 --- a/service/version_test.go +++ b/service/version_test.go @@ -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, @@ -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) })