diff options
author | Valery Piashchynski <[email protected]> | 2020-11-25 23:11:35 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-11-25 23:11:35 +0300 |
commit | dd224cf8616d2196150fb7a64397eb1a9567e11b (patch) | |
tree | c220ccbd908c78bfb682ef23af35c68e109f6752 /plugins | |
parent | 1b0b033bcf4969e401b0c34b82171aa0b60b12cf (diff) |
Finish tests for the http plugin
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/http/config.go | 3 | ||||
-rw-r--r-- | plugins/http/plugin.go | 15 | ||||
-rw-r--r-- | plugins/http/tests/configs/.rr-broken-pipes.yaml | 29 | ||||
-rw-r--r-- | plugins/http/tests/configs/.rr-env.yaml | 31 | ||||
-rw-r--r-- | plugins/http/tests/http_test.go | 109 | ||||
-rw-r--r-- | plugins/http/tests/plugin_test_old.go | 601 |
6 files changed, 175 insertions, 613 deletions
diff --git a/plugins/http/config.go b/plugins/http/config.go index d44b3ebd..3f1fa69e 100644 --- a/plugins/http/config.go +++ b/plugins/http/config.go @@ -75,6 +75,9 @@ type Config struct { // Pool configures worker pool. Pool *roadrunner.PoolConfig + // Env is environment variables passed to the http pool + Env map[string]string + cidrs Cidrs } diff --git a/plugins/http/plugin.go b/plugins/http/plugin.go index 78179242..7ce3a70d 100644 --- a/plugins/http/plugin.go +++ b/plugins/http/plugin.go @@ -92,10 +92,6 @@ func (s *Plugin) Init(cfg config.Configurer, log log.Logger, server factory.Serv return errors.E(op, errors.Disabled) } - // Set needed env vars - env := make(map[string]string) - env["RR_HTTP"] = "true" - s.pool, err = server.NewWorkerPool(context.Background(), roadrunner.PoolConfig{ Debug: s.cfg.Pool.Debug, NumWorkers: s.cfg.Pool.NumWorkers, @@ -103,7 +99,7 @@ func (s *Plugin) Init(cfg config.Configurer, log log.Logger, server factory.Serv AllocateTimeout: s.cfg.Pool.AllocateTimeout, DestroyTimeout: s.cfg.Pool.DestroyTimeout, Supervisor: s.cfg.Pool.Supervisor, - }, env) + }, s.cfg.Env) if err != nil { return errors.E(op, err) } @@ -297,13 +293,8 @@ func (s *Plugin) Reset() error { s.log.Info("Resetting http plugin") s.pool.Destroy(context.Background()) - // Set needed env vars - env := make(map[string]string) - env["RR_HTTP"] = "true" - var err error - // re-read the config - err = s.configurer.UnmarshalKey(ServiceName, &s.cfg) + err := s.configurer.UnmarshalKey(ServiceName, &s.cfg) if err != nil { return errors.E(op, err) } @@ -315,7 +306,7 @@ func (s *Plugin) Reset() error { AllocateTimeout: s.cfg.Pool.AllocateTimeout, DestroyTimeout: s.cfg.Pool.DestroyTimeout, Supervisor: s.cfg.Pool.Supervisor, - }, env) + }, s.cfg.Env) if err != nil { return errors.E(op, err) } diff --git a/plugins/http/tests/configs/.rr-broken-pipes.yaml b/plugins/http/tests/configs/.rr-broken-pipes.yaml new file mode 100644 index 00000000..9f599310 --- /dev/null +++ b/plugins/http/tests/configs/.rr-broken-pipes.yaml @@ -0,0 +1,29 @@ +rpc: + listen: tcp://127.0.0.1:6001 + disabled: false + +server: + command: "php ../../../tests/http/client.php broken pipes" + user: "" + group: "" + env: + "RR_HTTP": "true" + relay: "pipes" + relayTimeout: "20s" + +http: + debug: true + address: 127.0.0.1:8084 + maxRequestSize: 1024 + middleware: [ "" ] + uploads: + forbid: [ ".php", ".exe", ".bat" ] + trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ] + pool: + numWorkers: 12 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s + + + diff --git a/plugins/http/tests/configs/.rr-env.yaml b/plugins/http/tests/configs/.rr-env.yaml new file mode 100644 index 00000000..1e8b679f --- /dev/null +++ b/plugins/http/tests/configs/.rr-env.yaml @@ -0,0 +1,31 @@ +rpc: + listen: tcp://127.0.0.1:6001 + disabled: false + +server: + command: "php ../../../tests/http/client.php env pipes" + user: "" + group: "" + env: + "env_key": "ENV_VALUE" + relay: "pipes" + relayTimeout: "20s" + +http: + debug: true + address: 127.0.0.1:8084 + maxRequestSize: 1024 + middleware: [ "" ] + env: + "RR_HTTP": "true" + "env_key": "ENV_VALUE" + uploads: + forbid: [ ".php", ".exe", ".bat" ] + trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ] + pool: + numWorkers: 12 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s + + diff --git a/plugins/http/tests/http_test.go b/plugins/http/tests/http_test.go index bd329758..2c380bf7 100644 --- a/plugins/http/tests/http_test.go +++ b/plugins/http/tests/http_test.go @@ -961,6 +961,115 @@ func echoError(t *testing.T) { assert.NoError(t, err) } +func TestHttpEnvVariables(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-env.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &PluginMiddleware{}, + &PluginMiddleware2{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + go func() { + tt := time.NewTimer(time.Second * 5) + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + t.Run("EnvVariablesTest", envVarsTest) + wg.Wait() +} + +func envVarsTest(t *testing.T) { + req, err := http.NewRequest("GET", "http://localhost:8084", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, "ENV_VALUE", string(b)) + + err = r.Body.Close() + assert.NoError(t, err) +} + +func TestHttpBrokenPipes(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-broken-pipes.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &PluginMiddleware{}, + &PluginMiddleware2{}, + ) + assert.NoError(t, err) + + err = cont.Init() + assert.Error(t, err) + + _, err = cont.Serve() + assert.Error(t, err) +} + + func get(url string) (string, *http.Response, error) { r, err := http.Get(url) if err != nil { diff --git a/plugins/http/tests/plugin_test_old.go b/plugins/http/tests/plugin_test_old.go deleted file mode 100644 index dae18bb4..00000000 --- a/plugins/http/tests/plugin_test_old.go +++ /dev/null @@ -1,601 +0,0 @@ -package tests - -//import ( -// "github.com/cenkalti/backoff/v4" -// json "github.com/json-iterator/go" -// "github.com/sirupsen/logrus" -// "github.com/sirupsen/logrus/hooks/test" -// "github.com/spiral/roadrunner" -// "github.com/spiral/roadrunner/service" -// "github.com/spiral/roadrunner/service/env" -// "github.com/spiral/roadrunner/service/rpc" -// "github.com/stretchr/testify/assert" -// "io/ioutil" -// "net/http" -// "os" -// "testing" -// "time" -//) -// -//type testCfg struct { -// httpCfg string -// rpcCfg string -// envCfg string -// target string -//} -// - -//func Test_Service_Configure_Disable(t *testing.T) { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// assert.NoError(t, c.Init(&testCfg{})) -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusInactive, st) -//} -// -//func Test_Service_Configure_Enable(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":8070", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// if err != nil { -// return err -// } -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusOK, st) -// -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -// -//} -// -//func Test_Service_Echo(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":6536", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// if err != nil { -// return err -// } -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusOK, st) -// -// // should do nothing -// s.(*Service).Stop() -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Millisecond * 100) -// -// req, err := http.NewRequest("GET", "http://localhost:6536?hello=world", nil) -// if err != nil { -// c.Stop() -// return err -// } -// -// r, err := http.DefaultClient.Do(req) -// if err != nil { -// c.Stop() -// return err -// } -// b, err := ioutil.ReadAll(r.Body) -// if err != nil { -// c.Stop() -// return err -// } -// assert.Equal(t, 201, r.StatusCode) -// assert.Equal(t, "WORLD", string(b)) -// -// err = r.Body.Close() -// if err != nil { -// c.Stop() -// return err -// } -// -// c.Stop() -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -//} -// -//func Test_Service_Env(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(env.ID, env.NewService(map[string]string{"pool": "test"})) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":10031", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php env pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`, envCfg: `{"env_key":"ENV_VALUE"}`}) -// if err != nil { -// return err -// } -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusOK, st) -// -// // should do nothing -// s.(*Service).Stop() -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Millisecond * 500) -// -// req, err := http.NewRequest("GET", "http://localhost:10031", nil) -// if err != nil { -// c.Stop() -// return err -// } -// -// r, err := http.DefaultClient.Do(req) -// if err != nil { -// c.Stop() -// return err -// } -// -// b, err := ioutil.ReadAll(r.Body) -// if err != nil { -// c.Stop() -// return err -// } -// -// assert.Equal(t, 200, r.StatusCode) -// assert.Equal(t, "ENV_VALUE", string(b)) -// -// err = r.Body.Close() -// if err != nil { -// c.Stop() -// return err -// } -// -// c.Stop() -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -// -//} -// -//func Test_Service_ErrorEcho(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":6030", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echoerr pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// if err != nil { -// return err -// } -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusOK, st) -// -// goterr := make(chan interface{}) -// s.(*Service).AddListener(func(event int, ctx interface{}) { -// if event == roadrunner.EventStderrOutput { -// if string(ctx.([]byte)) == "WORLD\n" { -// goterr <- nil -// } -// } -// }) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Millisecond * 500) -// -// req, err := http.NewRequest("GET", "http://localhost:6030?hello=world", nil) -// if err != nil { -// c.Stop() -// return err -// } -// -// r, err := http.DefaultClient.Do(req) -// if err != nil { -// c.Stop() -// return err -// } -// -// b, err := ioutil.ReadAll(r.Body) -// if err != nil { -// c.Stop() -// return err -// } -// -// <-goterr -// -// assert.Equal(t, 201, r.StatusCode) -// assert.Equal(t, "WORLD", string(b)) -// err = r.Body.Close() -// if err != nil { -// c.Stop() -// return err -// } -// -// c.Stop() -// -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -//} -// - -// -//func Test_Service_Listener(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":6033", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// if err != nil { -// return err -// } -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusOK, st) -// -// stop := make(chan interface{}) -// s.(*Service).AddListener(func(event int, ctx interface{}) { -// if event == roadrunner.EventServerStart { -// stop <- nil -// } -// }) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// time.Sleep(time.Millisecond * 500) -// -// c.Stop() -// assert.True(t, true) -// -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -//} -// -//func Test_Service_Error(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":6034", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "---", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// if err != nil { -// return err -// } -// -// // assert error -// err = c.Serve() -// if err == nil { -// return err -// } -// -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -//} -// -//func Test_Service_Error2(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":6035", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php broken pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// if err != nil { -// return err -// } -// -// // assert error -// err = c.Serve() -// if err == nil { -// return err -// } -// -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -//} -// -//func Test_Service_Error3(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":6036", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers" -// "command": "php ../../tests/http/client.php broken pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// // assert error -// if err == nil { -// return err -// } -// -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -// -//} -// -//func Test_Service_Error4(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": "----", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php broken pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// // assert error -// if err != nil { -// return nil -// } -// -// return err -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -//} -// -//func tmpDir() string { -// p := os.TempDir() -// j := json.ConfigCompatibleWithStandardLibrary -// r, _ := j.Marshal(p) -// -// return string(r) -//} |