tags.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 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 qubesadmin.tests
  21. import qubesadmin.tags
  22. class TC_00_Tags(qubesadmin.tests.QubesTestCase):
  23. def setUp(self):
  24. super(TC_00_Tags, self).setUp()
  25. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  26. b'0\0test-vm class=AppVM state=Running\n' \
  27. b'test-vm2 class=AppVM state=Running\n' \
  28. b'test-vm3 class=AppVM state=Running\n'
  29. self.vm = self.app.domains['test-vm']
  30. self.tags = qubesadmin.tags.Tags(self.vm)
  31. def test_000_list(self):
  32. self.app.expected_calls[
  33. ('test-vm', 'admin.vm.tag.List', None, None)] = \
  34. b'0\0tag1\ntag2\n'
  35. self.assertEqual(sorted(self.tags),
  36. ['tag1', 'tag2'])
  37. self.assertAllCalled()
  38. def test_010_get(self):
  39. self.app.expected_calls[
  40. ('test-vm', 'admin.vm.tag.Get', 'tag1', None)] = \
  41. b'0\x001'
  42. self.assertIn('tag1', self.tags)
  43. self.assertAllCalled()
  44. def test_011_get_missing(self):
  45. self.app.expected_calls[
  46. ('test-vm', 'admin.vm.tag.Get', 'tag1', None)] = \
  47. b'0\x000'
  48. self.assertNotIn('tag1', self.tags)
  49. self.assertAllCalled()
  50. def test_020_set(self):
  51. self.app.expected_calls[
  52. ('test-vm', 'admin.vm.tag.Set', 'tag1', None)] = b'0\0'
  53. self.tags.add('tag1')
  54. self.assertAllCalled()
  55. def test_030_update(self):
  56. self.app.expected_calls[
  57. ('test-vm', 'admin.vm.tag.Set', 'tag1', None)] = b'0\0'
  58. self.app.expected_calls[
  59. ('test-vm', 'admin.vm.tag.Set', 'tag2', None)] = b'0\0'
  60. self.tags.update(['tag1', 'tag2'])
  61. self.assertAllCalled()
  62. def test_031_update_from_other(self):
  63. self.app.expected_calls[
  64. ('test-vm2', 'admin.vm.tag.List', None, None)] = \
  65. b'0\0tag3\ntag4\n'
  66. self.app.expected_calls[
  67. ('test-vm', 'admin.vm.tag.Set', 'tag3', None)] = b'0\0'
  68. self.app.expected_calls[
  69. ('test-vm', 'admin.vm.tag.Set', 'tag4', None)] = b'0\0'
  70. self.tags.update(self.app.domains['test-vm2'].tags)
  71. self.assertAllCalled()
  72. def test_040_remove(self):
  73. self.app.expected_calls[
  74. ('test-vm', 'admin.vm.tag.Remove', 'tag1', None)] = \
  75. b'0\0'
  76. self.tags.remove('tag1')
  77. self.assertAllCalled()
  78. def test_040_remove_missing(self):
  79. self.app.expected_calls[
  80. ('test-vm', 'admin.vm.tag.Remove', 'tag1', None)] = \
  81. b'2\0QubesTagNotFoundError\0\0Tag not set for domain test-vm: ' \
  82. b'tag1\0'
  83. with self.assertRaises(KeyError):
  84. self.tags.remove('tag1')
  85. self.assertAllCalled()
  86. def test_050_discard(self):
  87. self.app.expected_calls[
  88. ('test-vm', 'admin.vm.tag.Remove', 'tag1', None)] = \
  89. b'0\0'
  90. self.tags.discard('tag1')
  91. self.assertAllCalled()
  92. def test_051_discard_missing(self):
  93. self.app.expected_calls[
  94. ('test-vm', 'admin.vm.tag.Remove', 'tag1', None)] = \
  95. b'2\0QubesTagNotFoundError\0\0Tag not set for domain test-vm: ' \
  96. b'tag1\0'
  97. self.tags.discard('tag1')
  98. self.assertAllCalled()