Skip to content

Commit

Permalink
chore: added unit test for extension service function
Browse files Browse the repository at this point in the history
Signed-off-by: Ankit152 <[email protected]>
  • Loading branch information
Ankit152 committed Nov 7, 2024
1 parent af1bbcb commit eb14bf7
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/manifests/collector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
const (
headlessLabel = "operator.opentelemetry.io/collector-headless-service"
monitoringLabel = "operator.opentelemetry.io/collector-monitoring-service"
extensionService = "operator.opentelemetry.io/collector-extension-service"
serviceTypeLabel = "operator.opentelemetry.io/collector-service-type"
valueExists = "Exists"
)
Expand All @@ -42,10 +43,11 @@ const (
BaseServiceType ServiceType = iota
HeadlessServiceType
MonitoringServiceType
ExtensionServiceType
)

func (s ServiceType) String() string {
return [...]string{"base", "headless", "monitoring"}[s]
return [...]string{"base", "headless", "monitoring", "extension"}[s]
}

func HeadlessService(params manifests.Params) (*corev1.Service, error) {
Expand Down Expand Up @@ -111,6 +113,8 @@ func MonitoringService(params manifests.Params) (*corev1.Service, error) {
func ExtensionService(params manifests.Params) (*corev1.Service, error) {
name := naming.Service(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels[extensionService] = valueExists
labels[serviceTypeLabel] = ExtensionServiceType.String()

annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
Expand All @@ -122,6 +126,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,
Expand Down
155 changes: 155 additions & 0 deletions internal/manifests/collector/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,161 @@ 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{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
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{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
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{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
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{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
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{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
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)
}
Expand Down

0 comments on commit eb14bf7

Please sign in to comment.