Revision 8b46606c

b/lib/cli.py
54 54
           "GetOnlineNodes", "JobExecutor", "SYNC_OPT", "CONFIRM_OPT",
55 55
           ]
56 56

  
57
NO_PREFIX = "no_"
58
UN_PREFIX = "-"
57 59

  
58 60
def _ExtractTagsObject(opts, args):
59 61
  """Extract the tag type object.
......
256 258
  @raises errors.ParameterError: if there are duplicate keys
257 259

  
258 260
  """
259
  NO_PREFIX = "no_"
260
  UN_PREFIX = "-"
261 261
  kv_dict = {}
262 262
  if data:
263 263
    for elem in data.split(","):
......
282 282

  
283 283
  """
284 284
  if ":" not in value:
285
    retval =  (value, {})
285
    ident, rest = value, ''
286 286
  else:
287 287
    ident, rest = value.split(":", 1)
288

  
289
  if ident.startswith(NO_PREFIX):
290
    if rest:
291
      msg = "Cannot pass options when removing parameter groups: %s" % value
292
      raise errors.ParameterError(msg)
293
    retval = (ident[len(NO_PREFIX):], False)
294
  elif ident.startswith(UN_PREFIX):
295
    if rest:
296
      msg = "Cannot pass options when removing parameter groups: %s" % value
297
      raise errors.ParameterError(msg)
298
    retval = (ident[len(UN_PREFIX):], None)
299
  else:
288 300
    kv_dict = _SplitKeyVal(opt, rest)
289 301
    retval = (ident, kv_dict)
290 302
  return retval
b/lib/cmdlib.py
5887 5887
      else:
5888 5888
        if not isinstance(disk_op, int):
5889 5889
          raise errors.OpPrereqError("Invalid disk index")
5890
        if not isinstance(disk_dict, dict):
5891
          msg = "Invalid disk value: expected dict, got '%s'" % disk_dict
5892
          raise errors.OpPrereqError(msg)
5893

  
5890 5894
      if disk_op == constants.DDM_ADD:
5891 5895
        mode = disk_dict.setdefault('mode', constants.DISK_RDWR)
5892 5896
        if mode not in constants.DISK_ACCESS_SET:
......
5921 5925
      else:
5922 5926
        if not isinstance(nic_op, int):
5923 5927
          raise errors.OpPrereqError("Invalid nic index")
5928
        if not isinstance(nic_dict, dict):
5929
          msg = "Invalid nic value: expected dict, got '%s'" % nic_dict
5930
          raise errors.OpPrereqError(msg)
5924 5931

  
5925 5932
      # nic_dict should be a dict
5926 5933
      nic_ip = nic_dict.get('ip', None)
b/lib/utils.py
388 388
  if allowed_values is None:
389 389
    allowed_values = []
390 390

  
391
  if not isinstance(target, dict):
392
    msg = "Expected dictionary, got '%s'" % target
393
    raise errors.TypeEnforcementError(msg)
394

  
391 395
  for key in target:
392 396
    if key not in key_types:
393 397
      msg = "Unknown key '%s'" % key
b/scripts/gnt-backup
104 104
    nics = [{}] * nic_max
105 105
    for nidx, ndict in opts.nics.items():
106 106
      nidx = int(nidx)
107
      if not isinstance(ndict, dict):
108
        msg = "Invalid nic/%d value: expected dict, got %s" % (nidx, ndict)
109
        raise errors.OpPrereqError(msg)
107 110
      nics[nidx] = ndict
108 111
  elif opts.no_nics:
109 112
    # no nics
......
132 135
    disks = [{}] * disk_max
133 136
    for didx, ddict in opts.disks:
134 137
      didx = int(didx)
135
      if "size" not in ddict:
138
      if not isinstance(ddict, dict):
139
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
140
        raise errors.OpPrereqError(msg)
141
      elif "size" not in ddict:
136 142
        raise errors.OpPrereqError("Missing size for disk %d" % didx)
137 143
      try:
138 144
        ddict["size"] = utils.ParseUnit(ddict["size"])
b/scripts/gnt-instance
302 302
    nics = [{}] * nic_max
303 303
    for nidx, ndict in opts.nics:
304 304
      nidx = int(nidx)
305
      if not isinstance(ndict, dict):
306
        msg = "Invalid nic/%d value: expected dict, got %s" % (nidx, ndict)
307
        raise errors.OpPrereqError(msg)
305 308
      nics[nidx] = ndict
306 309
  elif opts.no_nics:
307 310
    # no nics
......
330 333
    disks = [{}] * disk_max
331 334
    for didx, ddict in opts.disks:
332 335
      didx = int(didx)
333
      if "size" not in ddict:
336
      if not isinstance(ddict, dict):
337
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
338
        raise errors.OpPrereqError(msg)
339
      elif "size" not in ddict:
334 340
        raise errors.OpPrereqError("Missing size for disk %d" % didx)
335 341
      try:
336 342
        ddict["size"] = utils.ParseUnit(ddict["size"])
b/test/ganeti.cli_unittest.py
79 79
    """Test how we handle splitting an empty string"""
80 80
    self.failUnlessEqual(cli._SplitKeyVal("option", ""), {})
81 81

  
82
class TestIdentKeyVal(unittest.TestCase):
83
  """Testing case for cli.check_ident_key_val"""
84

  
85
  def testIdentKeyVal(self):
86
    """Test identkeyval"""
87
    def cikv(value):
88
      return cli.check_ident_key_val("option", "opt", value)
89

  
90
    self.assertEqual(cikv("foo:bar"), ("foo", {"bar": True}))
91
    self.assertEqual(cikv("foo:bar=baz"), ("foo", {"bar": "baz"}))
92
    self.assertEqual(cikv("bar:b=c,c=a"), ("bar", {"b": "c", "c": "a"}))
93
    self.assertEqual(cikv("no_bar"), ("bar", False))
94
    self.assertRaises(ParameterError, cikv, "no_bar:foo")
95
    self.assertRaises(ParameterError, cikv, "no_bar:foo=baz")
96
    self.assertEqual(cikv("-foo"), ("foo", None))
97
    self.assertRaises(ParameterError, cikv, "-foo:a=c")
98

  
82 99

  
83 100
class TestToStream(unittest.TestCase):
84 101
  """Thes the ToStream functions"""

Also available in: Unified diff