2020-08-26 03:46:22 +00:00
|
|
|
// Copyright 2020
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/clientv3"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEtcdConfigerProvider_Parse(t *testing.T) {
|
|
|
|
provider := &EtcdConfigerProvider{}
|
|
|
|
cfger, err := provider.Parse(readEtcdConfig())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, cfger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEtcdConfiger(t *testing.T) {
|
|
|
|
|
|
|
|
provider := &EtcdConfigerProvider{}
|
|
|
|
cfger, _ := provider.Parse(readEtcdConfig())
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
subCfger, err := cfger.Sub("sub.")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, subCfger)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
subSubCfger, err := subCfger.Sub("sub.")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.NotNil(t, subSubCfger)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
str, err := subSubCfger.String("key1")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "sub.sub.key", str)
|
|
|
|
|
|
|
|
// we cannot test it
|
2020-10-13 14:32:41 +00:00
|
|
|
subSubCfger.OnChange("watch", func(value string) {
|
2020-08-26 03:46:22 +00:00
|
|
|
// do nothing
|
|
|
|
})
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
defStr := cfger.DefaultString("not_exit", "default value")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Equal(t, "default value", defStr)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
defInt64 := cfger.DefaultInt64("not_exit", -1)
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Equal(t, int64(-1), defInt64)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
defInt := cfger.DefaultInt("not_exit", -2)
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Equal(t, -2, defInt)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
defFlt := cfger.DefaultFloat("not_exit", 12.3)
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Equal(t, 12.3, defFlt)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
defBl := cfger.DefaultBool("not_exit", true)
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.True(t, defBl)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
defStrs := cfger.DefaultStrings("not_exit", []string{"hello"})
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Equal(t, []string{"hello"}, defStrs)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
fl, err := cfger.Float("current.float")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 1.23, fl)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
bl, err := cfger.Bool("current.bool")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, bl)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
it, err := cfger.Int("current.int")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 11, it)
|
|
|
|
|
2020-10-13 14:32:41 +00:00
|
|
|
str, err = cfger.String("current.string")
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "hello", str)
|
|
|
|
|
|
|
|
tn := &TestEntity{}
|
2020-10-13 14:32:41 +00:00
|
|
|
err = cfger.Unmarshaler("current.serialize.", tn)
|
2020-08-26 03:46:22 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "test", tn.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestEntity struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Sub SubEntity `yaml:"sub"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubEntity struct {
|
|
|
|
SubName string `yaml:"subName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func readEtcdConfig() string {
|
|
|
|
addr := os.Getenv("ETCD_ADDR")
|
|
|
|
if addr == "" {
|
|
|
|
addr = "localhost:2379"
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := clientv3.Config{
|
|
|
|
Endpoints: []string{addr},
|
|
|
|
DialTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
cfg, _ := json.Marshal(obj)
|
|
|
|
return string(cfg)
|
|
|
|
}
|