rename config tests

This commit is contained in:
Asim Aslam 2019-07-28 19:52:01 +01:00
parent b7b8f8bf11
commit 2b5bf1154a
2 changed files with 46 additions and 89 deletions

View File

@ -8,9 +8,25 @@ import (
"testing" "testing"
"time" "time"
"github.com/micro/go-micro/config/source/env"
"github.com/micro/go-micro/config/source/file" "github.com/micro/go-micro/config/source/file"
) )
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
}
func createFileForTest(t *testing.T) *os.File { func createFileForTest(t *testing.T) *os.File {
data := []byte(`{"foo": "bar"}`) data := []byte(`{"foo": "bar"}`)
path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano())) path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
@ -26,7 +42,7 @@ func createFileForTest(t *testing.T) *os.File {
return fh return fh
} }
func TestLoadWithGoodFile(t *testing.T) { func TestConfigLoadWithGoodFile(t *testing.T) {
fh := createFileForTest(t) fh := createFileForTest(t)
path := fh.Name() path := fh.Name()
defer func() { defer func() {
@ -44,7 +60,7 @@ func TestLoadWithGoodFile(t *testing.T) {
} }
} }
func TestLoadWithInvalidFile(t *testing.T) { func TestConfigLoadWithInvalidFile(t *testing.T) {
fh := createFileForTest(t) fh := createFileForTest(t)
path := fh.Name() path := fh.Name()
defer func() { defer func() {
@ -68,34 +84,35 @@ func TestLoadWithInvalidFile(t *testing.T) {
} }
} }
func TestConsul(t *testing.T) { func TestConfigMerge(t *testing.T) {
/*consulSource := consul.NewSource( fh := createFileForIssue18(t, `{
// optionally specify consul address; default to localhost:8500 "amqp": {
consul.WithAddress("131.150.38.111:8500"), "host": "rabbit.platform",
// optionally specify prefix; defaults to /micro/config "port": 80
consul.WithPrefix("/project"), },
// optionally strip the provided prefix from the keys, defaults to false "handler": {
consul.StripPrefix(true), "exchange": "springCloudBus"
consul.WithDatacenter("dc1"), }
consul.WithToken("xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), }`)
path := fh.Name()
defer func() {
fh.Close()
os.Remove(path)
}()
os.Setenv("AMQP_HOST", "rabbit.testing.com")
conf := NewConfig()
conf.Load(
file.NewSource(
file.WithPath(path),
),
env.NewSource(),
) )
// Create new config actualHost := conf.Get("amqp", "host").String("backup")
conf := NewConfig() if actualHost != "rabbit.testing.com" {
t.Fatalf("Expected %v but got %v",
// Load file source "rabbit.testing.com",
err := conf.Load(consulSource) actualHost)
if err != nil {
t.Error(err)
return
} }
m := conf.Map()
t.Log("m: ", m)
v := conf.Get("project", "dc111", "port")
t.Log("v: ", v.Int(13))*/
t.Log("OK")
} }

View File

@ -1,60 +0,0 @@
package config
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/micro/go-micro/config/source/env"
"github.com/micro/go-micro/config/source/file"
)
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
}
func TestIssue18(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")
conf := NewConfig()
conf.Load(
file.NewSource(
file.WithPath(path),
),
env.NewSource(),
)
actualHost := conf.Get("amqp", "host").String("backup")
if actualHost != "rabbit.testing.com" {
t.Fatalf("Expected %v but got %v",
"rabbit.testing.com",
actualHost)
}
}