test_libcloud_compute.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. import salt.modules.libcloud_compute as libcloud_compute
  9. from salt.utils.versions import LooseVersion as _LooseVersion
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase, skipIf
  14. REQUIRED_LIBCLOUD_VERSION = "2.0.0"
  15. try:
  16. import libcloud
  17. from libcloud.compute.base import (
  18. BaseDriver,
  19. Node,
  20. NodeSize,
  21. NodeState,
  22. NodeLocation,
  23. StorageVolume,
  24. StorageVolumeState,
  25. VolumeSnapshot,
  26. NodeImage,
  27. KeyPair,
  28. )
  29. if hasattr(libcloud, "__version__") and _LooseVersion(
  30. libcloud.__version__
  31. ) < _LooseVersion(REQUIRED_LIBCLOUD_VERSION):
  32. raise ImportError()
  33. logging.getLogger("libcloud").setLevel(logging.CRITICAL)
  34. HAS_LIBCLOUD = True
  35. except ImportError:
  36. HAS_LIBCLOUD = False
  37. if HAS_LIBCLOUD:
  38. class MockComputeDriver(BaseDriver):
  39. def __init__(self): # pylint: disable=W0231
  40. self._TEST_SIZE = NodeSize(
  41. id="test_id",
  42. name="test_size",
  43. ram=4096,
  44. disk=10240,
  45. bandwidth=100000,
  46. price=0,
  47. driver=self,
  48. )
  49. self._TEST_NODE = Node(
  50. id="test_id",
  51. name="test_node",
  52. state=NodeState.RUNNING,
  53. public_ips=["1.2.3.4"],
  54. private_ips=["2.3.4.5"],
  55. driver=self,
  56. size=self._TEST_SIZE,
  57. extra={"ex_key": "ex_value"},
  58. )
  59. self._TEST_LOCATION = NodeLocation(
  60. id="test_location", name="location1", country="Australia", driver=self
  61. )
  62. self._TEST_VOLUME = StorageVolume(
  63. id="vol1",
  64. name="vol_name",
  65. size=40960,
  66. driver=self,
  67. state=StorageVolumeState.AVAILABLE,
  68. extra={"ex_key": "ex_value"},
  69. )
  70. self._TEST_VOLUME_SNAPSHOT = VolumeSnapshot(
  71. id="snap1", size=80960, driver=self
  72. )
  73. self._TEST_IMAGE = NodeImage(
  74. id="image1",
  75. name="test_image",
  76. extra={"ex_key": "ex_value"},
  77. driver=self,
  78. )
  79. self._TEST_KEY_PAIR = KeyPair(
  80. name="test_key",
  81. fingerprint="abc123",
  82. public_key="pub123",
  83. private_key="priv123",
  84. driver=self,
  85. extra={"ex_key": "ex_value"},
  86. )
  87. def list_nodes(self):
  88. return [self._TEST_NODE]
  89. def list_sizes(self, location=None):
  90. if location:
  91. assert location.id == "test_location"
  92. return [self._TEST_SIZE]
  93. def list_locations(self):
  94. return [self._TEST_LOCATION]
  95. def reboot_node(self, node):
  96. assert node.id == "test_id"
  97. return True
  98. def destroy_node(self, node):
  99. assert node.id == "test_id"
  100. return True
  101. def list_volumes(self):
  102. return [self._TEST_VOLUME]
  103. def list_volume_snapshots(self, volume):
  104. assert volume.id == "vol1"
  105. return [self._TEST_VOLUME_SNAPSHOT]
  106. def create_volume(self, size, name, location=None, snapshot=None):
  107. assert size == 9000
  108. assert name == "test_new_volume"
  109. if location:
  110. assert location.country == "Australia"
  111. return self._TEST_VOLUME
  112. def create_volume_snapshot(self, volume, name=None):
  113. assert volume.id == "vol1"
  114. if name:
  115. assert name == "test_snapshot"
  116. return self._TEST_VOLUME_SNAPSHOT
  117. def attach_volume(self, node, volume, device=None):
  118. assert node.id == "test_id"
  119. assert volume.id == "vol1"
  120. if device:
  121. assert device == "/dev/sdc"
  122. return True
  123. def detach_volume(self, volume):
  124. assert volume.id == "vol1"
  125. return True
  126. def destroy_volume(self, volume):
  127. assert volume.id == "vol1"
  128. return True
  129. def destroy_volume_snapshot(self, snapshot):
  130. assert snapshot.id == "snap1"
  131. return True
  132. def list_images(self, location=None):
  133. if location:
  134. assert location.id == "test_location"
  135. return [self._TEST_IMAGE]
  136. def create_image(self, node, name, description=None):
  137. assert node.id == "test_id"
  138. return self._TEST_IMAGE
  139. def delete_image(self, node_image):
  140. return True
  141. def get_image(self, image_id):
  142. assert image_id == "image1"
  143. return self._TEST_IMAGE
  144. def copy_image(self, source_region, node_image, name, description=None):
  145. assert source_region == "us-east1"
  146. assert node_image.id == "image1"
  147. assert name == "copy_test"
  148. return self._TEST_IMAGE
  149. def list_key_pairs(self):
  150. return [self._TEST_KEY_PAIR]
  151. def get_key_pair(self, name):
  152. assert name == "test_key"
  153. return self._TEST_KEY_PAIR
  154. def create_key_pair(self, name):
  155. assert name == "test_key"
  156. return self._TEST_KEY_PAIR
  157. def import_key_pair_from_string(self, name, key_material):
  158. assert name == "test_key"
  159. assert key_material == "test_key_value"
  160. return self._TEST_KEY_PAIR
  161. def import_key_pair_from_file(self, name, key_file_path):
  162. assert name == "test_key"
  163. assert key_file_path == "/path/to/key"
  164. return self._TEST_KEY_PAIR
  165. def delete_key_pair(self, key_pair):
  166. assert key_pair.name == "test_key"
  167. return True
  168. else:
  169. MockComputeDriver = object
  170. @skipIf(not HAS_LIBCLOUD, "No libcloud installed")
  171. @patch(
  172. "salt.modules.libcloud_compute._get_driver",
  173. MagicMock(return_value=MockComputeDriver()),
  174. )
  175. class LibcloudComputeModuleTestCase(TestCase, LoaderModuleMockMixin):
  176. def setup_loader_modules(self):
  177. module_globals = {
  178. "__salt__": {
  179. "config.option": MagicMock(
  180. return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
  181. )
  182. }
  183. }
  184. if libcloud_compute.HAS_LIBCLOUD is False:
  185. module_globals["sys.modules"] = {"libcloud": MagicMock()}
  186. return {libcloud_compute: module_globals}
  187. def test_module_creation(self):
  188. client = libcloud_compute._get_driver("test")
  189. self.assertFalse(client is None)
  190. def test_init(self):
  191. with patch("salt.utils.compat.pack_dunder", return_value=False) as dunder:
  192. libcloud_compute.__init__(None)
  193. dunder.assert_called_with("salt.modules.libcloud_compute")
  194. def _validate_node(self, node):
  195. self.assertEqual(node["name"], "test_node")
  196. self.assertEqual(node["id"], "test_id")
  197. self.assertEqual(node["private_ips"], ["2.3.4.5"])
  198. self.assertEqual(node["public_ips"], ["1.2.3.4"])
  199. self.assertEqual(node["size"]["name"], "test_size")
  200. def _validate_size(self, size):
  201. self.assertEqual(size["id"], "test_id")
  202. self.assertEqual(size["name"], "test_size")
  203. self.assertEqual(size["ram"], 4096)
  204. def _validate_location(self, location):
  205. self.assertEqual(location["id"], "test_location")
  206. self.assertEqual(location["name"], "location1")
  207. self.assertEqual(location["country"], "Australia")
  208. def _validate_volume(self, volume):
  209. self.assertEqual(volume["id"], "vol1")
  210. self.assertEqual(volume["name"], "vol_name")
  211. self.assertEqual(volume["size"], 40960)
  212. self.assertEqual(volume["state"], "available")
  213. self.assertEqual(volume["extra"], {"ex_key": "ex_value"})
  214. def _validate_volume_snapshot(self, volume):
  215. self.assertEqual(volume["id"], "snap1")
  216. self.assertEqual(volume["size"], 80960)
  217. def _validate_image(self, image):
  218. self.assertEqual(image["id"], "image1")
  219. self.assertEqual(image["name"], "test_image")
  220. self.assertEqual(image["extra"], {"ex_key": "ex_value"})
  221. def _validate_key_pair(self, key):
  222. self.assertEqual(key["name"], "test_key")
  223. self.assertEqual(key["fingerprint"], "abc123")
  224. self.assertEqual(key["extra"], {"ex_key": "ex_value"})
  225. def test_list_nodes(self):
  226. nodes = libcloud_compute.list_nodes("test")
  227. self.assertEqual(len(nodes), 1)
  228. self._validate_node(nodes[0])
  229. def test_list_sizes(self):
  230. sizes = libcloud_compute.list_sizes("test")
  231. self.assertEqual(len(sizes), 1)
  232. self._validate_size(sizes[0])
  233. def test_list_sizes_location(self):
  234. sizes = libcloud_compute.list_sizes("test", location_id="test_location")
  235. self.assertEqual(len(sizes), 1)
  236. self._validate_size(sizes[0])
  237. def test_list_locations(self):
  238. locations = libcloud_compute.list_locations("test")
  239. self.assertEqual(len(locations), 1)
  240. self._validate_location(locations[0])
  241. def test_reboot_node(self):
  242. result = libcloud_compute.reboot_node("test_id", "test")
  243. self.assertTrue(result)
  244. def test_reboot_node_invalid(self):
  245. with self.assertRaises(ValueError):
  246. libcloud_compute.reboot_node("foo_node", "test")
  247. def test_destroy_node(self):
  248. result = libcloud_compute.destroy_node("test_id", "test")
  249. self.assertTrue(result)
  250. def test_destroy_node_invalid(self):
  251. with self.assertRaises(ValueError):
  252. libcloud_compute.destroy_node("foo_node", "test")
  253. def test_list_volumes(self):
  254. volumes = libcloud_compute.list_volumes("test")
  255. self.assertEqual(len(volumes), 1)
  256. self._validate_volume(volumes[0])
  257. def test_list_volume_snapshots(self):
  258. volumes = libcloud_compute.list_volume_snapshots("vol1", "test")
  259. self.assertEqual(len(volumes), 1)
  260. self._validate_volume_snapshot(volumes[0])
  261. def test_create_volume(self):
  262. volume = libcloud_compute.create_volume(9000, "test_new_volume", "test")
  263. self._validate_volume(volume)
  264. def test_create_volume_in_location(self):
  265. volume = libcloud_compute.create_volume(
  266. 9000, "test_new_volume", "test", location_id="test_location"
  267. )
  268. self._validate_volume(volume)
  269. def test_create_volume_snapshot(self):
  270. snapshot = libcloud_compute.create_volume_snapshot("vol1", "test")
  271. self._validate_volume_snapshot(snapshot)
  272. def test_create_volume_snapshot_named(self):
  273. snapshot = libcloud_compute.create_volume_snapshot(
  274. "vol1", "test", name="test_snapshot"
  275. )
  276. self._validate_volume_snapshot(snapshot)
  277. def test_attach_volume(self):
  278. result = libcloud_compute.attach_volume("test_id", "vol1", "test")
  279. self.assertTrue(result)
  280. def test_detatch_volume(self):
  281. result = libcloud_compute.detach_volume("vol1", "test")
  282. self.assertTrue(result)
  283. def test_destroy_volume(self):
  284. result = libcloud_compute.destroy_volume("vol1", "test")
  285. self.assertTrue(result)
  286. def test_destroy_volume_snapshot(self):
  287. result = libcloud_compute.destroy_volume_snapshot("vol1", "snap1", "test")
  288. self.assertTrue(result)
  289. def test_list_images(self):
  290. images = libcloud_compute.list_images("test")
  291. self.assertEqual(len(images), 1)
  292. self._validate_image(images[0])
  293. def test_list_images_in_location(self):
  294. images = libcloud_compute.list_images("test", location_id="test_location")
  295. self.assertEqual(len(images), 1)
  296. self._validate_image(images[0])
  297. def test_create_image(self):
  298. image = libcloud_compute.create_image("test_id", "new_image", "test")
  299. self._validate_image(image)
  300. def test_delete_image(self):
  301. result = libcloud_compute.delete_image("image1", "test")
  302. self.assertTrue(result)
  303. def test_get_image(self):
  304. image = libcloud_compute.get_image("image1", "test")
  305. self._validate_image(image)
  306. def test_copy_image(self):
  307. new_image = libcloud_compute.copy_image(
  308. "us-east1", "image1", "copy_test", "test"
  309. )
  310. self._validate_image(new_image)
  311. def test_list_key_pairs(self):
  312. keys = libcloud_compute.list_key_pairs("test")
  313. self.assertEqual(len(keys), 1)
  314. self._validate_key_pair(keys[0])
  315. def test_get_key_pair(self):
  316. key = libcloud_compute.get_key_pair("test_key", "test")
  317. self._validate_key_pair(key)
  318. def test_create_key_pair(self):
  319. key = libcloud_compute.create_key_pair("test_key", "test")
  320. self._validate_key_pair(key)
  321. def test_import_key_string(self):
  322. key = libcloud_compute.import_key_pair("test_key", "test_key_value", "test")
  323. self._validate_key_pair(key)
  324. def test_import_key_file(self):
  325. key = libcloud_compute.import_key_pair(
  326. "test_key", "/path/to/key", "test", key_type="FILE"
  327. )
  328. self._validate_key_pair(key)
  329. def test_delete_key_pair(self):
  330. result = libcloud_compute.delete_key_pair("test_key", "test")
  331. self.assertTrue(result)