-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathdatastore_test.go
106 lines (90 loc) · 3.03 KB
/
datastore_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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 datastore
import (
"testing"
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vmware/govmomi/simulator"
)
func TestFetchEventContents(t *testing.T) {
// Creating a new simulator model with VPX server to collect broad range of data.
model := simulator.VPX()
err := model.Create()
require.NoError(t, err, "failed to create model")
t.Cleanup(model.Remove)
ts := model.Service.NewServer()
t.Cleanup(ts.Close)
f := mbtest.NewReportingMetricSetV2WithContext(t, getConfig(ts))
events, errs := mbtest.ReportingFetchV2WithContext(f)
require.Empty(t, errs, "Expected no errors during fetch")
require.NotEmpty(t, events, "Expected to receive at least one event")
event := events[0].MetricSetFields
t.Logf("Fetched event from %s/%s event: %+v", f.Module().Name(), f.Name(), event)
assert.EqualValues(t, "LocalDS_0", event["name"])
assert.EqualValues(t, "OTHER", event["fstype"])
// Values are based on the result 'df -k'.
fields := []string{
"capacity.total.bytes",
"capacity.free.bytes",
"status",
"host.count",
"vm.count",
"write.bytes",
"capacity.used.bytes",
"disk.capacity.usage.bytes",
"disk.capacity.bytes",
"disk.provisioned.bytes",
}
for _, field := range fields {
value, err := event.GetValue(field)
if err != nil {
t.Error(field, err)
return
}
switch field {
case "status":
assert.NotNil(t, value)
case "vm.count", "host.count":
assert.GreaterOrEqual(t, value, 0)
default:
assert.GreaterOrEqual(t, value, int64(0))
}
}
}
func TestDataStoreMetricSetData(t *testing.T) {
model := simulator.ESX()
err := model.Create()
require.NoError(t, err, "failed to create model")
t.Cleanup(model.Remove)
ts := model.Service.NewServer()
t.Cleanup(ts.Close)
f := mbtest.NewReportingMetricSetV2WithContext(t, getConfig(ts))
err = mbtest.WriteEventsReporterV2WithContext(f, t, "")
assert.NoError(t, err, "failed to write events with reporter")
}
func getConfig(ts *simulator.Server) map[string]interface{} {
return map[string]interface{}{
"module": "vsphere",
"metricsets": []string{"datastore"},
"hosts": []string{ts.URL.String()},
"username": "user",
"password": "pass",
"insecure": true,
}
}