@@ -19,6 +19,7 @@ import (
19
19
"github.com/aws/aws-node-termination-handler/pkg/ec2helper"
20
20
h "github.com/aws/aws-node-termination-handler/pkg/test"
21
21
"github.com/aws/aws-sdk-go/aws"
22
+ "github.com/aws/aws-sdk-go/aws/awserr"
22
23
"github.com/aws/aws-sdk-go/service/ec2"
23
24
)
24
25
@@ -40,6 +41,85 @@ func TestGetInstanceIdsByTagKey(t *testing.T) {
40
41
h .Equals (t , instanceId2 , instanceIds [1 ])
41
42
}
42
43
44
+ func TestGetInstanceIdsByTagKeyAPIError (t * testing.T ) {
45
+ ec2Mock := h.MockedEC2 {
46
+ DescribeInstancesResp : getDescribeInstancesResp (),
47
+ DescribeInstancesErr : awserr .New ("ThrottlingException" , "Rate exceeded" , nil ),
48
+ }
49
+ ec2Helper := ec2helper .New (ec2Mock )
50
+ _ , err := ec2Helper .GetInstanceIdsByTagKey ("myNTHManagedTag" )
51
+ h .Nok (t , err )
52
+ }
53
+
54
+ func TestGetInstanceIdsByTagKeyNilResponse (t * testing.T ) {
55
+ ec2Mock := h.MockedEC2 {}
56
+ ec2Helper := ec2helper .New (ec2Mock )
57
+ _ , err := ec2Helper .GetInstanceIdsByTagKey ("myNTHManagedTag" )
58
+ h .Nok (t , err )
59
+ }
60
+
61
+ func TestGetInstanceIdsByTagKeyNilReservations (t * testing.T ) {
62
+ ec2Mock := h.MockedEC2 {
63
+ DescribeInstancesResp : ec2.DescribeInstancesOutput {
64
+ Reservations : nil ,
65
+ },
66
+ }
67
+ ec2Helper := ec2helper .New (ec2Mock )
68
+ _ , err := ec2Helper .GetInstanceIdsByTagKey ("myNTHManagedTag" )
69
+ h .Nok (t , err )
70
+ }
71
+
72
+ func TestGetInstanceIdsByTagKeyEmptyReservation (t * testing.T ) {
73
+ ec2Mock := h.MockedEC2 {
74
+ DescribeInstancesResp : ec2.DescribeInstancesOutput {
75
+ Reservations : []* ec2.Reservation {},
76
+ },
77
+ }
78
+ ec2Helper := ec2helper .New (ec2Mock )
79
+ instanceIds , err := ec2Helper .GetInstanceIdsByTagKey ("myNTHManagedTag" )
80
+ h .Ok (t , err )
81
+ h .Equals (t , 0 , len (instanceIds ))
82
+ }
83
+
84
+ func TestGetInstanceIdsByTagKeyEmptyInstances (t * testing.T ) {
85
+ ec2Mock := h.MockedEC2 {
86
+ DescribeInstancesResp : ec2.DescribeInstancesOutput {
87
+ Reservations : []* ec2.Reservation {
88
+ {
89
+ Instances : []* ec2.Instance {},
90
+ },
91
+ },
92
+ },
93
+ }
94
+ ec2Helper := ec2helper .New (ec2Mock )
95
+ instanceIds , err := ec2Helper .GetInstanceIdsByTagKey ("myNTHManagedTag" )
96
+ h .Ok (t , err )
97
+ h .Equals (t , 0 , len (instanceIds ))
98
+ }
99
+
100
+ func TestGetInstanceIdsByTagKeyNilInstancesId (t * testing.T ) {
101
+ ec2Mock := h.MockedEC2 {
102
+ DescribeInstancesResp : ec2.DescribeInstancesOutput {
103
+ Reservations : []* ec2.Reservation {
104
+ {
105
+ Instances : []* ec2.Instance {
106
+ {
107
+ InstanceId : nil ,
108
+ },
109
+ {
110
+ InstanceId : aws .String (instanceId1 ),
111
+ },
112
+ },
113
+ },
114
+ },
115
+ },
116
+ }
117
+ ec2Helper := ec2helper .New (ec2Mock )
118
+ instanceIds , err := ec2Helper .GetInstanceIdsByTagKey ("myNTHManagedTag" )
119
+ h .Ok (t , err )
120
+ h .Equals (t , 1 , len (instanceIds ))
121
+ }
122
+
43
123
func TestGetInstanceIdsMapByTagKey (t * testing.T ) {
44
124
ec2Mock := h.MockedEC2 {
45
125
DescribeInstancesResp : getDescribeInstancesResp (),
0 commit comments