salt.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. # -*- encoding: utf-8 -*-
  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 General Public License as published by
  10. # the Free Software Foundation; either version 2 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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import asyncio
  21. import json
  22. import os
  23. import shutil
  24. import subprocess
  25. import sys
  26. import qubes.tests
  27. class SaltTestMixin(object):
  28. def setUp(self):
  29. super().setUp()
  30. self.salt_testdir = '/srv/salt/test_salt'
  31. os.makedirs(self.salt_testdir, exist_ok=True)
  32. def tearDown(self):
  33. shutil.rmtree(self.salt_testdir)
  34. try:
  35. tops = '/srv/salt/_tops/base'
  36. for top_link in os.listdir(tops):
  37. path = os.path.join(tops, top_link)
  38. target = os.readlink(path)
  39. if target.startswith(self.salt_testdir):
  40. os.unlink(path)
  41. except FileNotFoundError:
  42. pass
  43. super().tearDown()
  44. def salt_call(self, cmd):
  45. full_cmd = ['qubesctl']
  46. if '--dom0-only' in cmd:
  47. full_cmd.insert(1, '--dom0-only')
  48. cmd.remove('--dom0-only')
  49. full_cmd.extend(cmd)
  50. full_cmd.append('--out=json')
  51. p = self.loop.run_until_complete(asyncio.create_subprocess_exec(
  52. *full_cmd, stdout=subprocess.PIPE))
  53. output, _ = self.loop.run_until_complete(p.communicate())
  54. if p.returncode != 0:
  55. raise AssertionError(
  56. 'Salt command \'{}\' failed with code {}. '
  57. 'Full output: {}'.format(full_cmd, p.returncode, output))
  58. return output.decode()
  59. def dom0_salt_call_json(self, cmd):
  60. return json.loads(self.salt_call(['--dom0-only'] + cmd))
  61. class TC_00_Dom0(SaltTestMixin, qubes.tests.SystemTestCase):
  62. def test_000_top_enable_disable(self):
  63. with open(os.path.join(self.salt_testdir, 'something.sls'), 'w') as f:
  64. f.write('test-top-enable:\n')
  65. f.write(' test.succeed_without_changes: []\n')
  66. with open(os.path.join(self.salt_testdir, 'something.top'), 'w') as f:
  67. f.write('base:\n')
  68. f.write(' dom0:\n')
  69. f.write(' - test_salt.something\n')
  70. cmd_output = self.dom0_salt_call_json(
  71. ['top.enable', 'test_salt.something'])
  72. self.assertEqual(cmd_output,
  73. {'local': {'test_salt.something.top': {'status': 'enabled'}}})
  74. cmd_output = self.dom0_salt_call_json(['state.show_top'])
  75. self.assertIn('local', cmd_output)
  76. self.assertIn('base', cmd_output['local'])
  77. self.assertIn('test_salt.something', cmd_output['local']['base'])
  78. cmd_output = self.dom0_salt_call_json(
  79. ['top.disable', 'test_salt.something'])
  80. #self.assertEqual(cmd_output,
  81. # {'local': {'test_salt.something.top': {'status': 'disabled'}}})
  82. cmd_output = self.dom0_salt_call_json(['state.show_top'])
  83. self.assertIn('local', cmd_output)
  84. self.assertIn('base', cmd_output['local'])
  85. self.assertNotIn('test_salt.something', cmd_output['local']['base'])
  86. def test_001_state_sls(self):
  87. with open(os.path.join(self.salt_testdir, 'something.sls'), 'w') as f:
  88. f.write('test-top-enable:\n')
  89. f.write(' test.succeed_without_changes: []\n')
  90. cmd_output = self.dom0_salt_call_json(
  91. ['state.sls', 'test_salt.something'])
  92. state_id = 'test_|-test-top-enable_|-test-top-enable_|-succeed_without_changes'
  93. self.assertIn('local', cmd_output)
  94. self.assertIn(state_id, cmd_output['local'])
  95. self.assertIn('start_time', cmd_output['local'][state_id])
  96. del cmd_output['local'][state_id]['start_time']
  97. self.assertIn('duration', cmd_output['local'][state_id])
  98. del cmd_output['local'][state_id]['duration']
  99. self.assertEqual(cmd_output,
  100. {'local': {state_id: {
  101. 'name': 'test-top-enable',
  102. 'comment': 'Success!',
  103. 'result': True,
  104. '__run_num__': 0,
  105. '__sls__': 'test_salt.something',
  106. 'changes': {},
  107. '__id__': 'test-top-enable'
  108. }}})
  109. def test_010_create_vm(self):
  110. vmname = self.make_vm_name('appvm')
  111. with open(os.path.join(self.salt_testdir, 'create_vm.sls'), 'w') as f:
  112. f.write(vmname + ':\n')
  113. f.write(' qvm.vm:\n')
  114. f.write(' - present:\n')
  115. f.write(' - label: orange\n')
  116. f.write(' - prefs:\n')
  117. f.write(' - vcpus: 1\n')
  118. cmd_output = self.dom0_salt_call_json(
  119. ['state.sls', 'test_salt.create_vm'])
  120. state_out = list(cmd_output['local'].values())[0]
  121. del state_out['start_time']
  122. del state_out['duration']
  123. self.assertEqual(state_out, {
  124. 'comment': '====== [\'present\'] ======\n'
  125. '/usr/bin/qvm-create {} --class=AppVM --label=orange \n'
  126. '\n'
  127. '====== [\'prefs\'] ======\n'.format(vmname),
  128. 'name': vmname,
  129. 'result': True,
  130. 'changes': {
  131. 'qvm.prefs': {'qvm.create': {
  132. 'vcpus': {'new': 1, 'old': '*default*'}
  133. }
  134. },
  135. },
  136. '__sls__': 'test_salt.create_vm',
  137. '__run_num__': 0,
  138. '__id__': vmname,
  139. })
  140. self.assertIn(vmname, self.app.domains)
  141. vm = self.app.domains[vmname]
  142. self.assertEqual(str(vm.label), 'orange')
  143. self.assertEqual(vm.vcpus, 1)
  144. def test_011_set_prefs(self):
  145. vmname = self.make_vm_name('appvm')
  146. vm = self.app.add_new_vm('AppVM', label='red',
  147. name=vmname)
  148. self.loop.run_until_complete(vm.create_on_disk())
  149. with open(os.path.join(self.salt_testdir, 'create_vm.sls'), 'w') as f:
  150. f.write(vmname + ':\n')
  151. f.write(' qvm.vm:\n')
  152. f.write(' - present:\n')
  153. f.write(' - label: orange\n')
  154. f.write(' - prefs:\n')
  155. f.write(' - vcpus: 1\n')
  156. cmd_output = self.dom0_salt_call_json(
  157. ['state.sls', 'test_salt.create_vm'])
  158. state_out = list(cmd_output['local'].values())[0]
  159. del state_out['start_time']
  160. del state_out['duration']
  161. self.assertEqual(state_out, {
  162. 'comment': '====== [\'present\'] ======\n'
  163. '[SKIP] A VM with the name \'{}\' already exists.\n'
  164. '\n'
  165. '====== [\'prefs\'] ======\n'.format(vmname),
  166. 'name': vmname,
  167. 'result': True,
  168. 'changes': {
  169. 'qvm.prefs': {'qvm.create': {
  170. 'vcpus': {'new': 1, 'old': '*default*'}
  171. }
  172. },
  173. },
  174. '__sls__': 'test_salt.create_vm',
  175. '__run_num__': 0,
  176. '__id__': vmname,
  177. })
  178. self.assertIn(vmname, self.app.domains)
  179. vm = self.app.domains[vmname]
  180. self.assertEqual(str(vm.label), 'red')
  181. self.assertEqual(vm.vcpus, 1)
  182. def test_012_tags(self):
  183. vmname = self.make_vm_name('appvm')
  184. vm = self.app.add_new_vm('AppVM', label='red',
  185. name=vmname)
  186. self.loop.run_until_complete(vm.create_on_disk())
  187. vm.tags.add('tag1')
  188. vm.tags.add('tag3')
  189. with open(os.path.join(self.salt_testdir, 'test_state.sls'), 'w') as f:
  190. f.write(vmname + ':\n')
  191. f.write(' qvm.vm:\n')
  192. f.write(' - tags:\n')
  193. f.write(' - present:\n')
  194. f.write(' - tag1\n')
  195. f.write(' - tag2\n')
  196. f.write(' - absent:\n')
  197. f.write(' - tag3\n')
  198. f.write(' - tag4\n')
  199. cmd_output = self.dom0_salt_call_json(
  200. ['state.sls', 'test_salt.test_state'])
  201. state_out = list(cmd_output['local'].values())[0]
  202. del state_out['start_time']
  203. del state_out['duration']
  204. self.assertEqual(state_out, {
  205. 'comment': '====== [\'tags\'] ======\n',
  206. 'name': vmname,
  207. 'result': True,
  208. 'changes': {
  209. 'qvm.tags': {'qvm.tags': {
  210. 'new': ['tag1', 'tag2'], 'old': ['tag1', 'tag3'],
  211. }
  212. },
  213. },
  214. '__sls__': 'test_salt.test_state',
  215. '__run_num__': 0,
  216. '__id__': vmname,
  217. })
  218. self.assertIn(vmname, self.app.domains)
  219. vm = self.app.domains[vmname]
  220. self.assertEqual(set(vm.tags), {'tag1', 'tag2'})
  221. def test_020_qubes_pillar(self):
  222. vmname = self.make_vm_name('appvm')
  223. vm = self.app.add_new_vm('AppVM', label='red',
  224. name=vmname)
  225. self.loop.run_until_complete(vm.create_on_disk())
  226. cmd_output = self.dom0_salt_call_json(
  227. ['pillar.items', '--id=' + vmname])
  228. self.assertIn('local', cmd_output)
  229. self.assertIn('qubes', cmd_output['local'])
  230. qubes_pillar = cmd_output['local']['qubes']
  231. self.assertEqual(qubes_pillar, {
  232. 'type': 'app',
  233. 'netvm': str(vm.netvm),
  234. 'template': str(vm.template),
  235. })
  236. class SaltVMTestMixin(SaltTestMixin):
  237. template = None
  238. def setUp(self):
  239. if self.template.startswith('whonix'):
  240. self.skipTest('Whonix not supported as salt VM')
  241. super(SaltVMTestMixin, self).setUp()
  242. self.init_default_template(self.template)
  243. dispvm_tpl_name = self.make_vm_name('disp-tpl')
  244. dispvm_tpl = self.app.add_new_vm('AppVM', label='red',
  245. template_for_dispvms=True, name=dispvm_tpl_name)
  246. self.loop.run_until_complete(dispvm_tpl.create_on_disk())
  247. self.app.default_dispvm = dispvm_tpl
  248. def tearDown(self):
  249. self.app.default_dispvm = None
  250. super(SaltVMTestMixin, self).tearDown()
  251. def test_000_simple_sls(self):
  252. vmname = self.make_vm_name('target')
  253. self.vm = self.app.add_new_vm('AppVM', name=vmname, label='red')
  254. self.loop.run_until_complete(self.vm.create_on_disk())
  255. # start the VM manually, so it stays running after applying salt state
  256. self.loop.run_until_complete(self.vm.start())
  257. with open(os.path.join(self.salt_testdir, 'something.sls'), 'w') as f:
  258. f.write('/home/user/testfile:\n')
  259. f.write(' file.managed:\n')
  260. f.write(' - contents: |\n')
  261. f.write(' this is test\n')
  262. with open(os.path.join(self.salt_testdir, 'something.top'), 'w') as f:
  263. f.write('base:\n')
  264. f.write(' {}:\n'.format(vmname))
  265. f.write(' - test_salt.something\n')
  266. # enable so state.show_top will not be empty, otherwise qubesctl will
  267. # skip the VM; but we don't use state.highstate
  268. self.dom0_salt_call_json(['top.enable', 'test_salt.something'])
  269. state_output = self.salt_call(
  270. ['--skip-dom0', '--show-output', '--targets=' + vmname,
  271. 'state.sls', 'test_salt.something'])
  272. expected_output = vmname + ':\n'
  273. self.assertTrue(state_output.startswith(expected_output),
  274. 'Full output: ' + state_output)
  275. state_id = 'file_|-/home/user/testfile_|-/home/user/testfile_|-managed'
  276. # drop the header
  277. state_output_json = json.loads(state_output[len(expected_output):])
  278. state_output_json = state_output_json[vmname][state_id]
  279. try:
  280. del state_output_json['duration']
  281. del state_output_json['start_time']
  282. except KeyError:
  283. pass
  284. try:
  285. del state_output_json['pchanges']
  286. except KeyError:
  287. pass
  288. try:
  289. # older salt do not report this
  290. self.assertEqual(state_output_json['__id__'], '/home/user/testfile')
  291. del state_output_json['__id__']
  292. except KeyError:
  293. pass
  294. try:
  295. # or sls file
  296. self.assertEqual(state_output_json['__sls__'],
  297. 'test_salt.something')
  298. del state_output_json['__sls__']
  299. except KeyError:
  300. pass
  301. # different output depending on salt version
  302. expected_output = {
  303. '__run_num__': 0,
  304. 'changes': {
  305. 'diff': 'New file',
  306. },
  307. 'name': '/home/user/testfile',
  308. 'comment': 'File /home/user/testfile updated',
  309. 'result': True,
  310. }
  311. self.assertEqual(state_output_json, expected_output)
  312. stdout, stderr = self.loop.run_until_complete(self.vm.run_for_stdio(
  313. 'cat /home/user/testfile'))
  314. self.assertEqual(stdout, b'this is test\n')
  315. self.assertEqual(stderr, b'')
  316. def test_001_multi_state_highstate(self):
  317. vmname = self.make_vm_name('target')
  318. self.vm = self.app.add_new_vm('AppVM', name=vmname, label='red')
  319. self.loop.run_until_complete(self.vm.create_on_disk())
  320. # start the VM manually, so it stays running after applying salt state
  321. self.loop.run_until_complete(self.vm.start())
  322. states = ('something', 'something2')
  323. for state in states:
  324. with open(os.path.join(self.salt_testdir, state + '.sls'), 'w') as f:
  325. f.write('/home/user/{}:\n'.format(state))
  326. f.write(' file.managed:\n')
  327. f.write(' - contents: |\n')
  328. f.write(' this is test\n')
  329. with open(os.path.join(self.salt_testdir, state + '.top'), 'w') as f:
  330. f.write('base:\n')
  331. f.write(' {}:\n'.format(vmname))
  332. f.write(' - test_salt.{}\n'.format(state))
  333. self.dom0_salt_call_json(['top.enable', 'test_salt.something'])
  334. self.dom0_salt_call_json(['top.enable', 'test_salt.something2'])
  335. state_output = self.salt_call(
  336. ['--skip-dom0', '--show-output', '--targets=' + vmname,
  337. 'state.highstate'])
  338. expected_output = vmname + ':\n'
  339. self.assertTrue(state_output.startswith(expected_output),
  340. 'Full output: ' + state_output)
  341. state_output_json = json.loads(state_output[len(expected_output):])
  342. for state in states:
  343. state_id = \
  344. 'file_|-/home/user/{0}_|-/home/user/{0}_|-managed'.format(state)
  345. # drop the header
  346. self.assertIn(state_id, state_output_json[vmname])
  347. state_output_single = state_output_json[vmname][state_id]
  348. self.assertTrue(state_output_single['result'])
  349. self.assertNotEqual(state_output_single['changes'], {})
  350. stdout, stderr = self.loop.run_until_complete(self.vm.run_for_stdio(
  351. 'cat /home/user/' + state))
  352. self.assertEqual(stdout, b'this is test\n')
  353. self.assertEqual(stderr, b'')
  354. def create_testcases_for_templates():
  355. return qubes.tests.create_testcases_for_templates('TC_10_VMSalt',
  356. SaltVMTestMixin, qubes.tests.SystemTestCase,
  357. module=sys.modules[__name__])
  358. def load_tests(loader, tests, pattern):
  359. tests.addTests(loader.loadTestsFromNames(
  360. create_testcases_for_templates()))
  361. return tests
  362. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)