Skip to content

Commit

Permalink
fix: allow elasticsearch settings to be read from ENV vars if no defa…
Browse files Browse the repository at this point in the history
…ults are defined within yaml files

For more information see: spf13/viper#761
  • Loading branch information
nielskrijger committed Sep 11, 2021
1 parent b95ef22 commit 1f39998
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -14,7 +14,11 @@ integration:

.PHONY: humantest
humantest:
LOG_HUMAN=true richgo test -v -p=1 -timeout=60s $(PKGS)
ifndef run
LOG_DEBUG=true LOG_HUMAN=true richgo test -v -p=1 -timeout=60s $(PKGS)
else
LOG_DEBUG=true LOG_HUMAN=true richgo test -v -p=1 -timeout=60s $(PKGS) -run $(run)
endif

.PHONY: coverage
coverage:
Expand Down
2 changes: 1 addition & 1 deletion config.go
Expand Up @@ -58,7 +58,7 @@ func LoadConfig(log zerolog.Logger, dir string, env string) (*viper.Viper, error
}

// Viper ignores environment variables when unmarshalling if no defaults are set.
// This should that, see also https://github.com/spf13/viper/issues/188
// This should fix that in some scenarios, see also https://github.com/spf13/viper/issues/188
for _, key := range v.AllKeys() {
val := v.Get(key)
v.Set(key, val)
Expand Down
2 changes: 2 additions & 0 deletions config_test.go
Expand Up @@ -63,4 +63,6 @@ func TestConfig_OverrideEnvVariables(t *testing.T) {
err = cfg.Sub("vars").Unmarshal(cfgStruct)
assert.Nil(t, err)
assert.Equal(t, "from-env", cfgStruct.Filename)

os.Clearenv()
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Expand Up @@ -14,7 +14,7 @@ services:
- "5432:5432"

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.12.0
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.1
environment:
- bootstrap.memory_lock=true
- discovery.type=single-node
Expand Down
16 changes: 7 additions & 9 deletions elasticsearch.go
Expand Up @@ -44,15 +44,13 @@ func (s *Elasticsearch) Name() string {
func (s *Elasticsearch) Configure(ctx *AppEnv) error {
s.log = ctx.Log

// unmarshal config and set defaults
s.Config = &elasticsearch7.Config{}

if !ctx.Config.InConfig("elasticsearch") {
return errMissingElasticsearchConfig
}

if err := ctx.Config.Sub("elasticsearch").Unmarshal(&s.Config); err != nil {
return fmt.Errorf("parsing elasticsearch configuration: %w", err)
// Fetch config from viper. Avoid unmarshal directly into elasticsearch7.Config
// as it doesn't work with env vars:
// https://github.com/spf13/viper/issues/761
s.Config = &elasticsearch7.Config{
Addresses: ctx.Config.GetStringSlice("elasticsearch.addresses"),
Username: ctx.Config.GetString("elasticsearch.username"),
Password: ctx.Config.GetString("elasticsearch.password"),
}

if len(s.Config.Addresses) == 0 {
Expand Down
15 changes: 12 additions & 3 deletions elasticsearch_test.go
Expand Up @@ -2,6 +2,7 @@ package goboot_test

import (
"context"
"os"
"testing"

"github.com/nielskrijger/goboot"
Expand Down Expand Up @@ -32,10 +33,18 @@ func TestElasticsearch_Success(t *testing.T) {
assert.NotNil(t, s.Config)
}

func TestElasticsearch_ErrorMissingConfig(t *testing.T) {
func TestElasticsearch_SuccessEnvs(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

s := &goboot.Elasticsearch{}
err := s.Configure(goboot.NewAppEnv("./testdata/elasticsearch", ""))
assert.EqualError(t, err, "missing \"elasticsearch\" configuration")
_ = os.Setenv("ELASTICSEARCH_USERNAME", "elastic")
_ = os.Setenv("ELASTICSEARCH_PASSWORD", "secret")

err := s.Configure(goboot.NewAppEnv("./testdata/elasticsearch", "using-env-vars"))
assert.Nil(t, err)
os.Clearenv()
}

func TestElasticsearch_ErrorNoAddresses(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions testdata/elasticsearch/config.using-env-vars.yaml
@@ -0,0 +1,3 @@
elasticsearch:
addresses:
- http://localhost:9200

0 comments on commit 1f39998

Please sign in to comment.