88
99 "github.com/stretchr/testify/assert"
1010 "go.opentelemetry.io/collector/confmap"
11+
12+ "github.com/aws/amazon-cloudwatch-agent/internal/entity"
13+ "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsentity/entityattributes"
1114)
1215
1316func TestUnmarshalDefaultConfig (t * testing.T ) {
@@ -16,3 +19,126 @@ func TestUnmarshalDefaultConfig(t *testing.T) {
1619 assert .NoError (t , confmap .New ().Unmarshal (cfg ))
1720 assert .Equal (t , factory .CreateDefaultConfig (), cfg )
1821}
22+
23+ func TestUnmarshalConfig (t * testing.T ) {
24+ tests := []struct {
25+ name string
26+ conf * confmap.Conf
27+ expected * Config
28+ expectError bool
29+ }{
30+ {
31+ name : "TestValidEntityTransform" ,
32+ conf : confmap .NewFromStringMap (map [string ]interface {}{
33+ "entity_type" : entityattributes .Service ,
34+ "platform" : "ec2" ,
35+ "transform_entity" : map [string ]interface {}{
36+ "key_attributes" : []interface {}{
37+ map [string ]interface {}{
38+ "key" : entityattributes .ServiceName ,
39+ "value" : "config-service-name" ,
40+ },
41+ map [string ]interface {}{
42+ "key" : entityattributes .DeploymentEnvironment ,
43+ "value" : "config-environment-name" ,
44+ },
45+ },
46+ "attributes" : []interface {}{
47+ map [string ]interface {}{
48+ "key" : entityattributes .ServiceNameSource ,
49+ "value" : "UserConfiguration" ,
50+ },
51+ },
52+ },
53+ }),
54+ expected : & Config {
55+ EntityType : entityattributes .Service ,
56+ Platform : "ec2" ,
57+ TransformEntity : & entity.Transform {
58+ KeyAttributes : []entity.KeyPair {
59+ {
60+ Key : entityattributes .ServiceName ,
61+ Value : "config-service-name" ,
62+ },
63+ {
64+ Key : entityattributes .DeploymentEnvironment ,
65+ Value : "config-environment-name" ,
66+ },
67+ },
68+ Attributes : []entity.KeyPair {
69+ {
70+ Key : entityattributes .ServiceNameSource ,
71+ Value : "UserConfiguration" ,
72+ },
73+ },
74+ },
75+ },
76+ expectError : false ,
77+ },
78+ {
79+ name : "TestInvalidEntityTransform" ,
80+ conf : confmap .NewFromStringMap (map [string ]interface {}{
81+ "entity_type" : entityattributes .Service ,
82+ "platform" : "ec2" ,
83+ "transform_entity" : map [string ]interface {}{
84+ "key_attributes" : []interface {}{
85+ map [string ]interface {}{
86+ "key" : "InvalidKey" ,
87+ "value" : "some-value" ,
88+ },
89+ },
90+ },
91+ }),
92+ expectError : true ,
93+ },
94+ {
95+ name : "TestEmptyEntityTransform" ,
96+ conf : confmap .NewFromStringMap (map [string ]interface {}{
97+ "entity_type" : entityattributes .Service ,
98+ "platform" : "ec2" ,
99+ }),
100+ expected : & Config {
101+ EntityType : entityattributes .Service ,
102+ Platform : "ec2" ,
103+ },
104+ expectError : false ,
105+ },
106+ {
107+ name : "TestMissingRequiredFieldEntityTransform" ,
108+ conf : confmap .NewFromStringMap (map [string ]interface {}{
109+ "transform_entity" : map [string ]interface {}{
110+ "key_attributes" : []interface {}{
111+ map [string ]interface {}{
112+ "key" : entityattributes .ServiceName ,
113+ "value" : "" ,
114+ },
115+ },
116+ },
117+ }),
118+ expectError : true ,
119+ },
120+ }
121+
122+ for _ , tt := range tests {
123+ t .Run (tt .name , func (t * testing.T ) {
124+ factory := NewFactory ()
125+ cfg := factory .CreateDefaultConfig ()
126+
127+ err := tt .conf .Unmarshal (cfg )
128+
129+ assert .NoError (t , err )
130+
131+ // Validate the configuration
132+ err = cfg .(* Config ).Validate ()
133+ if tt .expectError {
134+ assert .Error (t , err )
135+ } else {
136+ assert .NoError (t , err )
137+ }
138+
139+ if err == nil {
140+ assert .Equal (t , tt .expected , cfg )
141+ }
142+ })
143+ }
144+ }
0 commit comments