forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistentvolume_test.go
100 lines (91 loc) · 2.26 KB
/
persistentvolume_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
package kube_inventory
import (
"testing"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
func TestPersistentVolume(t *testing.T) {
cli := &client{}
now := time.Now()
now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 1, 36, 0, now.Location())
tests := []struct {
name string
handler *mockHandler
output []telegraf.Metric
hasError bool
}{
{
name: "no pv",
handler: &mockHandler{
responseMap: map[string]interface{}{
"/persistentvolumes/": &corev1.PersistentVolumeList{},
},
},
hasError: false,
},
{
name: "collect pvs",
handler: &mockHandler{
responseMap: map[string]interface{}{
"/persistentvolumes/": &corev1.PersistentVolumeList{
Items: []corev1.PersistentVolume{
{
Status: corev1.PersistentVolumeStatus{
Phase: "pending",
},
Spec: corev1.PersistentVolumeSpec{
StorageClassName: "ebs-1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "pv1",
Labels: map[string]string{
"lab1": "v1",
"lab2": "v2",
},
CreationTimestamp: metav1.Time{Time: now},
},
},
},
},
},
},
output: []telegraf.Metric{
testutil.MustMetric(
"kubernetes_persistentvolume",
map[string]string{
"pv_name": "pv1",
"storageclass": "ebs-1",
"phase": "pending",
},
map[string]interface{}{
"phase_type": 2,
},
time.Unix(0, 0),
),
},
hasError: false,
},
}
for _, v := range tests {
ks := &KubernetesInventory{
client: cli,
}
acc := new(testutil.Accumulator)
for _, pv := range ((v.handler.responseMap["/persistentvolumes/"]).(*corev1.PersistentVolumeList)).Items {
ks.gatherPersistentVolume(pv, acc)
}
err := acc.FirstError()
if v.hasError {
require.Errorf(t, err, "%s failed, should have error", v.name)
continue
}
// No error case
require.NoErrorf(t, err, "%s failed, err: %v", v.name, err)
require.Len(t, acc.Metrics, len(v.output))
testutil.RequireMetricsEqual(t, acc.GetTelegrafMetrics(), v.output, testutil.IgnoreTime())
}
}