2014-04-05 01:21:59 +04:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package viper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-09 23:48:55 +04:00
|
|
|
"fmt"
|
2014-09-28 01:03:00 +04:00
|
|
|
"os"
|
2014-09-28 01:00:51 +04:00
|
|
|
"sort"
|
2015-03-06 22:21:17 +03:00
|
|
|
"strings"
|
2014-04-05 01:21:59 +04:00
|
|
|
"testing"
|
2014-09-28 01:00:51 +04:00
|
|
|
"time"
|
2014-04-05 01:21:59 +04:00
|
|
|
|
2014-10-09 23:48:55 +04:00
|
|
|
"github.com/spf13/pflag"
|
2014-04-05 01:21:59 +04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var yamlExample = []byte(`Hacker: true
|
|
|
|
name: steve
|
|
|
|
hobbies:
|
|
|
|
- skateboarding
|
|
|
|
- snowboarding
|
|
|
|
- go
|
2014-05-30 00:48:24 +04:00
|
|
|
clothing:
|
|
|
|
jacket: leather
|
|
|
|
trousers: denim
|
2014-08-05 15:35:21 +04:00
|
|
|
age: 35
|
2014-10-10 00:39:24 +04:00
|
|
|
eyes : brown
|
2014-08-05 15:35:21 +04:00
|
|
|
beard: true
|
|
|
|
`)
|
2014-04-05 01:21:59 +04:00
|
|
|
|
|
|
|
var tomlExample = []byte(`
|
|
|
|
title = "TOML Example"
|
|
|
|
|
|
|
|
[owner]
|
|
|
|
organization = "MongoDB"
|
2014-04-05 09:19:39 +04:00
|
|
|
Bio = "MongoDB Chief Developer Advocate & Hacker at Large"
|
2014-04-05 01:21:59 +04:00
|
|
|
dob = 1979-05-27T07:32:00Z # First class dates? Why not?`)
|
|
|
|
|
|
|
|
var jsonExample = []byte(`{
|
|
|
|
"id": "0001",
|
|
|
|
"type": "donut",
|
|
|
|
"name": "Cake",
|
|
|
|
"ppu": 0.55,
|
|
|
|
"batters": {
|
|
|
|
"batter": [
|
|
|
|
{ "type": "Regular" },
|
|
|
|
{ "type": "Chocolate" },
|
|
|
|
{ "type": "Blueberry" },
|
|
|
|
{ "type": "Devil's Food" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
2015-04-14 21:15:02 +03:00
|
|
|
var propertiesExample = []byte(`
|
|
|
|
p_id: 0001
|
|
|
|
p_type: donut
|
|
|
|
p_name: Cake
|
|
|
|
p_ppu: 0.55
|
|
|
|
p_batters.batter.type: Regular
|
|
|
|
`)
|
|
|
|
|
2014-10-27 22:32:46 +03:00
|
|
|
var remoteExample = []byte(`{
|
|
|
|
"id":"0002",
|
|
|
|
"type":"cronut",
|
|
|
|
"newkey":"remote"
|
|
|
|
}`)
|
|
|
|
|
2014-12-23 02:31:11 +03:00
|
|
|
func initConfigs() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("yaml")
|
|
|
|
r := bytes.NewReader(yamlExample)
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
|
|
|
|
SetConfigType("json")
|
|
|
|
r = bytes.NewReader(jsonExample)
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
|
2015-04-14 21:15:02 +03:00
|
|
|
SetConfigType("properties")
|
|
|
|
r = bytes.NewReader(propertiesExample)
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
|
2014-12-23 02:31:11 +03:00
|
|
|
SetConfigType("toml")
|
|
|
|
r = bytes.NewReader(tomlExample)
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
|
|
|
|
SetConfigType("json")
|
|
|
|
remote := bytes.NewReader(remoteExample)
|
|
|
|
marshalReader(remote, v.kvstore)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initYAML() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("yaml")
|
|
|
|
r := bytes.NewReader(yamlExample)
|
|
|
|
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initJSON() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("json")
|
|
|
|
r := bytes.NewReader(jsonExample)
|
|
|
|
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:15:02 +03:00
|
|
|
func initProperties() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("properties")
|
|
|
|
r := bytes.NewReader(propertiesExample)
|
|
|
|
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:31:11 +03:00
|
|
|
func initTOML() {
|
|
|
|
Reset()
|
|
|
|
SetConfigType("toml")
|
|
|
|
r := bytes.NewReader(tomlExample)
|
|
|
|
|
|
|
|
marshalReader(r, v.config)
|
|
|
|
}
|
|
|
|
|
2014-10-09 23:48:55 +04:00
|
|
|
//stubs for PFlag Values
|
|
|
|
type stringValue string
|
|
|
|
|
|
|
|
func newStringValue(val string, p *string) *stringValue {
|
|
|
|
*p = val
|
|
|
|
return (*stringValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) Set(val string) error {
|
|
|
|
*s = stringValue(val)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) Type() string {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) String() string {
|
|
|
|
return fmt.Sprintf("%s", *s)
|
|
|
|
}
|
|
|
|
|
2014-04-05 01:21:59 +04:00
|
|
|
func TestBasics(t *testing.T) {
|
|
|
|
SetConfigFile("/tmp/config.yaml")
|
2014-12-05 05:55:51 +03:00
|
|
|
assert.Equal(t, "/tmp/config.yaml", v.getConfigFile())
|
2014-04-05 01:21:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
SetDefault("age", 45)
|
|
|
|
assert.Equal(t, 45, Get("age"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalling(t *testing.T) {
|
|
|
|
SetConfigType("yaml")
|
|
|
|
r := bytes.NewReader(yamlExample)
|
|
|
|
|
2014-12-06 11:48:28 +03:00
|
|
|
marshalReader(r, v.config)
|
2014-04-05 01:21:59 +04:00
|
|
|
assert.True(t, InConfig("name"))
|
|
|
|
assert.False(t, InConfig("state"))
|
|
|
|
assert.Equal(t, "steve", Get("name"))
|
|
|
|
assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, Get("hobbies"))
|
2014-05-30 00:48:24 +04:00
|
|
|
assert.Equal(t, map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, Get("clothing"))
|
2014-04-05 01:21:59 +04:00
|
|
|
assert.Equal(t, 35, Get("age"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverrides(t *testing.T) {
|
|
|
|
Set("age", 40)
|
|
|
|
assert.Equal(t, 40, Get("age"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultPost(t *testing.T) {
|
|
|
|
assert.NotEqual(t, "NYC", Get("state"))
|
|
|
|
SetDefault("state", "NYC")
|
|
|
|
assert.Equal(t, "NYC", Get("state"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAliases(t *testing.T) {
|
|
|
|
RegisterAlias("years", "age")
|
|
|
|
assert.Equal(t, 40, Get("years"))
|
|
|
|
Set("years", 45)
|
|
|
|
assert.Equal(t, 45, Get("age"))
|
|
|
|
}
|
|
|
|
|
2014-08-05 15:35:21 +04:00
|
|
|
func TestAliasInConfigFile(t *testing.T) {
|
|
|
|
// the config file specifies "beard". If we make this an alias for
|
|
|
|
// "hasbeard", we still want the old config file to work with beard.
|
|
|
|
RegisterAlias("beard", "hasbeard")
|
|
|
|
assert.Equal(t, true, Get("hasbeard"))
|
|
|
|
Set("hasbeard", false)
|
|
|
|
assert.Equal(t, false, Get("beard"))
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:40:14 +04:00
|
|
|
func TestYML(t *testing.T) {
|
2014-12-23 02:31:11 +03:00
|
|
|
initYAML()
|
2014-05-08 18:40:14 +04:00
|
|
|
assert.Equal(t, "steve", Get("name"))
|
|
|
|
}
|
|
|
|
|
2014-04-05 01:21:59 +04:00
|
|
|
func TestJSON(t *testing.T) {
|
2014-12-23 02:31:11 +03:00
|
|
|
initJSON()
|
2014-04-05 01:21:59 +04:00
|
|
|
assert.Equal(t, "0001", Get("id"))
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:15:02 +03:00
|
|
|
func TestProperties(t *testing.T) {
|
|
|
|
initProperties()
|
|
|
|
assert.Equal(t, "0001", Get("p_id"))
|
|
|
|
}
|
|
|
|
|
2014-04-05 01:21:59 +04:00
|
|
|
func TestTOML(t *testing.T) {
|
2014-12-23 02:31:11 +03:00
|
|
|
initTOML()
|
2014-04-05 01:21:59 +04:00
|
|
|
assert.Equal(t, "TOML Example", Get("title"))
|
|
|
|
}
|
|
|
|
|
2014-10-27 22:32:46 +03:00
|
|
|
func TestRemotePrecedence(t *testing.T) {
|
2014-12-23 02:31:11 +03:00
|
|
|
initJSON()
|
|
|
|
|
2014-10-27 22:32:46 +03:00
|
|
|
remote := bytes.NewReader(remoteExample)
|
|
|
|
assert.Equal(t, "0001", Get("id"))
|
2014-12-06 11:48:28 +03:00
|
|
|
marshalReader(remote, v.kvstore)
|
2014-10-27 22:32:46 +03:00
|
|
|
assert.Equal(t, "0001", Get("id"))
|
|
|
|
assert.NotEqual(t, "cronut", Get("type"))
|
|
|
|
assert.Equal(t, "remote", Get("newkey"))
|
|
|
|
Set("newkey", "newvalue")
|
|
|
|
assert.NotEqual(t, "remote", Get("newkey"))
|
|
|
|
assert.Equal(t, "newvalue", Get("newkey"))
|
|
|
|
Set("newkey", "remote")
|
|
|
|
}
|
|
|
|
|
2014-09-28 01:03:00 +04:00
|
|
|
func TestEnv(t *testing.T) {
|
2014-12-23 02:31:11 +03:00
|
|
|
initJSON()
|
|
|
|
|
2014-09-28 01:03:00 +04:00
|
|
|
BindEnv("id")
|
|
|
|
BindEnv("f", "FOOD")
|
|
|
|
|
|
|
|
os.Setenv("ID", "13")
|
|
|
|
os.Setenv("FOOD", "apple")
|
2014-09-28 01:01:11 +04:00
|
|
|
os.Setenv("NAME", "crunk")
|
2014-09-28 01:03:00 +04:00
|
|
|
|
|
|
|
assert.Equal(t, "13", Get("id"))
|
|
|
|
assert.Equal(t, "apple", Get("f"))
|
2014-09-28 01:01:11 +04:00
|
|
|
assert.Equal(t, "Cake", Get("name"))
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
|
|
|
|
assert.Equal(t, "crunk", Get("name"))
|
2015-03-06 22:21:17 +03:00
|
|
|
|
2014-09-28 01:03:00 +04:00
|
|
|
}
|
|
|
|
|
2014-12-23 02:31:11 +03:00
|
|
|
func TestEnvPrefix(t *testing.T) {
|
|
|
|
initJSON()
|
|
|
|
|
|
|
|
SetEnvPrefix("foo") // will be uppercased automatically
|
|
|
|
BindEnv("id")
|
|
|
|
BindEnv("f", "FOOD") // not using prefix
|
|
|
|
|
|
|
|
os.Setenv("FOO_ID", "13")
|
|
|
|
os.Setenv("FOOD", "apple")
|
|
|
|
os.Setenv("FOO_NAME", "crunk")
|
|
|
|
|
|
|
|
assert.Equal(t, "13", Get("id"))
|
|
|
|
assert.Equal(t, "apple", Get("f"))
|
|
|
|
assert.Equal(t, "Cake", Get("name"))
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
|
|
|
|
assert.Equal(t, "crunk", Get("name"))
|
|
|
|
}
|
|
|
|
|
2015-02-17 07:32:10 +03:00
|
|
|
func TestAutoEnv(t *testing.T) {
|
|
|
|
Reset()
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
os.Setenv("FOO_BAR", "13")
|
|
|
|
assert.Equal(t, "13", Get("foo_bar"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoEnvWithPrefix(t *testing.T) {
|
|
|
|
Reset()
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
SetEnvPrefix("Baz")
|
|
|
|
os.Setenv("BAZ_BAR", "13")
|
|
|
|
assert.Equal(t, "13", Get("bar"))
|
|
|
|
}
|
|
|
|
|
2015-03-06 22:21:17 +03:00
|
|
|
func TestSetEnvReplacer(t *testing.T) {
|
|
|
|
Reset()
|
|
|
|
|
|
|
|
AutomaticEnv()
|
|
|
|
os.Setenv("REFRESH_INTERVAL", "30s")
|
|
|
|
|
|
|
|
replacer := strings.NewReplacer("-", "_")
|
|
|
|
SetEnvKeyReplacer(replacer)
|
|
|
|
|
|
|
|
assert.Equal(t, "30s", Get("refresh-interval"))
|
|
|
|
}
|
|
|
|
|
2014-09-28 01:00:51 +04:00
|
|
|
func TestAllKeys(t *testing.T) {
|
2014-12-23 02:31:11 +03:00
|
|
|
initConfigs()
|
|
|
|
|
2015-04-14 21:15:02 +03:00
|
|
|
ks := sort.StringSlice{"title", "newkey", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type", "eyes", "p_id", "p_ppu", "p_batters.batter.type", "p_type", "p_name"}
|
2014-09-28 01:00:51 +04:00
|
|
|
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
2015-04-14 21:15:02 +03:00
|
|
|
all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "Bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[interface{}]interface{}{"trousers": "denim", "jacket": "leather"}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters.batter.type": "Regular", "p_type": "donut"}
|
2014-09-28 01:00:51 +04:00
|
|
|
|
|
|
|
var allkeys sort.StringSlice
|
|
|
|
allkeys = AllKeys()
|
|
|
|
allkeys.Sort()
|
|
|
|
ks.Sort()
|
|
|
|
|
|
|
|
assert.Equal(t, ks, allkeys)
|
|
|
|
assert.Equal(t, all, AllSettings())
|
|
|
|
}
|
|
|
|
|
2014-04-05 09:19:39 +04:00
|
|
|
func TestCaseInSensitive(t *testing.T) {
|
|
|
|
assert.Equal(t, true, Get("hacker"))
|
|
|
|
Set("Title", "Checking Case")
|
|
|
|
assert.Equal(t, "Checking Case", Get("tItle"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAliasesOfAliases(t *testing.T) {
|
|
|
|
RegisterAlias("Foo", "Bar")
|
|
|
|
RegisterAlias("Bar", "Title")
|
|
|
|
assert.Equal(t, "Checking Case", Get("FOO"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecursiveAliases(t *testing.T) {
|
|
|
|
RegisterAlias("Baz", "Roo")
|
|
|
|
RegisterAlias("Roo", "baz")
|
2014-04-05 01:21:59 +04:00
|
|
|
}
|
2014-06-27 01:58:55 +04:00
|
|
|
|
2014-08-05 14:07:25 +04:00
|
|
|
func TestMarshal(t *testing.T) {
|
2014-06-27 01:58:55 +04:00
|
|
|
SetDefault("port", 1313)
|
|
|
|
Set("name", "Steve")
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Port int
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
var C config
|
|
|
|
|
2014-08-05 14:07:25 +04:00
|
|
|
err := Marshal(&C)
|
2014-06-27 01:58:55 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, &C, &config{Name: "Steve", Port: 1313})
|
|
|
|
|
|
|
|
Set("port", 1234)
|
2014-08-05 14:07:25 +04:00
|
|
|
err = Marshal(&C)
|
2014-06-27 01:58:55 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decode into struct, %v", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, &C, &config{Name: "Steve", Port: 1234})
|
2014-09-28 01:03:00 +04:00
|
|
|
}
|
2014-10-09 23:48:55 +04:00
|
|
|
|
2015-04-02 04:38:54 +03:00
|
|
|
func TestBindPFlags(t *testing.T) {
|
|
|
|
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
|
|
|
|
|
|
|
var testValues = map[string]*string{
|
|
|
|
"host": nil,
|
|
|
|
"port": nil,
|
|
|
|
"endpoint": nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
var mutatedTestValues = map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"port": "6060",
|
|
|
|
"endpoint": "/public",
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, _ := range testValues {
|
|
|
|
testValues[name] = flagSet.String(name, "", "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := BindPFlags(flagSet)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error binding flag set, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
flagSet.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flag.Value.Set(mutatedTestValues[flag.Name])
|
|
|
|
flag.Changed = true
|
|
|
|
})
|
|
|
|
|
|
|
|
for name, expected := range mutatedTestValues {
|
|
|
|
assert.Equal(t, Get(name), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-09 23:48:55 +04:00
|
|
|
func TestBindPFlag(t *testing.T) {
|
|
|
|
var testString = "testing"
|
|
|
|
var testValue = newStringValue(testString, &testString)
|
|
|
|
|
|
|
|
flag := &pflag.Flag{
|
|
|
|
Name: "testflag",
|
|
|
|
Value: testValue,
|
|
|
|
Changed: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
BindPFlag("testvalue", flag)
|
|
|
|
|
|
|
|
assert.Equal(t, testString, Get("testvalue"))
|
|
|
|
|
|
|
|
flag.Value.Set("testing_mutate")
|
|
|
|
flag.Changed = true //hack for pflag usage
|
|
|
|
|
|
|
|
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
|
|
|
|
|
|
|
}
|
2014-10-10 00:39:24 +04:00
|
|
|
|
|
|
|
func TestBoundCaseSensitivity(t *testing.T) {
|
|
|
|
|
|
|
|
assert.Equal(t, "brown", Get("eyes"))
|
|
|
|
|
|
|
|
BindEnv("eYEs", "TURTLE_EYES")
|
|
|
|
os.Setenv("TURTLE_EYES", "blue")
|
|
|
|
|
|
|
|
assert.Equal(t, "blue", Get("eyes"))
|
|
|
|
|
|
|
|
var testString = "green"
|
|
|
|
var testValue = newStringValue(testString, &testString)
|
|
|
|
|
|
|
|
flag := &pflag.Flag{
|
|
|
|
Name: "eyeballs",
|
|
|
|
Value: testValue,
|
|
|
|
Changed: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
BindPFlag("eYEs", flag)
|
|
|
|
assert.Equal(t, "green", Get("eyes"))
|
|
|
|
|
|
|
|
}
|
2015-03-01 00:03:22 +03:00
|
|
|
|
|
|
|
func TestSizeInBytes(t *testing.T) {
|
|
|
|
input := map[string]uint{
|
|
|
|
"": 0,
|
|
|
|
"b": 0,
|
|
|
|
"12 bytes": 0,
|
|
|
|
"200000000000gb": 0,
|
|
|
|
"12 b": 12,
|
|
|
|
"43 MB": 43 * (1 << 20),
|
|
|
|
"10mb": 10 * (1 << 20),
|
|
|
|
"1gb": 1 << 30,
|
|
|
|
}
|
|
|
|
|
|
|
|
for str, expected := range input {
|
|
|
|
assert.Equal(t, expected, parseSizeInBytes(str), str)
|
|
|
|
}
|
|
|
|
}
|