Revision a5728081 test/ganeti.utils_unittest.py

b/test/ganeti.utils_unittest.py
38 38
import testutils
39 39
from ganeti import constants
40 40
from ganeti import utils
41
from ganeti import errors
41 42
from ganeti.utils import IsProcessAlive, RunCmd, \
42 43
     RemoveFile, CheckDict, MatchNameComponent, FormatUnit, \
43 44
     ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
44 45
     ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \
45 46
     SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree, OwnIpAddress, \
46
     TailFile
47
     TailFile, ForceDictType
47 48

  
48 49
from ganeti.errors import LockError, UnitParseError, GenericError, \
49 50
     ProgrammerError
......
921 922
    self.failIf(f.NonMatching(["b12", "c"]))
922 923
    self.failUnless(f.NonMatching(["a", "1"]))
923 924

  
925
class TestForceDictType(unittest.TestCase):
926
  """Test case for ForceDictType"""
927

  
928
  def setUp(self):
929
    self.key_types = {
930
      'a': constants.VTYPE_INT,
931
      'b': constants.VTYPE_BOOL,
932
      'c': constants.VTYPE_STRING,
933
      'd': constants.VTYPE_SIZE,
934
      }
935

  
936
  def _fdt(self, dict, allowed_values=None):
937
    if allowed_values is None:
938
      ForceDictType(dict, self.key_types)
939
    else:
940
      ForceDictType(dict, self.key_types, allowed_values=allowed_values)
941

  
942
    return dict
943

  
944
  def testSimpleDict(self):
945
    self.assertEqual(self._fdt({}), {})
946
    self.assertEqual(self._fdt({'a': 1}), {'a': 1})
947
    self.assertEqual(self._fdt({'a': '1'}), {'a': 1})
948
    self.assertEqual(self._fdt({'a': 1, 'b': 1}), {'a':1, 'b': True})
949
    self.assertEqual(self._fdt({'b': 1, 'c': 'foo'}), {'b': True, 'c': 'foo'})
950
    self.assertEqual(self._fdt({'b': 1, 'c': False}), {'b': True, 'c': ''})
951
    self.assertEqual(self._fdt({'b': 'false'}), {'b': False})
952
    self.assertEqual(self._fdt({'b': 'False'}), {'b': False})
953
    self.assertEqual(self._fdt({'b': 'true'}), {'b': True})
954
    self.assertEqual(self._fdt({'b': 'True'}), {'b': True})
955
    self.assertEqual(self._fdt({'d': '4'}), {'d': 4})
956
    self.assertEqual(self._fdt({'d': '4M'}), {'d': 4})
957

  
958
  def testErrors(self):
959
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'a': 'astring'})
960
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'c': True})
961
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': 'astring'})
962
    self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'})
963

  
924 964

  
925 965
if __name__ == '__main__':
926 966
  unittest.main()

Also available in: Unified diff