Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cluster address in env (close #131) #133

Merged
merged 8 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions config/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ import (
"testing"
"time"

"github.com/DoNewsCode/core/internal"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
)

func TestRemote(t *testing.T) {
if os.Getenv("ETCD_ADDR") == "" {
t.Skip("Set env ETCD_ADDR to run remote tests")
var envDefaultEtcdAddrs, envDefaultEtcdAddrsIsSet = internal.GetDefaultAddrsFromEnv("ETCD_ADDR", "127.0.0.1:2379")

func TestMain(m *testing.M) {
if !envDefaultEtcdAddrsIsSet {
fmt.Println("Set env ETCD_ADDR to run remote tests")
os.Exit(0)
}
os.Exit(m.Run())
}

func TestRemote(t *testing.T) {
cfg := &clientv3.Config{
Endpoints: []string{os.Getenv("ETCD_ADDR")},
Endpoints: envDefaultEtcdAddrs,
DialTimeout: 2 * time.Second,
}

Expand Down
9 changes: 6 additions & 3 deletions dtx/sagas/mysqlstore/mysql_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/DoNewsCode/core"
"github.com/DoNewsCode/core/dtx"
"github.com/DoNewsCode/core/dtx/sagas"
"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/otgorm"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
Expand All @@ -23,8 +24,10 @@ func (m module) ProvideMigration() []*otgorm.Migration {
return Migrations("default")
}

var envDefaultMysqlDsn, envDefaultMysqlDsnIsSet = internal.GetDefaultAddrFromEnv("MYSQL_DSN", "root@tcp(127.0.0.1:3306)/app?charset=utf8mb4&parseTime=True&loc=Local")

func TestMain(m *testing.M) {
if os.Getenv("MYSQL_DSN") == "" {
if !envDefaultMysqlDsnIsSet {
fmt.Println("Set env MYSQL_DSN to run mysqlstore tests")
os.Exit(0)
}
Expand Down Expand Up @@ -262,7 +265,7 @@ func TestMySQLStore(t *testing.T) {
c := core.New(
core.WithInline("log.level", "debug"),
core.WithInline("gorm.default.database", "mysql"),
core.WithInline("gorm.default.dsn", "root@tcp(127.0.0.1:3306)/app?charset=utf8mb4&parseTime=True&loc=Local"),
core.WithInline("gorm.default.dsn", envDefaultMysqlDsn),
)
c.ProvideEssentials()
c.Provide(otgorm.Providers())
Expand All @@ -277,7 +280,7 @@ func TestStore_CleanUp(t *testing.T) {
c := core.New(
core.WithInline("log.level", "error"),
core.WithInline("gorm.default.database", "mysql"),
core.WithInline("gorm.default.dsn", "root@tcp(127.0.0.1:3306)/app?charset=utf8mb4&parseTime=True&loc=Local"),
core.WithInline("gorm.default.dsn", envDefaultMysqlDsn),
)
c.ProvideEssentials()
c.Provide(otgorm.Providers())
Expand Down
22 changes: 22 additions & 0 deletions internal/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package internal

import (
"os"
"strings"
)

// GetDefaultAddrsFromEnv return addrs
func GetDefaultAddrsFromEnv(env, defaultVal string) ([]string, bool) {
if v := os.Getenv(env); v != "" {
return strings.Split(v, ","), true
}
return []string{defaultVal}, false
}

// GetDefaultAddrFromEnv return addr/dsn
func GetDefaultAddrFromEnv(env, defaultVal string) (string, bool) {
if v := os.Getenv(env); v != "" {
return v, true
}
return defaultVal, false
}
7 changes: 5 additions & 2 deletions leader/election_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import (
"time"

"github.com/DoNewsCode/core/events"
"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/key"
leaderetcd2 "github.com/DoNewsCode/core/leader/leaderetcd"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
"go.uber.org/atomic"
)

var envDefaultEtcdAddrs, envDefaultEtcdAddrsIsSet = internal.GetDefaultAddrsFromEnv("ETCD_ADDR", "127.0.0.1:2379")

func TestMain(m *testing.M) {
if os.Getenv("ETCD_ADDR") == "" {
if !envDefaultEtcdAddrsIsSet {
fmt.Println("Set env ETCD_ADDR to run leader tests")
os.Exit(0)
}
Expand All @@ -27,7 +30,7 @@ func TestElection(t *testing.T) {
var dispatcher = &events.SyncDispatcher{}
var e1, e2 Election

client, err := clientv3.New(clientv3.Config{Endpoints: []string{"localhost:2379"}})
client, err := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs})
assert.NoError(t, err)
e1 = Election{
dispatcher: dispatcher,
Expand Down
17 changes: 13 additions & 4 deletions leader/leaderetcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ package leaderetcd

import (
"context"
"fmt"
"os"
"testing"

"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/key"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
)

func TestNewEtcdDriver(t *testing.T) {
if os.Getenv("ETCD_ADDR") == "" {
t.Skip("Set env ETCD_ADDR to run leaderetcd tests")
var envDefaultEtcdAddrs, envDefaultEtcdAddrsIsSet = internal.GetDefaultAddrsFromEnv("ETCD_ADDR", "127.0.0.1:2379")

func TestMain(m *testing.M) {
if !envDefaultEtcdAddrsIsSet {
fmt.Println("Set env ETCD_ADDR to run leaderetcd tests")
os.Exit(0)
}
client, _ := clientv3.New(clientv3.Config{Endpoints: []string{os.Getenv("ETCD_ADDR")}})
os.Exit(m.Run())
}

func TestNewEtcdDriver(t *testing.T) {
client, _ := clientv3.New(clientv3.Config{Endpoints: envDefaultEtcdAddrs})
e1 := NewEtcdDriver(client, key.New("test"))
e2 := NewEtcdDriver(client, key.New("test"))

Expand Down
17 changes: 13 additions & 4 deletions leader/leaderredis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ package leaderredis

import (
"context"
"fmt"
"os"
"testing"
"time"

"github.com/DoNewsCode/core/events"
"github.com/DoNewsCode/core/internal"
"github.com/DoNewsCode/core/key"
"github.com/DoNewsCode/core/leader"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
)

func TestCampaign(t *testing.T) {
if os.Getenv("REDIS_ADDR") == "" {
t.Skip("Set env REDIS_ADDR to run leaderredis tests")
var envDefaultRedisAddrs, envDefaultRedisAddrsIsSet = internal.GetDefaultAddrsFromEnv("REDIS_ADDR", "127.0.0.1:6379")

func TestMain(m *testing.M) {
if !envDefaultRedisAddrsIsSet {
fmt.Println("Set env REDIS_ADDR to run leaderredis tests")
os.Exit(0)
}
client := redis.NewUniversalClient(&redis.UniversalOptions{Addrs: []string{os.Getenv("REDIS_ADDR")}})
os.Exit(m.Run())
}

func TestCampaign(t *testing.T) {
client := redis.NewUniversalClient(&redis.UniversalOptions{Addrs: envDefaultRedisAddrs})
driver := RedisDriver{
client: client,
keyer: key.New(),
Expand Down
12 changes: 5 additions & 7 deletions otes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package otes
import (
"fmt"
"net/http"
"os"

"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/internal"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/olivere/elastic/v7"
Expand Down Expand Up @@ -95,11 +95,7 @@ func provideEsFactory(p in) (out, func()) {
if name != "default" {
return di.Pair{}, fmt.Errorf("elastic configuration %s not valid", name)
}
defaultURL := "http://localhost:9200"
if os.Getenv("ELASTICSEARCH_ADDR") != "" {
defaultURL = os.Getenv("ELASTICSEARCH_ADDR")
}
conf.URL = []string{defaultURL}
conf.URL = envDefaultElasticsearchAddrs
}
if p.Interceptor != nil {
p.Interceptor(name, &conf)
Expand Down Expand Up @@ -169,7 +165,7 @@ func provideConfig() configOut {
Data: map[string]interface{}{
"es": map[string]Config{
"default": {
URL: []string{"http://localhost:9200"},
URL: envDefaultElasticsearchAddrs,
Shards: 1,
},
},
Expand All @@ -179,3 +175,5 @@ func provideConfig() configOut {
}
return configOut{Config: configs}
}

var envDefaultElasticsearchAddrs, envDefaultElasticsearchAddrsIsSet = internal.GetDefaultAddrsFromEnv("ELASTICSEARCH_ADDR", "http://127.0.0.1:9200")
14 changes: 8 additions & 6 deletions otes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ package otes

import (
"fmt"
"os"
"testing"

"github.com/DoNewsCode/core/config"
"github.com/go-kit/kit/log"
"github.com/olivere/elastic/v7"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestMain(m *testing.M) {
if os.Getenv("ELASTICSEARCH_ADDR") == "" {
if !envDefaultElasticsearchAddrsIsSet {
fmt.Println("Set env ELASTICSEARCH_ADDR to run otes tests")
os.Exit(0)
}

os.Exit(m.Run())
}

func TestNewEsFactory(t *testing.T) {
esFactory, cleanup := provideEsFactory(in{
Conf: config.MapAdapter{"es": map[string]Config{
"default": {URL: []string{os.Getenv("ELASTICSEARCH_ADDR")}},
"alternative": {URL: []string{os.Getenv("ELASTICSEARCH_ADDR")}},
"default": {URL: envDefaultElasticsearchAddrs},
"alternative": {URL: envDefaultElasticsearchAddrs},
}},
Logger: log.NewNopLogger(),
Tracer: nil,
Expand All @@ -41,7 +43,7 @@ func TestNewEsFactoryWithOptions(t *testing.T) {
var called bool
esFactory, cleanup := provideEsFactory(in{
Conf: config.MapAdapter{"es": map[string]Config{
"default": {URL: []string{os.Getenv("ELASTICSEARCH_ADDR")}},
"default": {URL: envDefaultElasticsearchAddrs},
}},
Logger: log.NewNopLogger(),
Options: []elastic.ClientOptionFunc{
Expand Down
7 changes: 3 additions & 4 deletions otes/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package otes
import (
"context"
"net/http"
"os"
"testing"

"github.com/DoNewsCode/core/config"
Expand All @@ -18,8 +17,8 @@ func TestTracing(t *testing.T) {
opentracing.SetGlobalTracer(tracer)
factory, cleanup := provideEsFactory(in{
Conf: config.MapAdapter{"es": map[string]Config{
"default": {URL: []string{"http://localhost:9200"}},
"alternative": {URL: []string{"http://localhost:9200"}},
"default": {URL: envDefaultElasticsearchAddrs},
"alternative": {URL: envDefaultElasticsearchAddrs},
}},
Logger: log.NewNopLogger(),
Tracer: tracer,
Expand All @@ -31,7 +30,7 @@ func TestTracing(t *testing.T) {
span, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "es.query")
defer span.Finish()

res, code, err := client.Ping(os.Getenv("ELASTICSEARCH_ADDR")).Do(ctx)
res, code, err := client.Ping(envDefaultElasticsearchAddrs[0]).Do(ctx)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, code)
assert.NotNil(t, res)
Expand Down
14 changes: 5 additions & 9 deletions otetcd/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package otetcd

import (
"fmt"
"os"
"time"

"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/internal"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/opentracing-contrib/go-grpc"
Expand Down Expand Up @@ -95,13 +95,7 @@ func provideFactory(p factoryIn) (FactoryOut, func()) {
if name != "default" {
return di.Pair{}, fmt.Errorf("etcd configuration %s not valid", name)
}

defaultEndpoint := "127.0.0.1:2379"

if os.Getenv("ETCD_ADDR") != "" {
defaultEndpoint = os.Getenv("ETCD_ADDR")
}
conf = Option{Endpoints: []string{defaultEndpoint}}
conf = Option{Endpoints: envDefaultEtcdAddrs}
}
co := clientv3.Config{
Endpoints: conf.Endpoints,
Expand Down Expand Up @@ -164,7 +158,7 @@ func provideConfig() configOut {
map[string]interface{}{
"etcd": map[string]Option{
"default": {
Endpoints: []string{"127.0.0.1:2379"},
Endpoints: envDefaultEtcdAddrs,
AutoSyncInterval: config.Duration{},
DialTimeout: config.Duration{},
DialKeepAliveTime: config.Duration{},
Expand All @@ -191,3 +185,5 @@ func provideConfig() configOut {
func duration(d config.Duration) time.Duration {
return d.Duration
}

var envDefaultEtcdAddrs, envDefaultEtcdAddrsIsSet = internal.GetDefaultAddrsFromEnv("ETCD_ADDR", "127.0.0.1:2379")
7 changes: 4 additions & 3 deletions otetcd/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
)

func TestMain(m *testing.M) {
if os.Getenv("ETCD_ADDR") == "" {
if !envDefaultEtcdAddrsIsSet {
fmt.Println("Set env ETCD_ADDR to run otetcd tests")
os.Exit(0)
}

os.Exit(m.Run())
}

Expand Down Expand Up @@ -47,10 +48,10 @@ func TestProvideFactory(t *testing.T) {
out, cleanup := provideFactory(factoryIn{
Conf: config.MapAdapter{"etcd": map[string]Option{
"default": {
Endpoints: []string{"localhost:2379"},
Endpoints: envDefaultEtcdAddrs,
},
"alternative": {
Endpoints: []string{"localhost:2379"},
Endpoints: envDefaultEtcdAddrs,
},
}},
Logger: log.NewNopLogger(),
Expand Down
6 changes: 3 additions & 3 deletions otetcd/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package otetcd

import (
"context"
"testing"

"github.com/DoNewsCode/core/config"
"github.com/go-kit/kit/log"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/client/v3"
"os"
"testing"
)

func TestTracing(t *testing.T) {
Expand All @@ -19,7 +19,7 @@ func TestTracing(t *testing.T) {
Logger: log.NewNopLogger(),
Conf: config.MapAdapter{"etcd": map[string]Option{
"default": {
Endpoints: []string{os.Getenv("ETCD_ADDR")},
Endpoints: envDefaultEtcdAddrs,
},
}},
Interceptor: func(name string, options *clientv3.Config) {
Expand Down
Loading