-
Notifications
You must be signed in to change notification settings - Fork 7
/
main_test.go
104 lines (93 loc) · 5.51 KB
/
main_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
package main
import (
"encoding/json"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
_ "k8s.io/client-go/plugin/pkg/client/auth"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
)
func Test_collectSummaryMetrics(t *testing.T) {
expectedOut := `# HELP kube_summary_container_logs_available_bytes Number of bytes that aren't consumed by the container logs
# TYPE kube_summary_container_logs_available_bytes gauge
kube_summary_container_logs_available_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 9.0016837632e+10
# HELP kube_summary_container_logs_capacity_bytes Number of bytes that can be consumed by the container logs
# TYPE kube_summary_container_logs_capacity_bytes gauge
kube_summary_container_logs_capacity_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 1.01535985664e+11
# HELP kube_summary_container_logs_inodes Number of Inodes for logs
# TYPE kube_summary_container_logs_inodes gauge
kube_summary_container_logs_inodes{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5474432e+07
# HELP kube_summary_container_logs_inodes_free Number of available Inodes for logs
# TYPE kube_summary_container_logs_inodes_free gauge
kube_summary_container_logs_inodes_free{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5355212e+07
# HELP kube_summary_container_logs_inodes_used Number of used Inodes for logs
# TYPE kube_summary_container_logs_inodes_used gauge
kube_summary_container_logs_inodes_used{name="dev-server",namespace="mon",pod="dev-server-0"} 1
# HELP kube_summary_container_logs_used_bytes Number of bytes that are consumed by the container logs
# TYPE kube_summary_container_logs_used_bytes gauge
kube_summary_container_logs_used_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 8192
# HELP kube_summary_container_rootfs_available_bytes Number of bytes that aren't consumed by the container
# TYPE kube_summary_container_rootfs_available_bytes gauge
kube_summary_container_rootfs_available_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 9.0016837632e+10
# HELP kube_summary_container_rootfs_capacity_bytes Number of bytes that can be consumed by the container
# TYPE kube_summary_container_rootfs_capacity_bytes gauge
kube_summary_container_rootfs_capacity_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 1.01535985664e+11
# HELP kube_summary_container_rootfs_inodes Number of Inodes
# TYPE kube_summary_container_rootfs_inodes gauge
kube_summary_container_rootfs_inodes{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5474432e+07
# HELP kube_summary_container_rootfs_inodes_free Number of available Inodes
# TYPE kube_summary_container_rootfs_inodes_free gauge
kube_summary_container_rootfs_inodes_free{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5355212e+07
# HELP kube_summary_container_rootfs_inodes_used Number of used Inodes
# TYPE kube_summary_container_rootfs_inodes_used gauge
kube_summary_container_rootfs_inodes_used{name="dev-server",namespace="mon",pod="dev-server-0"} 14
# HELP kube_summary_container_rootfs_used_bytes Number of bytes that are consumed by the container
# TYPE kube_summary_container_rootfs_used_bytes gauge
kube_summary_container_rootfs_used_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 114688
# HELP kube_summary_pod_ephemeral_storage_available_bytes Number of bytes of Ephemeral storage that aren't consumed by the pod
# TYPE kube_summary_pod_ephemeral_storage_available_bytes gauge
kube_summary_pod_ephemeral_storage_available_bytes{namespace="mon",pod="dev-server-0"} 9.0016837632e+10
# HELP kube_summary_pod_ephemeral_storage_capacity_bytes Number of bytes of Ephemeral storage that can be consumed by the pod
# TYPE kube_summary_pod_ephemeral_storage_capacity_bytes gauge
kube_summary_pod_ephemeral_storage_capacity_bytes{namespace="mon",pod="dev-server-0"} 1.01535985664e+11
# HELP kube_summary_pod_ephemeral_storage_inodes Number of Inodes for pod Ephemeral storage
# TYPE kube_summary_pod_ephemeral_storage_inodes gauge
kube_summary_pod_ephemeral_storage_inodes{namespace="mon",pod="dev-server-0"} 2.5474432e+07
# HELP kube_summary_pod_ephemeral_storage_inodes_free Number of available Inodes for pod Ephemeral storage
# TYPE kube_summary_pod_ephemeral_storage_inodes_free gauge
kube_summary_pod_ephemeral_storage_inodes_free{namespace="mon",pod="dev-server-0"} 2.5355212e+07
# HELP kube_summary_pod_ephemeral_storage_inodes_used Number of used Inodes for pod Ephemeral storage
# TYPE kube_summary_pod_ephemeral_storage_inodes_used gauge
kube_summary_pod_ephemeral_storage_inodes_used{namespace="mon",pod="dev-server-0"} 63
# HELP kube_summary_pod_ephemeral_storage_used_bytes Number of bytes of Ephemeral storage that are consumed by the pod
# TYPE kube_summary_pod_ephemeral_storage_used_bytes gauge
kube_summary_pod_ephemeral_storage_used_bytes{namespace="mon",pod="dev-server-0"} 1.33947392e+08
`
d, err := os.ReadFile("test-summary.json")
if err != nil {
t.Fatal(err)
}
var summary stats.Summary
registry := prometheus.NewRegistry()
err = json.Unmarshal(d, &summary)
if err != nil {
t.Fatal(err)
}
collectSummaryMetrics(&summary, registry)
tmpfile, err := os.CreateTemp("", "test-summary.prom")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if err := prometheus.WriteToTextfile(tmpfile.Name(), registry); err != nil {
t.Fatal(err)
}
fileBytes, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(string(fileBytes), expectedOut); diff != "" {
t.Errorf("collectSummaryMetrics() metrics mismatch (-want +got):\n%s", diff)
}
}