2020-08-25 13:44:41 +03:00
|
|
|
// +build ignore
|
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-06-16 19:10:52 +03:00
|
|
|
"runtime"
|
2019-05-31 01:11:13 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config/source"
|
|
|
|
"github.com/unistack-org/micro/v3/config/source/env"
|
|
|
|
"github.com/unistack-org/micro/v3/config/source/file"
|
|
|
|
"github.com/unistack-org/micro/v3/config/source/memory"
|
2019-05-31 01:11:13 +03:00
|
|
|
)
|
|
|
|
|
2019-07-28 21:52:01 +03:00
|
|
|
func createFileForIssue18(t *testing.T, content string) *os.File {
|
|
|
|
data := []byte(content)
|
|
|
|
path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
|
|
|
|
fh, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_, err = fh.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fh
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
func createFileForTest(t *testing.T) *os.File {
|
|
|
|
data := []byte(`{"foo": "bar"}`)
|
|
|
|
path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
|
|
|
|
fh, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_, err = fh.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fh
|
|
|
|
}
|
|
|
|
|
2019-07-28 21:52:01 +03:00
|
|
|
func TestConfigLoadWithGoodFile(t *testing.T) {
|
2019-05-31 01:11:13 +03:00
|
|
|
fh := createFileForTest(t)
|
|
|
|
path := fh.Name()
|
|
|
|
defer func() {
|
|
|
|
fh.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Create new config
|
2020-01-19 11:31:02 +03:00
|
|
|
conf, err := NewConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected no error but got %v", err)
|
|
|
|
}
|
2019-05-31 01:11:13 +03:00
|
|
|
// Load file source
|
|
|
|
if err := conf.Load(file.NewSource(
|
|
|
|
file.WithPath(path),
|
|
|
|
)); err != nil {
|
|
|
|
t.Fatalf("Expected no error but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 21:52:01 +03:00
|
|
|
func TestConfigLoadWithInvalidFile(t *testing.T) {
|
2019-05-31 01:11:13 +03:00
|
|
|
fh := createFileForTest(t)
|
|
|
|
path := fh.Name()
|
|
|
|
defer func() {
|
|
|
|
fh.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Create new config
|
2020-01-19 11:31:02 +03:00
|
|
|
conf, err := NewConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected no error but got %v", err)
|
|
|
|
}
|
2019-05-31 01:11:13 +03:00
|
|
|
// Load file source
|
2020-01-19 11:31:02 +03:00
|
|
|
err = conf.Load(file.NewSource(
|
2019-05-31 01:11:13 +03:00
|
|
|
file.WithPath(path),
|
|
|
|
file.WithPath("/i/do/not/exists.json"),
|
|
|
|
))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error but none !")
|
|
|
|
}
|
|
|
|
if !strings.Contains(fmt.Sprintf("%v", err), "/i/do/not/exists.json") {
|
|
|
|
t.Fatalf("Expected error to contain the unexisting file but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 21:52:01 +03:00
|
|
|
func TestConfigMerge(t *testing.T) {
|
|
|
|
fh := createFileForIssue18(t, `{
|
|
|
|
"amqp": {
|
|
|
|
"host": "rabbit.platform",
|
|
|
|
"port": 80
|
|
|
|
},
|
|
|
|
"handler": {
|
|
|
|
"exchange": "springCloudBus"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
path := fh.Name()
|
|
|
|
defer func() {
|
|
|
|
fh.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
}()
|
|
|
|
os.Setenv("AMQP_HOST", "rabbit.testing.com")
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2020-01-19 11:31:02 +03:00
|
|
|
conf, err := NewConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected no error but got %v", err)
|
|
|
|
}
|
|
|
|
if err := conf.Load(
|
2019-07-28 21:52:01 +03:00
|
|
|
file.NewSource(
|
|
|
|
file.WithPath(path),
|
|
|
|
),
|
|
|
|
env.NewSource(),
|
2020-01-19 11:31:02 +03:00
|
|
|
); err != nil {
|
|
|
|
t.Fatalf("Expected no error but got %v", err)
|
|
|
|
}
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2019-07-28 21:52:01 +03:00
|
|
|
actualHost := conf.Get("amqp", "host").String("backup")
|
|
|
|
if actualHost != "rabbit.testing.com" {
|
|
|
|
t.Fatalf("Expected %v but got %v",
|
|
|
|
"rabbit.testing.com",
|
|
|
|
actualHost)
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 19:10:52 +03:00
|
|
|
|
|
|
|
func equalS(t *testing.T, actual, expect string) {
|
|
|
|
if actual != expect {
|
|
|
|
t.Errorf("Expected %s but got %s", actual, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigWatcherDirtyOverrite(t *testing.T) {
|
|
|
|
n := runtime.GOMAXPROCS(0)
|
|
|
|
defer runtime.GOMAXPROCS(n)
|
|
|
|
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
|
|
|
|
l := 100
|
|
|
|
|
2020-08-21 14:53:21 +03:00
|
|
|
ss := make([]source.Source, l)
|
2020-06-16 19:10:52 +03:00
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
ss[i] = memory.NewSource(memory.WithJSON([]byte(fmt.Sprintf(`{"key%d": "val%d"}`, i, i))))
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, _ := NewConfig()
|
|
|
|
|
|
|
|
for _, s := range ss {
|
|
|
|
_ = conf.Load(s)
|
|
|
|
}
|
|
|
|
runtime.Gosched()
|
|
|
|
|
2020-08-25 14:33:36 +03:00
|
|
|
for i := range ss {
|
2020-06-16 19:10:52 +03:00
|
|
|
k := fmt.Sprintf("key%d", i)
|
|
|
|
v := fmt.Sprintf("val%d", i)
|
|
|
|
equalS(t, conf.Get(k).String(""), v)
|
|
|
|
}
|
|
|
|
}
|