Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/cloudwatch_exporter/config_test.go
5399 views
1
package cloudwatch_exporter
2
3
import (
4
"testing"
5
6
yaceConf "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/config"
7
yaceModel "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/model"
8
"github.com/stretchr/testify/assert"
9
"github.com/stretchr/testify/require"
10
"gopkg.in/yaml.v2"
11
)
12
13
const configString = `
14
sts_region: us-east-2
15
discovery:
16
exported_tags:
17
AWS/EC2:
18
- name
19
- type
20
jobs:
21
- type: AWS/EC2
22
search_tags:
23
- key: instance_type
24
value: spot
25
regions:
26
- us-east-2
27
roles:
28
- role_arn: arn:aws:iam::878167871295:role/yace_testing
29
custom_tags:
30
- key: alias
31
value: tesis
32
metrics:
33
- name: CPUUtilization
34
period: 5m
35
statistics:
36
- Maximum
37
- Average
38
static:
39
- regions:
40
- us-east-2
41
name: custom_tesis_metrics
42
namespace: CoolApp
43
dimensions:
44
- name: PURCHASES_SERVICE
45
value: CoolService
46
- name: APP_VERSION
47
value: 1.0
48
metrics:
49
- name: KPIs
50
period: 5m
51
statistics:
52
- Average
53
`
54
55
// for testing fips_disabled behaviour
56
const configString2 = `
57
sts_region: us-east-2
58
fips_disabled: true
59
discovery:
60
exported_tags:
61
AWS/EC2:
62
- name
63
- type
64
jobs:
65
- type: AWS/EC2
66
search_tags:
67
- key: instance_type
68
value: spot
69
regions:
70
- us-east-2
71
roles:
72
- role_arn: arn:aws:iam::878167871295:role/yace_testing
73
custom_tags:
74
- key: alias
75
value: tesis
76
metrics:
77
- name: CPUUtilization
78
period: 5m
79
statistics:
80
- Maximum
81
- Average
82
static:
83
- regions:
84
- us-east-2
85
name: custom_tesis_metrics
86
namespace: CoolApp
87
dimensions:
88
- name: PURCHASES_SERVICE
89
value: CoolService
90
- name: APP_VERSION
91
value: 1.0
92
metrics:
93
- name: KPIs
94
period: 5m
95
statistics:
96
- Average
97
`
98
99
var falsePtr = false
100
var truePtr = true
101
102
var expectedConfig = yaceConf.ScrapeConf{
103
APIVersion: "v1alpha1",
104
StsRegion: "us-east-2",
105
Discovery: yaceConf.Discovery{
106
ExportedTagsOnMetrics: map[string][]string{
107
"AWS/EC2": {"name", "type"},
108
},
109
Jobs: []*yaceConf.Job{
110
{
111
Type: "AWS/EC2",
112
Regions: []string{"us-east-2"},
113
Roles: []yaceConf.Role{
114
{
115
RoleArn: "arn:aws:iam::878167871295:role/yace_testing",
116
},
117
},
118
CustomTags: []yaceModel.Tag{
119
{
120
Key: "alias",
121
Value: "tesis",
122
},
123
},
124
SearchTags: []yaceModel.Tag{
125
{
126
Key: "instance_type",
127
Value: "spot",
128
},
129
},
130
Metrics: []*yaceConf.Metric{
131
{
132
Name: "CPUUtilization",
133
Statistics: []string{"Maximum", "Average"},
134
// Defaults get configured from general settings
135
Period: 300,
136
Length: 300,
137
Delay: 0,
138
NilToZero: &nilToZero,
139
AddCloudwatchTimestamp: &addCloudwatchTimestamp,
140
},
141
},
142
RoundingPeriod: nil,
143
JobLevelMetricFields: yaceConf.JobLevelMetricFields{
144
Period: 0,
145
Length: 0,
146
Delay: 0,
147
AddCloudwatchTimestamp: &falsePtr,
148
NilToZero: &nilToZero,
149
},
150
},
151
},
152
},
153
Static: []*yaceConf.Static{
154
{
155
Name: "custom_tesis_metrics",
156
Regions: []string{"us-east-2"},
157
Roles: []yaceConf.Role{{}},
158
Namespace: "CoolApp",
159
CustomTags: []yaceModel.Tag{},
160
Dimensions: []yaceConf.Dimension{
161
{
162
Name: "PURCHASES_SERVICE",
163
Value: "CoolService",
164
},
165
{
166
Name: "APP_VERSION",
167
Value: "1.0",
168
},
169
},
170
Metrics: []*yaceConf.Metric{
171
{
172
Name: "KPIs",
173
Period: 300,
174
Length: 300,
175
Statistics: []string{"Average"},
176
Delay: 0,
177
NilToZero: &nilToZero,
178
AddCloudwatchTimestamp: &addCloudwatchTimestamp,
179
},
180
},
181
},
182
},
183
}
184
185
func TestTranslateConfigToYACEConfig(t *testing.T) {
186
c := Config{}
187
err := yaml.Unmarshal([]byte(configString), &c)
188
require.NoError(t, err, "failed to unmarshall config")
189
190
yaceConf, fipsEnabled, err := ToYACEConfig(&c)
191
require.NoError(t, err, "failed to translate to YACE configuration")
192
193
require.EqualValues(t, expectedConfig, yaceConf)
194
require.EqualValues(t, truePtr, fipsEnabled)
195
196
err = yaml.Unmarshal([]byte(configString2), &c)
197
require.NoError(t, err, "failed to unmarshall config")
198
199
yaceConf, fipsEnabled2, err := ToYACEConfig(&c)
200
require.NoError(t, err, "failed to translate to YACE configuration")
201
202
require.EqualValues(t, expectedConfig, yaceConf)
203
require.EqualValues(t, falsePtr, fipsEnabled2)
204
}
205
206
func TestCloudwatchExporterConfigInstanceKey(t *testing.T) {
207
cfg1 := &Config{
208
STSRegion: "us-east-2",
209
}
210
cfg2 := &Config{
211
STSRegion: "us-east-3",
212
}
213
214
cfg1Hash, err := cfg1.InstanceKey("")
215
require.NoError(t, err)
216
cfg2Hash, err := cfg2.InstanceKey("")
217
require.NoError(t, err)
218
219
assert.NotEqual(t, cfg1Hash, cfg2Hash)
220
221
// test that making them equal in values leads to the same instance key
222
cfg2.STSRegion = "us-east-2"
223
cfg2Hash, err = cfg2.InstanceKey("")
224
require.NoError(t, err)
225
226
assert.Equal(t, cfg1Hash, cfg2Hash)
227
}
228
229