From d742325a3ecf09879b09adf2628d118d84e0f33e Mon Sep 17 00:00:00 2001 From: Ankit152 Date: Fri, 1 Nov 2024 23:51:17 +0530 Subject: [PATCH] chore: added unit test for extension service function Signed-off-by: Ankit152 --- internal/manifests/collector/service.go | 4 + internal/manifests/collector/service_test.go | 140 +++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/internal/manifests/collector/service.go b/internal/manifests/collector/service.go index 1782335ac5..e114681bc3 100644 --- a/internal/manifests/collector/service.go +++ b/internal/manifests/collector/service.go @@ -122,6 +122,10 @@ func ExtensionService(params manifests.Params) (*corev1.Service, error) { return nil, err } + if len(ports) == 0 { + return nil, nil + } + return &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: name, diff --git a/internal/manifests/collector/service_test.go b/internal/manifests/collector/service_test.go index 11ac981585..6956f40271 100644 --- a/internal/manifests/collector/service_test.go +++ b/internal/manifests/collector/service_test.go @@ -321,6 +321,146 @@ func TestMonitoringService(t *testing.T) { }) } +func TestExtensionService(t *testing.T) { + t.Run("when the extension has http endpoint", func(t *testing.T) { + params := manifests.Params{ + Config: config.Config{}, + Log: logger, + OtelCol: v1beta1.OpenTelemetryCollector{ + Spec: v1beta1.OpenTelemetryCollectorSpec{ + Config: v1beta1.Config{ + Service: v1beta1.Service{ + Extensions: []string{"jaeger_query"}, + }, + Extensions: &v1beta1.AnyConfig{ + Object: map[string]interface{}{ + "jaeger_query": map[string]interface{}{ + "http": map[string]interface{}{ + "endpoint": "0.0.0.0:16686", + }, + }, + }, + }, + }, + }, + }, + } + + actual, err := ExtensionService(params) + assert.NotNil(t, actual) + assert.NoError(t, err) + }) + + t.Run("when the extension has grpc endpoint", func(t *testing.T) { + params := manifests.Params{ + Config: config.Config{}, + Log: logger, + OtelCol: v1beta1.OpenTelemetryCollector{ + Spec: v1beta1.OpenTelemetryCollectorSpec{ + Config: v1beta1.Config{ + Service: v1beta1.Service{ + Extensions: []string{"jaeger_query"}, + }, + Extensions: &v1beta1.AnyConfig{ + Object: map[string]interface{}{ + "jaeger_query": map[string]interface{}{ + "grpc": map[string]interface{}{ + "endpoint": "0.0.0.0:16686", + }, + }, + }, + }, + }, + }, + }, + } + + actual, err := ExtensionService(params) + assert.NotNil(t, actual) + assert.NoError(t, err) + }) + + t.Run("when the extension has both http and grpc endpoint", func(t *testing.T) { + params := manifests.Params{ + Config: config.Config{}, + Log: logger, + OtelCol: v1beta1.OpenTelemetryCollector{ + Spec: v1beta1.OpenTelemetryCollectorSpec{ + Config: v1beta1.Config{ + Service: v1beta1.Service{ + Extensions: []string{"jaeger_query"}, + }, + Extensions: &v1beta1.AnyConfig{ + Object: map[string]interface{}{ + "jaeger_query": map[string]interface{}{ + "http": map[string]interface{}{ + "endpoint": "0.0.0.0:16686", + }, + "grpc": map[string]interface{}{ + "endpoint": "0.0.0.0:16686", + }, + }, + }, + }, + }, + }, + }, + } + + actual, err := ExtensionService(params) + assert.NotNil(t, actual) + assert.NoError(t, err) + }) + + t.Run("when the extension has no extensions defined", func(t *testing.T) { + params := manifests.Params{ + Config: config.Config{}, + Log: logger, + OtelCol: v1beta1.OpenTelemetryCollector{ + Spec: v1beta1.OpenTelemetryCollectorSpec{ + Config: v1beta1.Config{ + Service: v1beta1.Service{ + Extensions: []string{"jaeger_query"}, + }, + Extensions: &v1beta1.AnyConfig{ + Object: map[string]interface{}{}, + }, + }, + }, + }, + } + + actual, err := ExtensionService(params) + assert.Nil(t, actual) + assert.NoError(t, err) + }) + + t.Run("when the extension has no endpoint defined", func(t *testing.T) { + params := manifests.Params{ + Config: config.Config{}, + Log: logger, + OtelCol: v1beta1.OpenTelemetryCollector{ + Spec: v1beta1.OpenTelemetryCollectorSpec{ + Config: v1beta1.Config{ + Service: v1beta1.Service{ + Extensions: []string{"jaeger_query"}, + }, + Extensions: &v1beta1.AnyConfig{ + Object: map[string]interface{}{ + "jaeger_query": map[string]interface{}{}, + }, + }, + }, + }, + }, + } + + actual, err := ExtensionService(params) + assert.NotNil(t, actual) + assert.NoError(t, err) + }) +} + func service(name string, ports []v1beta1.PortsSpec) v1.Service { return serviceWithInternalTrafficPolicy(name, ports, v1.ServiceInternalTrafficPolicyCluster) }