qvm_service.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import unittest
  21. import qubesadmin.tests
  22. import qubesadmin.tools.qvm_service
  23. class TC_00_qvm_service(qubesadmin.tests.QubesTestCase):
  24. def test_000_list(self):
  25. self.app.expected_calls[
  26. ('dom0', 'admin.vm.List', None, None)] = \
  27. b'0\x00some-vm class=AppVM state=Running\n'
  28. self.app.expected_calls[
  29. ('some-vm', 'admin.vm.feature.List', None, None)] = \
  30. b'0\x00feature1\nservice.service1\nservice.service2\n'
  31. self.app.expected_calls[
  32. ('some-vm', 'admin.vm.feature.Get', 'service.service1', None)] = \
  33. b'0\x00value1'
  34. self.app.expected_calls[
  35. ('some-vm', 'admin.vm.feature.Get', 'service.service2', None)] = \
  36. b'0\x00'
  37. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  38. self.assertEqual(
  39. qubesadmin.tools.qvm_service.main(['some-vm'], app=self.app),
  40. 0)
  41. self.assertEqual(stdout.getvalue(),
  42. 'service1 on\n'
  43. 'service2 off\n')
  44. self.assertAllCalled()
  45. def test_001_list_l(self):
  46. self.app.expected_calls[
  47. ('dom0', 'admin.vm.List', None, None)] = \
  48. b'0\x00some-vm class=AppVM state=Running\n'
  49. self.app.expected_calls[
  50. ('some-vm', 'admin.vm.feature.List', None, None)] = \
  51. b'0\x00feature1\nservice.service1\nservice.service2\n'
  52. self.app.expected_calls[
  53. ('some-vm', 'admin.vm.feature.Get', 'service.service1', None)] = \
  54. b'0\x00value1'
  55. self.app.expected_calls[
  56. ('some-vm', 'admin.vm.feature.Get', 'service.service2', None)] = \
  57. b'0\x00'
  58. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  59. self.assertEqual(
  60. qubesadmin.tools.qvm_service.main(['-l', 'some-vm'],
  61. app=self.app),
  62. 0)
  63. self.assertEqual(stdout.getvalue(),
  64. 'service1 on\n'
  65. 'service2 off\n')
  66. self.assertAllCalled()
  67. def test_002_enable(self):
  68. self.app.expected_calls[
  69. ('dom0', 'admin.vm.List', None, None)] = \
  70. b'0\x00some-vm class=AppVM state=Running\n'
  71. self.app.expected_calls[
  72. ('some-vm', 'admin.vm.feature.Set',
  73. 'service.service1', b'1')] = b'0\x00'
  74. self.assertEqual(
  75. qubesadmin.tools.qvm_service.main(['some-vm', 'service1',
  76. 'on'],
  77. app=self.app),
  78. 0)
  79. self.assertAllCalled()
  80. def test_003_enable_opt(self):
  81. self.app.expected_calls[
  82. ('dom0', 'admin.vm.List', None, None)] = \
  83. b'0\x00some-vm class=AppVM state=Running\n'
  84. self.app.expected_calls[
  85. ('some-vm', 'admin.vm.feature.Set',
  86. 'service.service1', b'1')] = b'0\x00'
  87. self.assertEqual(
  88. qubesadmin.tools.qvm_service.main(['--enable', 'some-vm',
  89. 'service1'],
  90. app=self.app),
  91. 0)
  92. self.assertAllCalled()
  93. @unittest.expectedFailure
  94. def test_004_enable_opt_mixed(self):
  95. self.app.expected_calls[
  96. ('dom0', 'admin.vm.List', None, None)] = \
  97. b'0\x00some-vm class=AppVM state=Running\n'
  98. self.app.expected_calls[
  99. ('some-vm', 'admin.vm.feature.Set',
  100. 'service.service1', b'1')] = b'0\x00'
  101. with self.assertNotRaises(SystemExit):
  102. self.assertEqual(
  103. qubesadmin.tools.qvm_service.main(
  104. ['some-vm', '--enable', 'service1'],
  105. app=self.app),
  106. 0)
  107. self.assertAllCalled()
  108. def test_005_disable(self):
  109. self.app.expected_calls[
  110. ('dom0', 'admin.vm.List', None, None)] = \
  111. b'0\x00some-vm class=AppVM state=Running\n'
  112. self.app.expected_calls[
  113. ('some-vm', 'admin.vm.feature.Set',
  114. 'service.service1', b'')] = b'0\x00'
  115. self.assertEqual(
  116. qubesadmin.tools.qvm_service.main(
  117. ['some-vm', 'service1', 'off'],
  118. app=self.app),
  119. 0)
  120. self.assertAllCalled()
  121. def test_006_disable_opt(self):
  122. self.app.expected_calls[
  123. ('dom0', 'admin.vm.List', None, None)] = \
  124. b'0\x00some-vm class=AppVM state=Running\n'
  125. self.app.expected_calls[
  126. ('some-vm', 'admin.vm.feature.Set',
  127. 'service.service1', b'')] = b'0\x00'
  128. with self.assertNotRaises(SystemExit):
  129. self.assertEqual(
  130. qubesadmin.tools.qvm_service.main(
  131. ['--disable', 'some-vm', 'service1'],
  132. app=self.app),
  133. 0)
  134. self.assertAllCalled()
  135. def test_007_get(self):
  136. self.app.expected_calls[
  137. ('dom0', 'admin.vm.List', None, None)] = \
  138. b'0\x00some-vm class=AppVM state=Running\n'
  139. self.app.expected_calls[
  140. ('some-vm', 'admin.vm.feature.Get', 'service.service3', None)] = \
  141. b'0\x00value2 with spaces'
  142. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  143. self.assertEqual(
  144. qubesadmin.tools.qvm_service.main(['some-vm', 'service3'],
  145. app=self.app),
  146. 0)
  147. self.assertEqual(stdout.getvalue(),
  148. 'on\n')
  149. self.assertAllCalled()
  150. def test_008_del(self):
  151. self.app.expected_calls[
  152. ('dom0', 'admin.vm.List', None, None)] = \
  153. b'0\x00some-vm class=AppVM state=Running\n'
  154. self.app.expected_calls[
  155. ('some-vm', 'admin.vm.feature.Remove', 'service.srv4', None)] = \
  156. b'0\x00'
  157. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  158. self.assertEqual(
  159. qubesadmin.tools.qvm_service.main(
  160. ['--unset', 'some-vm', 'srv4'],
  161. app=self.app),
  162. 0)
  163. self.assertEqual(stdout.getvalue(),
  164. '')
  165. self.assertAllCalled()
  166. def test_009_del_legacy(self):
  167. self.app.expected_calls[
  168. ('dom0', 'admin.vm.List', None, None)] = \
  169. b'0\x00some-vm class=AppVM state=Running\n'
  170. self.app.expected_calls[
  171. ('some-vm', 'admin.vm.feature.Remove', 'service.srv4', None)] = \
  172. b'0\x00'
  173. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  174. self.assertEqual(
  175. qubesadmin.tools.qvm_service.main(
  176. ['--default', 'some-vm', 'srv4'],
  177. app=self.app),
  178. 0)
  179. self.assertEqual(stdout.getvalue(),
  180. '')
  181. self.assertAllCalled()
  182. def test_010_set_invalid(self):
  183. self.app.expected_calls[
  184. ('dom0', 'admin.vm.List', None, None)] = \
  185. b'0\x00some-vm class=AppVM state=Running\n'
  186. with self.assertRaises(SystemExit):
  187. qubesadmin.tools.qvm_service.main(
  188. ['some-vm', 'service1', 'invalid-value'],
  189. app=self.app),
  190. self.assertAllCalled()