test: increase endpoint,env, options and reader tests coverage. (#2192)

* test(config/file): add format test (#2147)

* test(internal/endpoint): increase endpoint tests coverage.(#2147)

* test(config/env): increase env tests coverage.(#2147)

* test(config/options): increase options tests coverage.(#2147)

* test(config/reader): increase reader tests coverage.(#2147)
pull/2207/head
shengzhou 2 years ago committed by GitHub
parent bcef2b8e3f
commit ede5f889d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      config/env/env_test.go
  2. 29
      config/options_test.go
  3. 115
      config/reader_test.go
  4. 84
      internal/endpoint/endpoint_test.go

@ -417,3 +417,13 @@ func Test_matchPrefix(t *testing.T) {
})
}
}
func Test_env_watch(t *testing.T) {
prefixs := []string{"BAR", "FOO"}
source := NewSource(prefixs...)
w, err := source.Watch()
if err != nil {
t.Errorf("expect no err, got %v", err)
}
_ = w.Stop()
}

@ -2,6 +2,7 @@ package config
import (
"reflect"
"strings"
"testing"
)
@ -197,3 +198,31 @@ func TestDefaultResolver(t *testing.T) {
})
}
}
func TestExpand(t *testing.T) {
tests := []struct {
input string
mapping func(string) string
want string
}{
{
input: "${a}",
mapping: func(s string) string {
return strings.ToUpper(s)
},
want: "A",
},
{
input: "a",
mapping: func(s string) string {
return strings.ToUpper(s)
},
want: "a",
},
}
for _, tt := range tests {
if got := expand(tt.input, tt.mapping); got != tt.want {
t.Errorf("expand() want: %s, got: %s", tt.want, got)
}
}
}

@ -208,3 +208,118 @@ func TestReader_Source(t *testing.T) {
t.Fatal("[]byte(`{\"a\":{\"b\":{\"X\":1}}}`) is not equal to b")
}
}
func TestCloneMap(t *testing.T) {
tests := []struct {
input map[string]interface{}
want map[string]interface{}
}{
{
input: map[string]interface{}{
"a": 1,
"b": "2",
"c": true,
},
want: map[string]interface{}{
"a": 1,
"b": "2",
"c": true,
},
},
{
input: map[string]interface{}{},
want: map[string]interface{}{},
},
{
input: nil,
want: map[string]interface{}{},
},
}
for _, tt := range tests {
if got, err := cloneMap(tt.input); err != nil {
t.Errorf("expect no err, got %v", err)
} else if !reflect.DeepEqual(got, tt.want) {
t.Errorf("cloneMap(%v) = %v, want %v", tt.input, got, tt.want)
}
}
}
func TestConvertMap(t *testing.T) {
tests := []struct {
input interface{}
want interface{}
}{
{
input: map[string]interface{}{
"a": 1,
"b": "2",
"c": true,
"d": []byte{65, 66, 67},
},
want: map[string]interface{}{
"a": 1,
"b": "2",
"c": true,
"d": "ABC",
},
},
{
input: []interface{}{1, 2.0, "3", true, nil, []interface{}{1, 2.0, "3", true, nil}},
want: []interface{}{1, 2.0, "3", true, nil, []interface{}{1, 2.0, "3", true, nil}},
},
{
input: []byte{65, 66, 67},
want: "ABC",
},
}
for _, tt := range tests {
if got := convertMap(tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertMap(%v) = %v, want %v", tt.input, got, tt.want)
}
}
}
func TestReadValue(t *testing.T) {
m := map[string]interface{}{
"a": 1,
"b": map[string]interface{}{
"c": "3",
"d": map[string]interface{}{
"e": true,
},
},
}
va := atomicValue{}
va.Store(1)
vbc := atomicValue{}
vbc.Store("3")
vbde := atomicValue{}
vbde.Store(true)
tests := []struct {
path string
want atomicValue
}{
{
path: "a",
want: va,
},
{
path: "b.c",
want: vbc,
},
{
path: "b.d.e",
want: vbde,
},
}
for _, tt := range tests {
if got, found := readValue(m, tt.path); !found {
t.Errorf("expect found %v in %v, but not.", tt.path, m)
} else if got.Load() != tt.want.Load() {
t.Errorf("readValue(%v, %v) = %v, want %v", m, tt.path, got, tt.want)
}
}
}

@ -139,3 +139,87 @@ func TestParseEndpoint(t *testing.T) {
})
}
}
func TestIsSecure(t *testing.T) {
tests := []struct {
url *url.URL
want bool
}{
{
url: &url.URL{Scheme: "http", Host: "127.0.0.1"},
want: false,
},
{
url: &url.URL{Scheme: "http", Host: "127.0.0.1", RawQuery: "isSecure=false"},
want: false,
},
{
url: &url.URL{Scheme: "grpc", Host: "127.0.0.1", RawQuery: "isSecure=true"},
want: true,
},
}
for _, tt := range tests {
if got := IsSecure(tt.url); got != tt.want {
t.Errorf("IsSecure() = %v, want %v", got, tt.want)
}
}
}
func TestLegacyURLToNew(t *testing.T) {
tests := []struct {
url *url.URL
want *url.URL
}{
{
url: &url.URL{Scheme: "http", Host: "www.google.com", RawQuery: "isSecure=true"},
want: &url.URL{Scheme: "https", Host: "www.google.com"},
},
{
url: &url.URL{Scheme: "https", Host: "www.google.com", RawQuery: "isSecure=true"},
want: &url.URL{Scheme: "https", Host: "www.google.com", RawQuery: "isSecure=true"},
},
{
url: &url.URL{Scheme: "http", Host: "go-kratos.dev", RawQuery: "isSecure=false"},
want: &url.URL{Scheme: "http", Host: "go-kratos.dev", RawQuery: "isSecure=false"},
},
}
for _, tt := range tests {
if got := legacyURLToNew(tt.url); !reflect.DeepEqual(got, tt.want) {
t.Errorf("legacyURLToNew() = %v, want %v", got, tt.want)
}
}
}
func TestSchema(t *testing.T) {
tests := []struct {
schema string
secure bool
want string
}{
{
schema: "http",
secure: true,
want: "https",
},
{
schema: "http",
secure: false,
want: "http",
},
{
schema: "grpc",
secure: true,
want: "grpcs",
},
{
schema: "grpc",
secure: false,
want: "grpc",
},
}
for _, tt := range tests {
if got := Scheme(tt.schema, tt.secure); got != tt.want {
t.Errorf("Schema() = %v, want %v", got, tt.want)
}
}
}

Loading…
Cancel
Save