diff options
author | Valery Piashchynski <[email protected]> | 2021-04-04 18:39:52 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-04-04 18:39:52 +0300 |
commit | cc56299b877f3fbbae1e3368d98804d06564a424 (patch) | |
tree | 6b2bcd13eb32e31cef556f57869b9dcdeea4472a /tests | |
parent | c1664e0815727e599dcb7f7a0a7a95a5be974197 (diff) |
- 🔥 Support Readiness checks (via `/ready`) status plugin endpoint.
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/plugins/status/configs/.rr-ready-init.yaml | 28 | ||||
-rw-r--r-- | tests/plugins/status/plugin_test.go | 198 |
2 files changed, 226 insertions, 0 deletions
diff --git a/tests/plugins/status/configs/.rr-ready-init.yaml b/tests/plugins/status/configs/.rr-ready-init.yaml new file mode 100755 index 00000000..da9e84c3 --- /dev/null +++ b/tests/plugins/status/configs/.rr-ready-init.yaml @@ -0,0 +1,28 @@ +rpc: + listen: tcp://127.0.0.1:6007 + +server: + command: "php ../../sleep.php" + user: "" + group: "" + relay: "pipes" + relay_timeout: "20s" + +status: + address: "127.0.0.1:34334" + +logs: + mode: development + level: error +http: + address: 127.0.0.1:11933 + max_request_size: 1024 + middleware: [ "" ] + uploads: + forbid: [ ".php", ".exe", ".bat" ] + trusted_subnets: [ "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: + num_workers: 1 + max_jobs: 0 + allocate_timeout: 60s + destroy_timeout: 60s diff --git a/tests/plugins/status/plugin_test.go b/tests/plugins/status/plugin_test.go index a7322353..85e07fcf 100644 --- a/tests/plugins/status/plugin_test.go +++ b/tests/plugins/status/plugin_test.go @@ -188,3 +188,201 @@ func checkRPCStatus(t *testing.T) { assert.NoError(t, err) assert.Equal(t, st.Code, 200) } + +func TestReadyHttp(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-status-init.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &status.Plugin{}, + ) + 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) + + stopCh := make(chan struct{}, 1) + + go func() { + 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 <-stopCh: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + time.Sleep(time.Second) + t.Run("CheckerGetReadiness", checkHTTPReadiness) + + stopCh <- struct{}{} + wg.Wait() +} + +const resp2 = `Service: http: Status: 204 +Service: rpc not found` + +func checkHTTPReadiness(t *testing.T) { + req, err := http.NewRequest("GET", "http://127.0.0.1:34333/ready?plugin=http&plugin=rpc", 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, resp, string(b)) + + err = r.Body.Close() + assert.NoError(t, err) +} + +func TestReadinessRPCWorkerNotReady(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel), endure.GracefulShutdownTimeout(time.Second*2)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-ready-init.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &status.Plugin{}, + ) + 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) + + stopCh := make(chan struct{}, 1) + + go func() { + 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 <-stopCh: + // timeout, error here is OK, because in the PHP we are sleeping for the 300s + _ = cont.Stop() + return + } + } + }() + + time.Sleep(time.Second) + t.Run("DoHttpReq", doHTTPReq) + time.Sleep(time.Second * 5) + t.Run("CheckerGetReadiness2", checkHTTPReadiness2) + t.Run("CheckerGetRpcReadiness", checkRPCReadiness) + stopCh <- struct{}{} + wg.Wait() +} + +func doHTTPReq(t *testing.T) { + go func() { + req, err := http.NewRequest("GET", "http://localhost:11933", 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, resp2, string(b)) + + err = r.Body.Close() + assert.NoError(t, err) + }() +} + +func checkHTTPReadiness2(t *testing.T) { + req, err := http.NewRequest("GET", "http://127.0.0.1:34334/ready?plugin=http&plugin=rpc", 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, resp2, string(b)) + + err = r.Body.Close() + assert.NoError(t, err) +} + +func checkRPCReadiness(t *testing.T) { + conn, err := net.Dial("tcp", "127.0.0.1:6007") + assert.NoError(t, err) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) + + st := &status.Status{} + + err = client.Call("status.Ready", "http", &st) + assert.NoError(t, err) + assert.Equal(t, st.Code, 204) +} |