4
4
package entity
5
5
6
6
import (
7
- "bytes"
8
- "context"
9
- "crypto/sha256"
10
- "encoding/hex"
11
7
"encoding/json"
12
8
"fmt"
13
9
"log"
@@ -18,8 +14,6 @@ import (
18
14
"time"
19
15
20
16
"github.com/aws/aws-sdk-go-v2/aws"
21
- v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
22
- "github.com/aws/aws-sdk-go-v2/config"
23
17
"github.com/stretchr/testify/assert"
24
18
25
19
"github.com/aws/amazon-cloudwatch-agent-test/environment"
@@ -256,56 +250,16 @@ func TestResourceMetrics(t *testing.T) {
256
250
time .Sleep (sleepForFlush )
257
251
common .StopAgent ()
258
252
259
- // this section builds, signs, and sends the request
260
- cfg , err := config .LoadDefaultConfig (context .TODO (), config .WithRegion (region ))
261
- assert .NoError (t , err )
262
- signer := v4 .NewSigner ()
263
-
264
- body := []byte (fmt .Sprintf (`{
265
- "Namespace": "CWAgent",
266
- "MetricName": "cpu_usage_idle",
267
- "Dimensions": [
268
- {"Name": "InstanceId", "Value": "%s"},
269
- {"Name": "cpu", "Value": "cpu-total"}
270
- ]
271
- }` , instanceId ))
272
-
273
- h := sha256 .New ()
274
- h .Write (body )
275
- payloadHash := hex .EncodeToString (h .Sum (nil ))
276
-
277
- // essentially trying to convert this curl command:
278
-
279
- // curl -i -X POST monitoring.us-west-2.amazonaws.com -H 'Content-Type: application/json' \
280
- // -H 'Content-Encoding: amz-1.0' \
281
- // --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
282
- // -H "x-amz-security-token: $AWS_SESSION_TOKEN" \
283
- // --aws-sigv4 "aws:amz:us-west-2:monitoring" \
284
- // -H 'X-Amz-Target: com.amazonaws.cloudwatch.v2013_01_16.CloudWatchVersion20130116.ListEntitiesForMetric' \
285
- // -d '{
286
- // "Namespace": "CWAgent",
287
- // "MetricName": "cpu_usage_idle",
288
- // "Dimensions": [{"Name": "InstanceId", "Value": "i-0123456789012"}, { "Name": "cpu", "Value": "cpu-total"}]
289
- // }'
290
-
291
- // build the request
292
- req , err := http .NewRequest ("POST" , "https://monitoring." + region + ".amazonaws.com/" , bytes .NewReader (body ))
293
- assert .NoError (t , err , "Error creating request" )
294
-
295
- // set headers
296
- req .Header .Set ("Content-Type" , "application/json" )
297
- req .Header .Set ("X-Amz-Target" , "com.amazonaws.cloudwatch.v2013_01_16.CloudWatchVersion20130116.ListEntitiesForMetric" )
298
- req .Header .Set ("Content-Encoding" , "amz-1.0" )
299
-
300
- // set creds
301
- credentials , err := cfg .Credentials .Retrieve (context .TODO ())
302
- assert .NoError (t , err , "Error getting credentials" )
303
-
304
- req .Header .Set ("x-amz-security-token" , credentials .SessionToken )
305
-
306
- // sign the request
307
- err = signer .SignHTTP (context .TODO (), credentials , req , payloadHash , "monitoring" , region , time .Now ())
308
- assert .NoError (t , err , "Error signing the request" )
253
+ // build ListEntitiesForMetric request:
254
+ requestBody := []byte (fmt .Sprintf (`{
255
+ "Namespace": "CWAgent",
256
+ "MetricName": "cpu_usage_idle",
257
+ "Dimensions": [
258
+ {"Name": "InstanceId", "Value": "%s"},
259
+ {"Name": "cpu", "Value": "cpu-total"}
260
+ ]
261
+ }` , instanceId ))
262
+ req = BuildListEntitiesForMetricRequest (requestBody , region )
309
263
310
264
// send the request
311
265
client := & http.Client {}
@@ -328,22 +282,18 @@ func TestResourceMetrics(t *testing.T) {
328
282
assert .NoError (t , err , "Error parsing JSON response" )
329
283
330
284
// Verify the KeyAttributes
331
- assert .NotEmpty (t , response .Entities , "No entities found in the response" )
332
- entity := response .Entities [0 ]
333
- // assert.Equal(t, "AWS::Resource", entity.KeyAttributes.Type)
334
- // assert.Equal(t, "AWS::EC2::Instance", entity.KeyAttributes.ResourceType)
335
- // assert.Equal(t, instanceId, entity.KeyAttributes.Identifier)
336
-
337
285
expectedEntity := expectedEntity {
338
286
entityType : "AWS::Resource" ,
339
287
platformType : "AWS::EC2::Instance" ,
340
288
instanceId : instanceId ,
341
289
}
290
+ assert .NotEmpty (t , response .Entities , "No entities found in the response" )
291
+
292
+ entity := response .Entities [0 ]
342
293
validator := NewEntityValidator ("EC2" , expectedEntity )
343
294
validator .ValidateField (entityType , entity .KeyAttributes .Type , t )
344
295
validator .ValidateField (entityPlatform , entity .KeyAttributes .ResourceType , t )
345
296
validator .ValidateField (entityInstanceId , entity .KeyAttributes .Identifier , t )
346
-
347
297
}
348
298
349
299
// ValidateLogEntity performs the entity validation for PutLogEvents.
0 commit comments