forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxmox_test.go
142 lines (122 loc) · 4.97 KB
/
proxmox_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
package proxmox
import (
"github.com/bmizerany/assert"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
"net/url"
"strings"
"testing"
)
var nodeSearchDomainTestData = `{"data":{"search":"test.example.com","dns1":"1.0.0.1"}}`
var qemuTestData = `{"data":[{"name":"qemu1","status":"running","maxdisk":10737418240,"cpu":0.029336643550795,"vmid":"113","uptime":2159739,"disk":0,"maxmem":2147483648,"mem":1722451796}]}`
var qemuConfigTestData = `{"data":{"hostname":"qemu1","searchdomain":"test.example.com"}}`
var lxcTestData = `{"data":[{"vmid":"111","type":"lxc","uptime":2078164,"swap":9412608,"disk":"744189952","maxmem":536870912,"mem":98500608,"maxswap":536870912,"cpu":0.00371567669193613,"status":"running","maxdisk":"5217320960","name":"container1"}]}`
var lxcConfigTestData = `{"data":{"hostname":"container1","searchdomain":"test.example.com"}}`
var lxcCurrentStatusTestData = `{"data":{"vmid":"111","type":"lxc","uptime":2078164,"swap":9412608,"disk":"744189952","maxmem":536870912,"mem":98500608,"maxswap":536870912,"cpu":0.00371567669193613,"status":"running","maxdisk":"5217320960","name":"container1"}}`
var qemuCurrentStatusTestData = `{"data":{"name":"qemu1","status":"running","maxdisk":10737418240,"cpu":0.029336643550795,"vmid":"113","uptime":2159739,"disk":0,"maxmem":2147483648,"mem":1722451796}}`
func performTestRequest(px *Proxmox, apiUrl string, method string, data url.Values) ([]byte, error) {
var bytedata = []byte("")
if strings.HasSuffix(apiUrl, "dns") {
bytedata = []byte(nodeSearchDomainTestData)
} else if strings.HasSuffix(apiUrl, "qemu") {
bytedata = []byte(qemuTestData)
} else if strings.HasSuffix(apiUrl, "113/config") {
bytedata = []byte(qemuConfigTestData)
} else if strings.HasSuffix(apiUrl, "lxc") {
bytedata = []byte(lxcTestData)
} else if strings.HasSuffix(apiUrl, "111/config") {
bytedata = []byte(lxcConfigTestData)
} else if strings.HasSuffix(apiUrl, "111/status/current") {
bytedata = []byte(lxcCurrentStatusTestData)
} else if strings.HasSuffix(apiUrl, "113/status/current") {
bytedata = []byte(qemuCurrentStatusTestData)
}
return bytedata, nil
}
func setUp(t *testing.T) *Proxmox {
px := &Proxmox{
requestFunction: performTestRequest,
NodeName: "testnode",
}
require.NoError(t, px.Init())
// Override logger for test
px.Log = testutil.Logger{}
return px
}
func TestGetNodeSearchDomain(t *testing.T) {
px := setUp(t)
err := getNodeSearchDomain(px)
require.NoError(t, err)
assert.Equal(t, px.nodeSearchDomain, "test.example.com")
}
func TestGatherLxcData(t *testing.T) {
px := setUp(t)
px.nodeSearchDomain = "test.example.com"
acc := &testutil.Accumulator{}
gatherLxcData(px, acc)
assert.Equal(t, acc.NFields(), 15)
testFields := map[string]interface{}{
"status": "running",
"uptime": int64(2078164),
"cpuload": float64(0.00371567669193613),
"mem_used": int64(98500608),
"mem_total": int64(536870912),
"mem_free": int64(438370304),
"mem_used_percentage": float64(18.34716796875),
"swap_used": int64(9412608),
"swap_total": int64(536870912),
"swap_free": int64(527458304),
"swap_used_percentage": float64(1.75323486328125),
"disk_used": int64(744189952),
"disk_total": int64(5217320960),
"disk_free": int64(4473131008),
"disk_used_percentage": float64(14.26383306117322),
}
testTags := map[string]string{
"node_fqdn": "testnode.test.example.com",
"vm_name": "container1",
"vm_fqdn": "container1.test.example.com",
"vm_type": "lxc",
}
acc.AssertContainsTaggedFields(t, "proxmox", testFields, testTags)
}
func TestGatherQemuData(t *testing.T) {
px := setUp(t)
px.nodeSearchDomain = "test.example.com"
acc := &testutil.Accumulator{}
gatherQemuData(px, acc)
assert.Equal(t, acc.NFields(), 15)
testFields := map[string]interface{}{
"status": "running",
"uptime": int64(2159739),
"cpuload": float64(0.029336643550795),
"mem_used": int64(1722451796),
"mem_total": int64(2147483648),
"mem_free": int64(425031852),
"mem_used_percentage": float64(80.20791206508875),
"swap_used": int64(0),
"swap_total": int64(0),
"swap_free": int64(0),
"swap_used_percentage": float64(0),
"disk_used": int64(0),
"disk_total": int64(10737418240),
"disk_free": int64(10737418240),
"disk_used_percentage": float64(0),
}
testTags := map[string]string{
"node_fqdn": "testnode.test.example.com",
"vm_name": "qemu1",
"vm_fqdn": "qemu1.test.example.com",
"vm_type": "qemu",
}
acc.AssertContainsTaggedFields(t, "proxmox", testFields, testTags)
}
func TestGather(t *testing.T) {
px := setUp(t)
px.nodeSearchDomain = "test.example.com"
acc := &testutil.Accumulator{}
err := px.Gather(acc)
require.NoError(t, err)
// Results from both tests above
assert.Equal(t, acc.NFields(), 30)
}