test_lxd_container.py 4.9 KB

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