66from botocore .stub import Stubber , ANY
77import botocore .exceptions
88from unittest .mock import Mock , patch
9+ from click .testing import CliRunner
10+
911
1012def test_add_node ():
1113 asg_client = boto3 .client ("autoscaling" , region_name = "ap-southeast-2" )
@@ -30,6 +32,7 @@ def test_add_node():
3032 stubber .activate ()
3133 assert add_node (asg_client = asg_client , asg_name = "foobar" ) is None
3234
35+
3336@patch ('eks_node_rollout.get_latest_lt_version' , return_value = "1" )
3437def test_describe_nodes_not_matching_lt (* args ):
3538 asg_client = boto3 .client ("autoscaling" , region_name = "ap-southeast-2" )
@@ -127,3 +130,39 @@ def test_describe_nodes_not_matching_lt(*args):
127130
128131 instances = describe_nodes_not_matching_lt (asg_client = asg_client , ec2_client = ec2_client , asg_name = "foobar" )
129132 assert len (instances ) == 2
133+
134+
135+ @patch ('eks_node_rollout.kubectl' , create = True )
136+ def test_wait_for_ready_node_rollout (* args ):
137+ args [0 ].side_effect = [sh .ErrorReturnCode_1 (full_cmd = "foo" , stdout = "" .encode (), stderr = "blah blah NotFound blah" .encode ())]* 20 + [None ]
138+ start_time = datetime .datetime .now (datetime .timezone .utc )
139+ assert wait_for_ready_node ("somenode" ) is None
140+ end_time = datetime .datetime .now (datetime .timezone .utc )
141+ duration = end_time - start_time
142+ assert duration > datetime .timedelta (seconds = 3 )
143+
144+
145+ @patch ('boto3.client' , return_value = None )
146+ @patch ('time.sleep' , return_value = None )
147+ @patch ('eks_node_rollout.get_matching_asgs' , return_value = ["asg1" , "asg2" , "asg3" ])
148+ @patch ('eks_node_rollout.describe_nodes_not_matching_lt' , return_value = [ # same instances each of the 3 ASGs
149+ {"PrivateDnsName" : "instance1" , "InstanceId" : "i-asdfasdfasdf" },
150+ {"PrivateDnsName" : "instance2" , "InstanceId" : "i-fdsaasdfdsasdf" },
151+ {"PrivateDnsName" : "instance3" , "InstanceId" : "i-dsadfasdfdsafa" }
152+ ]
153+ )
154+ @patch ('eks_node_rollout.check_is_cluster_autoscaler_tag_present' , return_value = True )
155+ @patch ('eks_node_rollout.disable_autoscaling' , return_value = None )
156+ @patch ('eks_node_rollout.get_num_of_instances' , side_effect = [3 , 4 ]* 9 ) # function is run twice for each of the 9 instances
157+ @patch ('eks_node_rollout.add_node' , return_value = None )
158+ @patch ('eks_node_rollout.get_latest_instance' , return_value = {"PrivateDnsName" : "instance4" })
159+ @patch ('eks_node_rollout.wait_for_ready_node' , return_value = None )
160+ @patch ('eks_node_rollout.kubectl' , return_value = "evicting pod foobar" , create = True )
161+ @patch ('eks_node_rollout.terminate_node' , return_value = None )
162+ @patch ('eks_node_rollout.enable_autoscaling' , return_value = None )
163+ def test_rollout_nodes_happy (* args ):
164+ cluster_name = "dev-apse2-main"
165+ runner = CliRunner ()
166+ result = runner .invoke (rollout_nodes , [f"--cluster-name={ cluster_name } " ])
167+ assert result .exit_code == 0
168+ assert result .output is not None
0 commit comments