devices.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access,pointless-statement
  4. #
  5. # The Qubes OS Project, https://www.qubes-os.org/
  6. #
  7. # Copyright (C) 2015-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  8. # Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import qubes.devices
  25. import qubes.tests
  26. class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
  27. def setUp(self):
  28. self.emitter = qubes.tests.TestEmitter()
  29. self.collection = qubes.devices.DeviceCollection(self.emitter, 'testclass')
  30. def test_000_init(self):
  31. self.assertFalse(self.collection._set)
  32. def test_001_attach(self):
  33. self.collection.attach('testdev')
  34. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  35. self.assertEventFired(self.emitter, 'device-attach:testclass')
  36. self.assertEventNotFired(self.emitter, 'device-pre-detach:testclass')
  37. self.assertEventNotFired(self.emitter, 'device-detach:testclass')
  38. def test_002_detach(self):
  39. self.collection.attach('testdev')
  40. self.collection.detach('testdev')
  41. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  42. self.assertEventFired(self.emitter, 'device-attach:testclass')
  43. self.assertEventFired(self.emitter, 'device-pre-detach:testclass')
  44. self.assertEventFired(self.emitter, 'device-detach:testclass')
  45. def test_010_empty_detach(self):
  46. with self.assertRaises(LookupError):
  47. self.collection.detach('testdev')
  48. def test_011_double_attach(self):
  49. self.collection.attach('testdev')
  50. with self.assertRaises(LookupError):
  51. self.collection.attach('testdev')
  52. def test_012_double_detach(self):
  53. self.collection.attach('testdev')
  54. self.collection.detach('testdev')
  55. with self.assertRaises(LookupError):
  56. self.collection.detach('testdev')
  57. class TC_01_DeviceManager(qubes.tests.QubesTestCase):
  58. def setUp(self):
  59. self.emitter = qubes.tests.TestEmitter()
  60. self.manager = qubes.devices.DeviceManager(self.emitter)
  61. def test_000_init(self):
  62. self.assertEqual(self.manager, {})
  63. def test_001_missing(self):
  64. self.manager['testclass'].attach('testdev')
  65. self.assertEventFired(self.emitter, 'device-attach:testclass')