Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort order of ports returned to fix flaky tests #1003

Merged
merged 6 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/collector/adapters/config_to_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package adapters

import (
"errors"
"sort"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -89,5 +90,12 @@ func ConfigToReceiverPorts(logger logr.Logger, config map[interface{}]interface{
ports = append(ports, rcvrPorts...)
}
}

if len(ports) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check should not be needed

sort.Slice(ports, func(i, j int) bool {
return ports[i].Name < ports[j].Name
})
}

return ports, nil
}
72 changes: 18 additions & 54 deletions pkg/collector/adapters/config_to_ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/open-telemetry/opentelemetry-operator/pkg/collector/adapters"
Expand Down Expand Up @@ -87,61 +88,24 @@ service:
assert.Len(t, ports, 11)

// verify
expectedPorts := map[int32]bool{}
expectedPorts[int32(12345)] = false
expectedPorts[int32(12346)] = false
expectedPorts[int32(14250)] = false
expectedPorts[int32(6831)] = false
expectedPorts[int32(6833)] = false
expectedPorts[int32(15268)] = false
expectedPorts[int32(4318)] = false
expectedPorts[int32(55681)] = false
expectedPorts[int32(55555)] = false
expectedPorts[int32(9411)] = false
httpAppProtocol := "http"
grpcAppProtocol := "grpc"
targetPortZero := intstr.IntOrString{Type: 0, IntVal: 0, StrVal: ""}
targetPort4317 := intstr.IntOrString{Type: 0, IntVal: 4317, StrVal: ""}
targetPort4318 := intstr.IntOrString{Type: 0, IntVal: 4318, StrVal: ""}

expectedNames := map[string]bool{}
expectedNames["examplereceiver"] = false
expectedNames["examplereceiver-settings"] = false
expectedNames["jaeger-grpc"] = false
expectedNames["jaeger-thrift-compact"] = false
expectedNames["jaeger-thrift-binary"] = false
expectedNames["jaeger-custom-thrift-http"] = false
expectedNames["otlp-grpc"] = false
expectedNames["otlp-http"] = false
expectedNames["otlp-http-legacy"] = false
expectedNames["otlp-2-grpc"] = false
expectedNames["zipkin"] = false

expectedAppProtocols := map[string]string{}
expectedAppProtocols["otlp-grpc"] = "grpc"
expectedAppProtocols["otlp-http"] = "http"
expectedAppProtocols["otlp-http-legacy"] = "http"
expectedAppProtocols["jaeger-custom-thrift-http"] = "http"
expectedAppProtocols["jaeger-grpc"] = "grpc"
expectedAppProtocols["otlp-2-grpc"] = "grpc"
expectedAppProtocols["zipkin"] = "http"

// make sure we only have the ports in the set
for _, port := range ports {
assert.NotNil(t, expectedPorts[port.Port])
assert.NotNil(t, expectedNames[port.Name])
expectedPorts[port.Port] = true
expectedNames[port.Name] = true

if appProtocol, ok := expectedAppProtocols[port.Name]; ok {
assert.Equal(t, appProtocol, *port.AppProtocol)
}
}

// and make sure all the ports from the set are there
for _, val := range expectedPorts {
assert.True(t, val)
}

// make sure we only have the ports names in the set
for _, val := range expectedNames {
assert.True(t, val)
}
assert.Len(t, ports, 11)
assert.Equal(t, corev1.ServicePort{Name: "examplereceiver", Port: int32(12345)}, ports[0])
assert.Equal(t, corev1.ServicePort{Name: "examplereceiver-settings", Port: int32(12346)}, ports[1])
assert.Equal(t, corev1.ServicePort{Name: "jaeger-custom-thrift-http", AppProtocol: &httpAppProtocol, Protocol: "TCP", Port: int32(15268), TargetPort: targetPortZero}, ports[2])
assert.Equal(t, corev1.ServicePort{Name: "jaeger-grpc", AppProtocol: &grpcAppProtocol, Protocol: "TCP", Port: int32(14250)}, ports[3])
assert.Equal(t, corev1.ServicePort{Name: "jaeger-thrift-binary", Protocol: "UDP", Port: int32(6833)}, ports[4])
assert.Equal(t, corev1.ServicePort{Name: "jaeger-thrift-compact", Protocol: "UDP", Port: int32(6831)}, ports[5])
assert.Equal(t, corev1.ServicePort{Name: "otlp-2-grpc", AppProtocol: &grpcAppProtocol, Protocol: "TCP", Port: int32(55555)}, ports[6])
assert.Equal(t, corev1.ServicePort{Name: "otlp-grpc", AppProtocol: &grpcAppProtocol, Port: int32(4317), TargetPort: targetPort4317}, ports[7])
assert.Equal(t, corev1.ServicePort{Name: "otlp-http", AppProtocol: &httpAppProtocol, Port: int32(4318), TargetPort: targetPort4318}, ports[8])
assert.Equal(t, corev1.ServicePort{Name: "otlp-http-legacy", AppProtocol: &httpAppProtocol, Port: int32(55681), TargetPort: targetPort4318}, ports[9])
assert.Equal(t, corev1.ServicePort{Name: "zipkin", AppProtocol: &httpAppProtocol, Protocol: "TCP", Port: int32(9411)}, ports[10])
}

func TestNoPortsParsed(t *testing.T) {
Expand Down