summaryrefslogtreecommitdiff
path: root/tests/unittests/helpers.py
diff options
context:
space:
mode:
authorScott Moser <smoser@brickies.net>2017-01-19 17:31:34 -0500
committerScott Moser <smoser@brickies.net>2017-01-24 16:32:30 -0500
commit65e01b463cee0bdb8c8b415e78abfcc3262aad89 (patch)
treedc0b0a5c0cfddda0723e62a214469ab5d8eb42eb /tests/unittests/helpers.py
parent8e9e47f2f06b4dcf017149699111e94ba7c1830b (diff)
downloadvyos-cloud-init-65e01b463cee0bdb8c8b415e78abfcc3262aad89.tar.gz
vyos-cloud-init-65e01b463cee0bdb8c8b415e78abfcc3262aad89.zip
tests: remove executable bit on test_net, so it runs, and fix it.
The test_user_data_normalize and test_net files had gotten the executable bit set, and thus are skipped by nose by default. We could set run with the --exe flag, but they should not have gotten this way. Other changes here: * replace TempDirTestCase with CiTestCase, which has some nice tmp_dir() and tmp_path() functions. Going forward the intent is to have CiTestCase be the base test case for tests. * test_net: switch to CiTestCase and fix usage that was silently broken, because of exe bit. * populate_dir: return the list of files that it writes rather than having no return value. * CiTestCase: * support tmp_path("foo") that returns a full path to 'foo' under a tmpdir. * add tmp_dir() to get a temp dir and clean up.
Diffstat (limited to 'tests/unittests/helpers.py')
-rw-r--r--tests/unittests/helpers.py47
1 files changed, 27 insertions, 20 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py
index a0933464..90e2431f 100644
--- a/tests/unittests/helpers.py
+++ b/tests/unittests/helpers.py
@@ -2,6 +2,7 @@
from __future__ import print_function
+import functools
import os
import shutil
import sys
@@ -82,6 +83,28 @@ class TestCase(unittest2.TestCase):
pass
+class CiTestCase(TestCase):
+ """This is the preferred test case base class unless user
+ needs other test case classes below."""
+ def tmp_dir(self, dir=None, cleanup=True):
+ # return a full path to a temporary directory that will be cleaned up.
+ if dir is None:
+ tmpd = tempfile.mkdtemp(
+ prefix="ci-%s." % self.__class__.__name__)
+ else:
+ tmpd = tempfile.mkdtemp(dir=dir)
+ self.addCleanup(functools.partial(shutil.rmtree, tmpd))
+ return tmpd
+
+ def tmp_path(self, path, dir=None):
+ # return an absolute path to 'path' under dir.
+ # if dir is None, one will be created with tmp_dir()
+ # the file is not created or modified.
+ if dir is None:
+ dir = self.tmp_dir()
+ return os.path.normpath(os.path.abspath(os.path.join(dir, path)))
+
+
class ResourceUsingTestCase(TestCase):
def setUp(self):
super(ResourceUsingTestCase, self).setUp()
@@ -227,29 +250,10 @@ class HttprettyTestCase(TestCase):
super(HttprettyTestCase, self).tearDown()
-class TempDirTestCase(TestCase):
- # provide a tempdir per class, not per test.
- @classmethod
- def setUpClass(cls):
- cls.tmpd = tempfile.mkdtemp(prefix="ci-%s." % cls.__name__)
- return TestCase.setUpClass()
-
- @classmethod
- def tearDownClass(cls):
- shutil.rmtree(cls.tmpd)
- return TestCase.tearDownClass()
-
- def tmp_path(self, path):
- # if absolute path (starts with /), then make ./path
- if path.startswith(os.path.sep):
- path = "." + path
-
- return os.path.normpath(os.path.join(self.tmpd, path))
-
-
def populate_dir(path, files):
if not os.path.exists(path):
os.makedirs(path)
+ ret = []
for (name, content) in files.items():
p = os.path.join(path, name)
util.ensure_dir(os.path.dirname(p))
@@ -259,6 +263,9 @@ def populate_dir(path, files):
else:
fp.write(content.encode('utf-8'))
fp.close()
+ ret.append(p)
+
+ return ret
def dir2dict(startdir, prefix=None):