mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-21 06:37:21 +00:00
Remove redundant tests.
This commit is contained in:
@@ -1,87 +0,0 @@
|
|||||||
package libpack_monitoring
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (suite *MonitoringTestSuite) Test_validate_metrics_name() {
|
|
||||||
type args struct {
|
|
||||||
name string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Test_validate_metrics_name - valid",
|
|
||||||
args: args{
|
|
||||||
name: "test_metrics_name",
|
|
||||||
},
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test_validate_metrics_name - invalid",
|
|
||||||
args: args{
|
|
||||||
name: "test metrics name",
|
|
||||||
},
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test_validate_metrics_name - invalid - special chars",
|
|
||||||
args: args{
|
|
||||||
name: "test_metrics_name!",
|
|
||||||
},
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
suite.T().Run(tt.name, func(t *testing.T) {
|
|
||||||
err := validate_metrics_name(tt.args.name)
|
|
||||||
if tt.wantErr {
|
|
||||||
assert.Error(t, err)
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *MonitoringTestSuite) TestValidateMetricsName() {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
expected error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "valid_name",
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "name with spaces",
|
|
||||||
expected: fmt.Errorf("Invalid metric name: %s, expected %s", "name with spaces", "name_with_spaces"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "name with non-alphanumeric characters",
|
|
||||||
expected: fmt.Errorf("Invalid metric name: %s, expected %s", "name with non-alphanumeric characters", "name_with_nonalphanumeric_characters"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "name__with__consecutive__underscores",
|
|
||||||
expected: fmt.Errorf("Invalid metric name: %s, expected %s", "name__with__consecutive__underscores", "name_with_consecutive_underscores"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "_name_with_leading_or_trailing_underscores_",
|
|
||||||
expected: fmt.Errorf("Invalid metric name: %s, expected %s", "_name_with_leading_or_trailing_underscores_", "name_with_leading_or_trailing_underscores"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
suite.T().Run(tt.name, func(t *testing.T) {
|
|
||||||
err := validate_metrics_name(tt.name)
|
|
||||||
assert.Equal(t, tt.expected, err)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
package libpack_monitoring
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/suite"
|
|
||||||
)
|
|
||||||
|
|
||||||
// import (
|
|
||||||
// "fmt"
|
|
||||||
// "io"
|
|
||||||
// "net/http"
|
|
||||||
// "testing"
|
|
||||||
// "time"
|
|
||||||
|
|
||||||
// "github.com/stretchr/testify/assert"
|
|
||||||
// "github.com/stretchr/testify/suite"
|
|
||||||
// libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config"
|
|
||||||
// )
|
|
||||||
|
|
||||||
type MonitoringTestSuite struct {
|
|
||||||
suite.Suite
|
|
||||||
metrics_endpoint string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *MonitoringTestSuite) SetupTest() {
|
|
||||||
suite.metrics_endpoint = "http://localhost:9393/metrics"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *MonitoringTestSuite) TearDownTest() {
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMonitoringSuite(t *testing.T) {
|
|
||||||
suite.Run(t, new(MonitoringTestSuite))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *MonitoringTestSuite) testing_call_metrics_endpoint() *http.Response {
|
|
||||||
resp, err := http.Get(suite.metrics_endpoint)
|
|
||||||
if err != nil {
|
|
||||||
suite.T().Error("Can't call metrics endpoint", err)
|
|
||||||
suite.T().FailNow()
|
|
||||||
}
|
|
||||||
return resp
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *MonitoringTestSuite) testing_get_body_as_text(resp *http.Response) string {
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
suite.T().Error("Can't read response body", err)
|
|
||||||
suite.T().FailNow()
|
|
||||||
}
|
|
||||||
return string(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *MonitoringTestSuite) TestNewMonitoring() {
|
|
||||||
metrics_prefix := "within_test"
|
|
||||||
|
|
||||||
suite.T().Run("TestWholeEndpoint", func(t *testing.T) {
|
|
||||||
mon := NewMonitoring()
|
|
||||||
mon.AddMetricsPrefix(metrics_prefix)
|
|
||||||
mon.RegisterDefaultMetrics()
|
|
||||||
|
|
||||||
resp := suite.testing_call_metrics_endpoint()
|
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
assert.Contains(t, suite.testing_get_body_as_text(resp), "within_test_requests_succesful", "Metrics endpoint should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
mon.RegisterMetricsGauge("test_gauge_metrics", nil, 14)
|
|
||||||
resp = suite.testing_call_metrics_endpoint()
|
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
assert.Contains(t, suite.testing_get_body_as_text(resp), "within_test_test_gauge_metrics", "Metrics endpoint should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
// Triggering it again will increase the gauge by 1
|
|
||||||
mon.RegisterMetricsGauge("test_gauge_metrics", nil, 7)
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
resp = suite.testing_call_metrics_endpoint()
|
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
assert.Contains(t, suite.testing_get_body_as_text(resp), "within_test_test_gauge_metrics 7", "Metrics endpoint should contain incremented () gauge metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
mon.RegisterMetricsCounter("test_counter_metrics", nil)
|
|
||||||
mon.Increment("test_counter_metrics", nil)
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
resp = suite.testing_call_metrics_endpoint()
|
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
assert.Contains(t, suite.testing_get_body_as_text(resp), "within_test_test_counter_metrics", "Metrics endpoint should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
mon.Increment("test_counter_metrics", nil)
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
resp = suite.testing_call_metrics_endpoint()
|
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
assert.Contains(t, suite.testing_get_body_as_text(resp), "within_test_test_counter_metrics 2", "Metrics endpoint should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
// mon.AddCustomMetrics(&CustomMetrics{
|
|
||||||
// Name: "test_custom_metrics",
|
|
||||||
// Help: "test custom metrics",
|
|
||||||
// Type: TypeHistogram,
|
|
||||||
// }, libpack_config.PKG_NAME)
|
|
||||||
// resp = suite.testing_call_metrics_endpoint()
|
|
||||||
// assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
// assert.Contains(t, suite.testing_get_body_as_text(resp), "within_test_test_custom_metrics", "Metrics endpoint should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
// fmt.Println(suite.testing_get_body_as_text(resp))
|
|
||||||
|
|
||||||
// assert.Containsf(t, mon.ListActiveMetrics(), "test_custom_metrics", "ListActiveMetrics() should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
// mon.AddCustomMetrics(&CustomMetrics{
|
|
||||||
// Name: "test_gauge_metrics",
|
|
||||||
// Help: "test gauge metrics",
|
|
||||||
// Type: TypeGauge,
|
|
||||||
// }, libpack_config.PKG_NAME)
|
|
||||||
// mon.Increment("test_gauge_metrics")
|
|
||||||
// time.Sleep(2 * time.Second)
|
|
||||||
// resp = suite.testing_call_metrics_endpoint()
|
|
||||||
// assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
// assert.Contains(t, suite.testing_get_body_as_text(resp), "test_gauge_metrics{microservice=\""+libpack_config.PKG_NAME+"\"} 1", "Metrics endpoint should contain incremented (1) gauge metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
// mon.Increment("test_gauge_metrics")
|
|
||||||
// time.Sleep(2 * time.Second)
|
|
||||||
// resp = suite.testing_call_metrics_endpoint()
|
|
||||||
// assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
// assert.Contains(t, suite.testing_get_body_as_text(resp), "test_gauge_metrics{microservice=\""+libpack_config.PKG_NAME+"\"} 2", "Metrics endpoint should contain incremented (2) gauge metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
// mon.AddCustomMetrics(&CustomMetrics{
|
|
||||||
// Name: "test_counter_metrics",
|
|
||||||
// Help: "test counter metrics",
|
|
||||||
// Type: TypeCounter,
|
|
||||||
// }, libpack_config.PKG_NAME)
|
|
||||||
// mon.Set("test_counter_metrics", 7)
|
|
||||||
// time.Sleep(2 * time.Second)
|
|
||||||
// resp = suite.testing_call_metrics_endpoint()
|
|
||||||
// assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
// assert.Contains(t, suite.testing_get_body_as_text(resp), "test_counter_metrics{microservice=\""+libpack_config.PKG_NAME+"\"} 7", "Metrics endpoint should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
resp = suite.testing_call_metrics_endpoint()
|
|
||||||
assert.Equal(t, 200, resp.StatusCode)
|
|
||||||
assert.Contains(t, suite.testing_get_body_as_text(resp), "go_goroutines", "Metrics endpoint should contain metrics with prefix %s", metrics_prefix)
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user