summaryrefslogtreecommitdiff
path: root/plugins/metrics/tests
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-11-13 17:36:22 +0300
committerValery Piashchynski <[email protected]>2020-11-13 17:36:22 +0300
commit99b6012400ab407cfcb04aab833640af565d550d (patch)
tree75a228cf96d90f99e7ee6367c5d7cbb4c465c61d /plugins/metrics/tests
parent6eefd067f4c08ed51834926abd1a4c60ec55b56d (diff)
Metrics config test
Config proper parsing Add metrics tests to the CI and Makefile
Diffstat (limited to 'plugins/metrics/tests')
-rw-r--r--plugins/metrics/tests/metrics_test.go103
-rw-r--r--plugins/metrics/tests/plugin1.go67
-rw-r--r--plugins/metrics/tests/plugin2.go49
3 files changed, 184 insertions, 35 deletions
diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go
index f21016d4..2900c38f 100644
--- a/plugins/metrics/tests/metrics_test.go
+++ b/plugins/metrics/tests/metrics_test.go
@@ -1,6 +1,8 @@
package tests
import (
+ "io/ioutil"
+ "net/http"
"os"
"os/signal"
"syscall"
@@ -15,6 +17,26 @@ import (
"github.com/stretchr/testify/assert"
)
+// get request and return body
+func get(url string) (string, *http.Response, error) {
+ r, err := http.Get(url)
+ if err != nil {
+ return "", nil, err
+ }
+
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ return "", nil, err
+ }
+
+ err = r.Body.Close()
+ if err != nil {
+ return "", nil, err
+ }
+ // unsafe
+ return string(b), r, err
+}
+
func TestMetricsInit(t *testing.T) {
cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel))
if err != nil {
@@ -62,6 +84,87 @@ func TestMetricsInit(t *testing.T) {
tt := time.NewTimer(time.Second * 5)
+ out, _, err := get("http://localhost:2112/metrics")
+ assert.NoError(t, err)
+
+ assert.Contains(t, out, "go_gc_duration_seconds")
+
+ 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
+ }
+ }
+}
+
+func TestMetricsGaugeCollector(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, ""))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ cfg := &config.Viper{}
+ cfg.Prefix = "rr"
+ cfg.Path = ".rr-test.yaml"
+
+ err = cont.Register(cfg)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = cont.Register(&metrics.Plugin{})
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = cont.Register(&rpc.Plugin{})
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = cont.Register(&logger.ZapLogger{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = cont.Register(&Plugin1{})
+ if err != nil {
+ t.Fatal(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)
+
+ time.Sleep(time.Second)
+ tt := time.NewTimer(time.Second * 5)
+
+ out, _, err := get("http://localhost:2112/metrics")
+ assert.Contains(t, out, "my_gauge 100")
+
for {
select {
case e := <-ch:
diff --git a/plugins/metrics/tests/plugin1.go b/plugins/metrics/tests/plugin1.go
index fdf10e54..cac41c82 100644
--- a/plugins/metrics/tests/plugin1.go
+++ b/plugins/metrics/tests/plugin1.go
@@ -1,12 +1,11 @@
package tests
import (
- "fmt"
-
"github.com/prometheus/client_golang/prometheus"
"github.com/spiral/roadrunner/v2/plugins/config"
)
+// Gauge //////////////
type Plugin1 struct {
config config.Configurer
}
@@ -30,6 +29,39 @@ func (p1 *Plugin1) Name() string {
}
func (p1 *Plugin1) MetricsCollector() prometheus.Collector {
+ collector := prometheus.NewGauge(prometheus.GaugeOpts{
+ Name: "my_gauge",
+ Help: "My gauge value",
+ })
+
+ collector.Set(100)
+ return collector
+}
+
+////////////////////////////////////////////////////////////////
+type Plugin3 struct {
+ config config.Configurer
+}
+
+func (p *Plugin3) Init(cfg config.Configurer) error {
+ p.config = cfg
+ return nil
+}
+
+func (p *Plugin3) Serve() chan error {
+ errCh := make(chan error, 1)
+ return errCh
+}
+
+func (p *Plugin3) Stop() error {
+ return nil
+}
+
+func (p *Plugin3) Name() string {
+ return "metrics_test.plugin1"
+}
+
+func (p *Plugin3) MetricsCollector() prometheus.Collector {
var (
cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu_temperature_celsius",
@@ -39,11 +71,34 @@ func (p1 *Plugin1) MetricsCollector() prometheus.Collector {
return cpuTemp
}
-type PluginRpc struct {
- srv *Plugin1
+type Plugin4 struct {
+ config config.Configurer
+}
+
+func (p *Plugin4) Init(cfg config.Configurer) error {
+ p.config = cfg
+ return nil
+}
+
+func (p *Plugin4) Serve() chan error {
+ errCh := make(chan error, 1)
+ return errCh
}
-func (r *PluginRpc) Hello(in string, out *string) error {
- *out = fmt.Sprintf("Hello, username: %s", in)
+func (p *Plugin4) Stop() error {
return nil
}
+
+func (p *Plugin4) Name() string {
+ return "metrics_test.plugin1"
+}
+
+func (p *Plugin4) MetricsCollector() prometheus.Collector {
+ var (
+ cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
+ Name: "cpu_temperature_celsius",
+ Help: "Current temperature of the CPU.",
+ })
+ )
+ return cpuTemp
+}
diff --git a/plugins/metrics/tests/plugin2.go b/plugins/metrics/tests/plugin2.go
index 4156db6d..4369971b 100644
--- a/plugins/metrics/tests/plugin2.go
+++ b/plugins/metrics/tests/plugin2.go
@@ -1,14 +1,5 @@
package tests
-import (
- "net"
- "net/rpc"
- "time"
-
- "github.com/spiral/errors"
- "github.com/spiral/goridge/v2"
-)
-
// plugin2 makes a call to the plugin1 via RPC
// this is just a simulation of external call FOR TEST
// you don't need to do such things :)
@@ -23,26 +14,26 @@ func (p2 *Plugin2) Serve() chan error {
errCh := make(chan error, 1)
go func() {
- time.Sleep(time.Second * 3)
-
- conn, err := net.Dial("tcp", "127.0.0.1:7001")
- if err != nil {
- errCh <- errors.E(errors.Serve, err)
- return
- }
- client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn))
- var ret string
- err = client.Call("metrics_test.plugin1.Hello", "Valery", &ret)
- if err != nil {
- errCh <- err
- return
- }
- if ret != "Hello, username: Valery" {
- errCh <- errors.E("wrong response")
- return
- }
- // to stop exec
- errCh <- errors.E(errors.Disabled)
+ //time.Sleep(time.Second * 3)
+ //
+ //conn, err := net.Dial("tcp", "127.0.0.1:6001")
+ //if err != nil {
+ // errCh <- errors.E(errors.Serve, err)
+ // return
+ //}
+ //client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn))
+ //var ret string
+ //err = client.Call("metrics_test.plugin1.Hello", "Valery", &ret)
+ //if err != nil {
+ // errCh <- err
+ // return
+ //}
+ //if ret != "Hello, username: Valery" {
+ // errCh <- errors.E("wrong response")
+ // return
+ //}
+ //// to stop exec
+ //errCh <- errors.E(errors.Disabled)
return
}()