albtest.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import unittest
  2. import utils
  3. from alibabacloud_alb20200616 import models as alb_models
  4. class TestAddServersToServerGroup(unittest.TestCase):
  5. def setUp(self):
  6. self.alb_client = FakeAlbClient() # 使用假客户端
  7. self.server_group_id = "sgp-h793418y" # 假的服务器组 ID
  8. self.instance_id = "i-123456" # 假的实例 ID
  9. self.weight = 10 # 权重值
  10. def test_add_servers_to_server_group_success(self):
  11. try:
  12. utils.add_servers_to_server_group(self.alb_client, self.server_group_id, self.instance_id, self.weight)
  13. print("Server added successfully.")
  14. except Exception as e:
  15. self.fail(f"add_servers_to_server_group raised an exception: {e}")
  16. class TestSetInstanceWeightProcessWithAlb(unittest.TestCase):
  17. def setUp(self):
  18. self.alb_client = FakeAlbClient() # 使用假客户端
  19. self.server_group_id = "sgp-h793418y" # 假的服务器组 ID
  20. self.instance_id = "i-123456" # 假的实例 ID
  21. self.weight = [(0, 1)] # 假的权重值
  22. def test_set_instance_weight_success(self):
  23. try:
  24. utils.set_instance_weight_process_with_alb(self.alb_client, self.server_group_id, self.instance_id,
  25. self.weight)
  26. print("Instance weight set successfully.")
  27. except Exception as e:
  28. self.fail(f"set_instance_weight_process_with_alb raised an exception: {e}")
  29. class TestGetInstanceIds(unittest.TestCase):
  30. def setUp(self):
  31. self.alb_client = FakeAlbClient() # 使用假客户端
  32. self.server_group_id = "sgp-h793418y" # 假的服务器组 ID
  33. def test_get_instance_ids_success(self):
  34. try:
  35. instance_ids = utils.get_instance_ids(self.alb_client, self.server_group_id)
  36. print(f"Retrieved instance IDs: {instance_ids}")
  37. self.assertEqual(instance_ids, ["i-123456", "i-789012"]) # 假数据
  38. except Exception as e:
  39. self.fail(f"get_instance_ids raised an exception: {e}")
  40. class FakeAlbClient:
  41. # 一个假 ALB 客户端,用于测试。
  42. def add_servers_to_server_group_with_options(self, request, runtime):
  43. if request.server_group_id == "sgp-h793418y" and request.servers:
  44. return {"Code": "Success"}
  45. else:
  46. raise Exception("Failed to add server")
  47. def add_servers_to_server_group_with_options(self, request, runtime):
  48. if request.server_group_id == "sgp-h793418y" and request.servers:
  49. return {"Code": "Success"}
  50. else:
  51. raise Exception("Failed to add server")
  52. def set_instance_weight_with_options(self, request, runtime):
  53. if request.server_group_id == "sgp-h793418y" and request.instance_id == "i-123456" and request.weight is not None:
  54. return {"Code": "Success"}
  55. else:
  56. raise Exception("Failed to set instance weight")
  57. def get_instance_ids_with_options(self, request, runtime):
  58. print(f"Request to get instance IDs for group: {request.server_group_id}")
  59. if request.server_group_id == "sgp-h793418y":
  60. return {"InstanceIds": ["i-123456", "i-789012"]}
  61. else:
  62. raise Exception("Failed to get instance IDs")
  63. def list_server_group_servers_with_options(self, request, runtime):
  64. print(f"Request to list servers for group: {request.server_group_id}")
  65. if request.server_group_id == "sgp-h793418y":
  66. return {
  67. "Servers": {
  68. "Server": [
  69. {"ServerId": "i-123456"},
  70. {"ServerId": "i-789012"}
  71. ]
  72. }
  73. }
  74. else:
  75. raise Exception("Failed to list server group servers")
  76. if __name__ == '__main__':
  77. unittest.main()