test_cloud_config.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import json
  3. import tempfile
  4. import unittest
  5. from unittest.mock import patch, MagicMock
  6. from utils.cloud_config import (
  7. compute_config_hash,
  8. fetch_cloud_config,
  9. save_local_cache,
  10. load_local_config,
  11. load_remote_or_cache,
  12. )
  13. class TestCloudConfig(unittest.TestCase):
  14. def test_compute_config_hash(self):
  15. data1 = {"b": 2, "a": 1}
  16. data2 = {"a": 1, "b": 2}
  17. data3 = {"a": 1, "b": 3}
  18. self.assertEqual(compute_config_hash(data1), compute_config_hash(data2))
  19. self.assertNotEqual(compute_config_hash(data1), compute_config_hash(data3))
  20. def test_fetch_cloud_config_success(self):
  21. with patch("toolkit.vs_cloud_api.VSCloudApi.Instance") as mock_api_inst:
  22. mock_inst = MagicMock()
  23. mock_inst.get_node_configurations.return_value = {
  24. "items": [{"identifier": "group_a", "enable": True}]
  25. }
  26. mock_api_inst.return_value = mock_inst
  27. data = fetch_cloud_config(role="sentinel", node_id="node-1")
  28. self.assertEqual(data, [{"identifier": "group_a", "enable": True}])
  29. def test_fetch_cloud_config_failure(self):
  30. with patch("toolkit.vs_cloud_api.VSCloudApi.Instance") as mock_api_inst:
  31. mock_inst = MagicMock()
  32. mock_inst.get_node_configurations.side_effect = Exception("API down")
  33. mock_api_inst.return_value = mock_inst
  34. data = fetch_cloud_config(role="sentinel", node_id="node-1")
  35. self.assertIsNone(data)
  36. def test_load_cloud_or_cache_success_and_save(self):
  37. with patch("toolkit.vs_cloud_api.VSCloudApi.Instance") as mock_api_inst:
  38. mock_inst = MagicMock()
  39. mock_inst.get_node_configurations.return_value = {
  40. "items": [{"identifier": "group_remote", "enable": True}]
  41. }
  42. mock_api_inst.return_value = mock_inst
  43. with tempfile.TemporaryDirectory() as tmp_dir:
  44. cache_path = os.path.join(tmp_dir, "cache.json")
  45. data, hash_val, loaded_cloud = load_remote_or_cache(
  46. role="sentinel",
  47. node_id="node-1",
  48. local_cache_path=cache_path
  49. )
  50. self.assertTrue(loaded_cloud)
  51. self.assertEqual(data, [{"identifier": "group_remote", "enable": True}])
  52. self.assertTrue(os.path.exists(cache_path))
  53. # 验证本地缓存是否被写入
  54. cached_data = load_local_config(cache_path)
  55. self.assertEqual(cached_data, [{"identifier": "group_remote", "enable": True}])
  56. def test_load_cloud_or_cache_fallback_to_local(self):
  57. with patch("toolkit.vs_cloud_api.VSCloudApi.Instance") as mock_api_inst:
  58. mock_inst = MagicMock()
  59. mock_inst.get_node_configurations.side_effect = Exception("API down")
  60. mock_api_inst.return_value = mock_inst
  61. with tempfile.TemporaryDirectory() as tmp_dir:
  62. cache_path = os.path.join(tmp_dir, "cache.json")
  63. save_local_cache(cache_path, [{"identifier": "group_cached", "enable": True}])
  64. data, hash_val, loaded_cloud = load_remote_or_cache(
  65. role="sentinel",
  66. node_id="node-1",
  67. local_cache_path=cache_path
  68. )
  69. self.assertFalse(loaded_cloud)
  70. self.assertEqual(data, [{"identifier": "group_cached", "enable": True}])
  71. if __name__ == "__main__":
  72. unittest.main()