|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package exporter |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + |
| 22 | + v1 "k8s.io/api/core/v1" |
| 23 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 24 | +) |
| 25 | + |
| 26 | +func TestPorts(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + testName string |
| 29 | + parser *PrometheusExporterParser |
| 30 | + want []v1.ServicePort |
| 31 | + }{ |
| 32 | + { |
| 33 | + testName: "Valid Configuration", |
| 34 | + parser: &PrometheusExporterParser{ |
| 35 | + name: "test-exporter", |
| 36 | + config: map[interface{}]interface{}{ |
| 37 | + "endpoint": "http://myprometheus.io:9090", |
| 38 | + }, |
| 39 | + }, |
| 40 | + want: []v1.ServicePort{ |
| 41 | + { |
| 42 | + Name: "test-exporter", |
| 43 | + Port: 9090, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + testName: "Empty Configuration", |
| 49 | + parser: &PrometheusExporterParser{ |
| 50 | + name: "test-exporter", |
| 51 | + config: nil, // Simulate no configuration provided |
| 52 | + }, |
| 53 | + want: []v1.ServicePort{ |
| 54 | + { |
| 55 | + Name: "test-exporter", |
| 56 | + Port: defaultPrometheusPort, |
| 57 | + TargetPort: intstr.FromInt(int(defaultPrometheusPort)), |
| 58 | + Protocol: v1.ProtocolTCP, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + testName: "Invalid Endpoint No Port", |
| 64 | + parser: &PrometheusExporterParser{ |
| 65 | + name: "test-exporter", |
| 66 | + config: map[interface{}]interface{}{ |
| 67 | + "endpoint": "invalidendpoint", |
| 68 | + }, |
| 69 | + }, |
| 70 | + want: []v1.ServicePort{}, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.testName, func(t *testing.T) { |
| 76 | + ports, _ := tt.parser.Ports() |
| 77 | + assert.Equal(t, tt.want, ports) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
0 commit comments