summaryrefslogtreecommitdiff
path: root/tests/plugins/logger
diff options
context:
space:
mode:
Diffstat (limited to 'tests/plugins/logger')
-rw-r--r--tests/plugins/logger/configs/.rr-file-logger.yaml23
-rw-r--r--tests/plugins/logger/configs/.rr-no-logger.yaml0
-rw-r--r--tests/plugins/logger/configs/.rr-no-logger2.yaml16
-rw-r--r--tests/plugins/logger/configs/.rr-raw-mode.yaml15
-rw-r--r--tests/plugins/logger/configs/.rr.yaml3
-rw-r--r--tests/plugins/logger/logger_test.go430
-rw-r--r--tests/plugins/logger/plugin.go71
7 files changed, 0 insertions, 558 deletions
diff --git a/tests/plugins/logger/configs/.rr-file-logger.yaml b/tests/plugins/logger/configs/.rr-file-logger.yaml
deleted file mode 100644
index 49c30d02..00000000
--- a/tests/plugins/logger/configs/.rr-file-logger.yaml
+++ /dev/null
@@ -1,23 +0,0 @@
-server:
- command: "php ../../http/client.php echo pipes"
- relay: "pipes"
- relay_timeout: "20s"
-
-http:
- address: 127.0.0.1:54224
- 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: 2
- max_jobs: 0
- allocate_timeout: 60s
- destroy_timeout: 60s
-
-logs:
- mode: development
- level: debug
- file_logger_options:
- log_output: "test.log"
diff --git a/tests/plugins/logger/configs/.rr-no-logger.yaml b/tests/plugins/logger/configs/.rr-no-logger.yaml
deleted file mode 100644
index e69de29b..00000000
--- a/tests/plugins/logger/configs/.rr-no-logger.yaml
+++ /dev/null
diff --git a/tests/plugins/logger/configs/.rr-no-logger2.yaml b/tests/plugins/logger/configs/.rr-no-logger2.yaml
deleted file mode 100644
index 810ea88f..00000000
--- a/tests/plugins/logger/configs/.rr-no-logger2.yaml
+++ /dev/null
@@ -1,16 +0,0 @@
-rpc:
- listen: tcp://127.0.0.1:6001
-
-server:
- command: "php ../../http/client.php echo pipes"
- relay: "pipes"
- relay_timeout: "20s"
-
-http:
- address: 127.0.0.1:18945
- max_request_size: 1024
- pool:
- num_workers: 2
- max_jobs: 0
- allocate_timeout: 60s
- destroy_timeout: 60s
diff --git a/tests/plugins/logger/configs/.rr-raw-mode.yaml b/tests/plugins/logger/configs/.rr-raw-mode.yaml
deleted file mode 100644
index fba25945..00000000
--- a/tests/plugins/logger/configs/.rr-raw-mode.yaml
+++ /dev/null
@@ -1,15 +0,0 @@
-server:
- command: "php ../../raw-error.php"
- relay: "pipes"
-
-http:
- address: 127.0.0.1:34999
- max_requestSize: 1024
- pool:
- num_workers: 1
- max_jobs: 0
- allocate_timeout: 10s
- destroy_timeout: 10s
-
-logs:
- mode: raw
diff --git a/tests/plugins/logger/configs/.rr.yaml b/tests/plugins/logger/configs/.rr.yaml
deleted file mode 100644
index 5ab359d3..00000000
--- a/tests/plugins/logger/configs/.rr.yaml
+++ /dev/null
@@ -1,3 +0,0 @@
-logs:
- mode: development
- level: error \ No newline at end of file
diff --git a/tests/plugins/logger/logger_test.go b/tests/plugins/logger/logger_test.go
deleted file mode 100644
index e077f0bc..00000000
--- a/tests/plugins/logger/logger_test.go
+++ /dev/null
@@ -1,430 +0,0 @@
-package logger
-
-import (
- "net/http"
- "os"
- "os/signal"
- "strings"
- "sync"
- "syscall"
- "testing"
- "time"
-
- "github.com/golang/mock/gomock"
- endure "github.com/spiral/endure/pkg/container"
- "github.com/spiral/roadrunner/v2/plugins/config"
- httpPlugin "github.com/spiral/roadrunner/v2/plugins/http"
- "github.com/spiral/roadrunner/v2/plugins/logger"
- "github.com/spiral/roadrunner/v2/plugins/rpc"
- "github.com/spiral/roadrunner/v2/plugins/server"
- "github.com/spiral/roadrunner/v2/tests/mocks"
- "github.com/stretchr/testify/assert"
-)
-
-func TestLogger(t *testing.T) {
- container, err := endure.NewContainer(nil, endure.RetryOnFail(false), endure.SetLogLevel(endure.ErrorLevel))
- if err != nil {
- t.Fatal(err)
- }
- // config plugin
- vp := &config.Viper{}
- vp.Path = "configs/.rr.yaml"
- vp.Prefix = "rr"
-
- err = container.RegisterAll(
- vp,
- &Plugin{},
- &logger.ZapLogger{},
- )
- assert.NoError(t, err)
-
- err = container.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- errCh, err := container.Serve()
- if err != nil {
- t.Fatal(err)
- }
-
- // stop by CTRL+C
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt)
-
- stopCh := make(chan struct{}, 1)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-errCh:
- assert.NoError(t, e.Error)
- assert.NoError(t, container.Stop())
- return
- case <-c:
- err = container.Stop()
- assert.NoError(t, err)
- return
- case <-stopCh:
- assert.NoError(t, container.Stop())
- return
- }
- }
- }()
-
- stopCh <- struct{}{}
- wg.Wait()
-}
-
-func TestLoggerRawErr(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- assert.NoError(t, err)
-
- // config plugin
- cfg := &config.Viper{}
- cfg.Path = "configs/.rr-raw-mode.yaml"
- cfg.Prefix = "rr"
-
- controller := gomock.NewController(t)
- mockLogger := mocks.NewMockLogger(controller)
-
- mockLogger.EXPECT().Debug("worker destructed", "pid", gomock.Any()).AnyTimes()
- mockLogger.EXPECT().Info("{\"field\": \"value\"}").MinTimes(1)
- mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).MinTimes(1)
-
- err = cont.RegisterAll(
- cfg,
- mockLogger,
- &server.Plugin{},
- &httpPlugin.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
- }
- }
- }()
-
- stopCh <- struct{}{}
- wg.Wait()
-}
-
-func TestLoggerNoConfig(t *testing.T) {
- container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.ErrorLevel))
- if err != nil {
- t.Fatal(err)
- }
- // config plugin
- vp := &config.Viper{}
- vp.Path = "configs/.rr-no-logger.yaml"
- vp.Prefix = "rr"
-
- err = container.RegisterAll(
- vp,
- &Plugin{},
- &logger.ZapLogger{},
- )
- assert.NoError(t, err)
-
- err = container.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- errCh, err := container.Serve()
- if err != nil {
- t.Fatal(err)
- }
-
- // stop by CTRL+C
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt)
-
- stopCh := make(chan struct{}, 1)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-errCh:
- assert.NoError(t, e.Error)
- assert.NoError(t, container.Stop())
- return
- case <-c:
- err = container.Stop()
- assert.NoError(t, err)
- return
- case <-stopCh:
- assert.NoError(t, container.Stop())
- return
- }
- }
- }()
-
- stopCh <- struct{}{}
- wg.Wait()
-}
-
-// Should no panic
-func TestLoggerNoConfig2(t *testing.T) {
- container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.ErrorLevel))
- if err != nil {
- t.Fatal(err)
- }
- // config plugin
- vp := &config.Viper{}
- vp.Path = "configs/.rr-no-logger2.yaml"
- vp.Prefix = "rr"
-
- err = container.RegisterAll(
- vp,
- &rpc.Plugin{},
- &logger.ZapLogger{},
- &httpPlugin.Plugin{},
- &server.Plugin{},
- )
- assert.NoError(t, err)
-
- err = container.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- errCh, err := container.Serve()
- if err != nil {
- t.Fatal(err)
- }
-
- // stop by CTRL+C
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt)
-
- stopCh := make(chan struct{}, 1)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-errCh:
- assert.NoError(t, e.Error)
- assert.NoError(t, container.Stop())
- return
- case <-c:
- err = container.Stop()
- assert.NoError(t, err)
- return
- case <-stopCh:
- assert.NoError(t, container.Stop())
- return
- }
- }
- }()
-
- stopCh <- struct{}{}
- wg.Wait()
-}
-
-func TestFileLogger(t *testing.T) {
- container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.ErrorLevel))
- if err != nil {
- t.Fatal(err)
- }
- // config plugin
- vp := &config.Viper{}
- vp.Path = "configs/.rr-file-logger.yaml"
- vp.Prefix = "rr"
-
- err = container.RegisterAll(
- vp,
- &rpc.Plugin{},
- &logger.ZapLogger{},
- &httpPlugin.Plugin{},
- &server.Plugin{},
- )
- assert.NoError(t, err)
-
- err = container.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- errCh, err := container.Serve()
- if err != nil {
- t.Fatal(err)
- }
-
- // stop by CTRL+C
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt)
-
- stopCh := make(chan struct{}, 1)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-errCh:
- assert.NoError(t, e.Error)
- assert.NoError(t, container.Stop())
- return
- case <-c:
- err = container.Stop()
- assert.NoError(t, err)
- return
- case <-stopCh:
- assert.NoError(t, container.Stop())
- return
- }
- }
- }()
-
- time.Sleep(time.Second * 2)
- t.Run("HTTPEchoReq", httpEcho)
-
- f, err := os.ReadFile("test.log")
- if err != nil {
- t.Fatal(err)
- }
-
- strings.Contains(string(f), "worker constructed")
- strings.Contains(string(f), "201 GET")
-
- _ = os.Remove("test.log")
-
- stopCh <- struct{}{}
- wg.Wait()
-}
-
-func TestMarshalObjectLogging(t *testing.T) {
- container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.ErrorLevel))
- if err != nil {
- t.Fatal(err)
- }
- // config plugin
- vp := &config.Viper{}
- vp.Path = "configs/.rr-file-logger.yaml"
- vp.Prefix = "rr"
-
- err = container.RegisterAll(
- vp,
- &Plugin{},
- &logger.ZapLogger{},
- )
- assert.NoError(t, err)
-
- err = container.Init()
- if err != nil {
- t.Fatal(err)
- }
-
- errCh, err := container.Serve()
- if err != nil {
- t.Fatal(err)
- }
-
- // stop by CTRL+C
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt)
-
- stopCh := make(chan struct{}, 1)
-
- wg := &sync.WaitGroup{}
- wg.Add(1)
-
- go func() {
- defer wg.Done()
- for {
- select {
- case e := <-errCh:
- assert.NoError(t, e.Error)
- assert.NoError(t, container.Stop())
- return
- case <-c:
- err = container.Stop()
- assert.NoError(t, err)
- return
- case <-stopCh:
- assert.NoError(t, container.Stop())
- return
- }
- }
- }()
-
- time.Sleep(time.Second * 2)
-
- f, err := os.ReadFile("test.log")
- if err != nil {
- t.Fatal(err)
- }
-
- assert.Contains(t, string(f), "Example marshaller error")
- assert.Equal(t, 4, strings.Count(string(f), "Example marshaller error"))
-
- _ = os.Remove("test.log")
-
- stopCh <- struct{}{}
- wg.Wait()
-}
-
-func httpEcho(t *testing.T) {
- req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:54224?hello=world", nil)
- assert.NoError(t, err)
-
- r, err := http.DefaultClient.Do(req)
- assert.NoError(t, err)
- assert.Equal(t, http.StatusCreated, r.StatusCode)
-
- err = r.Body.Close()
- assert.NoError(t, err)
-}
diff --git a/tests/plugins/logger/plugin.go b/tests/plugins/logger/plugin.go
deleted file mode 100644
index 54e78d7b..00000000
--- a/tests/plugins/logger/plugin.go
+++ /dev/null
@@ -1,71 +0,0 @@
-package logger
-
-import (
- "strings"
-
- "github.com/spiral/errors"
- "github.com/spiral/roadrunner/v2/plugins/config"
- "github.com/spiral/roadrunner/v2/plugins/logger"
- "go.uber.org/zap"
- core "go.uber.org/zap/zapcore"
-)
-
-type Plugin struct {
- config config.Configurer
- log logger.Logger
-}
-
-type Loggable struct {
-}
-
-func (l *Loggable) MarshalLogObject(encoder core.ObjectEncoder) error {
- encoder.AddString("error", "Example marshaller error")
- return nil
-}
-
-func (p1 *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
- p1.config = cfg
- p1.log = log
- return nil
-}
-
-func (p1 *Plugin) Serve() chan error {
- errCh := make(chan error, 1)
- p1.log.Error("error", "test", errors.E(errors.Str("test")))
- p1.log.Info("error", "test", errors.E(errors.Str("test")))
- p1.log.Debug("error", "test", errors.E(errors.Str("test")))
- p1.log.Warn("error", "test", errors.E(errors.Str("test")))
-
- field := zap.String("error", "Example field error")
-
- p1.log.Error("error", field)
- p1.log.Info("error", field)
- p1.log.Debug("error", field)
- p1.log.Warn("error", field)
-
- marshalledObject := &Loggable{}
-
- p1.log.Error("error", marshalledObject)
- p1.log.Info("error", marshalledObject)
- p1.log.Debug("error", marshalledObject)
- p1.log.Warn("error", marshalledObject)
-
- p1.log.Error("error", "test")
- p1.log.Info("error", "test")
- p1.log.Debug("error", "test")
- p1.log.Warn("error", "test")
-
- // test the `raw` mode
- messageJSON := []byte(`{"field": "value"}`)
- p1.log.Debug(strings.TrimRight(string(messageJSON), " \n\t"))
-
- return errCh
-}
-
-func (p1 *Plugin) Stop() error {
- return nil
-}
-
-func (p1 *Plugin) Name() string {
- return "logger_plugin"
-}