diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/container.go | 17 | ||||
-rw-r--r-- | service/container_test.go | 69 | ||||
-rw-r--r-- | service/env/config.go | 6 | ||||
-rw-r--r-- | service/env/config_test.go | 6 | ||||
-rw-r--r-- | service/http/config.go | 21 | ||||
-rw-r--r-- | service/http/config_test.go | 16 | ||||
-rw-r--r-- | service/http/handler.go | 2 | ||||
-rw-r--r-- | service/http/handler_test.go | 36 | ||||
-rw-r--r-- | service/http/request.go | 2 | ||||
-rw-r--r-- | service/http/rpc_test.go | 6 | ||||
-rw-r--r-- | service/http/service.go | 25 | ||||
-rw-r--r-- | service/http/service_test.go | 43 | ||||
-rw-r--r-- | service/http/uploads_config.go | 6 | ||||
-rw-r--r-- | service/http/uploads_test.go | 8 | ||||
-rw-r--r-- | service/rpc/config.go | 8 | ||||
-rw-r--r-- | service/rpc/config_test.go | 14 | ||||
-rw-r--r-- | service/rpc/service_test.go | 13 | ||||
-rw-r--r-- | service/static/service_test.go | 30 |
18 files changed, 220 insertions, 108 deletions
diff --git a/service/container.go b/service/container.go index 861e1aac..fc1012c8 100644 --- a/service/container.go +++ b/service/container.go @@ -66,6 +66,11 @@ type HydrateConfig interface { Hydrate(cfg Config) error } +type DefaultsConfig interface { + // InitDefaults allows to init blank config with pre-defined set of default values. + InitDefaults() error +} + type container struct { log logrus.FieldLogger mu sync.Mutex @@ -257,11 +262,19 @@ func (c *container) resolveValues(s interface{}, m reflect.Method, cfg Config) ( values = append(values, reflect.ValueOf(c.log)) case v.Implements(reflect.TypeOf((*HydrateConfig)(nil)).Elem()): // injectable config - if cfg == nil { + sc := reflect.New(v.Elem()) + + if dsc, ok := sc.Interface().(DefaultsConfig); ok { + dsc.InitDefaults() + if cfg == nil { + values = append(values, sc) + continue + } + + } else if cfg == nil { return nil, errNoConfig } - sc := reflect.New(v.Elem()) if err := sc.Interface().(HydrateConfig).Hydrate(cfg); err != nil { return nil, err } diff --git a/service/container_test.go b/service/container_test.go index efccc182..8eeb647a 100644 --- a/service/container_test.go +++ b/service/container_test.go @@ -79,6 +79,37 @@ func (cfg *testCfg) Get(name string) Config { } func (cfg *testCfg) Unmarshal(out interface{}) error { return json.Unmarshal([]byte(cfg.cfg), out) } +// Config defines RPC service config. +type dConfig struct { + // Indicates if RPC connection is enabled. + Value string +} + +// Hydrate must populate Config values using given Config source. Must return error if Config is not valid. +func (c *dConfig) Hydrate(cfg Config) error { + if err := cfg.Unmarshal(c); err != nil { + return err + } + + return nil +} + +// InitDefaults allows to init blank config with pre-defined set of default values. +func (c *dConfig) InitDefaults() error { + c.Value = "default" + + return nil +} + +type dService struct { + Cfg *dConfig +} + +func (s *dService) Init(cfg *dConfig) (bool, error) { + s.Cfg = cfg + return true, nil +} + func TestContainer_Register(t *testing.T) { logger, hook := test.NewNullLogger() logger.SetLevel(logrus.DebugLevel) @@ -148,6 +179,44 @@ func TestContainer_Configure(t *testing.T) { assert.Equal(t, StatusOK, st) } +func TestContainer_Init_Default(t *testing.T) { + logger, hook := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + svc := &dService{} + + c := NewContainer(logger) + c.Register("test", svc) + assert.Equal(t, 1, len(hook.Entries)) + + assert.NoError(t, c.Init(&testCfg{`{}`})) + + s, st := c.Get("test") + assert.IsType(t, &dService{}, s) + assert.Equal(t, StatusOK, st) + + assert.Equal(t, "default", svc.Cfg.Value) +} + +func TestContainer_Init_Default_Overwrite(t *testing.T) { + logger, hook := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + svc := &dService{} + + c := NewContainer(logger) + c.Register("test", svc) + assert.Equal(t, 1, len(hook.Entries)) + + assert.NoError(t, c.Init(&testCfg{`{"test":{"value": "something"}}`})) + + s, st := c.Get("test") + assert.IsType(t, &dService{}, s) + assert.Equal(t, StatusOK, st) + + assert.Equal(t, "something", svc.Cfg.Value) +} + func TestContainer_ConfigureNull(t *testing.T) { logger, hook := test.NewNullLogger() logger.SetLevel(logrus.DebugLevel) diff --git a/service/env/config.go b/service/env/config.go index d0ba686b..a7da695e 100644 --- a/service/env/config.go +++ b/service/env/config.go @@ -14,3 +14,9 @@ type Config struct { 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 index 3ae2afbc..50fbdaa5 100644 --- a/service/env/config_test.go +++ b/service/env/config_test.go @@ -27,3 +27,9 @@ func Test_Config_Hydrate_Empty(t *testing.T) { assert.NoError(t, c.Hydrate(cfg)) assert.Len(t, c.Values, 0) } + +func Test_Config_Defaults(t *testing.T) { + c := &Config{} + c.InitDefaults() + assert.Len(t, c.Values, 0) +} diff --git a/service/http/config.go b/service/http/config.go index 5be42ae6..b11d807c 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -9,9 +9,6 @@ import ( // Config configures RoadRunner HTTP server. type Config struct { - // Enable enables http service. - Enable bool - // Address and port to handle as http server. Address string @@ -27,21 +24,27 @@ type Config struct { // 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 { - if err := cfg.Unmarshal(c); err != nil { - return err + if c.Workers == nil { + c.Workers = &roadrunner.ServerConfig{} } - if !c.Enable { - return nil + if c.Uploads == nil { + c.Uploads = &UploadsConfig{} } - if err := c.Valid(); err != nil { + c.Uploads.InitDefaults() + c.Workers.InitDefaults() + + if err := cfg.Unmarshal(c); err != nil { return err } - c.Workers.SetDefaults() c.Workers.UpscaleDurations() + if err := c.Valid(); err != nil { + return err + } + return nil } diff --git a/service/http/config_test.go b/service/http/config_test.go index 2e3fe731..823efb32 100644 --- a/service/http/config_test.go +++ b/service/http/config_test.go @@ -31,7 +31,6 @@ func Test_Config_Hydrate_Error2(t *testing.T) { func Test_Config_Valid(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -39,7 +38,7 @@ func Test_Config_Valid(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -54,11 +53,10 @@ func Test_Config_Valid(t *testing.T) { func Test_Config_NoUploads(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -73,7 +71,6 @@ func Test_Config_NoUploads(t *testing.T) { func Test_Config_NoWorkers(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -87,7 +84,6 @@ func Test_Config_NoWorkers(t *testing.T) { func Test_Config_NoPool(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -95,7 +91,7 @@ func Test_Config_NoPool(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 0, @@ -110,7 +106,6 @@ func Test_Config_NoPool(t *testing.T) { func Test_Config_DeadPool(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -118,7 +113,7 @@ func Test_Config_DeadPool(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", }, } @@ -128,7 +123,6 @@ func Test_Config_DeadPool(t *testing.T) { func Test_Config_InvalidAddress(t *testing.T) { cfg := &Config{ - Enable: true, Address: "", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -136,7 +130,7 @@ func Test_Config_InvalidAddress(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, diff --git a/service/http/handler.go b/service/http/handler.go index 9e67e5b4..945cd51e 100644 --- a/service/http/handler.go +++ b/service/http/handler.go @@ -51,7 +51,7 @@ func (h *Handler) Listen(l func(event int, ctx interface{})) { h.lsn = l } -// middleware serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled. +// mdwr serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // validating request size if h.cfg.MaxRequest != 0 { diff --git a/service/http/handler_test.go b/service/http/handler_test.go index 3b51c873..e864b86e 100644 --- a/service/http/handler_test.go +++ b/service/http/handler_test.go @@ -39,7 +39,7 @@ func TestServer_Echo(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -74,7 +74,7 @@ func Test_HandlerErrors(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -101,7 +101,7 @@ func Test_Handler_JSON_error(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -130,7 +130,7 @@ func TestServer_Headers(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php header pipes", + Command: "php ../../tests/http/client.php header pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -177,7 +177,7 @@ func TestServer_Cookies(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php cookie pipes", + Command: "php ../../tests/http/client.php cookie pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -228,7 +228,7 @@ func TestServer_JsonPayload_POST(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php payload pipes", + Command: "php ../../tests/http/client.php payload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -278,7 +278,7 @@ func TestServer_JsonPayload_PUT(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php payload pipes", + Command: "php ../../tests/http/client.php payload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -324,7 +324,7 @@ func TestServer_JsonPayload_PATCH(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php payload pipes", + Command: "php ../../tests/http/client.php payload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -370,7 +370,7 @@ func TestServer_FormData_POST(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -428,7 +428,7 @@ func TestServer_FormData_PUT(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -486,7 +486,7 @@ func TestServer_FormData_PATCH(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -544,7 +544,7 @@ func TestServer_Multipart_POST(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -606,7 +606,7 @@ func TestServer_Multipart_PUT(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -668,7 +668,7 @@ func TestServer_Multipart_PATCH(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -730,7 +730,7 @@ func TestServer_Error(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php error pipes", + Command: "php ../../tests/http/client.php error pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -764,7 +764,7 @@ func TestServer_Error2(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php error2 pipes", + Command: "php ../../tests/http/client.php error2 pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -798,7 +798,7 @@ func TestServer_Error3(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php pid pipes", + Command: "php ../../tests/http/client.php pid pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -843,7 +843,7 @@ func BenchmarkHandler_Listen_Echo(b *testing.B) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: int64(runtime.NumCPU()), diff --git a/service/http/request.go b/service/http/request.go index 531a1efd..d733b20c 100644 --- a/service/http/request.go +++ b/service/http/request.go @@ -49,7 +49,7 @@ type Request struct { // Uploads contains list of uploaded files, their names, sized and associations with temporary files. Uploads *Uploads `json:"uploads"` - // Attributes can be set by chained middleware to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions. + // Attributes can be set by chained mdwr to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions. Attributes map[string]interface{} `json:"attributes"` // request body can be parsedData or []byte diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go index 32bb776c..ba3efd2e 100644 --- a/service/http/rpc_test.go +++ b/service/http/rpc_test.go @@ -33,7 +33,7 @@ func Test_RPC(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -94,7 +94,7 @@ func Test_RPC_Unix(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -148,7 +148,7 @@ func Test_Workers(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, diff --git a/service/http/service.go b/service/http/service.go index ecce1c15..bb75a2c0 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -27,7 +27,7 @@ type Service struct { cfg *Config env env.Environment lsns []func(event int, ctx interface{}) - mdws []middleware + mdwr []middleware mu sync.Mutex rr *roadrunner.Server stopping int32 @@ -35,9 +35,9 @@ type Service struct { http *http.Server } -// AddMiddleware adds new net/http middleware. +// AddMiddleware adds new net/http mdwr. func (s *Service) AddMiddleware(m middleware) { - s.mdws = append(s.mdws, m) + s.mdwr = append(s.mdwr, m) } // AddListener attaches server event watcher. @@ -48,10 +48,6 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) { // 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, r *rpc.Service, e env.Environment) (bool, error) { - if !cfg.Enable { - return false, nil - } - s.cfg = cfg s.env = e if r != nil { @@ -87,17 +83,14 @@ func (s *Service) Serve() error { s.rr.Listen(s.listener) s.srv.Listen(s.listener) - if len(s.mdws) == 0 { - s.http.Handler = s.srv - } else { - s.http.Handler = s - } + s.http.Handler = s + s.mu.Unlock() if err := rr.Start(); err != nil { return err } - defer s.rr.Stop() + defer rr.Stop() return s.http.ListenAndServe() } @@ -118,13 +111,13 @@ func (s *Service) Stop() { s.http.Shutdown(context.Background()) } -// middleware handles connection using set of mdws and rr PSR-7 server. +// mdwr handles connection using set of mdwr and rr PSR-7 server. func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { r = attributes.Init(r) - // chaining middlewares + // chaining mdwr f := s.srv.ServeHTTP - for _, m := range s.mdws { + for _, m := range s.mdwr { f = m(f) } f(w, r) diff --git a/service/http/service_test.go b/service/http/service_test.go index 8dab7cd4..d1d601dc 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -25,6 +25,10 @@ type testCfg struct { func (cfg *testCfg) Get(name string) service.Config { if name == ID { + if cfg.httpCfg == "" { + return nil + } + return &testCfg{target: cfg.httpCfg} } @@ -63,24 +67,7 @@ func Test_Service_Configure_Disable(t *testing.T) { c := service.NewContainer(logger) c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{httpCfg: `{ - "enable": false, - "address": ":8070", - "maxRequest": 1024, - "uploads": { - "dir": ` + tmpDir() + `, - "forbid": [] - }, - "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", - "relay": "pipes", - "pool": { - "numWorkers": 1, - "allocateTimeout": 10000000, - "destroyTimeout": 10000000 - } - } - }`})) + assert.NoError(t, c.Init(&testCfg{})) s, st := c.Get(ID) assert.NotNil(t, s) @@ -103,7 +90,7 @@ func Test_Service_Configure_Enable(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -134,7 +121,7 @@ func Test_Service_Echo(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -187,7 +174,7 @@ func Test_Service_Env(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php env pipes", + "command": "php ../../tests/http/client.php env pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -239,7 +226,7 @@ func Test_Service_ErrorEcho(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echoerr pipes", + "command": "php ../../tests/http/client.php echoerr pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -299,7 +286,7 @@ func Test_Service_Middleware(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -373,7 +360,7 @@ func Test_Service_Listener(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -417,7 +404,7 @@ func Test_Service_Error(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "---", "pool": { "numWorkers": 1, @@ -446,7 +433,7 @@ func Test_Service_Error2(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php broken pipes", + "command": "php ../../tests/http/client.php broken pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -475,7 +462,7 @@ func Test_Service_Error3(t *testing.T) { "forbid": [] }, "workers" - "command": "php ../../php-src/tests/http/client.php broken pipes", + "command": "php ../../tests/http/client.php broken pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -502,7 +489,7 @@ func Test_Service_Error4(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php broken pipes", + "command": "php ../../tests/http/client.php broken pipes", "relay": "pipes", "pool": { "numWorkers": 1, diff --git a/service/http/uploads_config.go b/service/http/uploads_config.go index e90d9b70..3f655064 100644 --- a/service/http/uploads_config.go +++ b/service/http/uploads_config.go @@ -16,6 +16,12 @@ type UploadsConfig struct { Forbid []string } +// InitDefaults sets missing values to their default values. +func (cfg *UploadsConfig) InitDefaults() error { + cfg.Forbid = []string{".php", ".exe", ".bat"} + return nil +} + // TmpDir returns temporary directory. func (cfg *UploadsConfig) TmpDir() string { if cfg.Dir != "" { diff --git a/service/http/uploads_test.go b/service/http/uploads_test.go index b2662bf7..96e95733 100644 --- a/service/http/uploads_test.go +++ b/service/http/uploads_test.go @@ -27,7 +27,7 @@ func TestServer_Upload_File(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -88,7 +88,7 @@ func TestServer_Upload_NestedFile(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -149,7 +149,7 @@ func TestServer_Upload_File_NoTmpDir(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -210,7 +210,7 @@ func TestServer_Upload_File_Forbids(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, diff --git a/service/rpc/config.go b/service/rpc/config.go index 93fce7ca..653da6ea 100644 --- a/service/rpc/config.go +++ b/service/rpc/config.go @@ -26,6 +26,14 @@ func (c *Config) Hydrate(cfg service.Config) error { return c.Valid() } +// InitDefaults allows to init blank config with pre-defined set of default values. +func (c *Config) InitDefaults() error { + c.Enable = true + c.Listen = "tcp://127.0.0.1:6001" + + return nil +} + // Valid returns nil if config is valid. func (c *Config) Valid() error { if dsn := strings.Split(c.Listen, "://"); len(dsn) != 2 { diff --git a/service/rpc/config_test.go b/service/rpc/config_test.go index a7c51c0f..b335be19 100644 --- a/service/rpc/config_test.go +++ b/service/rpc/config_test.go @@ -27,6 +27,13 @@ func Test_Config_Hydrate_Error(t *testing.T) { assert.Error(t, c.Hydrate(cfg)) } +func Test_Config_Hydrate_Error2(t *testing.T) { + cfg := &testCfg{`{"enable": true, "listen": "invalid"`} + c := &Config{} + + assert.Error(t, c.Hydrate(cfg)) +} + func TestConfig_Listener(t *testing.T) { cfg := &Config{Listen: "tcp://:18001"} @@ -128,3 +135,10 @@ func Test_Config_DialerErrorMethod(t *testing.T) { assert.Nil(t, ln) assert.Error(t, err) } + +func Test_Config_Defaults(t *testing.T) { + c := &Config{} + c.InitDefaults() + assert.Equal(t, true, c.Enable) + assert.Equal(t, "tcp://127.0.0.1:6001", c.Listen) +} diff --git a/service/rpc/service_test.go b/service/rpc/service_test.go index 59e0e05d..467cbe3f 100644 --- a/service/rpc/service_test.go +++ b/service/rpc/service_test.go @@ -1,6 +1,7 @@ package rpc import ( + "github.com/spiral/roadrunner/service/env" "github.com/stretchr/testify/assert" "testing" "time" @@ -80,3 +81,15 @@ func Test_Serve_Client(t *testing.T) { assert.NoError(t, client.Call("test.Echo", "hello world", &resp)) assert.Equal(t, "hello world", resp) } + +func TestSetEnv(t *testing.T) { + s := &Service{} + e := env.NewService(map[string]string{}) + ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9018"}, e) + + assert.NoError(t, err) + assert.True(t, ok) + + v, _ := e.GetEnv() + assert.Equal(t, "tcp://localhost:9018", v["rr_rpc"]) +} diff --git a/service/static/service_test.go b/service/static/service_test.go index 842e5e0b..7b40b8ad 100644 --- a/service/static/service_test.go +++ b/service/static/service_test.go @@ -56,7 +56,7 @@ func Test_Files(t *testing.T) { c.Register(ID, &Service{}) assert.NoError(t, c.Init(&testCfg{ - static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[]}`, + static: `{"enable":true, "dir":"../../tests", "forbid":[]}`, httpCfg: `{ "enable": true, "address": ":6029", @@ -66,7 +66,7 @@ func Test_Files(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -93,7 +93,7 @@ func Test_Files_Disable(t *testing.T) { c.Register(ID, &Service{}) assert.NoError(t, c.Init(&testCfg{ - static: `{"enable":false, "dir":"../../php-src/tests", "forbid":[".php"]}`, + static: `{"enable":false, "dir":"../../tests", "forbid":[".php"]}`, httpCfg: `{ "enable": true, "address": ":6029", @@ -103,7 +103,7 @@ func Test_Files_Disable(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -140,7 +140,7 @@ func Test_Files_Error(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -170,7 +170,7 @@ func Test_Files_Error2(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -190,7 +190,7 @@ func Test_Files_Forbid(t *testing.T) { c.Register(ID, &Service{}) assert.NoError(t, c.Init(&testCfg{ - static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[".php"]}`, + static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`, httpCfg: `{ "enable": true, "address": ":6029", @@ -200,7 +200,7 @@ func Test_Files_Forbid(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -227,7 +227,7 @@ func Test_Files_NotFound(t *testing.T) { c.Register(ID, &Service{}) assert.NoError(t, c.Init(&testCfg{ - static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[".php"]}`, + static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`, httpCfg: `{ "enable": true, "address": ":6029", @@ -237,7 +237,7 @@ func Test_Files_NotFound(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -264,7 +264,7 @@ func Test_Files_Dir(t *testing.T) { c.Register(ID, &Service{}) assert.NoError(t, c.Init(&testCfg{ - static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[".php"]}`, + static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`, httpCfg: `{ "enable": true, "address": ":6029", @@ -274,7 +274,7 @@ func Test_Files_Dir(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -301,7 +301,7 @@ func Test_Files_NotForbid(t *testing.T) { c.Register(ID, &Service{}) assert.NoError(t, c.Init(&testCfg{ - static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[]}`, + static: `{"enable":true, "dir":"../../tests", "forbid":[]}`, httpCfg: `{ "enable": true, "address": ":6029", @@ -311,7 +311,7 @@ func Test_Files_NotForbid(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -326,7 +326,7 @@ func Test_Files_NotForbid(t *testing.T) { defer c.Stop() b, _, _ := get("http://localhost:6029/client.php") - assert.Equal(t, all("../../php-src/tests/client.php"), b) + assert.Equal(t, all("../../tests/client.php"), b) } func tmpDir() string { |