qvm_clone.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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={})
  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_004_pool(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. qubesadmin.tools.qvm_clone.main(['-P', 'some-pool', 'test-vm', 'new-vm'],
  48. app=self.app)
  49. self.app.clone_vm.assert_called_with(self.app.domains['test-vm'],
  50. 'new-vm', new_cls=None, pool='some-pool', pools={})
  51. self.assertAllCalled()
  52. def test_005_pools(self):
  53. self.app.clone_vm = mock.Mock()
  54. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  55. b'0\x00new-vm class=AppVM state=Halted\n' \
  56. b'test-vm class=AppVM state=Halted\n'
  57. qubesadmin.tools.qvm_clone.main(['--pool', 'private=some-pool',
  58. '--pool', 'volatile=other-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=None, pools={'private': 'some-pool',
  62. 'volatile': 'other-pool'})
  63. self.assertAllCalled()
  64. def test_006_new_cls(self):
  65. self.app.clone_vm = mock.Mock()
  66. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  67. b'0\x00new-vm class=AppVM state=Halted\n' \
  68. b'test-vm class=AppVM state=Halted\n'
  69. qubesadmin.tools.qvm_clone.main(['--class', 'StandaloneVM',
  70. 'test-vm', 'new-vm'],
  71. app=self.app)
  72. self.app.clone_vm.assert_called_with(self.app.domains['test-vm'],
  73. 'new-vm', new_cls='StandaloneVM', pool=None, pools={})
  74. self.assertAllCalled()