From 84c1b239ffa4b6e27493230732d9e8f275ad40f5 Mon Sep 17 00:00:00 2001 From: oliveromahony Date: Fri, 30 Aug 2024 16:17:43 +0100 Subject: [PATCH] added leak tests for agent v2 (#807) --- .../register_software_details_test.go | 40 ++++++ .../advanced-metrics/aggregator/leak_test.go | 18 +++ .../advanced-metrics/ingester/leak_test.go | 18 +++ .../advanced-metrics/advanced_metrics_test.go | 132 ++++++++++++++++++ .../pkg/advanced-metrics/leak_test.go | 18 +++ .../pkg/publisher/leak_test.go | 18 +++ .../advanced-metrics/pkg/schema/leak_test.go | 18 +++ .../pkg/schema/schema_builder_test.go | 94 +++++++++++++ .../advanced-metrics/reader/leak_test.go | 18 +++ .../advanced-metrics/tables/leak_test.go | 18 +++ .../tables/limits/leak_test.go | 18 +++ .../tables/lookup/leak_test.go | 18 +++ .../tables/priority_table/leak_test.go | 18 +++ .../tables/sample/leak_test.go | 18 +++ .../tables/schema/field_test.go | 93 ++++++++++++ .../tables/schema/leak_test.go | 18 +++ .../tables/schema/schema_test.go | 125 +++++++++++++++++ src/extensions/leak_test.go | 18 +++ .../monitoring/manager/leak_test.go | 18 +++ .../monitoring/processor/leak_test.go | 18 +++ .../nginx-app-protect/nap/leak_test.go | 18 +++ .../prometheus-metrics/leak_test.go | 18 +++ 22 files changed, 790 insertions(+) create mode 100644 src/core/payloads/register_software_details_test.go create mode 100644 src/extensions/advanced-metrics/aggregator/leak_test.go create mode 100644 src/extensions/advanced-metrics/ingester/leak_test.go create mode 100644 src/extensions/advanced-metrics/pkg/advanced-metrics/advanced_metrics_test.go create mode 100644 src/extensions/advanced-metrics/pkg/advanced-metrics/leak_test.go create mode 100644 src/extensions/advanced-metrics/pkg/publisher/leak_test.go create mode 100644 src/extensions/advanced-metrics/pkg/schema/leak_test.go create mode 100644 src/extensions/advanced-metrics/pkg/schema/schema_builder_test.go create mode 100644 src/extensions/advanced-metrics/reader/leak_test.go create mode 100644 src/extensions/advanced-metrics/tables/leak_test.go create mode 100644 src/extensions/advanced-metrics/tables/limits/leak_test.go create mode 100644 src/extensions/advanced-metrics/tables/lookup/leak_test.go create mode 100644 src/extensions/advanced-metrics/tables/priority_table/leak_test.go create mode 100644 src/extensions/advanced-metrics/tables/sample/leak_test.go create mode 100644 src/extensions/advanced-metrics/tables/schema/field_test.go create mode 100644 src/extensions/advanced-metrics/tables/schema/leak_test.go create mode 100644 src/extensions/advanced-metrics/tables/schema/schema_test.go create mode 100644 src/extensions/leak_test.go create mode 100644 src/extensions/nginx-app-protect/monitoring/manager/leak_test.go create mode 100644 src/extensions/nginx-app-protect/monitoring/processor/leak_test.go create mode 100644 src/extensions/nginx-app-protect/nap/leak_test.go create mode 100644 src/extensions/prometheus-metrics/leak_test.go diff --git a/src/core/payloads/register_software_details_test.go b/src/core/payloads/register_software_details_test.go new file mode 100644 index 000000000..9a187e3ce --- /dev/null +++ b/src/core/payloads/register_software_details_test.go @@ -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") +} diff --git a/src/extensions/advanced-metrics/aggregator/leak_test.go b/src/extensions/advanced-metrics/aggregator/leak_test.go new file mode 100644 index 000000000..3587de6f6 --- /dev/null +++ b/src/extensions/advanced-metrics/aggregator/leak_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/ingester/leak_test.go b/src/extensions/advanced-metrics/ingester/leak_test.go new file mode 100644 index 000000000..cf9644ff9 --- /dev/null +++ b/src/extensions/advanced-metrics/ingester/leak_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/pkg/advanced-metrics/advanced_metrics_test.go b/src/extensions/advanced-metrics/pkg/advanced-metrics/advanced_metrics_test.go new file mode 100644 index 000000000..d52bf9ad3 --- /dev/null +++ b/src/extensions/advanced-metrics/pkg/advanced-metrics/advanced_metrics_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/pkg/advanced-metrics/leak_test.go b/src/extensions/advanced-metrics/pkg/advanced-metrics/leak_test.go new file mode 100644 index 000000000..80f827f6b --- /dev/null +++ b/src/extensions/advanced-metrics/pkg/advanced-metrics/leak_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/pkg/publisher/leak_test.go b/src/extensions/advanced-metrics/pkg/publisher/leak_test.go new file mode 100644 index 000000000..d697fd726 --- /dev/null +++ b/src/extensions/advanced-metrics/pkg/publisher/leak_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/pkg/schema/leak_test.go b/src/extensions/advanced-metrics/pkg/schema/leak_test.go new file mode 100644 index 000000000..8f5bb521e --- /dev/null +++ b/src/extensions/advanced-metrics/pkg/schema/leak_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/pkg/schema/schema_builder_test.go b/src/extensions/advanced-metrics/pkg/schema/schema_builder_test.go new file mode 100644 index 000000000..065c5e6ef --- /dev/null +++ b/src/extensions/advanced-metrics/pkg/schema/schema_builder_test.go @@ -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") +} diff --git a/src/extensions/advanced-metrics/reader/leak_test.go b/src/extensions/advanced-metrics/reader/leak_test.go new file mode 100644 index 000000000..6d7f5c34a --- /dev/null +++ b/src/extensions/advanced-metrics/reader/leak_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/tables/leak_test.go b/src/extensions/advanced-metrics/tables/leak_test.go new file mode 100644 index 000000000..fe4f7fcdd --- /dev/null +++ b/src/extensions/advanced-metrics/tables/leak_test.go @@ -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 tables + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/advanced-metrics/tables/limits/leak_test.go b/src/extensions/advanced-metrics/tables/limits/leak_test.go new file mode 100644 index 000000000..20f717e80 --- /dev/null +++ b/src/extensions/advanced-metrics/tables/limits/leak_test.go @@ -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 limits + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/advanced-metrics/tables/lookup/leak_test.go b/src/extensions/advanced-metrics/tables/lookup/leak_test.go new file mode 100644 index 000000000..7f9306bb8 --- /dev/null +++ b/src/extensions/advanced-metrics/tables/lookup/leak_test.go @@ -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 lookup + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/advanced-metrics/tables/priority_table/leak_test.go b/src/extensions/advanced-metrics/tables/priority_table/leak_test.go new file mode 100644 index 000000000..cacb425b7 --- /dev/null +++ b/src/extensions/advanced-metrics/tables/priority_table/leak_test.go @@ -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 priority_table + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/advanced-metrics/tables/sample/leak_test.go b/src/extensions/advanced-metrics/tables/sample/leak_test.go new file mode 100644 index 000000000..77b3912b0 --- /dev/null +++ b/src/extensions/advanced-metrics/tables/sample/leak_test.go @@ -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 sample + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/advanced-metrics/tables/schema/field_test.go b/src/extensions/advanced-metrics/tables/schema/field_test.go new file mode 100644 index 000000000..87db4a363 --- /dev/null +++ b/src/extensions/advanced-metrics/tables/schema/field_test.go @@ -0,0 +1,93 @@ +/** + * 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 ( + "math" + "testing" + + "github.com/nginx/agent/v2/src/extensions/advanced-metrics/tables/limits" + "github.com/stretchr/testify/assert" +) + +func TestNewDimensionField(t *testing.T) { + name := "dimension1" + maxDimensionSetSize := uint32(16) + + // Create a new dimension field + field := NewDimensionField(name, maxDimensionSetSize) + + assert.NotNil(t, field, "NewDimensionField should not return nil") + assert.Equal(t, name, field.Name, "Field name should be set correctly") + assert.Equal(t, FieldTypeDimension, field.Type, "Field type should be set to FieldTypeDimension") + assert.Equal(t, maxDimensionSetSize, field.MaxDimensionSetSize, "MaxDimensionSetSize should be set correctly") + assert.Equal(t, int(math.Log2(float64(maxDimensionSetSize)))+1, field.KeyBitSize, "KeyBitSize should be calculated correctly") + assert.Nil(t, field.CollapsingLevel, "CollapsingLevel should be nil by default") +} + +func TestNewDimensionField_WithOptions(t *testing.T) { + name := "dimensionWithOptions" + maxDimensionSetSize := uint32(16) + customKeyBitSize := 8 + collapsingLevel := limits.CollapsingLevel(50) + transformFunc := &DimensionTransformFunction{ + FromDataToLookupCode: func(data []byte) (int, error) { return 1, nil }, + FromLookupCodeToValue: func(code int) (string, error) { return "value", nil }, + } + + // Create a new dimension field with options + field := NewDimensionField(name, maxDimensionSetSize, + WithKeyBitSize(customKeyBitSize), + WithLevel(collapsingLevel), + WithTransformFunction(transformFunc), + ) + + assert.Equal(t, customKeyBitSize, field.KeyBitSize, "KeyBitSize should be set by WithKeyBitSize option") + assert.Equal(t, &collapsingLevel, field.CollapsingLevel, "CollapsingLevel should be set by WithLevel option") + assert.Equal(t, transformFunc, field.Transform, "Transform function should be set by WithTransformFunction option") +} + +func TestNewMetricField(t *testing.T) { + name := "metric1" + + // Create a new metric field + field := NewMetricField(name) + + assert.NotNil(t, field, "NewMetricField should not return nil") + assert.Equal(t, name, field.Name, "Field name should be set correctly") + assert.Equal(t, FieldTypeMetric, field.Type, "Field type should be set to FieldTypeMetric") + assert.Equal(t, uint32(0), field.MaxDimensionSetSize, "MaxDimensionSetSize should be 0 for metric fields") + assert.Equal(t, 0, field.KeyBitSize, "KeyBitSize should be 0 for metric fields") + assert.Nil(t, field.CollapsingLevel, "CollapsingLevel should be nil for metric fields") + assert.Nil(t, field.Transform, "Transform function should be nil for metric fields") +} + +func TestField_Index(t *testing.T) { + field := NewMetricField("metric1") + field.index = 5 + + assert.Equal(t, 5, field.Index(), "Field index should return the correct value") +} + +func TestField_ShouldCollapse(t *testing.T) { + field := NewDimensionField("dimension1", 16) + level := limits.CollapsingLevel(50) + + // Test when CollapsingLevel is nil + assert.False(t, field.ShouldCollapse(level), "ShouldCollapse should return false if CollapsingLevel is nil") + + // Test when CollapsingLevel is set + field.CollapsingLevel = &level + assert.False(t, field.ShouldCollapse(level), "ShouldCollapse should return false if level is equal to CollapsingLevel") + + // Test when level is greater than CollapsingLevel + assert.True(t, field.ShouldCollapse(level+1), "ShouldCollapse should return true if level is greater than CollapsingLevel") + + // Test when level is less than CollapsingLevel + assert.False(t, field.ShouldCollapse(level-1), "ShouldCollapse should return false if level is less than CollapsingLevel") +} diff --git a/src/extensions/advanced-metrics/tables/schema/leak_test.go b/src/extensions/advanced-metrics/tables/schema/leak_test.go new file mode 100644 index 000000000..8f5bb521e --- /dev/null +++ b/src/extensions/advanced-metrics/tables/schema/leak_test.go @@ -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) +} diff --git a/src/extensions/advanced-metrics/tables/schema/schema_test.go b/src/extensions/advanced-metrics/tables/schema/schema_test.go new file mode 100644 index 000000000..3ed91393c --- /dev/null +++ b/src/extensions/advanced-metrics/tables/schema/schema_test.go @@ -0,0 +1,125 @@ +/** + * 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/stretchr/testify/assert" +) + +func TestNewSchema(t *testing.T) { + field1 := &Field{ + Name: "dimension1", + Type: FieldTypeDimension, + DimensionField: DimensionField{ + KeyBitSize: 8, + KeyBitPositionInCompoundKey: 0, + MaxDimensionSetSize: 0, + Transform: &DimensionTransformFunction{}, + CollapsingLevel: new(uint32), + }, + } + field2 := &Field{ + Name: "metric1", + Type: FieldTypeMetric, + DimensionField: DimensionField{ + KeyBitSize: 0, + KeyBitPositionInCompoundKey: 0, + MaxDimensionSetSize: 0, + Transform: &DimensionTransformFunction{}, + CollapsingLevel: new(uint32), + }, + } + field3 := &Field{ + Name: "dimension2", + Type: FieldTypeDimension, + DimensionField: DimensionField{ + KeyBitSize: 16, + KeyBitPositionInCompoundKey: 0, + MaxDimensionSetSize: 0, + Transform: &DimensionTransformFunction{}, + CollapsingLevel: new(uint32), + }, + } + + schema := NewSchema(field1, field2, field3) + + // Test Schema initialization + assert.NotNil(t, schema, "NewSchema should not return nil") + assert.Equal(t, 3, len(schema.Fields()), "Schema should have 3 fields") + assert.Equal(t, 2, schema.NumDimensions(), "Schema should have 2 dimensions") + assert.Equal(t, 1, schema.NumMetrics(), "Schema should have 1 metric") + assert.Equal(t, 24, schema.KeySize(), "Key size should be the sum of KeyBitSizes for dimensions (8 + 16)") + + // Test Field indexing and KeyBitPositionInCompoundKey + assert.Equal(t, 0, field1.KeyBitPositionInCompoundKey, "Field1 KeyBitPositionInCompoundKey should be 0") + assert.Equal(t, 8, field3.KeyBitPositionInCompoundKey, "Field3 KeyBitPositionInCompoundKey should be 8") + assert.Equal(t, 0, field1.index, "Field1 index should be 0") + assert.Equal(t, 1, field3.index, "Field3 index should be 1") + assert.Equal(t, 0, field2.index, "Field2 index should be 0") + + // Test DimensionKeyPartSizes + assert.Equal(t, []int{8, 16}, schema.DimensionKeyPartSizes(), "DimensionKeyPartSizes should return correct bit sizes") +} + +func TestSchema_Field(t *testing.T) { + field1 := &Field{Name: "dimension1"} + field2 := &Field{Name: "metric1"} + + schema := NewSchema(field1, field2) + + assert.Equal(t, field1, schema.Field(0), "Field(0) should return the correct field") + assert.Equal(t, field2, schema.Field(1), "Field(1) should return the correct field") +} + +func TestSchema_Metric(t *testing.T) { + field := &Field{Name: "metric1", Type: FieldTypeMetric} + schema := NewSchema(field) + + assert.Equal(t, field, schema.Metric(0), "Metric(0) should return the correct metric field") +} + +func TestSchema_Dimension(t *testing.T) { + field := &Field{Name: "dimension1", Type: FieldTypeDimension} + schema := NewSchema(field) + + assert.Equal(t, field, schema.Dimension(0), "Dimension(0) should return the correct dimension field") +} + +func TestSchema_NumMetrics(t *testing.T) { + field1 := &Field{Name: "metric1", Type: FieldTypeMetric} + field2 := &Field{Name: "metric2", Type: FieldTypeMetric} + schema := NewSchema(field1, field2) + + assert.Equal(t, 2, schema.NumMetrics(), "NumMetrics should return the correct number of metrics") +} + +func TestSchema_NumDimensions(t *testing.T) { + field1 := &Field{Name: "dimension1", Type: FieldTypeDimension} + field2 := &Field{Name: "dimension2", Type: FieldTypeDimension} + schema := NewSchema(field1, field2) + + assert.Equal(t, 2, schema.NumDimensions(), "NumDimensions should return the correct number of dimensions") +} + +func TestSchema_KeySize(t *testing.T) { + field1 := &Field{Name: "dimension1", Type: FieldTypeDimension, DimensionField: DimensionField{KeyBitSize: 8}} + field2 := &Field{Name: "dimension2", Type: FieldTypeDimension, DimensionField: DimensionField{KeyBitSize: 16}} + schema := NewSchema(field1, field2) + + assert.Equal(t, 24, schema.KeySize(), "KeySize should return the sum of KeyBitSizes of all dimensions") +} + +func TestSchema_DimensionKeyPartSizes(t *testing.T) { + field1 := &Field{Name: "dimension1", Type: FieldTypeDimension, DimensionField: DimensionField{KeyBitSize: 8}} + field2 := &Field{Name: "dimension2", Type: FieldTypeDimension, DimensionField: DimensionField{KeyBitSize: 16}} + schema := NewSchema(field1, field2) + + assert.Equal(t, []int{8, 16}, schema.DimensionKeyPartSizes(), "DimensionKeyPartSizes should return the correct bit sizes for all dimensions") +} diff --git a/src/extensions/leak_test.go b/src/extensions/leak_test.go new file mode 100644 index 000000000..dc97c1c10 --- /dev/null +++ b/src/extensions/leak_test.go @@ -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 extensions + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/nginx-app-protect/monitoring/manager/leak_test.go b/src/extensions/nginx-app-protect/monitoring/manager/leak_test.go new file mode 100644 index 000000000..6a7a77074 --- /dev/null +++ b/src/extensions/nginx-app-protect/monitoring/manager/leak_test.go @@ -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 manager + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/nginx-app-protect/monitoring/processor/leak_test.go b/src/extensions/nginx-app-protect/monitoring/processor/leak_test.go new file mode 100644 index 000000000..20b2930c5 --- /dev/null +++ b/src/extensions/nginx-app-protect/monitoring/processor/leak_test.go @@ -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 processor + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/nginx-app-protect/nap/leak_test.go b/src/extensions/nginx-app-protect/nap/leak_test.go new file mode 100644 index 000000000..ed2b7224d --- /dev/null +++ b/src/extensions/nginx-app-protect/nap/leak_test.go @@ -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 nap + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/src/extensions/prometheus-metrics/leak_test.go b/src/extensions/prometheus-metrics/leak_test.go new file mode 100644 index 000000000..ce196fdac --- /dev/null +++ b/src/extensions/prometheus-metrics/leak_test.go @@ -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 prometheus_metrics + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +}