Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
97 lines (85 loc) · 2.72 KB
/
Copy pathmain_test.go
File metadata and controls
97 lines (85 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"net/http/httptest"
"testing"
"github.com/openstack-exporter/openstack-exporter/exporters"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSetAutoServicesState(t *testing.T) {
serviceStates := map[string]serviceState{
"compute": serviceEnabled,
"network": serviceAuto,
"image": serviceAuto,
}
setAutoServicesState(serviceStates, serviceDisabled)
assert.Equal(t, serviceEnabled, serviceStates["compute"])
assert.Equal(t, serviceDisabled, serviceStates["network"])
assert.Equal(t, serviceDisabled, serviceStates["image"])
}
func TestApplyAutodetection(t *testing.T) {
serviceStates := map[string]serviceState{
"compute": serviceEnabled,
"network": serviceAuto,
"image": serviceAuto,
"dns": serviceDisabled,
}
applyAutodetection(serviceStates, []string{"network"})
assert.Equal(t, serviceEnabled, serviceStates["compute"])
assert.Equal(t, serviceEnabled, serviceStates["network"])
assert.Equal(t, serviceDisabled, serviceStates["image"])
assert.Equal(t, serviceDisabled, serviceStates["dns"])
}
func TestGetEnabledServicesFromStates(t *testing.T) {
serviceStates := make(map[string]serviceState, len(exporters.SupportedExporters))
for _, service := range exporters.SupportedExporters {
serviceStates[service] = serviceDisabled
}
serviceStates["compute"] = serviceEnabled
serviceStates["network"] = serviceEnabled
services := getEnabledServicesFromStates(serviceStates)
assert.ElementsMatch(t, []string{"compute", "network"}, services)
}
func TestSelectServicesForRequest(t *testing.T) {
configured := []string{"compute", "network", "image"}
tests := []struct {
name string
url string
expected []string
errSubstr string
}{
{
name: "filters with include and exclude",
url: "/probe?include_services=compute,image&exclude_services=image",
expected: []string{"compute"},
},
{
name: "normalizes spaces and duplicates",
url: "/probe?include_services=compute,%20compute%20,network",
expected: []string{"compute", "network"},
},
{
name: "rejects invalid include service",
url: "/probe?include_services=compute,bad",
errSubstr: "invalid include_services",
},
{
name: "rejects invalid exclude service",
url: "/probe?exclude_services=bad",
errSubstr: "invalid exclude_services",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest("GET", tc.url, nil)
services, err := selectServicesForRequest(configured, req)
if tc.errSubstr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.errSubstr)
return
}
require.NoError(t, err)
assert.Equal(t, tc.expected, services)
})
}
}