-
Notifications
You must be signed in to change notification settings - Fork 9
/
classic_test.go
150 lines (114 loc) · 2.65 KB
/
classic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package uconfig_test
import (
"encoding/json"
"flag"
"fmt"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/omeid/uconfig"
"github.com/omeid/uconfig/internal/f"
"github.com/omeid/uconfig/plugins/secret"
)
func TestMain(m *testing.M) {
// for go test framework.
flag.Parse()
os.Exit(m.Run())
}
func TestClassicBasic(t *testing.T) {
expect := f.Config{
Anon: f.Anon{
Version: "from-flags",
},
GoHard: true,
Redis: f.Redis{
Host: "from-envs",
Port: 6379,
},
Rethink: f.RethinkConfig{
Host: f.Host{
Address: "rethink-cluster",
Port: "28015",
},
Db: "base",
},
}
files := uconfig.Files{
{"testdata/classic.json", json.Unmarshal, true},
}
value := f.Config{}
// set some env vars to test env var and plugin orders.
os.Setenv("VERSION", "bad-value-overrided-with-flags")
os.Setenv("REDIS_HOST", "from-envs")
// patch the os.Args. for our tests.
os.Args = append(os.Args[:1], "-version=from-flags")
_, err := uconfig.Classic(&value, files)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expect, value); diff != "" {
t.Error(diff)
}
}
func TestClassicWithSecret(t *testing.T) {
//Config is part of text fixtures.
type Creds struct {
APIKey string `secret:""`
APIToken string `secret:"API_TOKEN"`
}
type Config struct {
Redis f.Redis
Rethink f.RethinkConfig
Creds Creds
}
expect := Config{
Redis: f.Redis{
Host: "redis-host",
Port: 6379,
},
Rethink: f.RethinkConfig{
Host: f.Host{
Address: "rethink-cluster",
Port: "28015",
},
Db: "base",
Password: "top secret token",
},
Creds: Creds{
APIKey: "top secret token",
APIToken: "top secret token",
},
}
files := uconfig.Files{
{"testdata/classic.json", json.Unmarshal, true},
}
value := Config{}
SecretProvider := func(name string) (string, error) {
// known secrets.
if name == "API_TOKEN" || name == "RETHINK_PASSWORD" || name == "CREDS_APIKEY" {
return "top secret token", nil
}
return "", fmt.Errorf("Secret not found %s", name)
}
// patch the os.Args. for our tests.
os.Args = os.Args[:1]
os.Unsetenv("REDIS_HOST")
_, err := uconfig.Classic(&value, files, secret.New(SecretProvider))
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expect, value); diff != "" {
t.Error(diff)
}
}
func TestClassicBadPlugin(t *testing.T) {
var badPlugin BadPlugin
config := f.Config{}
_, err := uconfig.Classic(&config, nil, badPlugin)
if err == nil {
t.Error("expected error for bad plugin, got nil")
}
if err.Error() != "Unsupported plugins. Expecting a Walker or Visitor" {
t.Errorf("Expected unsupported plugin error, got: %v", err)
}
}