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 <ankitkurmi152@gmail.com>
  • Loading branch information
Ankit152 committed Nov 1, 2024
1 parent 8dd10ad commit d742325
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/manifests/collector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
140 changes: 140 additions & 0 deletions internal/manifests/collector/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d742325

Please sign in to comment.