diff --git a/slapos/test/recipe/test_free_port.py b/slapos/test/recipe/test_free_port.py index 7693d9e02cd3bbe10d4a20e6fd7e4c7b42979971..e2cea348bf548c5032c5c42cd6960279b9ff3703 100644 --- a/slapos/test/recipe/test_free_port.py +++ b/slapos/test/recipe/test_free_port.py @@ -4,8 +4,6 @@ import unittest from mock import patch -from slapos.recipe import free_port - class SocketMock(): def __init__(self, *args, **kw): self.args = args @@ -28,31 +26,16 @@ class FreePortTest(unittest.TestCase): SocketMock.bind = SocketMock.close = SocketMock.nothing_happen def new_recipe(self, **kw): - buildout = { - 'buildout': { - 'bin-directory': '', - 'find-links': '', - 'allow-hosts': '', - 'develop-eggs-directory': '', - 'eggs-directory': '', - 'python': 'testpython', - 'installed': '.installed.cfg', - }, - 'testpython': { - 'executable': sys.executable, - }, - 'slap-connection': { - 'computer-id': '', - 'partition-id': '', - 'server-url': '', - 'software-release-url': '', - } - } + from slapos.recipe import free_port + from slapos.test.utils import makeRecipe options = { 'ip': '127.0.0.1', } options.update(kw) - return free_port.Recipe(buildout=buildout, name='free_port', options=options) + return makeRecipe( + free_port.Recipe, + options=options, + name='free_port') @useMock def test_ifNoBusyPortThenMinPortIsAlwaysReturned(self): diff --git a/slapos/test/recipe/test_generic_cloudooo.py b/slapos/test/recipe/test_generic_cloudooo.py index d171efcb2d9b4493323b5825591758977a0239ae..29ddf64fdf3a8ff030df3a6e452bc3cc49b33072 100644 --- a/slapos/test/recipe/test_generic_cloudooo.py +++ b/slapos/test/recipe/test_generic_cloudooo.py @@ -4,30 +4,15 @@ import unittest from tempfile import mkdtemp from shutil import rmtree -from slapos.recipe import generic_cloudooo class TestGenericCloudooo(unittest.TestCase): def new_recipe(self, options): - buildout = { - 'buildout': { - 'bin-directory': '', - 'find-links': '', - 'allow-hosts': '', - 'develop-eggs-directory': '', - 'eggs-directory': '', - 'python': 'testpython', - }, - 'testpython': { - 'executable': sys.executable, - }, - 'slap-connection': { - 'computer-id': '', - 'partition-id': '', - 'server-url': '', - 'software-release-url': '', - } - } - return generic_cloudooo.Recipe(buildout=buildout, name='generic_cloudooo', options=options) + from slapos.recipe import generic_cloudooo + from slapos.test.utils import makeRecipe + return makeRecipe( + generic_cloudooo.Recipe, + options=options, + name='generic_cloudooo') def setUp(self): self.test_dir = mkdtemp() diff --git a/slapos/test/recipe/test_pbs.py b/slapos/test/recipe/test_pbs.py index ff7c04f28d97240912d21b3202b9f673967aac8c..08327f8272b7726231c2fcead1dbf8adbecc042e 100644 --- a/slapos/test/recipe/test_pbs.py +++ b/slapos/test/recipe/test_pbs.py @@ -5,37 +5,19 @@ import sys import tempfile import unittest -from slapos.recipe import pbs - class PBSTest(unittest.TestCase): def new_recipe(self): - buildout = { - 'buildout': { - 'bin-directory': '', - 'find-links': '', - 'allow-hosts': '', - 'develop-eggs-directory': '', - 'eggs-directory': '', - 'python': 'testpython', - }, - 'testpython': { - 'executable': sys.executable, - }, - 'slap-connection': { - 'computer-id': '', - 'partition-id': '', - 'server-url': '', - 'software-release-url': '', - } - } - + from slapos.recipe import pbs + from slapos.test.utils import makeRecipe options = { - 'rdiffbackup-binary': '' - } - - return pbs.Recipe(buildout=buildout, name='pbs', options=options) + 'rdiffbackup-binary': '' + } + return makeRecipe( + pbs.Recipe, + options=options, + name='pbs') def test_push(self): recipe = self.new_recipe() diff --git a/slapos/test/recipe/test_re6stnet.py b/slapos/test/recipe/test_re6stnet.py index ca2b5e635f0352742236242244db59284e618af7..0c3ac919b4f486f407b12ac033d5c53f0cff3be0 100644 --- a/slapos/test/recipe/test_re6stnet.py +++ b/slapos/test/recipe/test_re6stnet.py @@ -6,8 +6,6 @@ import tempfile import unittest from slapos.slap.slap import NotFoundError, ConnectionError -from slapos.recipe import re6stnet - class Re6stnetTest(unittest.TestCase): @@ -47,31 +45,21 @@ class Re6stnetTest(unittest.TestCase): shutil.rmtree(path) def new_recipe(self): - buildout = { - 'buildout': { - 'bin-directory': '', - 'find-links': '', - 'allow-hosts': '', - 'develop-eggs-directory': '', - 'eggs-directory': '', - 'python': 'testpython', - }, - 'testpython': { - 'executable': sys.executable, - }, - 'slap-connection': { + from slapos.recipe import re6stnet + from slapos.test.utils import makeRecipe + return makeRecipe( + re6stnet.Recipe, + options=self.options, + slap_connection={ 'computer-id': 'comp-test', 'partition-id': 'slappart0', 'server-url': 'http://server.com', 'software-release-url': 'http://software.com', 'key-file': '/path/to/key', 'cert-file': '/path/to/cert' - } - } - - options = self.options + }, + name='re6stnet') - return re6stnet.Recipe(buildout=buildout, name='re6stnet', options=options) def checkWrapper(self, path): self.assertTrue(os.path.exists(path)) diff --git a/slapos/test/utils.py b/slapos/test/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..d605bab9d326748881ef3858b81b1ed47c743b41 --- /dev/null +++ b/slapos/test/utils.py @@ -0,0 +1,62 @@ +"""Test helpers +""" +import sys +import os.path +from ConfigParser import ConfigParser + +import logging + +def makeRecipe(recipe_class, options, name='test', slap_connection=None): + """Instanciate a recipe of `recipe_class` with `options` with a buildout + mapping containing a python and an empty `slapos-connection` mapping, unless + provided as `slap_connection`. + + If running tests in a buildout folder, the test recipe will reuse the + `eggs-directory` and `develop-eggs-directory` from this buildout so that the + test recipe does not need to install eggs again when using working set. + To prevent test accidentally writing to the buildout's eggs repositories, we + set `newest` to false and `offline` to true in this case. + """ + buildout = { + 'buildout': { + 'bin-directory': '', + 'find-links': '', + 'allow-hosts': '', + 'develop-eggs-directory': '', + 'eggs-directory': '', + 'python': 'testpython', + }, + 'testpython': { + 'executable': sys.executable, + }, + 'slap-connection': { + 'computer-id': '', + 'partition-id': '', + 'server-url': '', + 'software-release-url': '', + } + } + if slap_connection is not None: + buildout['slap-connection'] = slap_connection + + # are we in buildout folder ? + # the usual layout is + # ${buildout:directory}/parts/slapos-repository/slapos/test/utils.py , so try + # to find a buildout relative to this file. + buildout_cfg = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'buildout.cfg') + if os.path.exists(buildout_cfg): + parser = ConfigParser() + parser.readfp(open(buildout_cfg)) + eggs_directory = parser.get('buildout', 'eggs-directory') + develop_eggs_directory = parser.get('buildout', 'develop-eggs-directory') + logging.getLogger(__name__).info( + 'Using eggs-directory (%s) and develop-eggs-directory (%s) from buildout at %s', + eggs_directory, + develop_eggs_directory, + buildout_cfg) + buildout['buildout']['eggs-directory'] = eggs_directory + buildout['buildout']['develop-eggs-directory'] = develop_eggs_directory + buildout['buildout']['newest'] = False + buildout['buildout']['offline'] = True + return recipe_class(buildout=buildout, name=name, options=options) +