-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_sources_test.go
173 lines (164 loc) · 3.49 KB
/
data_sources_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package v1alpha
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/nobl9/nobl9-go/manifest"
)
func TestHistoricalRetrievalDuration_durationInMinutes(t *testing.T) {
type fields struct {
Value int
Unit HistoricalRetrievalDurationUnit
}
tests := []struct {
name string
fields fields
want time.Duration
}{
{
name: "minutes",
fields: fields{
Value: 7,
Unit: HRDMinute,
},
want: 7 * time.Minute,
},
{
name: "hours",
fields: fields{
Value: 2,
Unit: HRDHour,
},
want: 2 * time.Hour,
},
{
name: "days",
fields: fields{
Value: 5,
Unit: HRDDay,
},
want: 5 * time.Hour * 24,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := HistoricalRetrievalDuration{
Value: &tt.fields.Value,
Unit: tt.fields.Unit,
}
assert.Equal(t, tt.want, d.Duration())
})
}
}
func TestHistoricalRetrievalDuration_durationInMinutes_unsupportedUnit(t *testing.T) {
testUnit := HistoricalRetrievalDurationUnit("test")
duration := HistoricalRetrievalDuration{
Value: ptr(12),
Unit: testUnit,
}
assert.Equal(t, time.Duration(0), duration.Duration())
}
func TestGetDataRetrievalMaxDuration(t *testing.T) {
for _, test := range []struct {
Kind manifest.Kind
SourceType DataSourceType
Valid bool
Expected HistoricalRetrievalDuration
}{
{
Kind: manifest.KindAgent,
SourceType: CloudWatch,
Valid: true,
Expected: agentDataRetrievalMaxDuration[CloudWatch],
},
{
Kind: manifest.KindAgent,
},
{
Kind: manifest.KindDirect,
SourceType: CloudWatch,
Valid: true,
Expected: directDataRetrievalMaxDuration[CloudWatch],
},
{
Kind: manifest.KindDirect,
SourceType: -1,
},
{
Kind: manifest.KindSLO,
},
} {
validStr := "valid"
if !test.Valid {
validStr = "invalid"
}
t.Run(fmt.Sprintf("%s %s %s", validStr, test.Kind, test.SourceType), func(t *testing.T) {
hdr, err := GetDataRetrievalMaxDuration(test.Kind, test.SourceType)
if test.Valid {
require.NoError(t, err)
assert.Equal(t, test.Expected, hdr)
} else {
assert.Error(t, err)
}
})
}
}
func TestGetStrAndStdDurationFromDuration(t *testing.T) {
for _, tc := range []struct {
duration Duration
expectStr string
expectDuration time.Duration
}{
{
duration: Duration{},
expectStr: "0s",
expectDuration: time.Second * 0,
},
{
duration: Duration{
Value: ptr(100),
Unit: Millisecond,
},
expectStr: "100ms",
expectDuration: time.Millisecond * 100,
},
{
duration: Duration{
Value: ptr(60),
Unit: Second,
},
expectStr: "60s",
expectDuration: time.Second * 60,
},
{
duration: Duration{
Value: ptr(5),
Unit: Minute,
},
expectStr: "5m",
expectDuration: time.Minute * 5,
},
{
duration: Duration{
Value: ptr(1000),
Unit: Minute,
},
expectStr: "1000m",
expectDuration: time.Minute * 1000,
},
} {
t.Run(fmt.Sprintf("%v should be represented as %s and %s", tc.duration, tc.expectStr, tc.expectDuration),
func(t *testing.T) {
assert.Equal(t, tc.expectStr, tc.duration.String())
assert.Equal(t, tc.expectDuration, tc.duration.Duration())
})
}
}
func TestGetQueryDelayDefaults_AllDataSourcesAreDefined(t *testing.T) {
defaults := GetQueryDelayDefaults()
for _, typ := range DataSourceTypeValues() {
assert.Contains(t, defaults, typ)
}
}