qvm_clone.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 qubesadmin.tests
  21. import qubesadmin.tests.tools
  22. import qubesadmin.tools.qvm_clone
  23. from unittest import mock
  24. class TC_00_qvm_clone(qubesadmin.tests.QubesTestCase):
  25. def test_000_simple(self):
  26. self.app.clone_vm = mock.Mock()
  27. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  28. b'0\x00new-vm class=AppVM state=Halted\n' \
  29. b'test-vm class=AppVM state=Halted\n'
  30. qubesadmin.tools.qvm_clone.main(['test-vm', 'new-vm'], app=self.app)
  31. self.app.clone_vm.assert_called_with(self.app.domains['test-vm'],
  32. 'new-vm', new_cls=None, pool=None, pools={}, ignore_errors=False)
  33. self.assertAllCalled()
  34. def test_001_missing_vm(self):
  35. self.app.clone_vm = mock.Mock()
  36. with self.assertRaises(SystemExit):
  37. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  38. qubesadmin.tools.qvm_clone.main(['test-vm'], app=self.app)
  39. self.assertIn('NAME', stderr.getvalue())
  40. self.assertFalse(self.app.clone_vm.called)
  41. self.assertAllCalled()
  42. def test_002_ignore_errors(self):
  43. self.app.clone_vm = mock.Mock()
  44. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  45. b'0\x00new-vm class=AppVM state=Halted\n' \
  46. b'test-vm class=AppVM state=Halted\n'
  47. test_args = ['test-vm', 'new-vm', '--ignore-errors']
  48. qubesadmin.tools.qvm_clone.main(test_args, app=self.app)
  49. self.app.clone_vm.assert_called_with(self.app.domains['test-vm'],
  50. 'new-vm', new_cls=None, pool=None, pools={},
  51. ignore_errors=True)
  52. self.assertAllCalled()
  53. def test_004_pool(self):
  54. self.app.clone_vm = mock.Mock()
  55. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  56. b'0\x00new-vm class=AppVM state=Halted\n' \
  57. b'test-vm class=AppVM state=Halted\n'
  58. qubesadmin.tools.qvm_clone.main(['-P', 'some-pool', 'test-vm', 'new-vm'],
  59. app=self.app)
  60. self.app.clone_vm.assert_called_with(self.app.domains['test-vm'],
  61. 'new-vm', new_cls=None, pool='some-pool', pools={}, ignore_errors=False)
  62. self.assertAllCalled()
  63. def test_005_pools(self):
  64. self.app.clone_vm = mock.Mock()
  65. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  66. b'0\x00new-vm class=AppVM state=Halted\n' \
  67. b'test-vm class=AppVM state=Halted\n'
  68. qubesadmin.tools.qvm_clone.main(['--pool', 'private=some-pool',
  69. '--pool', 'volatile=other-pool', 'test-vm', 'new-vm'],
  70. app=self.app)
  71. self.app.clone_vm.assert_called_with(self.app.domains['test-vm'],
  72. 'new-vm', new_cls=None, pool=None, pools={'private': 'some-pool',
  73. 'volatile': 'other-pool'}, ignore_errors=False)
  74. self.assertAllCalled()
  75. def test_006_new_cls(self):
  76. self.app.clone_vm = mock.Mock()
  77. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  78. b'0\x00new-vm class=AppVM state=Halted\n' \
  79. b'test-vm class=AppVM state=Halted\n'
  80. qubesadmin.tools.qvm_clone.main(['--class', 'StandaloneVM',
  81. 'test-vm', 'new-vm'],
  82. app=self.app)
  83. self.app.clone_vm.assert_called_with(self.app.domains['test-vm'],
  84. 'new-vm', new_cls='StandaloneVM', pool=None, pools={},
  85. ignore_errors=False)
  86. self.assertAllCalled()