micro/config/default_test.go

123 lines
2.3 KiB
Go
Raw Normal View History

2019-05-30 23:11:13 +01:00
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
2019-07-28 19:52:01 +01:00
"github.com/micro/go-micro/config/source/env"
2019-05-30 23:11:13 +01:00
"github.com/micro/go-micro/config/source/file"
)
var (
sep = string(os.PathSeparator)
)
2019-07-28 19:52:01 +01: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-30 23:11:13 +01: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 19:52:01 +01:00
func TestConfigLoadWithGoodFile(t *testing.T) {
2019-05-30 23:11:13 +01:00
fh := createFileForTest(t)
path := fh.Name()
defer func() {
fh.Close()
os.Remove(path)
}()
// Create new config
conf := NewConfig()
// 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 19:52:01 +01:00
func TestConfigLoadWithInvalidFile(t *testing.T) {
2019-05-30 23:11:13 +01:00
fh := createFileForTest(t)
path := fh.Name()
defer func() {
fh.Close()
os.Remove(path)
}()
// Create new config
conf := NewConfig()
// Load file source
err := conf.Load(file.NewSource(
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 19:52:01 +01: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-30 23:11:13 +01:00
conf := NewConfig()
2019-07-28 19:52:01 +01:00
conf.Load(
file.NewSource(
file.WithPath(path),
),
env.NewSource(),
)
2019-05-30 23:11:13 +01:00
2019-07-28 19:52:01 +01: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-30 23:11:13 +01:00
}
}