From 14233d6e9b37f5369839328b588aeab39c064f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=B7=E6=BA=AA?= Date: Thu, 25 Mar 2021 12:10:38 +0800 Subject: [PATCH] fix: sort otes configuration (#108) * fix: sort redis configuration * fix: yaml serialization * fix: add comment * fix: add comment * fix: doc * fix: doc * fix: sort otes config * test: fix different config * fix: test flakes * fix: test flakes * fix: test flakes * fix: test flakes Co-authored-by: marvin --- .github/workflows/go.yml | 8 ++++++++ otes/config.go | 13 +++++++++++++ otes/provider.go | 23 ++++++++--------------- otes/provider_test.go | 11 +++++------ otes/tracing_test.go | 12 ++++++------ ots3/example_test.go | 4 ++-- ots3/uploader_test.go | 10 +++++----- 7 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 otes/config.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index e084d186..e558f553 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -58,6 +58,14 @@ jobs: discovery.type: single-node ports: - 9200:9200 + minio: + image: bitnami/minio:latest + env: + MINIO_ACCESS_KEY: minioadmin + MINIO_SECRET_KEY: minioadmin + ports: + - 9000:9000 + options: --name minio-server steps: - uses: actions/checkout@v2 diff --git a/otes/config.go b/otes/config.go new file mode 100644 index 00000000..8391f853 --- /dev/null +++ b/otes/config.go @@ -0,0 +1,13 @@ +package otes + +// Config is the type of elasticsearch configurations. +type Config struct { + URL []string `json:"url" yaml:"url"` + Index string `json:"index" yaml:"index"` + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` + Shards int `json:"shards" yaml:"shards"` + Replicas int `json:"replicas" yaml:"replicas"` + Sniff *bool `json:"sniff" yaml:"sniff"` + Healthcheck *bool `json:"healthcheck" yaml:"healthcheck"` +} diff --git a/otes/provider.go b/otes/provider.go index 4f996db6..3e885e94 100644 --- a/otes/provider.go +++ b/otes/provider.go @@ -10,7 +10,6 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/olivere/elastic/v7" - esConfig "github.com/olivere/elastic/v7/config" "github.com/opentracing/opentracing-go" "go.uber.org/dig" ) @@ -34,7 +33,7 @@ func Providers() di.Deps { // EsConfigInterceptor is an injector type hint that allows user to do // last minute modification to es configurations. -type EsConfigInterceptor func(name string, opt *esConfig.Config) +type EsConfigInterceptor func(name string, opt *Config) // Maker models Factory type Maker interface { @@ -80,7 +79,7 @@ type out struct { // package core. func provideEsFactory(p in) (out, func()) { var err error - var esConfigs map[string]esConfig.Config + var esConfigs map[string]Config err = p.Conf.Unmarshal("es", &esConfigs) if err != nil { level.Warn(p.Logger).Log("err", err) @@ -88,14 +87,14 @@ func provideEsFactory(p in) (out, func()) { factory := di.NewFactory(func(name string) (di.Pair, error) { var ( ok bool - conf esConfig.Config + conf Config options []elastic.ClientOptionFunc ) if conf, ok = esConfigs[name]; !ok { if name != "default" { return di.Pair{}, fmt.Errorf("elastic configuration %s not valid", name) } - conf.URL = "http://localhost:9200" + conf.URL = []string{"http://localhost:9200"} } if p.Interceptor != nil { p.Interceptor(name, &conf) @@ -120,7 +119,7 @@ func provideEsFactory(p in) (out, func()) { } logger := log.With(p.Logger, "tag", "es") options = append(options, - elastic.SetURL(conf.URL), + elastic.SetURL(conf.URL...), elastic.SetBasicAuth(conf.Username, conf.Password), elastic.SetInfoLog(esLogAdapter{level.Info(logger)}), elastic.SetErrorLog(esLogAdapter{level.Error(logger)}), @@ -163,16 +162,10 @@ func provideConfig() configOut { { Owner: "otes", Data: map[string]interface{}{ - "es": map[string]map[string]interface{}{ + "es": map[string]Config{ "default": { - "url": "http://localhost:9200", - "index": "", - "username": "", - "password": "", - "shards": 1, - "replicas": 0, - "sniff": false, - "healthCheck": false, + URL: []string{"http://localhost:9200"}, + Shards: 1, }, }, }, diff --git a/otes/provider_test.go b/otes/provider_test.go index fca29044..9348957c 100644 --- a/otes/provider_test.go +++ b/otes/provider_test.go @@ -6,16 +6,15 @@ import ( "github.com/DoNewsCode/core/config" "github.com/go-kit/kit/log" "github.com/olivere/elastic/v7" - esConfig "github.com/olivere/elastic/v7/config" "github.com/stretchr/testify/assert" "testing" ) func TestNewEsFactory(t *testing.T) { esFactory, cleanup := provideEsFactory(in{ - Conf: config.MapAdapter{"es": map[string]esConfig.Config{ - "default": {URL: "http://localhost:9200"}, - "alternative": {URL: "http://localhost:9200"}, + Conf: config.MapAdapter{"es": map[string]Config{ + "default": {URL: []string{"http://localhost:9200"}}, + "alternative": {URL: []string{"http://localhost:9200"}}, }}, Logger: log.NewNopLogger(), Tracer: nil, @@ -33,8 +32,8 @@ func TestNewEsFactory(t *testing.T) { func TestNewEsFactoryWithOptions(t *testing.T) { var called bool esFactory, cleanup := provideEsFactory(in{ - Conf: config.MapAdapter{"es": map[string]esConfig.Config{ - "default": {URL: "http://localhost:9200"}, + Conf: config.MapAdapter{"es": map[string]Config{ + "default": {URL: []string{"http://localhost:9200"}}, }}, Logger: log.NewNopLogger(), Options: []elastic.ClientOptionFunc{ diff --git a/otes/tracing_test.go b/otes/tracing_test.go index 107abbf2..c0b51697 100644 --- a/otes/tracing_test.go +++ b/otes/tracing_test.go @@ -4,23 +4,23 @@ package otes import ( "context" + "net/http" + "testing" + "github.com/DoNewsCode/core/config" "github.com/go-kit/kit/log" - esConfig "github.com/olivere/elastic/v7/config" "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/mocktracer" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) func TestTracing(t *testing.T) { tracer := mocktracer.New() opentracing.SetGlobalTracer(tracer) factory, cleanup := provideEsFactory(in{ - Conf: config.MapAdapter{"es": map[string]esConfig.Config{ - "default": {URL: "http://localhost:9200"}, - "alternative": {URL: "http://localhost:9200"}, + Conf: config.MapAdapter{"es": map[string]Config{ + "default": {URL: []string{"http://localhost:9200"}}, + "alternative": {URL: []string{"http://localhost:9200"}}, }}, Logger: log.NewNopLogger(), Tracer: tracer, diff --git a/ots3/example_test.go b/ots3/example_test.go index b3f01060..e5ea372f 100644 --- a/ots3/example_test.go +++ b/ots3/example_test.go @@ -18,11 +18,11 @@ func Example() { panic(err) } defer file.Close() - uploader := NewManager("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "https://play.minio.io:9000", "asia", "mybucket") + uploader := NewManager("minioadmin", "minioadmin", "http://localhost:9000", "asia", "mybucket") _ = uploader.CreateBucket(context.Background(), "mybucket") url, _ := uploader.Upload(context.Background(), "foo", file) fmt.Println(url) // Output: - // https://play.minio.io:9000/mybucket/foo.png + // http://localhost:9000/mybucket/foo.png } diff --git a/ots3/uploader_test.go b/ots3/uploader_test.go index 3c1b972d..61afafaf 100644 --- a/ots3/uploader_test.go +++ b/ots3/uploader_test.go @@ -35,7 +35,7 @@ func TestNewManager(t *testing.T) { } func TestMain(m *testing.M) { - manager := NewManager("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "https://play.minio.io:9000", "asia", "mybucket") + manager := NewManager("minioadmin", "minioadmin", "http://localhost:9000", "asia", "mybucket") _ = manager.CreateBucket(context.Background(), "foo") os.Exit(m.Run()) } @@ -65,7 +65,7 @@ func TestManager_UploadFromUrl(t *testing.T) { tracer := mocktracer.New() m := setupManagerWithTracer(tracer) _ = m.CreateBucket(context.Background(), "mybucket") - newURL, err := m.UploadFromUrl(context.Background(), "https://www.donews.com/static/v2/images/full-logo.png") + newURL, err := m.UploadFromUrl(context.Background(), "https://avatars.githubusercontent.com/u/43054062") assert.NoError(t, err) assert.NotEmpty(t, newURL) assert.Len(t, tracer.FinishedSpans(), 2) @@ -77,9 +77,9 @@ func setupManager() *Manager { func setupManagerWithTracer(tracer opentracing.Tracer) *Manager { m := NewManager( - "Q3AM3UQ867SPQQA43P2F", - "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", - "https://play.minio.io:9000", + "minioadmin", + "minioadmin", + "http://localhost:9000", "asia", "mybucket", WithTracer(tracer),