import unittest import utils from alibabacloud_alb20200616 import models as alb_models class TestAddServersToServerGroup(unittest.TestCase): def setUp(self): self.alb_client = FakeAlbClient() # 使用假客户端 self.server_group_id = "sgp-h793418y" # 假的服务器组 ID self.instance_id = "i-123456" # 假的实例 ID self.weight = 10 # 权重值 def test_add_servers_to_server_group_success(self): try: utils.add_servers_to_server_group(self.alb_client, self.server_group_id, self.instance_id, self.weight) print("Server added successfully.") except Exception as e: self.fail(f"add_servers_to_server_group raised an exception: {e}") class TestSetInstanceWeightProcessWithAlb(unittest.TestCase): def setUp(self): self.alb_client = FakeAlbClient() # 使用假客户端 self.server_group_id = "sgp-h793418y" # 假的服务器组 ID self.instance_id = "i-123456" # 假的实例 ID self.weight = [(0, 1)] # 假的权重值 def test_set_instance_weight_success(self): try: utils.set_instance_weight_process_with_alb(self.alb_client, self.server_group_id, self.instance_id, self.weight) print("Instance weight set successfully.") except Exception as e: self.fail(f"set_instance_weight_process_with_alb raised an exception: {e}") class TestGetInstanceIds(unittest.TestCase): def setUp(self): self.alb_client = FakeAlbClient() # 使用假客户端 self.server_group_id = "sgp-h793418y" # 假的服务器组 ID def test_get_instance_ids_success(self): try: instance_ids = utils.get_instance_ids(self.alb_client, self.server_group_id) print(f"Retrieved instance IDs: {instance_ids}") self.assertEqual(instance_ids, ["i-123456", "i-789012"]) # 假数据 except Exception as e: self.fail(f"get_instance_ids raised an exception: {e}") class FakeAlbClient: # 一个假 ALB 客户端,用于测试。 def add_servers_to_server_group_with_options(self, request, runtime): if request.server_group_id == "sgp-h793418y" and request.servers: return {"Code": "Success"} else: raise Exception("Failed to add server") def add_servers_to_server_group_with_options(self, request, runtime): if request.server_group_id == "sgp-h793418y" and request.servers: return {"Code": "Success"} else: raise Exception("Failed to add server") def set_instance_weight_with_options(self, request, runtime): if request.server_group_id == "sgp-h793418y" and request.instance_id == "i-123456" and request.weight is not None: return {"Code": "Success"} else: raise Exception("Failed to set instance weight") def get_instance_ids_with_options(self, request, runtime): print(f"Request to get instance IDs for group: {request.server_group_id}") if request.server_group_id == "sgp-h793418y": return {"InstanceIds": ["i-123456", "i-789012"]} else: raise Exception("Failed to get instance IDs") def list_server_group_servers_with_options(self, request, runtime): print(f"Request to list servers for group: {request.server_group_id}") if request.server_group_id == "sgp-h793418y": return { "Servers": { "Server": [ {"ServerId": "i-123456"}, {"ServerId": "i-789012"} ] } } else: raise Exception("Failed to list server group servers") if __name__ == '__main__': unittest.main()