Skip to content

Commit

Permalink
chore(version): use struct and add tests (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed Jul 7, 2023
1 parent 4729c5b commit 274a9d5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
25 changes: 16 additions & 9 deletions internal/plugin/path_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plugin

import (
"context"
"encoding/json"

"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -30,15 +31,21 @@ func (b *backend) pathInfoRead(
_ *logical.Request,
_ *framework.FieldData,
) (*logical.Response, error) {
var m map[string]any

v := version.New()

bs, err := json.Marshal(v)
if err != nil {
return nil, err
}

err = json.Unmarshal(bs, &m)
if err != nil {
return nil, err
}

return &logical.Response{
Data: map[string]any{
"build_date": version.BuildDate,
"build_version": version.Version,
"build_commit": version.Commit,
"build_commit_date": version.CommitDate,
"build_branch": version.Branch,
"build_tag": version.Tag,
"build_dirty": version.Dirty,
},
Data: m,
}, nil
}
47 changes: 47 additions & 0 deletions internal/plugin/path_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package plugin

import (
"context"
"encoding/json"
"testing"
"time"

"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
"github.com/thevilledev/vault-plugin-secrets-vercel/internal/version"
)

func TestPathInfo(t *testing.T) {
t.Parallel()

t.Run("ValidInfo", func(t *testing.T) {
t.Parallel()

version.Branch = "main"
version.BuildDate = time.Now().String()
version.Commit = "xyz"
version.CommitDate = time.Now().String()
version.Dirty = "false"
version.Tag = "v0.0.1"
version.Version = "0.0.1"

ctx := context.Background()
b, storage := newTestBackend(t)

res, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.ReadOperation,
Path: pathPatternInfo,
})
require.NoError(t, err)

var vi version.VersionInfo
d, err := json.Marshal(res.Data)
require.NoError(t, err)
err = json.Unmarshal(d, &vi)
require.NoError(t, err)

vn := version.New()
require.Equal(t, *vn, vi)
})
}
22 changes: 22 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,25 @@ var (
Tag string
Dirty string
)

type VersionInfo struct {
BuildDate string `json:"build_date"`
Version string `json:"build_version"`
Commit string `json:"build_commit"`
CommitDate string `json:"build_commit_date"`
Branch string `json:"build_commit_branch"`
Tag string `json:"build_tag"`
Dirty string `json:"build_dirty"`
}

func New() *VersionInfo {
return &VersionInfo{
BuildDate: BuildDate,
Version: Version,
Commit: Commit,
CommitDate: CommitDate,
Branch: Branch,
Tag: Tag,
Dirty: Dirty,
}
}

0 comments on commit 274a9d5

Please sign in to comment.