Skip to content

Commit

Permalink
Merge pull request #2 from cybozu-go/prefix-in-config
Browse files Browse the repository at this point in the history
Add argument prefix to NewConfig() tp prevent miss configuration
  • Loading branch information
ymmt2005 committed Aug 6, 2018
2 parents af3efa3 + 4408e01 commit 4d7b9c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 1 addition & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func testEtcdClient(t *testing.T) {
clientURL = etcdClientURL
}

cfg := NewConfig()
cfg.Prefix = t.Name() + "/"
cfg := NewConfig(t.Name() + "/")
cfg.Endpoints = []string{clientURL}
_, err := NewClient(cfg)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ type Config struct {
}

// NewConfig creates Config with default values.
func NewConfig() *Config {
func NewConfig(prefix string) *Config {
return &Config{
Endpoints: DefaultEndpoints,
Prefix: prefix,
Timeout: DefaultTimeout,
}
}
15 changes: 12 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func testEtcdConfigYAML(t *testing.T) {
t.Parallel()

prefix := t.Name() + "/"
cases := []struct {
source string
expected Config
Expand Down Expand Up @@ -39,6 +40,7 @@ password: testpass
expected: Config{
Endpoints: DefaultEndpoints,
Timeout: DefaultTimeout,
Prefix: prefix,
Username: "testuser",
Password: "testpass",
},
Expand All @@ -52,6 +54,7 @@ tls-key: client.key
expected: Config{
Endpoints: DefaultEndpoints,
Timeout: DefaultTimeout,
Prefix: prefix,
TLSCA: "ca.crt",
TLSCert: "client.crt",
TLSKey: "client.key",
Expand All @@ -60,7 +63,7 @@ tls-key: client.key
}

for _, c := range cases {
cfg := NewConfig()
cfg := NewConfig(prefix)
err := yaml.Unmarshal([]byte(c.source), cfg)
if err != nil {
t.Error(err)
Expand All @@ -73,6 +76,7 @@ tls-key: client.key
func testEtcdConfigJSON(t *testing.T) {
t.Parallel()

prefix := t.Name() + "/"
cases := []struct {
source string
expected Config
Expand Down Expand Up @@ -105,6 +109,7 @@ func testEtcdConfigJSON(t *testing.T) {
expected: Config{
Endpoints: DefaultEndpoints,
Timeout: DefaultTimeout,
Prefix: prefix,
Username: "testuser",
Password: "testpass",
},
Expand All @@ -120,6 +125,7 @@ func testEtcdConfigJSON(t *testing.T) {
expected: Config{
Endpoints: DefaultEndpoints,
Timeout: DefaultTimeout,
Prefix: prefix,
TLSCA: "ca.crt",
TLSCert: "client.crt",
TLSKey: "client.key",
Expand All @@ -128,7 +134,7 @@ func testEtcdConfigJSON(t *testing.T) {
}

for _, c := range cases {
cfg := NewConfig()
cfg := NewConfig(prefix)
err := json.Unmarshal([]byte(c.source), cfg)
if err != nil {
t.Error(err)
Expand All @@ -141,6 +147,7 @@ func testEtcdConfigJSON(t *testing.T) {
func testEtcdConfigTOML(t *testing.T) {
t.Parallel()

prefix := t.Name() + "/"
cases := []struct {
source string
expected Config
Expand All @@ -165,6 +172,7 @@ password = "testpass"
expected: Config{
Endpoints: DefaultEndpoints,
Timeout: DefaultTimeout,
Prefix: prefix,
Username: "testuser",
Password: "testpass",
},
Expand All @@ -178,6 +186,7 @@ tls-key = "client.key"
expected: Config{
Endpoints: DefaultEndpoints,
Timeout: DefaultTimeout,
Prefix: prefix,
TLSCA: "ca.crt",
TLSCert: "client.crt",
TLSKey: "client.key",
Expand All @@ -186,7 +195,7 @@ tls-key = "client.key"
}

for _, c := range cases {
cfg := NewConfig()
cfg := NewConfig(prefix)
err := toml.Unmarshal([]byte(c.source), cfg)
if err != nil {
t.Error(err)
Expand Down

0 comments on commit 4d7b9c2

Please sign in to comment.