|
| 1 | +# ========================================================================= |
| 2 | +# Copyright 2012-present Yunify, Inc. |
| 3 | +# ------------------------------------------------------------------------- |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this work except in compliance with the License. |
| 6 | +# You may obtain a copy of the License in the LICENSE file, or at: |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ========================================================================= |
| 16 | + |
| 17 | +import mock |
| 18 | +import random |
| 19 | +import unittest |
| 20 | +from qingcloud.iaas.actions.instance_groups import InstanceGroupsAction |
| 21 | + |
| 22 | + |
| 23 | +class TestInstanceGroupsAction(unittest.TestCase): |
| 24 | + |
| 25 | + max_retry_times = 2 |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def setUpClass(cls): |
| 29 | + """ Initialization of mock test. """ |
| 30 | + |
| 31 | + cls.ig_action_object = InstanceGroupsAction(mock.Mock()) |
| 32 | + cls.group_dict = {} |
| 33 | + cls.existed_instances = ["i-s5sdo5of", "i-a1wy8cvt"] |
| 34 | + |
| 35 | + def test01_create_instance_groups(self): |
| 36 | + |
| 37 | + # Create a repel-group. |
| 38 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 39 | + "action": "CreateInstanceGroupsResponse", |
| 40 | + "instance_groups": ["ig-9edrghud"], |
| 41 | + "ret_code": 0 |
| 42 | + }) |
| 43 | + resp_repel_group = self.ig_action_object.create_instance_groups(relation='repel') |
| 44 | + self.group_dict.update(repel_group=resp_repel_group['instance_groups'].pop()) |
| 45 | + self.assertEqual(resp_repel_group['ret_code'], 0) |
| 46 | + |
| 47 | + # Create a attract-group. |
| 48 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 49 | + "action": "CreateInstanceGroupsResponse", |
| 50 | + "instance_groups": ["ig-ejtdp9su"], |
| 51 | + "ret_code": 0 |
| 52 | + }) |
| 53 | + resp_attract_group = self.ig_action_object.create_instance_groups(relation='attract') |
| 54 | + self.group_dict.update(attract_group=resp_attract_group['instance_groups'].pop()) |
| 55 | + self.assertEqual(resp_attract_group['ret_code'], 0) |
| 56 | + |
| 57 | + def test02_join_instance_group(self): |
| 58 | + |
| 59 | + existed_instances = [ |
| 60 | + self.existed_instances[0], |
| 61 | + self.existed_instances[1] |
| 62 | + ] |
| 63 | + tmp = {'instances': [random.choice(existed_instances)], |
| 64 | + 'group_id': self.group_dict.get('repel_group')} |
| 65 | + existed_instances.remove(tmp['instances'][0]) |
| 66 | + |
| 67 | + # Add an instance into repel-group. |
| 68 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 69 | + "action": "JoinInstanceGroupResponse", |
| 70 | + "job_id": "j-ggxi98503wy", |
| 71 | + "ret_code": 0 |
| 72 | + }) |
| 73 | + resp_repel = self.ig_action_object.join_instance_group( |
| 74 | + instances=tmp['instances'], |
| 75 | + instance_group=tmp['group_id'] |
| 76 | + ) |
| 77 | + self.group_dict.update(repel_group=tmp) |
| 78 | + self.assertEqual(resp_repel['ret_code'], 0) |
| 79 | + |
| 80 | + tmp = {'instances': [random.choice(existed_instances)], |
| 81 | + 'group_id': self.group_dict.get('attract_group')} |
| 82 | + |
| 83 | + # Add an instance into attract-group. |
| 84 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 85 | + "action": "JoinInstanceGroupResponse", |
| 86 | + "job_id": "j-j7e7lt0nk4c", |
| 87 | + "ret_code": 0 |
| 88 | + }) |
| 89 | + resp_attract = self.ig_action_object.join_instance_group( |
| 90 | + instances=tmp['instances'], |
| 91 | + instance_group=tmp['group_id'] |
| 92 | + ) |
| 93 | + self.group_dict.update(attract_group=tmp) |
| 94 | + self.assertEqual(resp_attract['ret_code'], 0) |
| 95 | + |
| 96 | + def test03_describe_instance_groups(self): |
| 97 | + |
| 98 | + repel_id = self.group_dict['repel_group'].get('group_id') |
| 99 | + |
| 100 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 101 | + "action": "DescribeInstanceGroupsResponse", |
| 102 | + "total_count": 1, |
| 103 | + "instance_group_set":[{ |
| 104 | + "instance_group_name": "\u5206\u6563", |
| 105 | + "description": "", |
| 106 | + "tags": [], |
| 107 | + "controller": "self", |
| 108 | + "console_id": "alphacloud", |
| 109 | + "instances": [{ |
| 110 | + "instance_id": "i-s5sdo5of", |
| 111 | + "instance_name": "test4", |
| 112 | + "status": "running", |
| 113 | + "instance_group_id": "ig-9edrghud" |
| 114 | + }], |
| 115 | + "root_user_id": "usr-jGys5Ecd", |
| 116 | + "create_time": "2021-02-08T09:00:38Z", |
| 117 | + "relation": "repel", |
| 118 | + "owner": "usr-jGys5Ecd", |
| 119 | + "resource_project_info": [], |
| 120 | + "instance_group_id": "ig-9edrghud", |
| 121 | + "zone_id": "test" |
| 122 | + }], |
| 123 | + "ret_code": 0 |
| 124 | + }) |
| 125 | + |
| 126 | + resp_repel = self.ig_action_object.describe_instance_groups( |
| 127 | + instance_groups=[repel_id] |
| 128 | + ) |
| 129 | + self.assertEqual(resp_repel['instance_group_set'][0]['instance_group_id'], repel_id) |
| 130 | + |
| 131 | + attract_id = self.group_dict['attract_group'].get('group_id') |
| 132 | + |
| 133 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 134 | + "action": "DescribeInstanceGroupsResponse", |
| 135 | + "total_count": 1, |
| 136 | + "instance_group_set": [{ |
| 137 | + "instance_group_name": "\u96c6\u4e2d", |
| 138 | + "description": "", |
| 139 | + "tags": [], |
| 140 | + "controller": "self", |
| 141 | + "console_id": "alphacloud", |
| 142 | + "instances":[{ |
| 143 | + "instance_id": "i-a1wy8cvt", |
| 144 | + "instance_name": "test3", |
| 145 | + "status": "running", |
| 146 | + "instance_group_id": "ig-ejtdp9su" |
| 147 | + }], |
| 148 | + "root_user_id": "usr-jGys5Ecd", |
| 149 | + "create_time": "2021-02-08T09:07:35Z", |
| 150 | + "relation": "attract", |
| 151 | + "owner": "usr-jGys5Ecd", |
| 152 | + "resource_project_info": [], |
| 153 | + "instance_group_id": "ig-ejtdp9su", |
| 154 | + "zone_id": "test" |
| 155 | + }], |
| 156 | + "ret_code": 0 |
| 157 | + }) |
| 158 | + |
| 159 | + resp_attract = self.ig_action_object.describe_instance_groups( |
| 160 | + instance_groups=[attract_id] |
| 161 | + ) |
| 162 | + self.assertEqual(resp_attract['instance_group_set'][0]['instance_group_id'], attract_id) |
| 163 | + |
| 164 | + def test04_leave_instance_group(self): |
| 165 | + |
| 166 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 167 | + "action": "LeaveInstanceGroupResponse", |
| 168 | + "job_id": "j-lxuwydwb0mk", |
| 169 | + "ret_code": 0 |
| 170 | + }) |
| 171 | + |
| 172 | + resp_repel = self.ig_action_object.leave_instance_group( |
| 173 | + instances=self.group_dict['repel_group'].get('instances'), |
| 174 | + instance_group=self.group_dict['repel_group'].get('group_id') |
| 175 | + ) |
| 176 | + self.assertEqual(resp_repel['ret_code'], 0) |
| 177 | + |
| 178 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 179 | + "action": "LeaveInstanceGroupResponse", |
| 180 | + "job_id": "j-oobmayrygpy", |
| 181 | + "ret_code": 0 |
| 182 | + }) |
| 183 | + |
| 184 | + resp_attract = self.ig_action_object.leave_instance_group( |
| 185 | + instances=self.group_dict['attract_group'].get('instances'), |
| 186 | + instance_group=self.group_dict['attract_group'].get('group_id') |
| 187 | + ) |
| 188 | + self.assertEqual(resp_attract['ret_code'], 0) |
| 189 | + |
| 190 | + def test05_delete_instance_groups(self): |
| 191 | + |
| 192 | + self.ig_action_object.conn.send_request = mock.Mock(return_value={ |
| 193 | + "action": "DeleteInstanceGroupsResponse", |
| 194 | + "instance_groups": ["ig-9edrghud", "ig-ejtdp9su"], |
| 195 | + "ret_code": 0 |
| 196 | + }) |
| 197 | + |
| 198 | + resp_del_groups = self.ig_action_object.delete_instance_groups( |
| 199 | + instance_groups=[ |
| 200 | + self.group_dict['repel_group'].get('group_id'), |
| 201 | + self.group_dict['attract_group'].get('group_id') |
| 202 | + ] |
| 203 | + ) |
| 204 | + self.assertEqual(resp_del_groups['ret_code'], 0) |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == '__main__': |
| 208 | + |
| 209 | + unittest.main() |
0 commit comments