@@ -10,13 +10,15 @@ import (
1010
1111 "github.com/stretchr/testify/assert"
1212
13+ "github.com/newrelic/infra-integrations-sdk/v4/args"
1314 "github.com/newrelic/infra-integrations-sdk/v4/data/event"
1415 "github.com/newrelic/infra-integrations-sdk/v4/log"
1516)
1617
1718var (
1819 integrationName = "TestIntegration"
1920 integrationVersion = "1.0"
21+ arguments args.DefaultArgumentList
2022)
2123
2224func Test_Integration_CreateIntegrationInitializesCorrectly (t * testing.T ) {
@@ -463,6 +465,98 @@ func Test_Integration_PublishThrowsNoError(t *testing.T) {
463465 assert .Empty (t , i .Entities )
464466}
465467
468+ func Test_Integration_HostId (t * testing.T ) {
469+ w := testWriter {
470+ func (integrationBytes []byte ) {
471+ expectedOutputRaw := []byte (`
472+ {
473+ "protocol_version": "4",
474+ "integration": {
475+ "name": "TestIntegration",
476+ "version": "1.0"
477+ },
478+ "data": [
479+ {
480+ "common": {},
481+ "entity": {
482+ "name": "id-1234567890abc:8080",
483+ "displayName": "",
484+ "type": "test",
485+ "metadata": {
486+ "tags.env": "prod"
487+ }
488+ },
489+ "metrics": [],
490+ "inventory": {},
491+ "events": []
492+ },
493+ {
494+ "common": {
495+ "attributes": {
496+ "targetName": "id-1234567890abc"
497+ }
498+ },
499+ "entity": {
500+ "name": "EntityOne",
501+ "displayName": "",
502+ "type": "test",
503+ "metadata": {}
504+ },
505+ "metrics": [
506+ {
507+ "timestamp": 10000000,
508+ "name": "metricOne",
509+ "type": "gauge",
510+ "attributes": {
511+ "processName": "java"
512+ },
513+ "value": 2
514+ }
515+ ],
516+ "inventory": {},
517+ "events": []
518+ }
519+ ]
520+ }` )
521+
522+ // awful but cannot compare with json.Unmarshal as it's not supported by Integration
523+ assert .Equal (t , stripBlanks (expectedOutputRaw ), stripBlanks (integrationBytes ))
524+ },
525+ }
526+
527+ _ = os .Setenv ("HOST_ID" , "id-1234567890abc" )
528+
529+ // os.ClearEnv breaks tests in Windows that use the fileStorer because clears the user env vars
530+ defer func () {
531+ _ = os .Unsetenv ("HOST_ID" )
532+ }()
533+
534+ i , err := New ("TestIntegration" , "1.0" , Logger (log .Discard ), Writer (w ), Args (& arguments ))
535+ assert .NoError (t , err )
536+
537+ e , err := i .NewEntity ("localhost:8080" , "test" , "" )
538+ assert .NoError (t , err )
539+ _ = e .AddTag ("env" , "prod" )
540+ i .AddEntity (e )
541+
542+ // add entity
543+ e2 , err := i .NewEntity ("EntityOne" , "test" , "" )
544+ assert .NoError (t , err )
545+ // add common attributes
546+ e2 .AddCommonDimension ("targetName" , i .GetHostID ())
547+ // add metric to entity 2
548+ gauge , _ := Gauge (time .Unix (10000000 , 0 ), "metricOne" , 2 )
549+ _ = gauge .AddDimension ("processName" , "java" )
550+ e2 .AddMetric (gauge )
551+ // add entity 2 to integration
552+ i .AddEntity (e2 )
553+
554+ assert .NoError (t , i .Publish ())
555+
556+ // check integration was reset
557+ assert .Empty (t , i .Entities )
558+ }
559+
466560func Test_Integration_FindEntity (t * testing.T ) {
467561 i := newTestIntegration (t )
468562
0 commit comments