forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_test.go
125 lines (113 loc) · 3.1 KB
/
data_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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package azure
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/elastic/beats/v7/libbeat/common"
)
func TestReturnAllDimensions(t *testing.T) {
dimensionList := []Dimension{
{
Value: "vm1",
Name: "VMName",
},
{
Value: "*",
Name: "SlotID",
},
}
result, dims := returnAllDimensions(dimensionList)
assert.True(t, result)
assert.Equal(t, len(dims), 1)
assert.Equal(t, dims[0].Name, "SlotID")
assert.Equal(t, dims[0].Value, "*")
}
func TestGetDimensionValue(t *testing.T) {
dimensionList := []Dimension{
{
Value: "vm1",
Name: "VMName",
},
{
Value: "*",
Name: "SlotID",
},
}
result := getDimensionValue("VMName", dimensionList)
assert.Equal(t, result, "vm1")
}
func TestReplaceUpperCase(t *testing.T) {
result := replaceUpperCase("TestReplaceUpper_Case")
assert.Equal(t, result, "Test_replace_upper_Case")
// should not split on acronyms
result = replaceUpperCase("CPU_Percentage")
assert.Equal(t, result, "CPU_Percentage")
}
func TestManagePropertyName(t *testing.T) {
result := managePropertyName("TestManageProperty_Name")
assert.Equal(t, result, "test_manage_property_name")
result = managePropertyName("Test ManageProperty_Name/sec")
assert.Equal(t, result, "test_manage_property_name_per_sec")
result = managePropertyName("Test_-_Manage:Property.Name")
assert.Equal(t, result, "test_manage_property_name")
result = managePropertyName("Percentage CPU")
assert.Equal(t, result, "percentage_cpu")
}
func TestCreateEvent(t *testing.T) {
createTime, err := time.Parse(time.RFC3339, "2020-02-28T20:53:03Z")
if !assert.NoError(t, err) {
t.Fatal(err)
}
metric := Metric{
Resource: Resource{
ID: "resId",
Name: "res",
Location: "west_europe",
Type: "resType",
Group: "resGroup",
Tags: nil,
Subscription: "subId",
},
Namespace: "namespace1",
Names: []string{"Percentage CPU"},
Aggregations: "",
Dimensions: nil,
Values: nil,
TimeGrain: "",
}
var total float64 = 23
metricValues := []MetricValue{
{
name: "Percentage CPU",
avg: nil,
min: nil,
max: nil,
total: &total,
count: nil,
timestamp: time.Time{},
dimensions: nil,
},
}
event, list := createEvent(createTime, metric, metricValues)
assert.NotNil(t, event)
assert.NotNil(t, list)
assert.Equal(t, event.Timestamp, createTime)
sub, err := event.ModuleFields.GetValue("subscription_id")
if !assert.NoError(t, err) {
t.Fatal(err)
}
assert.Equal(t, sub, metric.Resource.Subscription)
namespace, err := event.ModuleFields.GetValue("namespace")
if !assert.NoError(t, err) {
t.Fatal(err)
}
assert.Equal(t, namespace, metric.Namespace)
val, err := list.GetValue("percentage_cpu")
if !assert.NoError(t, err) {
t.Fatal(err)
}
assert.Equal(t, val.(common.MapStr), common.MapStr{"total": total})
}