Skip to content

Commit

Permalink
added leak tests for agent v2 (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony authored Aug 30, 2024
1 parent 196ac3f commit 84c1b23
Show file tree
Hide file tree
Showing 22 changed files with 790 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/core/payloads/register_software_details_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package payloads

import (
"testing"

"github.com/nginx/agent/sdk/v2/proto"
"github.com/stretchr/testify/assert"
)

func TestNewDataplaneSoftwareDetailsUpdate(t *testing.T) {
pluginName := "test-plugin"
details := &proto.DataplaneSoftwareDetails{}

update := NewDataplaneSoftwareDetailsUpdate(pluginName, details)

assert.NotNil(t, update, "NewDataplaneSoftwareDetailsUpdate should not return nil")
assert.Equal(t, pluginName, update.GetPluginName(), "PluginName should match the one passed to the constructor")
assert.Equal(t, details, update.GetDataplaneSoftwareDetails(), "DataplaneSoftwareDetails should match the one passed to the constructor")
}

func TestDataplaneSoftwareDetailsUpdate_GetPluginName(t *testing.T) {
pluginName := "test-plugin"
update := NewDataplaneSoftwareDetailsUpdate(pluginName, nil)

assert.Equal(t, pluginName, update.GetPluginName(), "GetPluginName should return the correct plugin name")
}

func TestDataplaneSoftwareDetailsUpdate_GetDataplaneSoftwareDetails(t *testing.T) {
details := &proto.DataplaneSoftwareDetails{}
update := NewDataplaneSoftwareDetailsUpdate("test-plugin", details)

assert.Equal(t, details, update.GetDataplaneSoftwareDetails(), "GetDataplaneSoftwareDetails should return the correct details")
}
18 changes: 18 additions & 0 deletions src/extensions/advanced-metrics/aggregator/leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package aggregator

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
18 changes: 18 additions & 0 deletions src/extensions/advanced-metrics/ingester/leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package ingester

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package advanced_metrics

import (
"context"
"testing"
"time"

"github.com/nginx/agent/v2/src/extensions/advanced-metrics/tables/schema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewAdvancedMetrics(t *testing.T) {
config := Config{
Address: "test_address",
AggregatorConfig: AggregatorConfig{
AggregationPeriod: 1 * time.Minute,
PublishingPeriod: 2 * time.Minute,
},
TableSizesLimits: TableSizesLimits{
StagingTableMaxSize: 1000,
StagingTableThreshold: 500,
PriorityTableMaxSize: 1000,
PriorityTableThreshold: 500,
},
}

schema := &schema.Schema{}

advancedMetrics, err := NewAdvancedMetrics(config, schema)
require.NoError(t, err, "Failed to create AdvancedMetrics instance")
assert.NotNil(t, advancedMetrics, "AdvancedMetrics instance should not be nil")

assert.Equal(t, config, advancedMetrics.config, "Config should match")
assert.NotNil(t, advancedMetrics.metricsChannel, "metricsChannel should not be nil")
assert.NotNil(t, advancedMetrics.publisher, "publisher should not be nil")
assert.NotNil(t, advancedMetrics.reader, "reader should not be nil")
assert.NotNil(t, advancedMetrics.ingester, "ingester should not be nil")
assert.NotNil(t, advancedMetrics.aggregator, "aggregator should not be nil")
}

func TestNewAdvancedMetrics_Failure(t *testing.T) {
// Invalid TableSizesLimits to trigger error
config := Config{
Address: "test_address",
AggregatorConfig: AggregatorConfig{
AggregationPeriod: 1 * time.Minute,
PublishingPeriod: 2 * time.Minute,
},
TableSizesLimits: TableSizesLimits{
StagingTableMaxSize: -1, // Invalid size
StagingTableThreshold: 500,
PriorityTableMaxSize: 1000,
PriorityTableThreshold: 500,
},
}

schema := &schema.Schema{}

advancedMetrics, err := NewAdvancedMetrics(config, schema)
require.Error(t, err, "Expected error due to invalid table sizes limits")
assert.Nil(t, advancedMetrics, "AdvancedMetrics instance should be nil")
}

func TestAdvancedMetrics_OutChannel(t *testing.T) {
config := Config{
Address: "test_address",
AggregatorConfig: AggregatorConfig{
AggregationPeriod: 1 * time.Minute,
PublishingPeriod: 2 * time.Minute,
},
TableSizesLimits: TableSizesLimits{
StagingTableMaxSize: 1000,
StagingTableThreshold: 500,
PriorityTableMaxSize: 1000,
PriorityTableThreshold: 500,
},
}

schema := &schema.Schema{}

advancedMetrics, err := NewAdvancedMetrics(config, schema)
require.NoError(t, err, "Failed to create AdvancedMetrics instance")

outChannel := advancedMetrics.OutChannel()
assert.NotNil(t, outChannel, "OutChannel should not be nil")
}

func TestAdvancedMetrics_Run(t *testing.T) {
config := Config{
Address: "test_address",
AggregatorConfig: AggregatorConfig{
AggregationPeriod: 1 * time.Second,
PublishingPeriod: 2 * time.Second,
},
TableSizesLimits: TableSizesLimits{
StagingTableMaxSize: 1000,
StagingTableThreshold: 500,
PriorityTableMaxSize: 1000,
PriorityTableThreshold: 500,
},
}

schema := &schema.Schema{}

advancedMetrics, err := NewAdvancedMetrics(config, schema)
require.NoError(t, err, "Failed to create AdvancedMetrics instance")

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Run AdvancedMetrics in a separate goroutine to avoid blocking
go func() {
err := advancedMetrics.Run(ctx)
assert.NoError(t, err, "Run should not return an error")
}()

// Allow some time for goroutines to start
time.Sleep(500 * time.Millisecond)

// After short delay, cancel the context to stop the run
cancel()

// Wait for all goroutines to finish
time.Sleep(500 * time.Millisecond)
}
18 changes: 18 additions & 0 deletions src/extensions/advanced-metrics/pkg/advanced-metrics/leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package advanced_metrics

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
18 changes: 18 additions & 0 deletions src/extensions/advanced-metrics/pkg/publisher/leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package publisher

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
18 changes: 18 additions & 0 deletions src/extensions/advanced-metrics/pkg/schema/leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package schema

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
94 changes: 94 additions & 0 deletions src/extensions/advanced-metrics/pkg/schema/schema_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package schema

import (
"testing"

"github.com/nginx/agent/v2/src/extensions/advanced-metrics/tables/limits"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewSchemaBuilder(t *testing.T) {
builder := NewSchemaBuilder()
assert.NotNil(t, builder, "SchemaBuilder should not be nil")
assert.Equal(t, 0, len(builder.fields), "SchemaBuilder should initialize with an empty fields slice")
}

func TestSchemaBuilder_NewDimension(t *testing.T) {
builder := NewSchemaBuilder()
builder.NewDimension("dimension1", 10)

assert.Equal(t, 1, len(builder.fields), "SchemaBuilder should contain one field after adding a dimension")
assert.Equal(t, "dimension1", builder.fields[0].Name, "The name of the dimension should be set correctly")
}

func TestSchemaBuilder_NewIntegerDimension(t *testing.T) {
builder := NewSchemaBuilder()
builder.NewIntegerDimension("intDimension", 100)

assert.Equal(t, 1, len(builder.fields), "SchemaBuilder should contain one field after adding an integer dimension")
assert.Equal(t, "intDimension", builder.fields[0].Name, "The name of the integer dimension should be set correctly")

transformFunc := builder.fields[0].Transform
assert.NotNil(t, transformFunc, "Integer dimension should have a transform function set")
assert.Equal(t, &integerDimensionTransformFunction, transformFunc, "Transform function should be correctly set for integer dimension")
}

func TestSchemaBuilder_NewMetric(t *testing.T) {
builder := NewSchemaBuilder()
builder.NewMetric("metric1")

assert.Equal(t, 1, len(builder.fields), "SchemaBuilder should contain one field after adding a metric")
assert.Equal(t, "metric1", builder.fields[0].Name, "The name of the metric should be set correctly")
}

func TestSchemaBuilder_Build_Success(t *testing.T) {
builder := NewSchemaBuilder()
builder.NewDimension("dimension1", 10, WithCollapsingLevel(50))
builder.NewMetric("metric1")

sch, err := builder.Build()
require.NoError(t, err, "Build should succeed with valid configuration")
assert.NotNil(t, sch, "Schema should not be nil after build")
assert.Equal(t, 1, len(sch.Metrics()), "Schema should have one metric")
assert.Equal(t, 1, len(sch.Dimensions()), "Schema should have one dimension")
}

func TestSchemaBuilder_Build_Failure_CollapsingLevel(t *testing.T) {
builder := NewSchemaBuilder()
invalidLevel := limits.MaxCollapseLevel + 1
builder.NewDimension("dimension1", 10, WithCollapsingLevel(invalidLevel))
builder.NewMetric("metric1")

sch, err := builder.Build()
assert.Error(t, err, "Build should fail if a dimension has a collapsing level greater than the maximum allowed")
assert.Nil(t, sch, "Schema should be nil if build fails")
assert.Contains(t, err.Error(), "greater than maximum allowed value", "Error message should indicate invalid collapsing level")
}

func TestIntegerDimensionTransformFromData(t *testing.T) {
data := []byte("1a")
expectedValue := 26
value, err := integerDimensionTransformFromData(data)
require.NoError(t, err, "integerDimensionTransformFromData should succeed for valid input")
assert.Equal(t, expectedValue, value, "Transform function should correctly parse hex string to int")

invalidData := []byte("zz")
_, err = integerDimensionTransformFromData(invalidData)
assert.Error(t, err, "integerDimensionTransformFromData should fail for invalid hex string")
}

func TestIntegerDimensionTransformFromLookupCode(t *testing.T) {
code := 42
expectedString := "42"
str, err := integerDimensionTransformFromLookupCode(code)
require.NoError(t, err, "integerDimensionTransformFromLookupCode should succeed for valid input")
assert.Equal(t, expectedString, str, "Transform function should correctly convert int to string")
}
18 changes: 18 additions & 0 deletions src/extensions/advanced-metrics/reader/leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

package reader

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
Loading

0 comments on commit 84c1b23

Please sign in to comment.