test_lxd_container.py 5.0 KB

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