|
| 1 | +package commonutils_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/newrelic/infra-integrations-sdk/v3/integration" |
| 7 | + "github.com/newrelic/nri-postgresql/src/args" |
| 8 | + commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSetMetric(t *testing.T) { |
| 13 | + pgIntegration, _ := integration.New("test", "1.0.0") |
| 14 | + entity, _ := pgIntegration.Entity("test-entity", "test-type") |
| 15 | + |
| 16 | + metricSet := entity.NewMetricSet("test-event") |
| 17 | + |
| 18 | + commonutils.SetMetric(metricSet, "testGauge", 123.0, "gauge") |
| 19 | + assert.Equal(t, 123.0, metricSet.Metrics["testGauge"]) |
| 20 | + |
| 21 | + commonutils.SetMetric(metricSet, "testAttribute", "value", "attribute") |
| 22 | + assert.Equal(t, "value", metricSet.Metrics["testAttribute"]) |
| 23 | + |
| 24 | + commonutils.SetMetric(metricSet, "testDefault", 456.0, "unknown") |
| 25 | + assert.Equal(t, 456.0, metricSet.Metrics["testDefault"]) |
| 26 | +} |
| 27 | + |
| 28 | +func TestIngestMetric(t *testing.T) { |
| 29 | + pgIntegration, _ := integration.New("test", "1.0.0") |
| 30 | + args := args.ArgumentList{ |
| 31 | + Hostname: "localhost", |
| 32 | + Port: "5432", |
| 33 | + } |
| 34 | + metricList := []interface{}{ |
| 35 | + struct { |
| 36 | + TestField int `metric_name:"testField" source_type:"gauge"` |
| 37 | + }{TestField: 123}, |
| 38 | + } |
| 39 | + |
| 40 | + commonutils.IngestMetric(metricList, "testEvent", pgIntegration, args) |
| 41 | + assert.NotEmpty(t, pgIntegration.Entities) |
| 42 | +} |
| 43 | + |
| 44 | +func TestCreateEntity(t *testing.T) { |
| 45 | + pgIntegration, _ := integration.New("test", "1.0.0") |
| 46 | + args := args.ArgumentList{ |
| 47 | + Hostname: "localhost", |
| 48 | + Port: "5432", |
| 49 | + } |
| 50 | + |
| 51 | + entity, err := commonutils.CreateEntity(pgIntegration, args) |
| 52 | + assert.NoError(t, err) |
| 53 | + assert.NotNil(t, entity) |
| 54 | + assert.Equal(t, "localhost:5432", entity.Metadata.Name) |
| 55 | +} |
| 56 | + |
| 57 | +func TestProcessModel(t *testing.T) { |
| 58 | + pgIntegration, _ := integration.New("test", "1.0.0") |
| 59 | + entity, _ := pgIntegration.Entity("test-entity", "test-type") |
| 60 | + |
| 61 | + metricSet := entity.NewMetricSet("test-event") |
| 62 | + |
| 63 | + model := struct { |
| 64 | + TestField int `metric_name:"testField" source_type:"gauge"` |
| 65 | + }{TestField: 123} |
| 66 | + |
| 67 | + err := commonutils.ProcessModel(model, metricSet) |
| 68 | + assert.NoError(t, err) |
| 69 | + assert.Equal(t, 123.0, metricSet.Metrics["testField"]) |
| 70 | +} |
| 71 | + |
| 72 | +func TestPublishMetrics(t *testing.T) { |
| 73 | + pgIntegration, _ := integration.New("test", "1.0.0") |
| 74 | + args := args.ArgumentList{ |
| 75 | + Hostname: "localhost", |
| 76 | + Port: "5432", |
| 77 | + } |
| 78 | + entity, _ := commonutils.CreateEntity(pgIntegration, args) |
| 79 | + |
| 80 | + err := commonutils.PublishMetrics(pgIntegration, &entity, args) |
| 81 | + assert.NoError(t, err) |
| 82 | + assert.NotNil(t, entity) |
| 83 | +} |
0 commit comments