@@ -17,26 +17,28 @@ import (
1717)
1818
1919var mockedInstanceIdentityDoc = & ec2metadata.EC2InstanceIdentityDocument {
20- InstanceID : "i-01d2417c27a396e44" ,
21- AccountID : "874389809020" ,
22- Region : "us-east-1" ,
23- InstanceType : "m5ad.large" ,
24- ImageID : "ami-09edd32d9b0990d49" ,
20+ InstanceID : "i-01d2417c27a396e44" ,
21+ AccountID : "874389809020" ,
22+ Region : "us-east-1" ,
23+ InstanceType : "m5ad.large" ,
24+ ImageID : "ami-09edd32d9b0990d49" ,
25+ AvailabilityZone : "us-east-1a" ,
2526}
2627
2728var mockedInstanceIdentityDocWithLargeInstanceId = & ec2metadata.EC2InstanceIdentityDocument {
28- InstanceID : "i-01d2417c27a396e44394824728" ,
29- AccountID : "874389809020" ,
30- Region : "us-east-1" ,
31- InstanceType : "m5ad.large" ,
32- ImageID : "ami-09edd32d9b0990d49" ,
29+ InstanceID : "i-01d2417c27a396e44394824728" ,
30+ AccountID : "874389809020" ,
31+ Region : "us-east-1" ,
32+ InstanceType : "m5ad.large" ,
33+ ImageID : "ami-09edd32d9b0990d49" ,
34+ AvailabilityZone : "us-east-1a" ,
3335}
3436
3537var (
3638 tagVal3 = "ASG-1"
3739)
3840
39- func TestSetInstanceIDAccountID (t * testing.T ) {
41+ func TestSetEC2Metadata (t * testing.T ) {
4042 type args struct {
4143 metadataProvider ec2metadataprovider.MetadataProvider
4244 }
@@ -53,8 +55,12 @@ func TestSetInstanceIDAccountID(t *testing.T) {
5355 },
5456 wantErr : false ,
5557 want : EC2Info {
56- InstanceID : mockedInstanceIdentityDoc .InstanceID ,
57- AccountID : mockedInstanceIdentityDoc .AccountID ,
58+ InstanceID : mockedInstanceIdentityDoc .InstanceID ,
59+ AccountID : mockedInstanceIdentityDoc .AccountID ,
60+ InstanceType : mockedInstanceIdentityDoc .InstanceType ,
61+ ImageID : mockedInstanceIdentityDoc .ImageID ,
62+ AvailabilityZone : mockedInstanceIdentityDoc .AvailabilityZone ,
63+ Hostname : "MockHostName" ,
5864 },
5965 },
6066 {
@@ -64,8 +70,12 @@ func TestSetInstanceIDAccountID(t *testing.T) {
6470 },
6571 wantErr : false ,
6672 want : EC2Info {
67- InstanceID : "" ,
68- AccountID : mockedInstanceIdentityDocWithLargeInstanceId .AccountID ,
73+ InstanceID : "" ,
74+ AccountID : mockedInstanceIdentityDocWithLargeInstanceId .AccountID ,
75+ InstanceType : mockedInstanceIdentityDocWithLargeInstanceId .InstanceType ,
76+ ImageID : mockedInstanceIdentityDocWithLargeInstanceId .ImageID ,
77+ AvailabilityZone : mockedInstanceIdentityDocWithLargeInstanceId .AvailabilityZone ,
78+ Hostname : "MockHostName" ,
6979 },
7080 },
7181 }
@@ -76,11 +86,15 @@ func TestSetInstanceIDAccountID(t *testing.T) {
7686 metadataProvider : tt .args .metadataProvider ,
7787 logger : logger ,
7888 }
79- if err := ei .setInstanceIDAccountID (); (err != nil ) != tt .wantErr {
80- t .Errorf ("setInstanceIDAccountID () error = %v, wantErr %v" , err , tt .wantErr )
89+ if err := ei .setEC2Metadata (); (err != nil ) != tt .wantErr {
90+ t .Errorf ("setEC2Metadata () error = %v, wantErr %v" , err , tt .wantErr )
8191 }
8292 assert .Equal (t , tt .want .InstanceID , ei .GetInstanceID ())
8393 assert .Equal (t , tt .want .AccountID , ei .GetAccountID ())
94+ assert .Equal (t , tt .want .InstanceType , ei .GetInstanceType ())
95+ assert .Equal (t , tt .want .ImageID , ei .GetImageID ())
96+ assert .Equal (t , tt .want .AvailabilityZone , ei .GetAvailabilityZone ())
97+ assert .Equal (t , tt .want .Hostname , ei .GetHostname ())
8498 })
8599 }
86100}
@@ -157,3 +171,32 @@ func TestNotInitIfMetadataProviderIsEmpty(t *testing.T) {
157171 })
158172 }
159173}
174+
175+ func TestGettersReturnEmptyBeforeInit (t * testing.T ) {
176+ ei := & EC2Info {}
177+ assert .Equal (t , "" , ei .GetInstanceID ())
178+ assert .Equal (t , "" , ei .GetAccountID ())
179+ assert .Equal (t , "" , ei .GetInstanceType ())
180+ assert .Equal (t , "" , ei .GetImageID ())
181+ assert .Equal (t , "" , ei .GetAvailabilityZone ())
182+ assert .Equal (t , "" , ei .GetHostname ())
183+ }
184+
185+ func TestHostnameFailureProceedsWithoutIt (t * testing.T ) {
186+ logger , _ := zap .NewDevelopment ()
187+ ei := & EC2Info {
188+ metadataProvider : & mockMetadataProvider {
189+ InstanceIdentityDocument : mockedInstanceIdentityDoc ,
190+ HostnameError : true ,
191+ },
192+ logger : logger ,
193+ }
194+ err := ei .setEC2Metadata ()
195+ assert .NoError (t , err , "should succeed even when Hostname() fails" )
196+ // Hostname is empty but all other fields are populated
197+ assert .Equal (t , "" , ei .GetHostname ())
198+ assert .Equal (t , mockedInstanceIdentityDoc .InstanceID , ei .GetInstanceID ())
199+ assert .Equal (t , mockedInstanceIdentityDoc .InstanceType , ei .GetInstanceType ())
200+ assert .Equal (t , mockedInstanceIdentityDoc .ImageID , ei .GetImageID ())
201+ assert .Equal (t , mockedInstanceIdentityDoc .AvailabilityZone , ei .GetAvailabilityZone ())
202+ }
0 commit comments