From 002eb4bb1981558fa5e614aed22d322f0f45d7ea Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 13 Nov 2020 13:42:40 +0300 Subject: Move all interfaces to the separate folder [RPC, METRICS, LOGGER] RPC for the metrics update to the working state RCP interface renamed to the RPCer --- plugins/metrics/tests/metrics_test.go | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 plugins/metrics/tests/metrics_test.go (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go new file mode 100644 index 00000000..d04c75d3 --- /dev/null +++ b/plugins/metrics/tests/metrics_test.go @@ -0,0 +1,51 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/spiral/roadrunner/v2/plugins/metrics" +) + +func TestMetricsInit(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel)) + 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(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + errCh, err := cont.Serve() + + for { + select { + case e := <-errCh: + fmt.Println(e) + } + } +} -- cgit v1.2.3 From 6eefd067f4c08ed51834926abd1a4c60ec55b56d Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 13 Nov 2020 16:29:08 +0300 Subject: Update tests --- plugins/metrics/tests/metrics_test.go | 45 +++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index d04c75d3..f21016d4 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -1,13 +1,18 @@ package tests import ( - "fmt" + "os" + "os/signal" + "syscall" "testing" + "time" "github.com/spiral/endure" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/metrics" + "github.com/spiral/roadrunner/v2/plugins/rpc" + "github.com/stretchr/testify/assert" ) func TestMetricsInit(t *testing.T) { @@ -30,22 +35,54 @@ func TestMetricsInit(t *testing.T) { 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) } - errCh, err := cont.Serve() + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + tt := time.NewTimer(time.Second * 5) for { select { - case e := <-errCh: - fmt.Println(e) + 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 } } } -- cgit v1.2.3 From 99b6012400ab407cfcb04aab833640af565d550d Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 13 Nov 2020 17:36:22 +0300 Subject: Metrics config test Config proper parsing Add metrics tests to the CI and Makefile --- plugins/metrics/tests/metrics_test.go | 103 ++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) (limited to 'plugins/metrics/tests/metrics_test.go') 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: -- cgit v1.2.3 From 9fd0b28d3a6a60b5e08af03bd86bcef042152e1c Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 13 Nov 2020 17:43:20 +0300 Subject: golangci linters warnings fix --- plugins/metrics/tests/metrics_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 2900c38f..2df011e6 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -18,23 +18,23 @@ import ( ) // get request and return body -func get(url string) (string, *http.Response, error) { +func get(url string) (string, error) { r, err := http.Get(url) if err != nil { - return "", nil, err + return "", err } b, err := ioutil.ReadAll(r.Body) if err != nil { - return "", nil, err + return "", err } err = r.Body.Close() if err != nil { - return "", nil, err + return "", err } // unsafe - return string(b), r, err + return string(b), err } func TestMetricsInit(t *testing.T) { @@ -84,7 +84,7 @@ func TestMetricsInit(t *testing.T) { tt := time.NewTimer(time.Second * 5) - out, _, err := get("http://localhost:2112/metrics") + out, err := get("http://localhost:2112/metrics") assert.NoError(t, err) assert.Contains(t, out, "go_gc_duration_seconds") @@ -162,7 +162,8 @@ func TestMetricsGaugeCollector(t *testing.T) { time.Sleep(time.Second) tt := time.NewTimer(time.Second * 5) - out, _, err := get("http://localhost:2112/metrics") + out, err := get("http://localhost:2112/metrics") + assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") for { -- cgit v1.2.3 From e45155e77e99e85e4ee9cd8863365bf6d599796e Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sat, 14 Nov 2020 17:18:28 +0300 Subject: More tests for the Metrics --- plugins/metrics/tests/metrics_test.go | 467 ++++++++++++++++++++++++++++++++-- 1 file changed, 442 insertions(+), 25 deletions(-) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 2df011e6..860528f8 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -2,7 +2,9 @@ package tests import ( "io/ioutil" + "net" "net/http" + "net/rpc" "os" "os/signal" "syscall" @@ -10,10 +12,11 @@ import ( "time" "github.com/spiral/endure" + "github.com/spiral/goridge/v2" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/metrics" - "github.com/spiral/roadrunner/v2/plugins/rpc" + rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" "github.com/stretchr/testify/assert" ) @@ -57,7 +60,7 @@ func TestMetricsInit(t *testing.T) { t.Fatal(err) } - err = cont.Register(&rpc.Plugin{}) + err = cont.Register(&rpcPlugin.Plugin{}) if err != nil { t.Fatal(err) } @@ -124,29 +127,14 @@ func TestMetricsGaugeCollector(t *testing.T) { 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.RegisterAll( + cfg, + &metrics.Plugin{}, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + &Plugin1{}, + ) + assert.NoError(t, err) err = cont.Init() if err != nil { @@ -166,6 +154,10 @@ func TestMetricsGaugeCollector(t *testing.T) { assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") + genericOut, err := get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "go_gc_duration_seconds") + for { select { case e := <-ch: @@ -190,3 +182,428 @@ func TestMetricsGaugeCollector(t *testing.T) { } } } + +func TestMetricsDifferentRPCCalls(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.RegisterAll( + cfg, + &metrics.Plugin{}, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + ) + 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) + + go func() { + tt := time.NewTimer(time.Minute * 3) + 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("DeclareMetric", declareMetricsTest) + genericOut, err := get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "test_metrics_named_collector") + + t.Run("AddMetric", addMetricsTest) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "test_metrics_named_collector 10000") + + t.Run("SetMetric", setMetric) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "user_gauge_collector 100") + + t.Run("VectorMetric", vectorMetric) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "gauge_2_collector{section=\"first\",type=\"core\"} 100") + + t.Run("MissingSection", missingSection) + t.Run("SetWithoutLabels", setWithoutLabels) + t.Run("SetOnHistogram", setOnHistogram) + t.Run("MetricSub", subMetric) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "sub_gauge_subMetric 1") + + t.Run("SubVector", subVector) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "sub_gauge_subVector{section=\"first\",type=\"core\"} 1") + + close(sig) +} + +func subVector(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "sub_gauge_subVector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Labels: []string{"type", "section"}, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m := metrics.Metric{ + Name: "sub_gauge_subVector", + Value: 100000, + Labels: []string{"core", "first"}, + } + + err = client.Call("metrics.Add", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m = metrics.Metric{ + Name: "sub_gauge_subVector", + Value: 99999, + Labels: []string{"core", "first"}, + } + + err = client.Call("metrics.Sub", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func subMetric(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "sub_gauge_subMetric", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: nil, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m := metrics.Metric{ + Name: "sub_gauge_subMetric", + Value: 100000, + } + + err = client.Call("metrics.Add", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m = metrics.Metric{ + Name: "sub_gauge_subMetric", + Value: 99999, + } + + err = client.Call("metrics.Sub", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func setOnHistogram(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "histogram_setOnHistogram", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Histogram, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_setOnHistogram", + Value: 100.0, + } + + err = client.Call("metrics.Set", m, &ret) // expected 2 label values but got 1 in []string{"missing"} + assert.Error(t, err) + assert.False(t, ret) +} + +func setWithoutLabels(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "gauge_setWithoutLabels", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_setWithoutLabels", + Value: 100.0, + } + + err = client.Call("metrics.Set", m, &ret) // expected 2 label values but got 1 in []string{"missing"} + assert.Error(t, err) + assert.False(t, ret) +} + +func missingSection(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "gauge_missing_section_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_missing_section_collector", + Value: 100.0, + Labels: []string{"missing"}, + } + + err = client.Call("metrics.Set", m, &ret) // expected 2 label values but got 1 in []string{"missing"} + assert.Error(t, err) + assert.False(t, ret) +} + +func vectorMetric(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "gauge_2_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_2_collector", + Value: 100.0, + Labels: []string{"core", "first"}, + } + + err = client.Call("metrics.Set", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func setMetric(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "user_gauge_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: nil, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m := metrics.Metric{ + Name: "user_gauge_collector", + Value: 100.0, + } + + err = client.Call("metrics.Set", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func addMetricsTest(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + m := metrics.Metric{ + Name: "test_metrics_named_collector", + Value: 10000, + Labels: nil, + } + + err = client.Call("metrics.Add", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func declareMetricsTest(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "test_metrics_named_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Counter, + Help: "NO HELP!", + Labels: nil, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} -- cgit v1.2.3 From 06cda58dd9e50928c5473425fefcd4b4be4598c2 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sat, 14 Nov 2020 17:37:25 +0300 Subject: Typo in go.mod --- plugins/metrics/tests/metrics_test.go | 110 ++++++++++++++-------------------- 1 file changed, 46 insertions(+), 64 deletions(-) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 860528f8..0eaa94a9 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -20,6 +20,10 @@ import ( "github.com/stretchr/testify/assert" ) +const dialAddr = "127.0.0.1:6001" +const dialNetwork = "tcp" +const getAddr = "http://localhost:2112/metrics" + // get request and return body func get(url string) (string, error) { r, err := http.Get(url) @@ -240,22 +244,22 @@ func TestMetricsDifferentRPCCalls(t *testing.T) { }() t.Run("DeclareMetric", declareMetricsTest) - genericOut, err := get("http://localhost:2112/metrics") + genericOut, err := get(getAddr) assert.NoError(t, err) assert.Contains(t, genericOut, "test_metrics_named_collector") t.Run("AddMetric", addMetricsTest) - genericOut, err = get("http://localhost:2112/metrics") + genericOut, err = get(getAddr) assert.NoError(t, err) assert.Contains(t, genericOut, "test_metrics_named_collector 10000") t.Run("SetMetric", setMetric) - genericOut, err = get("http://localhost:2112/metrics") + genericOut, err = get(getAddr) assert.NoError(t, err) assert.Contains(t, genericOut, "user_gauge_collector 100") t.Run("VectorMetric", vectorMetric) - genericOut, err = get("http://localhost:2112/metrics") + genericOut, err = get(getAddr) assert.NoError(t, err) assert.Contains(t, genericOut, "gauge_2_collector{section=\"first\",type=\"core\"} 100") @@ -263,12 +267,12 @@ func TestMetricsDifferentRPCCalls(t *testing.T) { t.Run("SetWithoutLabels", setWithoutLabels) t.Run("SetOnHistogram", setOnHistogram) t.Run("MetricSub", subMetric) - genericOut, err = get("http://localhost:2112/metrics") + genericOut, err = get(getAddr) assert.NoError(t, err) assert.Contains(t, genericOut, "sub_gauge_subMetric 1") t.Run("SubVector", subVector) - genericOut, err = get("http://localhost:2112/metrics") + genericOut, err = get(getAddr) assert.NoError(t, err) assert.Contains(t, genericOut, "sub_gauge_subVector{section=\"first\",type=\"core\"} 1") @@ -276,11 +280,11 @@ func TestMetricsDifferentRPCCalls(t *testing.T) { } func subVector(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -323,12 +327,11 @@ func subVector(t *testing.T) { } func subMetric(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -338,9 +341,6 @@ func subMetric(t *testing.T) { Namespace: "default", Subsystem: "default", Type: metrics.Gauge, - Help: "NO HELP!", - Labels: nil, - Buckets: nil, }, } @@ -370,12 +370,11 @@ func subMetric(t *testing.T) { } func setOnHistogram(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -385,9 +384,7 @@ func setOnHistogram(t *testing.T) { Namespace: "default", Subsystem: "default", Type: metrics.Histogram, - Help: "NO HELP!", Labels: []string{"type", "section"}, - Buckets: nil, }, } @@ -408,12 +405,11 @@ func setOnHistogram(t *testing.T) { } func setWithoutLabels(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -423,9 +419,7 @@ func setWithoutLabels(t *testing.T) { Namespace: "default", Subsystem: "default", Type: metrics.Gauge, - Help: "NO HELP!", Labels: []string{"type", "section"}, - Buckets: nil, }, } @@ -446,12 +440,11 @@ func setWithoutLabels(t *testing.T) { } func missingSection(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -461,9 +454,7 @@ func missingSection(t *testing.T) { Namespace: "default", Subsystem: "default", Type: metrics.Gauge, - Help: "NO HELP!", Labels: []string{"type", "section"}, - Buckets: nil, }, } @@ -485,12 +476,11 @@ func missingSection(t *testing.T) { } func vectorMetric(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -500,9 +490,7 @@ func vectorMetric(t *testing.T) { Namespace: "default", Subsystem: "default", Type: metrics.Gauge, - Help: "NO HELP!", Labels: []string{"type", "section"}, - Buckets: nil, }, } @@ -524,12 +512,11 @@ func vectorMetric(t *testing.T) { } func setMetric(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -539,9 +526,6 @@ func setMetric(t *testing.T) { Namespace: "default", Subsystem: "default", Type: metrics.Gauge, - Help: "NO HELP!", - Labels: nil, - Buckets: nil, }, } @@ -561,12 +545,11 @@ func setMetric(t *testing.T) { } func addMetricsTest(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool @@ -582,12 +565,11 @@ func addMetricsTest(t *testing.T) { } func declareMetricsTest(t *testing.T) { - time.Sleep(time.Second * 1) - - conn, err := net.Dial("tcp", "127.0.0.1:6001") + conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) - defer conn.Close() - + defer func() { + _ = conn.Close() + }() client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) var ret bool -- cgit v1.2.3 From 2d2d876af7e72c6cc448d8bce243b717481558d0 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sat, 14 Nov 2020 19:13:53 +0300 Subject: Complete tests for the Metrics plugin --- plugins/metrics/tests/metrics_test.go | 163 ++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 0eaa94a9..cee33514 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -276,9 +276,172 @@ func TestMetricsDifferentRPCCalls(t *testing.T) { assert.NoError(t, err) assert.Contains(t, genericOut, "sub_gauge_subVector{section=\"first\",type=\"core\"} 1") + t.Run("RegisterHistogram", registerHistogram) + + genericOut, err = get(getAddr) + assert.NoError(t, err) + assert.Contains(t, genericOut, `TYPE histogram_registerHistogram`) + + // check buckets + assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="0.1"} 0`) + assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="0.2"} 0`) + assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="0.5"} 0`) + assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="+Inf"} 0`) + assert.Contains(t, genericOut, `histogram_registerHistogram_sum 0`) + assert.Contains(t, genericOut, `histogram_registerHistogram_count 0`) + + t.Run("CounterMetric", counterMetric) + genericOut, err = get(getAddr) + assert.NoError(t, err) + assert.Contains(t, genericOut, "HELP default_default_counter_CounterMetric test_counter") + assert.Contains(t, genericOut, `default_default_counter_CounterMetric{section="section2",type="type2"}`) + + t.Run("ObserveMetric", observeMetric) + genericOut, err = get(getAddr) + assert.NoError(t, err) + assert.Contains(t, genericOut, "observe_observeMetric") + + t.Run("ObserveMetricNotEnoughLabels", observeMetricNotEnoughLabels) + close(sig) } +func observeMetricNotEnoughLabels(t *testing.T) { + conn, err := net.Dial(dialNetwork, dialAddr) + assert.NoError(t, err) + defer func() { + _ = conn.Close() + }() + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "observe_observeMetricNotEnoughLabels", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Help: "test_observe", + Type: metrics.Histogram, + Labels: []string{"type", "section"}, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + assert.Error(t, client.Call("metrics.Observe", metrics.Metric{ + Name: "observe_observeMetric", + Value: 100.0, + Labels: []string{"test"}, + }, &ret)) + assert.False(t, ret) +} + +func observeMetric(t *testing.T) { + conn, err := net.Dial(dialNetwork, dialAddr) + assert.NoError(t, err) + defer func() { + _ = conn.Close() + }() + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "observe_observeMetric", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Help: "test_observe", + Type: metrics.Histogram, + Labels: []string{"type", "section"}, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + assert.NoError(t, client.Call("metrics.Observe", metrics.Metric{ + Name: "observe_observeMetric", + Value: 100.0, + Labels: []string{"test", "test2"}, + }, &ret)) + assert.True(t, ret) +} + +func counterMetric(t *testing.T) { + conn, err := net.Dial(dialNetwork, dialAddr) + assert.NoError(t, err) + defer func() { + _ = conn.Close() + }() + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "counter_CounterMetric", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Help: "test_counter", + Type: metrics.Counter, + Labels: []string{"type", "section"}, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + assert.NoError(t, client.Call("metrics.Add", metrics.Metric{ + Name: "counter_CounterMetric", + Value: 100.0, + Labels: []string{"type2", "section2"}, + }, &ret)) + assert.True(t, ret) +} + +func registerHistogram(t *testing.T) { + conn, err := net.Dial(dialNetwork, dialAddr) + assert.NoError(t, err) + defer func() { + _ = conn.Close() + }() + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "histogram_registerHistogram", + Collector: metrics.Collector{ + Help: "test_histogram", + Type: metrics.Histogram, + Buckets: []float64{0.1, 0.2, 0.5}, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "histogram_registerHistogram", + Value: 10000, + Labels: nil, + } + + err = client.Call("metrics.Add", m, &ret) + assert.Error(t, err) + assert.False(t, ret) + +} + func subVector(t *testing.T) { conn, err := net.Dial(dialNetwork, dialAddr) assert.NoError(t, err) -- cgit v1.2.3 From cfae2ff7bf298ac291b5b09b73c6be1435651941 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sat, 14 Nov 2020 19:16:04 +0300 Subject: apply golangci linters fixes --- plugins/metrics/tests/metrics_test.go | 1 - 1 file changed, 1 deletion(-) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index cee33514..7528fc46 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -439,7 +439,6 @@ func registerHistogram(t *testing.T) { err = client.Call("metrics.Add", m, &ret) assert.Error(t, err) assert.False(t, ret) - } func subVector(t *testing.T) { -- cgit v1.2.3 From f592c6e410fd1f8075c8b75597eb8e5dc7ba9cf1 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 16 Nov 2020 15:36:52 +0300 Subject: Metrics collector returns slice of values --- plugins/metrics/tests/metrics_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 7528fc46..d3043fbb 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -158,9 +158,9 @@ func TestMetricsGaugeCollector(t *testing.T) { assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") - genericOut, err := get("http://localhost:2112/metrics") + out, err = get("http://localhost:2112/metrics") assert.NoError(t, err) - assert.Contains(t, genericOut, "go_gc_duration_seconds") + assert.Contains(t, out, "go_gc_duration_seconds") for { select { -- cgit v1.2.3 From eba447f39dfe085ece9bb50f26c2d1c6db9b1d89 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 16 Nov 2020 15:39:28 +0300 Subject: MetricsCollector returns multiply collectors --- plugins/metrics/tests/metrics_test.go | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/metrics/tests/metrics_test.go') diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index d3043fbb..ed5d085a 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -157,6 +157,7 @@ func TestMetricsGaugeCollector(t *testing.T) { out, err := get("http://localhost:2112/metrics") assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") + assert.Contains(t, out, "my_gauge2 100") out, err = get("http://localhost:2112/metrics") assert.NoError(t, err) -- cgit v1.2.3