diff options
author | Alois Mahdal <amahdal@redhat.com> | 2014-05-07 21:40:28 +0200 |
---|---|---|
committer | Alois Mahdal <amahdal@redhat.com> | 2014-05-15 17:02:30 +0200 |
commit | 4b8fe95796bcb80e4e94253a7bd14bc18279d38f (patch) | |
tree | 8169c471b313cf879b2bc34e6c3be49f5a986d07 | |
parent | 7fe33db03a3dae1b3e20e2b6409e53124836dfe1 (diff) | |
download | openlmi-providers-4b8fe95796bcb80e4e94253a7bd14bc18279d38f.tar.gz openlmi-providers-4b8fe95796bcb80e4e94253a7bd14bc18279d38f.tar.xz openlmi-providers-4b8fe95796bcb80e4e94253a7bd14bc18279d38f.zip |
Add utility classes for indication testing
-rw-r--r-- | src/account/test/common.py | 53 | ||||
-rw-r--r-- | src/python/lmi/test/util.py | 95 |
2 files changed, 148 insertions, 0 deletions
diff --git a/src/account/test/common.py b/src/account/test/common.py index 2818006..07543b3 100644 --- a/src/account/test/common.py +++ b/src/account/test/common.py @@ -210,3 +210,56 @@ class UserOps(object): Return true/false if user does/does not exist """ return name in cls.list_users() + + +class TestUserSet(object): + """ + Class to hold list of "testing" users, able to make up new names + """ + + def __init__(self, prefix="test_user_", strength=8): + self.prefix = prefix + self.strength = strength + self._our_users = set() + + def _new_name(self): + """ + Make up a name that is not yet on the system + """ + name = None + existing = UserOps.list_users() + rs = lmi.test.util.random_string + while not name or name in existing: + name = rs(strength=self.strength, prefix=self.prefix) + return name + + def add(self, name=None): + """ + Create a testing user; return the name + """ + name = name if name else self._new_name() + UserOps.create_account(name) + self._our_users.add(name) + return name + + def remove(self, name=None): + """ + Remove given user or a random one; return the removed name + """ + try: + name = name if name else next(iter(self._our_users)) + assert name in self._our_users + except StopIteration: # from above next() call + raise ValueError("no more testing users") + except AssertionError: + raise ValueError("not our testing user: %s" % name) + UserOps.clean_account(name) + self._our_users.remove(name) + return name + + def remove_all(self): + """ + Remove all testing users + """ + [UserOps.clean_account(n) for n in self._our_users] + self._our_users = set() diff --git a/src/python/lmi/test/util.py b/src/python/lmi/test/util.py index 699507c..428ac80 100644 --- a/src/python/lmi/test/util.py +++ b/src/python/lmi/test/util.py @@ -351,3 +351,98 @@ class BaseCrippler: self._append_to(path, self._get_content(path, case)) else: raise ValueError("unknown op: %s" % op) + + +class PackedSequence(object): + """ + Iterator for arbitrarily long sequence of repeating symbols + + This class serves to help describe (and store) sequence of + events in a human-readable way. + + For example, instead of writing: + + mytest(["a", "b", "c", "c", ... "c", "a", "c"]) + + you can write + + mytest(PackedSequence("a,b,1000c,a,c")) + + except that no huge array needs to be stored and the notation + is really simple and concise, no matter how huge your sequence + actually is. + + You can add items to the PackedSequence object using append + method. So you can use it to report a sequence of events in + a concise way, e.g.: + + ps = PackedSequence() + for i in long: # e.g. above list + ps.append(i) + print str(ps) # "a,b,1000c,a,c" + + Note that from list-like operations, only append, next, len and + iter are supported. + """ + + class _PackedItem(object): + + def __init__(self, item, num=1): + self.item = item + self.num = num + + def __str__(self): + if self.num: + n = "" if self.num == 1 else str(self.num) + return "%s%s" % (n, self.item) + return "" + + def __init__(self, seq=None): + self._items = self._parse(seq) + + def __iter__(self): + return self + + def __len__(self): + return sum([i.num for i in self._items if i.num]) + + def __str__(self): + return ",".join([str(i) for i in self._items]) + + def _parse(self, seq): + """ + Parse sequence string + """ + return [self._parse1(itm) for itm in seq.split(",")] if seq else [] + + def _parse1(self, itm): + ns = "" + while itm.startswith(tuple("1234567890")): + ns += itm[:1] + itm = itm[1:] + n = int(ns) if ns else 1 + return PackedSequence._PackedItem(itm, n) + + def append(self, item): + if self._items: + lst = self._items[-1] + if lst.item == item: + lst.num += 1 + return + self._items.append(PackedSequence._PackedItem(item)) + + def next(self): + + while self._items: + nxt = self._items[0] + if nxt.num: + nxt.num -= 1 + return nxt.item + else: + self._items.pop(0) + + raise StopIteration + + @classmethod + def normalize(cls, seq): + return str(cls(seq)) |