Revision 2241e2b9

b/Makefile.am
202 202
	test/ganeti.workerpool_unittest.py \
203 203
	test/ganeti.rapi.resources_unittest.py \
204 204
	test/ganeti.http_unittest.py \
205
	test/ganeti.constants_unittest.py
205
	test/ganeti.constants_unittest.py \
206
	test/ganeti.cli_unittest.py
206 207

  
207 208
nodist_TESTS =
208 209

  
b/lib/cli.py
707 707
    return '?'
708 708
  sec, usec = ts
709 709
  return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
710

  
711

  
712
def ParseTimespec(value):
713
  """Parse a time specification.
714

  
715
  The following suffixed will be recognized:
716

  
717
    - s: seconds
718
    - m: minutes
719
    - h: hours
720
    - d: day
721
    - w: weeks
722

  
723
  Without any suffix, the value will be taken to be in seconds.
724

  
725
  """
726
  value = str(value)
727
  if not value:
728
    raise errors.OpPrereqError("Empty time specification passed")
729
  suffix_map = {
730
    's': 1,
731
    'm': 60,
732
    'h': 3600,
733
    'd': 86400,
734
    'w': 604800,
735
    }
736
  if value[-1] not in suffix_map:
737
    try:
738
      value = int(value)
739
    except ValueError:
740
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
741
  else:
742
    multiplier = suffix_map[value[-1]]
743
    value = value[:-1]
744
    if not value: # no data left after stripping the suffix
745
      raise errors.OpPrereqError("Invalid time specification (only"
746
                                 " suffix passed)")
747
    try:
748
      value = int(value) * multiplier
749
    except ValueError:
750
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
751
  return value
b/test/ganeti.cli_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2008 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for unittesting the cli module"""
23

  
24
import unittest
25

  
26
import ganeti
27
import testutils
28
from ganeti import constants
29
from ganeti import cli
30
from ganeti.errors import OpPrereqError
31

  
32
class TestParseTimespec(unittest.TestCase):
33
  """Testing case for ParseTimespec"""
34

  
35
  def testValidTimes(self):
36
    """Test valid timespecs"""
37
    test_data = [
38
      ('1s', 1),
39
      ('1', 1),
40
      ('1m', 60),
41
      ('1h', 60 * 60),
42
      ('1d', 60 * 60 * 24),
43
      ('1w', 60 * 60 * 24 * 7),
44
      ('4h', 4 * 60 * 60),
45
      ('61m', 61 * 60),
46
      ]
47
    for value, expected_result in test_data:
48
      self.failUnlessEqual(cli.ParseTimespec(value), expected_result)
49

  
50
  def testInvalidTime(self):
51
    """Test invalid timespecs"""
52
    test_data = [
53
      '1y',
54
      '',
55
      'aaa',
56
      's',
57
      ]
58
    for value in test_data:
59
      self.failUnlessRaises(OpPrereqError, cli.ParseTimespec, value)
60

  
61

  
62
if __name__ == '__main__':
63
  unittest.main()

Also available in: Unified diff