summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Pavlov <andrey-mp@yandex.ru>2015-02-07 20:57:43 +0300
committerMatthew Treinish <mtreinish@kortar.org>2015-02-20 19:32:15 +0000
commitbd6d154f14e8ecabc5d7751163eec42bbfa22e59 (patch)
treec6ef05c47d6c0f81dad5db2741c71d21a4c06ad6
parenta6ff3bf7b56c9d70b61d79eab4f3682eb385bcd7 (diff)
downloadtempest-lib-bd6d154f14e8ecabc5d7751163eec42bbfa22e59.tar.gz
Move data_utils functionaly from tempest.0.3.0
This commit migrates all data_utils with tests and the misc utils unit tests from the tempest repo. This includes 3 files from tempest tempest/common/utils/data_utils.py, tempest/tests/common/test_data_utils.py, and tempest/tests/common/test_misc.py. This includes tempest commits: 2d88e49 Merge "DHCPv6 network tests" 41fa16d Hacking rule to forbid resource unsafe fixtures bfa86c6 Merge "Use random binary data for test images" 5c3b6fe Use random binary data for test images 165a743 Merge "Refactor random url generation into its own method" 064e965 Refactor random url generation into its own method 9f921d6 Merge "Refactor _find_caller into a public test finder utility" 7efa5c3 Refactor _find_caller into a public test finder utility f1794eb Merge "Add utils.misc unit tests" 4f46805 Add utils.misc unit tests 1fcf139 Merge "Add unit test for data_utils" 30f07b3 Merge "Remove unused build_url function in data_utils" ce7c696 Add unit test for data_utils c0a1e5c Remove unused build_url function in data_utils fea1a7e Merge "tighten up isolated creds create" 6969b90 tighten up isolated creds create 9051bb5 Merge "Remove vim headers" e8d31a0 Remove vim headers e40967e Merge "API tests for Ironic" 62b1ed1 API tests for Ironic 5331151 Merge "Test image member is enforced" a709b76 Test image member is enforced 4a2431d Inject "-tempest-" string to rand_name 39f9722 Replace OpenStack LLC with OpenStack Foundation bb7ce44 Merge "Reduce chance of name collision for resources." 88d4f7c Reduce chance of name collision for resources. d65aec0 Merge "Fix flavors tests so they can be run in parallel" 8abacf3 Fix flavors tests so they can be run in parallel fc9e333 Fix PEP8 compliance problems 59889b7 Merge "Fix T401 and T402 errors" f237ccb Fix T401 and T402 errors 34afe48 Merge "Fix import order to comply with import ordering rules." a83a16e Fix import order to comply with import ordering rules. 4ef897c Merge "Fix and simplify arbitrary_string. lp#1085048" 47737d8 Fix and simplify arbitrary_string. lp#1085048 7ccda8c Simplify parse_image_id. d246eb4 Merge "Initial add of Swift tests" 5d73443 Initial add of Swift tests a5feec9 Merge "make the rand_name value shorter" 6ec6fc2 make the rand_name value shorter aeddf63 Moved parse_image_id() to data_utils 3d9da9b Merge "Addresses lp#942382 - refactor configuration for clarity" 587385b Addresses lp#942382 - refactor configuration for clarity 25dd196 Merge "Fixed issue with white space after pep8 review Code... 7fb1efa Fixed issue with white space after pep8 review Code review... ed8bef3 Changes the namespace from storm to tempest, as well as ... e1b050d * Added build_url() utility that returns an endpoint URL based... cb5d954 Removed unnecessary 'self' reference 1465d61 Initial import of tests from the Zodiac project. On suggestion... to see the commit history for these files refer to the above sha1s in the tempest repository Change-Id: Idecf25bef6eeffb8b06387ac95d9060edb58be46
-rw-r--r--tempest_lib/common/utils/data_utils.py99
-rw-r--r--tempest_lib/tests/common/__init__.py0
-rw-r--r--tempest_lib/tests/common/utils/__init__.py0
-rw-r--r--tempest_lib/tests/common/utils/test_data_utils.py77
-rw-r--r--tempest_lib/tests/common/utils/test_misc.py88
5 files changed, 264 insertions, 0 deletions
diff --git a/tempest_lib/common/utils/data_utils.py b/tempest_lib/common/utils/data_utils.py
new file mode 100644
index 0000000..eec2474
--- /dev/null
+++ b/tempest_lib/common/utils/data_utils.py
@@ -0,0 +1,99 @@
+# Copyright 2012 OpenStack Foundation
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import itertools
+import netaddr
+import random
+import uuid
+
+
+def rand_uuid():
+ return str(uuid.uuid4())
+
+
+def rand_uuid_hex():
+ return uuid.uuid4().hex
+
+
+def rand_name(name=''):
+ randbits = str(random.randint(1, 0x7fffffff))
+ if name:
+ return name + '-' + randbits
+ else:
+ return randbits
+
+
+def rand_url():
+ randbits = str(random.randint(1, 0x7fffffff))
+ return 'https://url-' + randbits + '.com'
+
+
+def rand_int_id(start=0, end=0x7fffffff):
+ return random.randint(start, end)
+
+
+def rand_mac_address():
+ """Generate an Ethernet MAC address."""
+ # NOTE(vish): We would prefer to use 0xfe here to ensure that linux
+ # bridge mac addresses don't change, but it appears to
+ # conflict with libvirt, so we use the next highest octet
+ # that has the unicast and locally administered bits set
+ # properly: 0xfa.
+ # Discussion: https://bugs.launchpad.net/nova/+bug/921838
+ mac = [0xfa, 0x16, 0x3e,
+ random.randint(0x00, 0xff),
+ random.randint(0x00, 0xff),
+ random.randint(0x00, 0xff)]
+ return ':'.join(["%02x" % x for x in mac])
+
+
+def parse_image_id(image_ref):
+ """Return the image id from a given image ref."""
+ return image_ref.rsplit('/')[-1]
+
+
+def arbitrary_string(size=4, base_text=None):
+ """Return size characters from base_text
+
+ Repeating the base_text infinitely if needed.
+ """
+ if not base_text:
+ base_text = 'test'
+ return ''.join(itertools.islice(itertools.cycle(base_text), size))
+
+
+def random_bytes(size=1024):
+ """Return size randomly selected bytes as a string."""
+ return ''.join([chr(random.randint(0, 255))
+ for i in range(size)])
+
+
+def get_ipv6_addr_by_EUI64(cidr, mac):
+ # Check if the prefix is IPv4 address
+ is_ipv4 = netaddr.valid_ipv4(cidr)
+ if is_ipv4:
+ msg = "Unable to generate IP address by EUI64 for IPv4 prefix"
+ raise TypeError(msg)
+ try:
+ eui64 = int(netaddr.EUI(mac).eui64())
+ prefix = netaddr.IPNetwork(cidr)
+ return netaddr.IPAddress(prefix.first + eui64 ^ (1 << 57))
+ except (ValueError, netaddr.AddrFormatError):
+ raise TypeError('Bad prefix or mac format for generating IPv6 '
+ 'address by EUI-64: %(prefix)s, %(mac)s:'
+ % {'prefix': cidr, 'mac': mac})
+ except TypeError:
+ raise TypeError('Bad prefix type for generate IPv6 address by '
+ 'EUI-64: %s' % cidr)
diff --git a/tempest_lib/tests/common/__init__.py b/tempest_lib/tests/common/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tempest_lib/tests/common/__init__.py
diff --git a/tempest_lib/tests/common/utils/__init__.py b/tempest_lib/tests/common/utils/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tempest_lib/tests/common/utils/__init__.py
diff --git a/tempest_lib/tests/common/utils/test_data_utils.py b/tempest_lib/tests/common/utils/test_data_utils.py
new file mode 100644
index 0000000..76401cb
--- /dev/null
+++ b/tempest_lib/tests/common/utils/test_data_utils.py
@@ -0,0 +1,77 @@
+# Copyright 2014 NEC Corporation.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+
+from tempest_lib.common.utils import data_utils
+from tempest_lib.tests import base
+
+
+class TestDataUtils(base.TestCase):
+
+ def test_rand_uuid(self):
+ actual = data_utils.rand_uuid()
+ self.assertIsInstance(actual, str)
+ self.assertRegexpMatches(actual, "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]"
+ "{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
+ actual2 = data_utils.rand_uuid()
+ self.assertNotEqual(actual, actual2)
+
+ def test_rand_uuid_hex(self):
+ actual = data_utils.rand_uuid_hex()
+ self.assertIsInstance(actual, str)
+ self.assertRegexpMatches(actual, "^[0-9a-f]{32}$")
+
+ actual2 = data_utils.rand_uuid_hex()
+ self.assertNotEqual(actual, actual2)
+
+ def test_rand_name(self):
+ actual = data_utils.rand_name()
+ self.assertIsInstance(actual, str)
+ actual2 = data_utils.rand_name()
+ self.assertNotEqual(actual, actual2)
+
+ actual = data_utils.rand_name('foo')
+ self.assertTrue(actual.startswith('foo'))
+ actual2 = data_utils.rand_name('foo')
+ self.assertTrue(actual.startswith('foo'))
+ self.assertNotEqual(actual, actual2)
+
+ def test_rand_int(self):
+ actual = data_utils.rand_int_id()
+ self.assertIsInstance(actual, int)
+
+ actual2 = data_utils.rand_int_id()
+ self.assertNotEqual(actual, actual2)
+
+ def test_rand_mac_address(self):
+ actual = data_utils.rand_mac_address()
+ self.assertIsInstance(actual, str)
+ self.assertRegexpMatches(actual, "^([0-9a-f][0-9a-f]:){5}"
+ "[0-9a-f][0-9a-f]$")
+
+ actual2 = data_utils.rand_mac_address()
+ self.assertNotEqual(actual, actual2)
+
+ def test_parse_image_id(self):
+ actual = data_utils.parse_image_id("/foo/bar/deadbeaf")
+ self.assertEqual("deadbeaf", actual)
+
+ def test_arbitrary_string(self):
+ actual = data_utils.arbitrary_string()
+ self.assertEqual(actual, "test")
+ actual = data_utils.arbitrary_string(size=30, base_text="abc")
+ self.assertEqual(actual, "abc" * int(30 / len("abc")))
+ actual = data_utils.arbitrary_string(size=5, base_text="deadbeaf")
+ self.assertEqual(actual, "deadb")
diff --git a/tempest_lib/tests/common/utils/test_misc.py b/tempest_lib/tests/common/utils/test_misc.py
new file mode 100644
index 0000000..aefaeef
--- /dev/null
+++ b/tempest_lib/tests/common/utils/test_misc.py
@@ -0,0 +1,88 @@
+# Copyright 2014 NEC Corporation.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+
+from tempest_lib.common.utils import misc
+from tempest_lib.tests import base
+
+
+@misc.singleton
+class TestFoo(object):
+
+ count = 0
+
+ def increment(self):
+ self.count += 1
+ return self.count
+
+
+@misc.singleton
+class TestBar(object):
+
+ count = 0
+
+ def increment(self):
+ self.count += 1
+ return self.count
+
+
+class TestMisc(base.TestCase):
+
+ def test_singleton(self):
+ test = TestFoo()
+ self.assertEqual(0, test.count)
+ self.assertEqual(1, test.increment())
+ test2 = TestFoo()
+ self.assertEqual(1, test.count)
+ self.assertEqual(1, test2.count)
+ self.assertEqual(test, test2)
+ test3 = TestBar()
+ self.assertNotEqual(test, test3)
+
+ def test_find_test_caller_test_case(self):
+ # Calling it from here should give us the method we're in.
+ self.assertEqual('TestMisc:test_find_test_caller_test_case',
+ misc.find_test_caller())
+
+ def test_find_test_caller_setup_self(self):
+ def setUp(self):
+ return misc.find_test_caller()
+ self.assertEqual('TestMisc:setUp', setUp(self))
+
+ def test_find_test_caller_setup_no_self(self):
+ def setUp():
+ return misc.find_test_caller()
+ self.assertEqual(':setUp', setUp())
+
+ def test_find_test_caller_setupclass_cls(self):
+ def setUpClass(cls): # noqa
+ return misc.find_test_caller()
+ self.assertEqual('TestMisc:setUpClass', setUpClass(self.__class__))
+
+ def test_find_test_caller_teardown_self(self):
+ def tearDown(self):
+ return misc.find_test_caller()
+ self.assertEqual('TestMisc:tearDown', tearDown(self))
+
+ def test_find_test_caller_teardown_no_self(self):
+ def tearDown():
+ return misc.find_test_caller()
+ self.assertEqual(':tearDown', tearDown())
+
+ def test_find_test_caller_teardown_class(self):
+ def tearDownClass(cls): # noqa
+ return misc.find_test_caller()
+ self.assertEqual('TestMisc:tearDownClass',
+ tearDownClass(self.__class__))