diff options
Diffstat (limited to 'service/env')
-rw-r--r-- | service/env/config.go | 22 | ||||
-rw-r--r-- | service/env/config_test.go | 41 | ||||
-rw-r--r-- | service/env/environment.go | 23 | ||||
-rw-r--r-- | service/env/service.go | 55 | ||||
-rw-r--r-- | service/env/service_test.go | 80 |
5 files changed, 0 insertions, 221 deletions
diff --git a/service/env/config.go b/service/env/config.go deleted file mode 100644 index a7da695e..00000000 --- a/service/env/config.go +++ /dev/null @@ -1,22 +0,0 @@ -package env - -import ( - "github.com/spiral/roadrunner/service" -) - -// Config defines set of env values for RR workers. -type Config struct { - // values to set as worker _ENV. - Values map[string]string -} - -// Hydrate must populate Config values using given Config source. Must return error if Config is not valid. -func (c *Config) Hydrate(cfg service.Config) error { - return cfg.Unmarshal(&c.Values) -} - -// InitDefaults allows to init blank config with pre-defined set of default values. -func (c *Config) InitDefaults() error { - c.Values = make(map[string]string) - return nil -} diff --git a/service/env/config_test.go b/service/env/config_test.go deleted file mode 100644 index a526990d..00000000 --- a/service/env/config_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package env - -import ( - json "github.com/json-iterator/go" - "github.com/spiral/roadrunner/service" - "github.com/stretchr/testify/assert" - "testing" -) - -type mockCfg struct{ cfg string } - -func (cfg *mockCfg) Get(name string) service.Config { return nil } -func (cfg *mockCfg) Unmarshal(out interface{}) error { - j := json.ConfigCompatibleWithStandardLibrary - return j.Unmarshal([]byte(cfg.cfg), out) -} - -func Test_Config_Hydrate(t *testing.T) { - cfg := &mockCfg{`{"key":"value"}`} - c := &Config{} - - assert.NoError(t, c.Hydrate(cfg)) - assert.Len(t, c.Values, 1) -} - -func Test_Config_Hydrate_Empty(t *testing.T) { - cfg := &mockCfg{`{}`} - c := &Config{} - - assert.NoError(t, c.Hydrate(cfg)) - assert.Len(t, c.Values, 0) -} - -func Test_Config_Defaults(t *testing.T) { - c := &Config{} - err := c.InitDefaults() - if err != nil { - t.Errorf("Test_Config_Defaults failed: error %v", err) - } - assert.Len(t, c.Values, 0) -} diff --git a/service/env/environment.go b/service/env/environment.go deleted file mode 100644 index ab8febf7..00000000 --- a/service/env/environment.go +++ /dev/null @@ -1,23 +0,0 @@ -package env - -// Environment aggregates list of environment variables. This interface can be used in custom implementation to drive -// values from external sources. -type Environment interface { - Setter - Getter - - // Copy all environment values. - Copy(setter Setter) error -} - -// Setter provides ability to set environment value. -type Setter interface { - // SetEnv sets or creates environment value. - SetEnv(key, value string) -} - -// Getter provides ability to set environment value. -type Getter interface { - // GetEnv must return list of env variables. - GetEnv() (map[string]string, error) -} diff --git a/service/env/service.go b/service/env/service.go deleted file mode 100644 index 83175b36..00000000 --- a/service/env/service.go +++ /dev/null @@ -1,55 +0,0 @@ -package env - -// ID contains default service name. -const ID = "env" - -// Service provides ability to map _ENV values from config file. -type Service struct { - // values is default set of values. - values map[string]string -} - -// NewService creates new env service instance for given rr version. -func NewService(defaults map[string]string) *Service { - s := &Service{values: defaults} - return s -} - -// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of -// misconfiguration. Services must not be used without proper configuration pushed first. -func (s *Service) Init(cfg *Config) (bool, error) { - if s.values == nil { - s.values = make(map[string]string) - s.values["RR"] = "true" - } - - for k, v := range cfg.Values { - s.values[k] = v - } - - return true, nil -} - -// GetEnv must return list of env variables. -func (s *Service) GetEnv() (map[string]string, error) { - return s.values, nil -} - -// SetEnv sets or creates environment value. -func (s *Service) SetEnv(key, value string) { - s.values[key] = value -} - -// Copy all environment values. -func (s *Service) Copy(setter Setter) error { - values, err := s.GetEnv() - if err != nil { - return err - } - - for k, v := range values { - setter.SetEnv(k, v) - } - - return nil -} diff --git a/service/env/service_test.go b/service/env/service_test.go deleted file mode 100644 index 19cc03c7..00000000 --- a/service/env/service_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package env - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func Test_NewService(t *testing.T) { - s := NewService(map[string]string{"version": "test"}) - assert.Len(t, s.values, 1) -} - -func Test_Init(t *testing.T) { - var err error - s := &Service{} - _, err = s.Init(&Config{}) - if err != nil { - t.Errorf("error during the s.Init: error %v", err) - } - assert.Len(t, s.values, 1) - - values, err := s.GetEnv() - assert.NoError(t, err) - assert.Equal(t, "true", values["RR"]) -} - -func Test_Extend(t *testing.T) { - var err error - s := NewService(map[string]string{"RR": "version"}) - - _, err = s.Init(&Config{Values: map[string]string{"key": "value"}}) - if err != nil { - t.Errorf("error during the s.Init: error %v", err) - } - assert.Len(t, s.values, 2) - - values, err := s.GetEnv() - assert.NoError(t, err) - assert.Len(t, values, 2) - assert.Equal(t, "version", values["RR"]) - assert.Equal(t, "value", values["key"]) -} - -func Test_Set(t *testing.T) { - var err error - s := NewService(map[string]string{"RR": "version"}) - - _, err = s.Init(&Config{Values: map[string]string{"key": "value"}}) - if err != nil { - t.Errorf("error during the s.Init: error %v", err) - } - assert.Len(t, s.values, 2) - - s.SetEnv("key", "value-new") - s.SetEnv("other", "new") - - values, err := s.GetEnv() - assert.NoError(t, err) - assert.Len(t, values, 3) - assert.Equal(t, "version", values["RR"]) - assert.Equal(t, "value-new", values["key"]) - assert.Equal(t, "new", values["other"]) -} - -func Test_Copy(t *testing.T) { - s1 := NewService(map[string]string{"RR": "version"}) - s2 := NewService(map[string]string{}) - - s1.SetEnv("key", "value-new") - s1.SetEnv("other", "new") - - assert.NoError(t, s1.Copy(s2)) - - values, err := s2.GetEnv() - assert.NoError(t, err) - assert.Len(t, values, 3) - assert.Equal(t, "version", values["RR"]) - assert.Equal(t, "value-new", values["key"]) - assert.Equal(t, "new", values["other"]) -} |