forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_test.go
230 lines (221 loc) · 7.42 KB
/
node_test.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"testing"
"k8s.io/client-go/1.5/pkg/api/resource"
"k8s.io/client-go/1.5/pkg/api/v1"
)
type mockNodeStore struct {
list func() (v1.NodeList, error)
}
func (ns mockNodeStore) List() (v1.NodeList, error) {
return ns.list()
}
func TestNodeCollector(t *testing.T) {
// Fixed metadata on type and help text. We prepend this to every expected
// output so we only have to modify a single place when doing adjustments.
const metadata = `
# HELP kube_node_info Information about a cluster node.
# TYPE kube_node_info gauge
# HELP kube_node_spec_unschedulable Whether a node can schedule new pods.
# TYPE kube_node_spec_unschedulable gauge
# HELP kube_node_status_ready The ready status of a cluster node.
# TYPE kube_node_status_ready gauge
# TYPE kube_node_status_phase gauge
# HELP kube_node_status_phase The phase the node is currently in.
# TYPE kube_node_status_capacity_pods gauge
# HELP kube_node_status_capacity_pods The total pod resources of the node.
# TYPE kube_node_status_capacity_cpu_cores gauge
# HELP kube_node_status_capacity_cpu_cores The total CPU resources of the node.
# TYPE kube_node_status_capacity_memory_bytes gauge
# HELP kube_node_status_capacity_memory_bytes The total memory resources of the node.
# TYPE kube_node_status_allocatable_pods gauge
# HELP kube_node_status_allocatable_pods The pod resources of a node that are available for scheduling.
# TYPE kube_node_status_allocatable_cpu_cores gauge
# HELP kube_node_status_allocatable_cpu_cores The CPU resources of a node that are available for scheduling.
# TYPE kube_node_status_allocatable_memory_bytes gauge
# HELP kube_node_status_allocatable_memory_bytes The memory resources of a node that are available for scheduling.
`
cases := []struct {
nodes []v1.Node
metrics []string // which metrics should be checked
want string
}{
// Verify populating base metrics and that metrics for unset fields are skipped.
{
nodes: []v1.Node{
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.1",
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KernelVersion: "kernel",
KubeletVersion: "kubelet",
KubeProxyVersion: "kubeproxy",
OSImage: "osimage",
ContainerRuntimeVersion: "rkt",
},
},
},
},
want: metadata + `
kube_node_info{container_runtime_version="rkt",kernel_version="kernel",kubelet_version="kubelet",kubeproxy_version="kubeproxy",node="127.0.0.1",os_image="osimage"} 1
kube_node_spec_unschedulable{node="127.0.0.1"} 0
`,
},
// Verify resource metrics.
{
nodes: []v1.Node{
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.1",
},
Spec: v1.NodeSpec{
Unschedulable: true,
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
KernelVersion: "kernel",
KubeletVersion: "kubelet",
KubeProxyVersion: "kubeproxy",
OSImage: "osimage",
ContainerRuntimeVersion: "rkt",
},
Capacity: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("4.3"),
v1.ResourceMemory: resource.MustParse("2G"),
v1.ResourcePods: resource.MustParse("1000"),
},
Allocatable: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("3"),
v1.ResourceMemory: resource.MustParse("1G"),
v1.ResourcePods: resource.MustParse("555"),
},
},
},
},
want: metadata + `
kube_node_info{container_runtime_version="rkt",kernel_version="kernel",kubelet_version="kubelet",kubeproxy_version="kubeproxy",node="127.0.0.1",os_image="osimage"} 1
kube_node_spec_unschedulable{node="127.0.0.1"} 1
kube_node_status_capacity_cpu_cores{node="127.0.0.1"} 4.3
kube_node_status_capacity_memory_bytes{node="127.0.0.1"} 2e9
kube_node_status_capacity_pods{node="127.0.0.1"} 1000
kube_node_status_allocatable_cpu_cores{node="127.0.0.1"} 3
kube_node_status_allocatable_memory_bytes{node="127.0.0.1"} 1e9
kube_node_status_allocatable_pods{node="127.0.0.1"} 555
`,
},
// Verify condition enumerations.
{
nodes: []v1.Node{
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.1",
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{Type: v1.NodeReady, Status: v1.ConditionTrue},
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.2",
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{Type: v1.NodeReady, Status: v1.ConditionUnknown},
},
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.3",
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{Type: v1.NodeReady, Status: v1.ConditionFalse},
},
},
},
},
want: metadata + `
kube_node_status_ready{node="127.0.0.1",condition="true"} 1
kube_node_status_ready{node="127.0.0.1",condition="false"} 0
kube_node_status_ready{node="127.0.0.1",condition="unknown"} 0
kube_node_status_ready{node="127.0.0.2",condition="true"} 0
kube_node_status_ready{node="127.0.0.2",condition="false"} 0
kube_node_status_ready{node="127.0.0.2",condition="unknown"} 1
kube_node_status_ready{node="127.0.0.3",condition="true"} 0
kube_node_status_ready{node="127.0.0.3",condition="false"} 1
kube_node_status_ready{node="127.0.0.3",condition="unknown"} 0
`,
metrics: []string{"kube_node_status_ready"},
},
// Verify phase enumerations.
{
nodes: []v1.Node{
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.1",
},
Status: v1.NodeStatus{
Phase: v1.NodeRunning,
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.2",
},
Status: v1.NodeStatus{
Phase: v1.NodePending,
},
},
{
ObjectMeta: v1.ObjectMeta{
Name: "127.0.0.3",
},
Status: v1.NodeStatus{
Phase: v1.NodeTerminated,
},
},
},
want: metadata + `
kube_node_status_phase{node="127.0.0.1",phase="Terminated"} 0
kube_node_status_phase{node="127.0.0.1",phase="Running"} 1
kube_node_status_phase{node="127.0.0.1",phase="Pending"} 0
kube_node_status_phase{node="127.0.0.2",phase="Terminated"} 0
kube_node_status_phase{node="127.0.0.2",phase="Running"} 0
kube_node_status_phase{node="127.0.0.2",phase="Pending"} 1
kube_node_status_phase{node="127.0.0.3",phase="Terminated"} 1
kube_node_status_phase{node="127.0.0.3",phase="Running"} 0
kube_node_status_phase{node="127.0.0.3",phase="Pending"} 0
`,
metrics: []string{"kube_node_status_phase"},
},
}
for _, c := range cases {
dc := &nodeCollector{
store: &mockNodeStore{
list: func() (v1.NodeList, error) {
return v1.NodeList{Items: c.nodes}, nil
},
},
}
if err := gatherAndCompare(dc, c.want, c.metrics); err != nil {
t.Errorf("unexpected collecting result:\n%s", err)
}
}
}