test_lxd_container.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for the lxd states
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Lxd Test Case
  8. import tests.integration.states.test_lxd
  9. # Import Salt Testing libs
  10. from tests.support.helpers import flaky
  11. @flaky
  12. class LxdContainerTestCase(tests.integration.states.test_lxd.LxdTestCase):
  13. def setUp(self):
  14. self.run_state(
  15. "lxd_image.present",
  16. name="images:centos/7",
  17. source={
  18. "name": "centos/7",
  19. "type": "simplestreams",
  20. "server": "https://images.linuxcontainers.org",
  21. },
  22. )
  23. def tearDown(self):
  24. self.run_state(
  25. "lxd_image.absent", name="images:centos/7",
  26. )
  27. self.run_state(
  28. "lxd_container.absent", name="test-container",
  29. )
  30. def test_02__create_container(self):
  31. ret = self.run_state(
  32. "lxd_container.present",
  33. name="test-container",
  34. running=True,
  35. source={"type": "image", "alias": "images:centos/7"},
  36. )
  37. name = "lxd_container_|-test-container_|-test-container_|-present"
  38. self.assertSaltTrueReturn(ret)
  39. assert name in ret
  40. assert (
  41. ret[name]["changes"]["started"] == 'Started the container "test-container"'
  42. )
  43. def test_03__change_container(self):
  44. self.run_state(
  45. "lxd_container.present",
  46. name="test-container",
  47. running=True,
  48. source={"type": "image", "alias": "images:centos/7"},
  49. )
  50. ret = self.run_state(
  51. "lxd_container.present",
  52. name="test-container",
  53. running=True,
  54. source={"type": "image", "alias": "images:centos/7"},
  55. restart_on_change=True,
  56. config=[
  57. {"key": "boot.autostart", "value": 1},
  58. {"key": "security.privileged", "value": "1"},
  59. ],
  60. )
  61. name = "lxd_container_|-test-container_|-test-container_|-present"
  62. self.assertSaltTrueReturn(ret)
  63. assert name in ret
  64. assert ret[name]["changes"]["config"] == {
  65. "boot.autostart": 'Added config key "boot.autostart" = "1"',
  66. "security.privileged": 'Added config key "security.privileged" = "1"',
  67. }
  68. def test_08__running_container(self):
  69. self.run_state(
  70. "lxd_container.present",
  71. name="test-container",
  72. running=True,
  73. source={"type": "image", "alias": "images:centos/7"},
  74. )
  75. ret = self.run_state("lxd_container.running", name="test-container",)
  76. self.assertSaltTrueReturn(ret)
  77. name = "lxd_container_|-test-container_|-test-container_|-running"
  78. assert name in ret
  79. assert not ret[name]["changes"]
  80. assert (
  81. ret[name]["comment"] == 'The container "test-container" is already running'
  82. )
  83. ret = self.run_state(
  84. "lxd_container.running", name="test-container", restart=True,
  85. )
  86. self.assertSaltTrueReturn(ret)
  87. assert name in ret
  88. assert ret[name]["changes"] == {
  89. "restarted": 'Restarted the container "test-container"'
  90. }
  91. assert ret[name]["comment"] == 'Restarted the container "test-container"'
  92. def test_09__stop_container(self):
  93. self.run_state(
  94. "lxd_container.present",
  95. name="test-container",
  96. running=True,
  97. source={"type": "image", "alias": "images:centos/7"},
  98. )
  99. ret = self.run_state("lxd_container.stopped", name="test-container",)
  100. name = "lxd_container_|-test-container_|-test-container_|-stopped"
  101. self.assertSaltTrueReturn(ret)
  102. assert ret[name]["changes"] == {
  103. "stopped": 'Stopped the container "test-container"'
  104. }
  105. ret = self.run_state("lxd_container.stopped", name="test-container",)
  106. name = "lxd_container_|-test-container_|-test-container_|-stopped"
  107. self.assertSaltTrueReturn(ret)
  108. assert not ret[name]["changes"]
  109. def test_10__delete_container(self):
  110. self.run_state(
  111. "lxd_container.present",
  112. name="test-container",
  113. running=True,
  114. source={"type": "image", "alias": "images:centos/7"},
  115. )
  116. ret = self.run_state("lxd_container.absent", name="test-container",)
  117. name = "lxd_container_|-test-container_|-test-container_|-absent"
  118. assert name in ret
  119. assert ret[name]["result"] is False
  120. ret = self.run_state("lxd_container.stopped", name="test-container",)
  121. name = "lxd_container_|-test-container_|-test-container_|-stopped"
  122. assert name in ret
  123. assert ret[name]["result"] is True
  124. ret = self.run_state("lxd_container.absent", name="test-container",)
  125. name = "lxd_container_|-test-container_|-test-container_|-absent"
  126. self.assertSaltTrueReturn(ret)
  127. assert name in ret
  128. assert ret[name]["changes"] == {
  129. "deleted": 'Container "test-container" has been deleted.'
  130. }