Skip to content

Commit ab899d4

Browse files
committed
feat: adds tests for Ports in exporter.go
1 parent 3bb5fdf commit ab899d4

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package exporter
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
v1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/util/intstr"
9+
)
10+
11+
func TestPorts(t *testing.T) {
12+
tests := []struct {
13+
testName string
14+
parser *PrometheusExporterParser
15+
want []v1.ServicePort
16+
}{
17+
{
18+
testName: "Valid Configuration",
19+
parser: &PrometheusExporterParser{
20+
name: "test-exporter",
21+
config: map[interface{}]interface{}{
22+
"endpoint": "http://myprometheus.io:9090",
23+
},
24+
},
25+
want: []v1.ServicePort{
26+
{
27+
Name: "test-exporter",
28+
Port: 9091,
29+
},
30+
},
31+
},
32+
{
33+
testName: "Empty Configuration",
34+
parser: &PrometheusExporterParser{
35+
name: "test-exporter",
36+
config: nil, // Simulate no configuration provided
37+
},
38+
want: []v1.ServicePort{
39+
{
40+
Name: "test-exporter",
41+
Port: defaultPrometheusPort,
42+
TargetPort: intstr.FromInt(int(defaultPrometheusPort)),
43+
Protocol: v1.ProtocolTCP,
44+
},
45+
},
46+
},
47+
{
48+
testName: "Invalid Endpoint No Port",
49+
parser: &PrometheusExporterParser{
50+
name: "test-exporter",
51+
config: map[interface{}]interface{}{
52+
"endpoint": "invalidendpoint",
53+
},
54+
},
55+
want: []v1.ServicePort{},
56+
},
57+
}
58+
59+
for _, tt := range tests {
60+
t.Run(tt.testName, func(t *testing.T) {
61+
if got, _ := tt.parser.Ports(); !reflect.DeepEqual(got, tt.want) {
62+
t.Errorf("Ports(%v, = %v, want %v", tt.parser, got, tt.want)
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)