1
0

test_lxd_container.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Salt Testing libs
  8. from tests.support.helpers import flaky
  9. # Import Lxd Test Case
  10. import tests.integration.states.test_lxd
  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('lxd_image.absent', name='images:centos/7',)
  25. self.run_state('lxd_container.absent', name='test-container',)
  26. def test_02__create_container(self):
  27. ret = self.run_state(
  28. 'lxd_container.present',
  29. name='test-container',
  30. running=True,
  31. source={'type': 'image', 'alias': 'images:centos/7'}
  32. )
  33. name = 'lxd_container_|-test-container_|-test-container_|-present'
  34. self.assertSaltTrueReturn(ret)
  35. assert name in ret
  36. assert ret[name]['changes']['started'] == 'Started the container "test-container"'
  37. def test_03__change_container(self):
  38. self.run_state(
  39. 'lxd_container.present',
  40. name='test-container',
  41. running=True,
  42. source={'type': 'image', 'alias': 'images:centos/7'}
  43. )
  44. ret = self.run_state(
  45. 'lxd_container.present',
  46. name='test-container',
  47. running=True,
  48. source={'type': 'image', 'alias': 'images:centos/7'},
  49. restart_on_change=True,
  50. config=[
  51. {'key': 'boot.autostart', 'value': 1},
  52. {'key': 'security.privileged', 'value': '1'},
  53. ],
  54. )
  55. name = 'lxd_container_|-test-container_|-test-container_|-present'
  56. self.assertSaltTrueReturn(ret)
  57. assert name in ret
  58. assert ret[name]['changes']['config'] == {
  59. 'boot.autostart': 'Added config key "boot.autostart" = "1"',
  60. 'security.privileged': 'Added config key "security.privileged" = "1"',
  61. }
  62. def test_08__running_container(self):
  63. self.run_state(
  64. 'lxd_container.present',
  65. name='test-container',
  66. running=True,
  67. source={'type': 'image', 'alias': 'images:centos/7'}
  68. )
  69. ret = self.run_state(
  70. 'lxd_container.running',
  71. name='test-container',
  72. )
  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 ret[name]['comment'] == 'The container "test-container" is already running'
  78. ret = self.run_state(
  79. 'lxd_container.running',
  80. name='test-container',
  81. restart=True,
  82. )
  83. self.assertSaltTrueReturn(ret)
  84. assert name in ret
  85. assert ret[name]['changes'] == {'restarted': 'Restarted the container "test-container"'}
  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(
  95. 'lxd_container.stopped',
  96. name='test-container',
  97. )
  98. name = 'lxd_container_|-test-container_|-test-container_|-stopped'
  99. self.assertSaltTrueReturn(ret)
  100. assert ret[name]['changes'] == {'stopped': 'Stopped the container "test-container"'}
  101. ret = self.run_state(
  102. 'lxd_container.stopped',
  103. name='test-container',
  104. )
  105. name = 'lxd_container_|-test-container_|-test-container_|-stopped'
  106. self.assertSaltTrueReturn(ret)
  107. assert not ret[name]['changes']
  108. def test_10__delete_container(self):
  109. self.run_state(
  110. 'lxd_container.present',
  111. name='test-container',
  112. running=True,
  113. source={'type': 'image', 'alias': 'images:centos/7'}
  114. )
  115. ret = self.run_state(
  116. 'lxd_container.absent',
  117. name='test-container',
  118. )
  119. name = 'lxd_container_|-test-container_|-test-container_|-absent'
  120. assert name in ret
  121. assert ret[name]['result'] is False
  122. ret = self.run_state(
  123. 'lxd_container.stopped',
  124. name='test-container',
  125. )
  126. name = 'lxd_container_|-test-container_|-test-container_|-stopped'
  127. assert name in ret
  128. assert ret[name]['result'] is True
  129. ret = self.run_state(
  130. 'lxd_container.absent',
  131. name='test-container',
  132. )
  133. name = 'lxd_container_|-test-container_|-test-container_|-absent'
  134. self.assertSaltTrueReturn(ret)
  135. assert name in ret
  136. assert ret[name]['changes'] == {'deleted': 'Container "test-container" has been deleted.'}