salt.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. new_tags = list(sorted(list(vm.tags) + ['tag1', 'tag2']))
  188. vm.tags.add('tag1')
  189. vm.tags.add('tag3')
  190. old_tags = list(sorted(vm.tags))
  191. with open(os.path.join(self.salt_testdir, 'test_state.sls'), 'w') as f:
  192. f.write(vmname + ':\n')
  193. f.write(' qvm.vm:\n')
  194. f.write(' - tags:\n')
  195. f.write(' - present:\n')
  196. f.write(' - tag1\n')
  197. f.write(' - tag2\n')
  198. f.write(' - absent:\n')
  199. f.write(' - tag3\n')
  200. f.write(' - tag4\n')
  201. cmd_output = self.dom0_salt_call_json(
  202. ['state.sls', 'test_salt.test_state'])
  203. state_out = list(cmd_output['local'].values())[0]
  204. del state_out['start_time']
  205. del state_out['duration']
  206. self.maxDiff = None
  207. self.assertEqual(state_out, {
  208. 'comment': '====== [\'tags\'] ======\n',
  209. 'name': vmname,
  210. 'result': True,
  211. 'changes': {
  212. 'qvm.tags': {'qvm.tags': {
  213. 'new': new_tags, 'old': old_tags,
  214. }
  215. },
  216. },
  217. '__sls__': 'test_salt.test_state',
  218. '__run_num__': 0,
  219. '__id__': vmname,
  220. })
  221. self.assertIn(vmname, self.app.domains)
  222. vm = self.app.domains[vmname]
  223. self.assertEqual(set(vm.tags), set(new_tags))
  224. def test_020_qubes_pillar(self):
  225. vmname = self.make_vm_name('appvm')
  226. vm = self.app.add_new_vm('AppVM', label='red',
  227. name=vmname)
  228. self.loop.run_until_complete(vm.create_on_disk())
  229. cmd_output = self.dom0_salt_call_json(
  230. ['pillar.items', '--id=' + vmname])
  231. self.assertIn('local', cmd_output)
  232. self.assertIn('qubes', cmd_output['local'])
  233. qubes_pillar = cmd_output['local']['qubes']
  234. self.assertEqual(qubes_pillar, {
  235. 'type': 'app',
  236. 'netvm': str(vm.netvm),
  237. 'template': str(vm.template),
  238. })
  239. class SaltVMTestMixin(SaltTestMixin):
  240. template = None
  241. def setUp(self):
  242. if self.template.startswith('whonix'):
  243. self.skipTest('Whonix not supported as salt VM')
  244. super(SaltVMTestMixin, self).setUp()
  245. self.init_default_template(self.template)
  246. dispvm_tpl_name = self.make_vm_name('disp-tpl')
  247. dispvm_tpl = self.app.add_new_vm('AppVM', label='red',
  248. template_for_dispvms=True, name=dispvm_tpl_name)
  249. self.loop.run_until_complete(dispvm_tpl.create_on_disk())
  250. self.app.default_dispvm = dispvm_tpl
  251. def tearDown(self):
  252. self.app.default_dispvm = None
  253. super(SaltVMTestMixin, self).tearDown()
  254. def test_000_simple_sls(self):
  255. vmname = self.make_vm_name('target')
  256. self.vm = self.app.add_new_vm('AppVM', name=vmname, label='red')
  257. self.loop.run_until_complete(self.vm.create_on_disk())
  258. # start the VM manually, so it stays running after applying salt state
  259. self.loop.run_until_complete(self.vm.start())
  260. with open(os.path.join(self.salt_testdir, 'something.sls'), 'w') as f:
  261. f.write('/home/user/testfile:\n')
  262. f.write(' file.managed:\n')
  263. f.write(' - contents: |\n')
  264. f.write(' this is test\n')
  265. with open(os.path.join(self.salt_testdir, 'something.top'), 'w') as f:
  266. f.write('base:\n')
  267. f.write(' {}:\n'.format(vmname))
  268. f.write(' - test_salt.something\n')
  269. # enable so state.show_top will not be empty, otherwise qubesctl will
  270. # skip the VM; but we don't use state.highstate
  271. self.dom0_salt_call_json(['top.enable', 'test_salt.something'])
  272. state_output = self.salt_call(
  273. ['--skip-dom0', '--show-output', '--targets=' + vmname,
  274. 'state.sls', 'test_salt.something'])
  275. expected_output = vmname + ':\n'
  276. self.assertTrue(state_output.startswith(expected_output),
  277. 'Full output: ' + state_output)
  278. state_id = 'file_|-/home/user/testfile_|-/home/user/testfile_|-managed'
  279. # drop the header
  280. state_output_json = json.loads(state_output[len(expected_output):])
  281. state_output_json = state_output_json[vmname][state_id]
  282. try:
  283. del state_output_json['duration']
  284. del state_output_json['start_time']
  285. except KeyError:
  286. pass
  287. try:
  288. del state_output_json['pchanges']
  289. except KeyError:
  290. pass
  291. try:
  292. # older salt do not report this
  293. self.assertEqual(state_output_json['__id__'], '/home/user/testfile')
  294. del state_output_json['__id__']
  295. except KeyError:
  296. pass
  297. try:
  298. # or sls file
  299. self.assertEqual(state_output_json['__sls__'],
  300. 'test_salt.something')
  301. del state_output_json['__sls__']
  302. except KeyError:
  303. pass
  304. # different output depending on salt version
  305. expected_output = {
  306. '__run_num__': 0,
  307. 'changes': {
  308. 'diff': 'New file',
  309. },
  310. 'name': '/home/user/testfile',
  311. 'comment': 'File /home/user/testfile updated',
  312. 'result': True,
  313. }
  314. self.assertEqual(state_output_json, expected_output)
  315. stdout, stderr = self.loop.run_until_complete(self.vm.run_for_stdio(
  316. 'cat /home/user/testfile'))
  317. self.assertEqual(stdout, b'this is test\n')
  318. self.assertEqual(stderr, b'')
  319. def test_001_multi_state_highstate(self):
  320. vmname = self.make_vm_name('target')
  321. self.vm = self.app.add_new_vm('AppVM', name=vmname, label='red')
  322. self.loop.run_until_complete(self.vm.create_on_disk())
  323. # start the VM manually, so it stays running after applying salt state
  324. self.loop.run_until_complete(self.vm.start())
  325. states = ('something', 'something2')
  326. for state in states:
  327. with open(os.path.join(self.salt_testdir, state + '.sls'), 'w') as f:
  328. f.write('/home/user/{}:\n'.format(state))
  329. f.write(' file.managed:\n')
  330. f.write(' - contents: |\n')
  331. f.write(' this is test\n')
  332. with open(os.path.join(self.salt_testdir, state + '.top'), 'w') as f:
  333. f.write('base:\n')
  334. f.write(' {}:\n'.format(vmname))
  335. f.write(' - test_salt.{}\n'.format(state))
  336. self.dom0_salt_call_json(['top.enable', 'test_salt.something'])
  337. self.dom0_salt_call_json(['top.enable', 'test_salt.something2'])
  338. state_output = self.salt_call(
  339. ['--skip-dom0', '--show-output', '--targets=' + vmname,
  340. 'state.highstate'])
  341. expected_output = vmname + ':\n'
  342. self.assertTrue(state_output.startswith(expected_output),
  343. 'Full output: ' + state_output)
  344. state_output_json = json.loads(state_output[len(expected_output):])
  345. for state in states:
  346. state_id = \
  347. 'file_|-/home/user/{0}_|-/home/user/{0}_|-managed'.format(state)
  348. # drop the header
  349. self.assertIn(state_id, state_output_json[vmname])
  350. state_output_single = state_output_json[vmname][state_id]
  351. self.assertTrue(state_output_single['result'])
  352. self.assertNotEqual(state_output_single['changes'], {})
  353. stdout, stderr = self.loop.run_until_complete(self.vm.run_for_stdio(
  354. 'cat /home/user/' + state))
  355. self.assertEqual(stdout, b'this is test\n')
  356. self.assertEqual(stderr, b'')
  357. def create_testcases_for_templates():
  358. return qubes.tests.create_testcases_for_templates('TC_10_VMSalt',
  359. SaltVMTestMixin, qubes.tests.SystemTestCase,
  360. module=sys.modules[__name__])
  361. def load_tests(loader, tests, pattern):
  362. tests.addTests(loader.loadTestsFromNames(
  363. create_testcases_for_templates()))
  364. return tests
  365. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)