init.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with this program; if not, see <http://www.gnu.org/licenses/>.
  19. import argparse
  20. import qubesadmin
  21. import qubesadmin.tools
  22. import qubesadmin.tests
  23. class TC_00_PropertyAction(qubesadmin.tests.QubesTestCase):
  24. def test_000_default(self):
  25. parser = argparse.ArgumentParser()
  26. parser.add_argument('--property', '-p',
  27. action=qubesadmin.tools.PropertyAction)
  28. parser.set_defaults(properties={'defaultprop': 'defaultvalue'})
  29. args = parser.parse_args([])
  30. self.assertDictContainsSubset(
  31. {'defaultprop': 'defaultvalue'}, args.properties)
  32. def test_001_set_prop(self):
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument('--property', '-p',
  35. action=qubesadmin.tools.PropertyAction)
  36. args = parser.parse_args(['-p', 'testprop=testvalue'])
  37. self.assertDictContainsSubset(
  38. {'testprop': 'testvalue'}, args.properties)
  39. def test_002_set_prop_2(self):
  40. parser = argparse.ArgumentParser()
  41. parser.add_argument('--property', '-p',
  42. action=qubesadmin.tools.PropertyAction)
  43. parser.set_defaults(properties={'defaultprop': 'defaultvalue'})
  44. args = parser.parse_args(
  45. ['-p', 'testprop=testvalue', '-p', 'testprop2=testvalue2'])
  46. self.assertDictContainsSubset(
  47. {'testprop': 'testvalue', 'testprop2': 'testvalue2'},
  48. args.properties)
  49. def test_003_set_prop_with_default(self):
  50. parser = argparse.ArgumentParser()
  51. parser.add_argument('--property', '-p',
  52. action=qubesadmin.tools.PropertyAction)
  53. parser.set_defaults(properties={'defaultprop': 'defaultvalue'})
  54. args = parser.parse_args(['-p', 'testprop=testvalue'])
  55. self.assertDictContainsSubset(
  56. {'testprop': 'testvalue', 'defaultprop': 'defaultvalue'},
  57. args.properties)
  58. def test_003_set_prop_override_default(self):
  59. # pylint: disable=invalid-name
  60. parser = argparse.ArgumentParser()
  61. parser.add_argument('--property', '-p',
  62. action=qubesadmin.tools.PropertyAction)
  63. parser.set_defaults(properties={'testprop': 'defaultvalue'})
  64. args = parser.parse_args(['-p', 'testprop=testvalue'])
  65. self.assertDictContainsSubset(
  66. {'testprop': 'testvalue'},
  67. args.properties)
  68. class TC_01_SinglePropertyAction(qubesadmin.tests.QubesTestCase):
  69. def test_000_help(self):
  70. parser = argparse.ArgumentParser()
  71. action = parser.add_argument('--testprop', '-T',
  72. action=qubesadmin.tools.SinglePropertyAction)
  73. self.assertIn('testprop', action.help)
  74. def test_001_help_const(self):
  75. parser = argparse.ArgumentParser()
  76. action = parser.add_argument('--testprop', '-T',
  77. action=qubesadmin.tools.SinglePropertyAction,
  78. const='testvalue')
  79. self.assertIn('testvalue', action.help)
  80. def test_100_default(self):
  81. parser = argparse.ArgumentParser()
  82. parser.add_argument('--testprop', '-T',
  83. action=qubesadmin.tools.SinglePropertyAction)
  84. parser.set_defaults(properties={'testprop': 'defaultvalue'})
  85. args = parser.parse_args([])
  86. self.assertDictContainsSubset(
  87. {'testprop': 'defaultvalue'}, args.properties)
  88. def test_101_set_prop(self):
  89. parser = argparse.ArgumentParser()
  90. parser.add_argument('--testprop', '-T',
  91. action=qubesadmin.tools.SinglePropertyAction)
  92. args = parser.parse_args(['-T', 'testvalue'])
  93. self.assertDictContainsSubset(
  94. {'testprop': 'testvalue'}, args.properties)
  95. def test_102_set_prop_dest(self):
  96. parser = argparse.ArgumentParser()
  97. parser.add_argument('--testprop', '-T', dest='otherprop',
  98. action=qubesadmin.tools.SinglePropertyAction)
  99. args = parser.parse_args(['-T', 'testvalue'])
  100. self.assertDictContainsSubset(
  101. {'otherprop': 'testvalue'}, args.properties)
  102. def test_103_set_prop_const(self):
  103. parser = argparse.ArgumentParser()
  104. parser.add_argument('--testprop', '-T',
  105. action=qubesadmin.tools.SinglePropertyAction,
  106. const='testvalue')
  107. args = parser.parse_args(['-T'])
  108. self.assertDictContainsSubset(
  109. {'testprop': 'testvalue'}, args.properties)
  110. def test_104_set_prop_positional(self):
  111. parser = argparse.ArgumentParser()
  112. parser.add_argument('testprop',
  113. action=qubesadmin.tools.SinglePropertyAction)
  114. args = parser.parse_args(['testvalue'])
  115. self.assertDictContainsSubset(
  116. {'testprop': 'testvalue'}, args.properties)