2011-05-27 13:17:22 -05:00
|
|
|
# Authors:
|
|
|
|
# Jan Cholasta <jcholast@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
Test the `ipapython/ipautil.py` module.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import nose
|
|
|
|
|
|
|
|
from ipapython import ipautil
|
|
|
|
|
|
|
|
|
2014-10-23 06:31:45 -05:00
|
|
|
def make_ipaddress_checker(addr, words=None, prefixlen=None):
|
|
|
|
def check_ipaddress():
|
2011-05-27 13:17:22 -05:00
|
|
|
try:
|
|
|
|
ip = ipautil.CheckedIPAddress(addr, match_local=False)
|
|
|
|
assert ip.words == words and ip.prefixlen == prefixlen
|
|
|
|
except:
|
|
|
|
assert words is None and prefixlen is None
|
2014-10-23 06:31:45 -05:00
|
|
|
check_ipaddress.description = "Test IP address parsing and verification (%s)" % addr
|
|
|
|
return check_ipaddress
|
|
|
|
|
2011-05-27 13:17:22 -05:00
|
|
|
|
|
|
|
def test_ip_address():
|
|
|
|
addrs = [
|
|
|
|
('10.11.12.13', (10, 11, 12, 13), 8),
|
|
|
|
('10.11.12.13/14', (10, 11, 12, 13), 14),
|
2012-03-19 07:52:11 -05:00
|
|
|
('10.11.12.13%zoneid',),
|
|
|
|
('10.11.12.13%zoneid/14',),
|
2011-05-27 13:17:22 -05:00
|
|
|
('10.11.12.1337',),
|
|
|
|
('10.11.12.13/33',),
|
|
|
|
('127.0.0.1',),
|
2011-05-27 06:51:21 -05:00
|
|
|
('241.1.2.3',),
|
|
|
|
('169.254.1.2',),
|
|
|
|
('10.11.12.0/24',),
|
|
|
|
('224.5.6.7',),
|
|
|
|
('10.11.12.255/24',),
|
2011-05-27 13:17:22 -05:00
|
|
|
|
|
|
|
('2001::1', (0x2001, 0, 0, 0, 0, 0, 0, 1), 64),
|
|
|
|
('2001::1/72', (0x2001, 0, 0, 0, 0, 0, 0, 1), 72),
|
2012-03-19 07:52:11 -05:00
|
|
|
('2001::1%zoneid', (0x2001, 0, 0, 0, 0, 0, 0, 1), 64),
|
|
|
|
('2001::1%zoneid/72',),
|
2011-05-27 13:17:22 -05:00
|
|
|
('2001::1beef',),
|
|
|
|
('2001::1/129',),
|
|
|
|
('::1',),
|
2011-05-27 06:51:21 -05:00
|
|
|
('6789::1',),
|
|
|
|
('fe89::1',),
|
|
|
|
('2001::/64',),
|
|
|
|
('ff01::1',),
|
2011-05-27 13:17:22 -05:00
|
|
|
|
|
|
|
('junk',)
|
|
|
|
]
|
|
|
|
|
|
|
|
for addr in addrs:
|
2014-10-23 06:31:45 -05:00
|
|
|
yield make_ipaddress_checker(*addr)
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
class TestCIDict(object):
|
|
|
|
def setup(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
self.cidict = ipautil.CIDict()
|
|
|
|
self.cidict["Key1"] = "val1"
|
|
|
|
self.cidict["key2"] = "val2"
|
|
|
|
self.cidict["KEY3"] = "VAL3"
|
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
def test_init(self):
|
|
|
|
cidict = ipautil.CIDict()
|
|
|
|
assert dict(cidict.items()) == {}
|
|
|
|
cidict = ipautil.CIDict([('a', 2), ('b', 3), ('C', 4)])
|
|
|
|
assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
|
|
|
|
cidict = ipautil.CIDict([('a', 2), ('b', None)], b=3, C=4)
|
|
|
|
assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
|
|
|
|
cidict = ipautil.CIDict({'a': 2, 'b': None}, b=3, C=4)
|
|
|
|
assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
|
|
|
|
cidict = ipautil.CIDict(a=2, b=3, C=4)
|
|
|
|
assert dict(cidict.items()) == {'a': 2, 'b': 3, 'C': 4}
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_len(self):
|
|
|
|
nose.tools.assert_equal(3, len(self.cidict))
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_getitem(self):
|
|
|
|
nose.tools.assert_equal("val1", self.cidict["Key1"])
|
|
|
|
nose.tools.assert_equal("val1", self.cidict["key1"])
|
|
|
|
nose.tools.assert_equal("val2", self.cidict["KEY2"])
|
|
|
|
nose.tools.assert_equal("VAL3", self.cidict["key3"])
|
|
|
|
nose.tools.assert_equal("VAL3", self.cidict["KEY3"])
|
|
|
|
with nose.tools.assert_raises(KeyError):
|
2012-04-20 06:03:16 -05:00
|
|
|
self.cidict["key4"]
|
2012-06-15 04:17:49 -05:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
nose.tools.assert_equal("val1", self.cidict.get("Key1"))
|
|
|
|
nose.tools.assert_equal("val1", self.cidict.get("key1"))
|
|
|
|
nose.tools.assert_equal("val2", self.cidict.get("KEY2"))
|
|
|
|
nose.tools.assert_equal("VAL3", self.cidict.get("key3"))
|
|
|
|
nose.tools.assert_equal("VAL3", self.cidict.get("KEY3"))
|
|
|
|
nose.tools.assert_equal("default", self.cidict.get("key4", "default"))
|
|
|
|
|
|
|
|
def test_setitem(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
self.cidict["key4"] = "val4"
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal("val4", self.cidict["key4"])
|
2012-04-20 06:03:16 -05:00
|
|
|
self.cidict["KEY4"] = "newval4"
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal("newval4", self.cidict["key4"])
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_del(self):
|
|
|
|
assert self.cidict.has_key("Key1")
|
2012-04-20 06:03:16 -05:00
|
|
|
del(self.cidict["Key1"])
|
2012-06-15 04:17:49 -05:00
|
|
|
assert not self.cidict.has_key("Key1")
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
assert self.cidict.has_key("key2")
|
2012-04-20 06:03:16 -05:00
|
|
|
del(self.cidict["KEY2"])
|
2012-06-15 04:17:49 -05:00
|
|
|
assert not self.cidict.has_key("key2")
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_clear(self):
|
|
|
|
nose.tools.assert_equal(3, len(self.cidict))
|
2012-04-20 06:03:16 -05:00
|
|
|
self.cidict.clear()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(0, len(self.cidict))
|
2014-12-16 07:45:37 -06:00
|
|
|
assert self.cidict == {}
|
|
|
|
assert self.cidict.keys() == []
|
|
|
|
assert self.cidict.values() == []
|
|
|
|
assert self.cidict.items() == []
|
|
|
|
assert self.cidict._keys == {}
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_copy(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
copy = self.cidict.copy()
|
2013-02-05 09:24:46 -06:00
|
|
|
assert copy == self.cidict
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(copy))
|
|
|
|
assert copy.has_key("Key1")
|
2013-02-05 09:24:46 -06:00
|
|
|
assert copy.has_key("key1")
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal("val1", copy["Key1"])
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_haskey(self):
|
|
|
|
assert self.cidict.has_key("KEY1")
|
|
|
|
assert self.cidict.has_key("key2")
|
|
|
|
assert self.cidict.has_key("key3")
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
assert not self.cidict.has_key("Key4")
|
|
|
|
|
|
|
|
def test_contains(self):
|
|
|
|
assert "KEY1" in self.cidict
|
|
|
|
assert "key2" in self.cidict
|
|
|
|
assert "key3" in self.cidict
|
|
|
|
|
|
|
|
assert "Key4" not in self.cidict
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_items(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
items = self.cidict.items()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(items))
|
2012-04-20 06:03:16 -05:00
|
|
|
items_set = set(items)
|
2012-06-15 04:17:49 -05:00
|
|
|
assert ("Key1", "val1") in items_set
|
|
|
|
assert ("key2", "val2") in items_set
|
|
|
|
assert ("KEY3", "VAL3") in items_set
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
assert self.cidict.items() == list(self.cidict.iteritems()) == zip(
|
|
|
|
self.cidict.iterkeys(), self.cidict.itervalues())
|
|
|
|
|
|
|
|
def test_iter(self):
|
|
|
|
items = []
|
|
|
|
assert list(self.cidict) == list(self.cidict.keys())
|
|
|
|
assert sorted(self.cidict) == sorted(['Key1', 'key2', 'KEY3'])
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_iteritems(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
items = []
|
|
|
|
for (k,v) in self.cidict.iteritems():
|
|
|
|
items.append((k,v))
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(items))
|
2012-04-20 06:03:16 -05:00
|
|
|
items_set = set(items)
|
2012-06-15 04:17:49 -05:00
|
|
|
assert ("Key1", "val1") in items_set
|
|
|
|
assert ("key2", "val2") in items_set
|
|
|
|
assert ("KEY3", "VAL3") in items_set
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_iterkeys(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
keys = []
|
|
|
|
for k in self.cidict.iterkeys():
|
|
|
|
keys.append(k)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(keys))
|
2012-04-20 06:03:16 -05:00
|
|
|
keys_set = set(keys)
|
2012-06-15 04:17:49 -05:00
|
|
|
assert "Key1" in keys_set
|
|
|
|
assert "key2" in keys_set
|
|
|
|
assert "KEY3" in keys_set
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_itervalues(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
values = []
|
|
|
|
for k in self.cidict.itervalues():
|
|
|
|
values.append(k)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(values))
|
2012-04-20 06:03:16 -05:00
|
|
|
values_set = set(values)
|
2012-06-15 04:17:49 -05:00
|
|
|
assert "val1" in values_set
|
|
|
|
assert "val2" in values_set
|
|
|
|
assert "VAL3" in values_set
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_keys(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
keys = self.cidict.keys()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(keys))
|
2012-04-20 06:03:16 -05:00
|
|
|
keys_set = set(keys)
|
2012-06-15 04:17:49 -05:00
|
|
|
assert "Key1" in keys_set
|
|
|
|
assert "key2" in keys_set
|
|
|
|
assert "KEY3" in keys_set
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
assert self.cidict.keys() == list(self.cidict.iterkeys())
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_values(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
values = self.cidict.values()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(values))
|
2012-04-20 06:03:16 -05:00
|
|
|
values_set = set(values)
|
2012-06-15 04:17:49 -05:00
|
|
|
assert "val1" in values_set
|
|
|
|
assert "val2" in values_set
|
|
|
|
assert "VAL3" in values_set
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
assert self.cidict.values() == list(self.cidict.itervalues())
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_update(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
newdict = { "KEY2": "newval2",
|
|
|
|
"key4": "val4" }
|
|
|
|
self.cidict.update(newdict)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(4, len(self.cidict))
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
items = self.cidict.items()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(4, len(items))
|
2012-04-20 06:03:16 -05:00
|
|
|
items_set = set(items)
|
2012-06-15 04:17:49 -05:00
|
|
|
assert ("Key1", "val1") in items_set
|
2012-04-20 06:03:16 -05:00
|
|
|
# note the update "overwrites" the case of the key2
|
2012-06-15 04:17:49 -05:00
|
|
|
assert ("KEY2", "newval2") in items_set
|
|
|
|
assert ("KEY3", "VAL3") in items_set
|
|
|
|
assert ("key4", "val4") in items_set
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
def test_update_dict_and_kwargs(self):
|
|
|
|
self.cidict.update({'a': 'va', 'b': None}, b='vb', key2='v2')
|
|
|
|
assert dict(self.cidict.items()) == {
|
|
|
|
'a': 'va', 'b': 'vb',
|
|
|
|
'Key1': 'val1', 'key2': 'v2', 'KEY3': 'VAL3'}
|
|
|
|
|
|
|
|
def test_update_list_and_kwargs(self):
|
|
|
|
self.cidict.update([('a', 'va'), ('b', None)], b='vb', key2='val2')
|
|
|
|
assert dict(self.cidict.items()) == {
|
|
|
|
'a': 'va', 'b': 'vb',
|
|
|
|
'Key1': 'val1', 'key2': 'val2', 'KEY3': 'VAL3'}
|
|
|
|
|
2013-09-23 03:46:01 -05:00
|
|
|
def test_update_duplicate_values_dict(self):
|
|
|
|
with nose.tools.assert_raises(ValueError):
|
|
|
|
self.cidict.update({'a': 'va', 'A': None, 'b': 3})
|
|
|
|
|
|
|
|
def test_update_duplicate_values_list(self):
|
|
|
|
with nose.tools.assert_raises(ValueError):
|
|
|
|
self.cidict.update([('a', 'va'), ('A', None), ('b', 3)])
|
|
|
|
|
|
|
|
def test_update_duplicate_values_kwargs(self):
|
|
|
|
with nose.tools.assert_raises(ValueError):
|
|
|
|
self.cidict.update(a='va', A=None, b=3)
|
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
def test_update_kwargs(self):
|
|
|
|
self.cidict.update(b='vb', key2='val2')
|
|
|
|
assert dict(self.cidict.items()) == {
|
|
|
|
'b': 'vb', 'Key1': 'val1', 'key2': 'val2', 'KEY3': 'VAL3'}
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_setdefault(self):
|
|
|
|
nose.tools.assert_equal("val1", self.cidict.setdefault("KEY1", "default"))
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
assert not self.cidict.has_key("KEY4")
|
|
|
|
nose.tools.assert_equal("default", self.cidict.setdefault("KEY4", "default"))
|
|
|
|
assert self.cidict.has_key("KEY4")
|
|
|
|
nose.tools.assert_equal("default", self.cidict["key4"])
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
assert not self.cidict.has_key("KEY5")
|
|
|
|
nose.tools.assert_equal(None, self.cidict.setdefault("KEY5"))
|
|
|
|
assert self.cidict.has_key("KEY5")
|
|
|
|
nose.tools.assert_equal(None, self.cidict["key5"])
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_pop(self):
|
|
|
|
nose.tools.assert_equal("val1", self.cidict.pop("KEY1", "default"))
|
|
|
|
assert not self.cidict.has_key("key1")
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal("val2", self.cidict.pop("KEY2"))
|
|
|
|
assert not self.cidict.has_key("key2")
|
2012-04-20 06:03:16 -05:00
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal("default", self.cidict.pop("key4", "default"))
|
|
|
|
with nose.tools.assert_raises(KeyError):
|
2012-04-20 06:03:16 -05:00
|
|
|
self.cidict.pop("key4")
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
def test_popitem(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
items = set(self.cidict.items())
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(3, len(self.cidict))
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
item = self.cidict.popitem()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(2, len(self.cidict))
|
|
|
|
assert item in items
|
2012-04-20 06:03:16 -05:00
|
|
|
items.discard(item)
|
|
|
|
|
|
|
|
item = self.cidict.popitem()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(1, len(self.cidict))
|
|
|
|
assert item in items
|
2012-04-20 06:03:16 -05:00
|
|
|
items.discard(item)
|
|
|
|
|
|
|
|
item = self.cidict.popitem()
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(0, len(self.cidict))
|
|
|
|
assert item in items
|
2012-04-20 06:03:16 -05:00
|
|
|
items.discard(item)
|
|
|
|
|
2013-02-05 09:24:46 -06:00
|
|
|
def test_fromkeys(self):
|
|
|
|
dct = ipautil.CIDict.fromkeys(('A', 'b', 'C'))
|
|
|
|
assert sorted(dct.keys()) == sorted(['A', 'b', 'C'])
|
|
|
|
assert sorted(dct.values()) == [None] * 3
|
|
|
|
|
|
|
|
|
2012-06-15 04:17:49 -05:00
|
|
|
class TestTimeParser(object):
|
|
|
|
def test_simple(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
timestr = "20070803"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(2007, time.year)
|
|
|
|
nose.tools.assert_equal(8, time.month)
|
|
|
|
nose.tools.assert_equal(3, time.day)
|
|
|
|
nose.tools.assert_equal(0, time.hour)
|
|
|
|
nose.tools.assert_equal(0, time.minute)
|
|
|
|
nose.tools.assert_equal(0, time.second)
|
|
|
|
|
|
|
|
def test_hour_min_sec(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
timestr = "20051213141205"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(2005, time.year)
|
|
|
|
nose.tools.assert_equal(12, time.month)
|
|
|
|
nose.tools.assert_equal(13, time.day)
|
|
|
|
nose.tools.assert_equal(14, time.hour)
|
|
|
|
nose.tools.assert_equal(12, time.minute)
|
|
|
|
nose.tools.assert_equal(5, time.second)
|
|
|
|
|
|
|
|
def test_fractions(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
timestr = "2003092208.5"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(2003, time.year)
|
|
|
|
nose.tools.assert_equal(9, time.month)
|
|
|
|
nose.tools.assert_equal(22, time.day)
|
|
|
|
nose.tools.assert_equal(8, time.hour)
|
|
|
|
nose.tools.assert_equal(30, time.minute)
|
|
|
|
nose.tools.assert_equal(0, time.second)
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
timestr = "199203301544,25"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(1992, time.year)
|
|
|
|
nose.tools.assert_equal(3, time.month)
|
|
|
|
nose.tools.assert_equal(30, time.day)
|
|
|
|
nose.tools.assert_equal(15, time.hour)
|
|
|
|
nose.tools.assert_equal(44, time.minute)
|
|
|
|
nose.tools.assert_equal(15, time.second)
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
timestr = "20060401185912,8"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(2006, time.year)
|
|
|
|
nose.tools.assert_equal(4, time.month)
|
|
|
|
nose.tools.assert_equal(1, time.day)
|
|
|
|
nose.tools.assert_equal(18, time.hour)
|
|
|
|
nose.tools.assert_equal(59, time.minute)
|
|
|
|
nose.tools.assert_equal(12, time.second)
|
|
|
|
nose.tools.assert_equal(800000, time.microsecond)
|
|
|
|
|
|
|
|
def test_time_zones(self):
|
2012-04-20 06:03:16 -05:00
|
|
|
timestr = "20051213141205Z"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(0, time.tzinfo.houroffset)
|
|
|
|
nose.tools.assert_equal(0, time.tzinfo.minoffset)
|
2012-04-20 06:03:16 -05:00
|
|
|
offset = time.tzinfo.utcoffset(time.tzinfo.dst())
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(0, offset.seconds)
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
timestr = "20051213141205+0500"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(5, time.tzinfo.houroffset)
|
|
|
|
nose.tools.assert_equal(0, time.tzinfo.minoffset)
|
2012-04-20 06:03:16 -05:00
|
|
|
offset = time.tzinfo.utcoffset(time.tzinfo.dst())
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(5 * 60 * 60, offset.seconds)
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
timestr = "20051213141205-0500"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(-5, time.tzinfo.houroffset)
|
|
|
|
nose.tools.assert_equal(0, time.tzinfo.minoffset)
|
2012-04-20 06:03:16 -05:00
|
|
|
# NOTE - the offset is always positive - it's minutes
|
|
|
|
# _east_ of UTC
|
|
|
|
offset = time.tzinfo.utcoffset(time.tzinfo.dst())
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal((24 - 5) * 60 * 60, offset.seconds)
|
2012-04-20 06:03:16 -05:00
|
|
|
|
|
|
|
timestr = "20051213141205-0930"
|
|
|
|
|
|
|
|
time = ipautil.parse_generalized_time(timestr)
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(-9, time.tzinfo.houroffset)
|
|
|
|
nose.tools.assert_equal(-30, time.tzinfo.minoffset)
|
2012-04-20 06:03:16 -05:00
|
|
|
offset = time.tzinfo.utcoffset(time.tzinfo.dst())
|
2012-06-15 04:17:49 -05:00
|
|
|
nose.tools.assert_equal(((24 - 9) * 60 * 60) - (30 * 60), offset.seconds)
|