Skip to content

Commit 801a117

Browse files
committed
fix: add some token tests
1 parent 6315bbc commit 801a117

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

main.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ import (
1313
"github.com/hashicorp/go-plugin"
1414
)
1515

16-
type implIface interface {
17-
Value(token string, metadata []byte) (string, error)
18-
}
1916
type TokenStorePlugin struct {
20-
impl implIface
17+
impl tokenstore.TokenStore
2118
}
2219

2320
func (ts TokenStorePlugin) Value(key string, metadata []byte) (string, error) {
@@ -29,20 +26,28 @@ var (
2926
Revision string = "1111aaaa"
3027
)
3128

32-
func main() {
33-
34-
versionFlag := flag.Bool("version", false, "plugin version")
35-
flag.Parse()
29+
func ShowFlag(osArgs []string) bool {
30+
fs := flag.NewFlagSet("plugin", flag.ContinueOnError)
31+
vf := fs.Bool("version", false, "plugin version")
32+
if err := fs.Parse(osArgs); err != nil {
33+
return false
34+
}
3635

37-
if *versionFlag {
36+
if *vf {
3837
fmt.Printf("Version: %s-%s\n", Version, Revision)
39-
os.Exit(0)
38+
return true
4039
}
40+
return false
41+
}
42+
43+
func PluginSetup() (*impl.ParamStore, error) {
4144

4245
// log set up
4346
log := hclog.New(hclog.DefaultOptions)
47+
4448
log.SetLevel(hclog.LevelFromString("error"))
4549

50+
os.Environ()
4651
if val, ok := os.LookupEnv(config.CONFIGMANAGER_LOG); ok && len(val) > 0 {
4752
if logLevel := hclog.LevelFromString(val); logLevel != hclog.NoLevel {
4853
log.SetLevel(logLevel)
@@ -52,7 +57,22 @@ func main() {
5257
// initialize the implementation
5358
i, err := impl.NewParamStore(context.Background(), log)
5459
if err != nil {
55-
log.Error("error", err)
60+
log.Error("implementation init error", err, "impl", "awsparamstr")
61+
return nil, err
62+
}
63+
64+
return i, nil
65+
}
66+
67+
// main func should only be used for process terminantion
68+
// Inits and serves the plugin
69+
func main() {
70+
if ShowFlag(os.Args[1:]) {
71+
os.Exit(0)
72+
}
73+
74+
i, err := PluginSetup()
75+
if err != nil {
5676
os.Exit(1)
5777
}
5878

@@ -64,6 +84,7 @@ func main() {
6484
Plugins: map[string]plugin.Plugin{
6585
"configmanager_token_store": &tokenstore.GRPCPlugin{Impl: ts},
6686
},
87+
//
6788
VersionedPlugins: map[int]plugin.PluginSet{
6889
1: {
6990
"configmanager_token_store": &tokenstore.GRPCPlugin{Impl: ts},

main_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main_test
2+
3+
import (
4+
"testing"
5+
6+
main "github.com/DevLabFoundry/configmanager-plugin-awsparamstr"
7+
)
8+
9+
func Test_PluginInit(t *testing.T) {
10+
t.Run("init plugin", func(t *testing.T) {
11+
_, err := main.PluginSetup()
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
})
16+
}
17+
18+
func Test_ShowVersion(t *testing.T) {
19+
t.Run("show Version", func(t *testing.T) {
20+
// os.Args = append(os.Args[:1], "--version")
21+
if !main.ShowFlag([]string{"--version"}) {
22+
t.Error("should show flag")
23+
}
24+
})
25+
26+
t.Run("do not ShowVersion", func(t *testing.T) {
27+
if main.ShowFlag([]string{"foo"}) {
28+
t.Error("should show flag")
29+
}
30+
})
31+
}

0 commit comments

Comments
 (0)