@@ -22,10 +22,14 @@ groups() ->
2222 [
2323 {unit , [], [
2424 maybe_add_tag_filters ,
25+ maybe_add_instance_state_filters ,
26+ validate_instance_states ,
2527 get_hostname_name_from_reservation_set ,
2628 registration_support ,
2729 network_interface_sorting ,
28- private_ip_address_sorting
30+ private_ip_address_sorting ,
31+ get_hostname_by_instance_ids_with_state_filter ,
32+ get_hostname_by_tags_with_state_filter
2933 ]},
3034 {lock , [], [
3135 lock_single_node ,
@@ -48,6 +52,27 @@ maybe_add_tag_filters(_Config) ->
4852 Result = lists :sort (rabbit_peer_discovery_aws :maybe_add_tag_filters (Tags , [], 1 )),
4953 ? assertEqual (Expectation , Result ).
5054
55+ maybe_add_instance_state_filters (_Config ) ->
56+ application :set_env (rabbit , cluster_formation ,
57+ [{peer_discovery_aws , [{aws_ec2_instance_states , [" running" , " pending" ]}]}]),
58+ QArgs = [{" Action" , " DescribeInstances" }, {" Version" , " 2015-10-01" }],
59+ Result = rabbit_peer_discovery_aws :maybe_add_instance_state_filters (QArgs , 1 ),
60+ Expectation = [{" Filter.1.Name" , " instance-state-name" },
61+ {" Filter.1.Value.2" , " pending" },
62+ {" Filter.1.Value.1" , " running" },
63+ {" Action" , " DescribeInstances" },
64+ {" Version" , " 2015-10-01" }],
65+ ? assertEqual (Expectation , Result ),
66+ application :unset_env (rabbit , cluster_formation ).
67+
68+ validate_instance_states (_Config ) ->
69+ ValidStates = [" pending" , " running" , " shutting-down" , " terminated" , " stopping" , " stopped" ],
70+ ? assertEqual (ValidStates , rabbit_peer_discovery_aws :validate_instance_states (ValidStates )),
71+ ? assertEqual ([" running" ], rabbit_peer_discovery_aws :validate_instance_states ([" running" , " invalid" ])),
72+ ? assertEqual ([], rabbit_peer_discovery_aws :validate_instance_states ([" bogus" , " invalid" ])),
73+ ? assertEqual ([" running" , " pending" ], rabbit_peer_discovery_aws :validate_instance_states ([running , pending ])),
74+ ? assertEqual ([" running" ], rabbit_peer_discovery_aws :validate_instance_states ([running , invalid ])).
75+
5176get_hostname_name_from_reservation_set (_Config ) ->
5277 ok = eunit :test ({
5378 foreach ,
@@ -89,6 +114,44 @@ get_hostname_name_from_reservation_set(_Config) ->
89114 end }]
90115 }).
91116
117+ get_hostname_by_instance_ids_with_state_filter (_Config ) ->
118+ application :set_env (rabbit , cluster_formation ,
119+ [{peer_discovery_aws , [{aws_ec2_instance_states , [" running" , " pending" ]}]}]),
120+ meck :new (rabbitmq_aws , [passthrough ]),
121+ meck :expect (rabbitmq_aws , api_get_request ,
122+ fun (" ec2" , Path ) ->
123+ ? assert (string :str (Path , " Filter" ) > 0 ),
124+ ? assert (string :str (Path , " instance-state-name" ) > 0 ),
125+ ? assert (string :str (Path , " Value.1=running" ) > 0 ),
126+ ? assert (string :str (Path , " Value.2=pending" ) > 0 ),
127+ {ok , mock_describe_instances_response ()}
128+ end ),
129+ Result = rabbit_peer_discovery_aws :get_hostname_by_instance_ids (
130+ [" i-abc123" , " i-def456" ], #{}),
131+ ? assertEqual ([" ip-10-0-16-29.eu-west-1.compute.internal" ,
132+ " ip-10-0-16-31.eu-west-1.compute.internal" ], Result ),
133+ meck :unload (rabbitmq_aws ),
134+ application :unset_env (rabbit , cluster_formation ).
135+
136+ get_hostname_by_tags_with_state_filter (_Config ) ->
137+ application :set_env (rabbit , cluster_formation ,
138+ [{peer_discovery_aws , [{aws_ec2_instance_states , [" running" ]}]}]),
139+ meck :new (rabbitmq_aws , [passthrough ]),
140+ meck :expect (rabbitmq_aws , api_get_request ,
141+ fun (" ec2" , Path ) ->
142+ ? assert (string :str (Path , " Filter" ) > 0 ),
143+ ? assert (string :str (Path , " instance-state-name" ) > 0 ),
144+ ? assert (string :str (Path , " Value.1=running" ) > 0 ),
145+ ? assert (string :str (Path , " tag%3Aservice" ) > 0 ),
146+ {ok , mock_describe_instances_response ()}
147+ end ),
148+ Tags = maps :from_list ([{" service" , " rabbitmq" }]),
149+ Result = rabbit_peer_discovery_aws :get_hostname_by_tags (Tags ),
150+ ? assertEqual ([" ip-10-0-16-29.eu-west-1.compute.internal" ,
151+ " ip-10-0-16-31.eu-west-1.compute.internal" ], Result ),
152+ meck :unload (rabbitmq_aws ),
153+ application :unset_env (rabbit , cluster_formation ).
154+
92155registration_support (_Config ) ->
93156 ? assertEqual (false , rabbit_peer_discovery_aws :supports_registration ()).
94157
@@ -289,3 +352,7 @@ reservation_set() ->
289352 ]}
290353 ]}]}]},
291354 {" privateIpAddress" ," 10.0.16.31" }]}]}]}].
355+
356+ mock_describe_instances_response () ->
357+ [{" DescribeInstancesResponse" ,
358+ [{" reservationSet" , reservation_set ()}]}].
0 commit comments