Commit f8df048c authored by Jérome Perrin's avatar Jérome Perrin

test: introduce makeRecipe utility function

This function factorize the code to instanciate a recipe in a fake
buildout.
The new feature is that if running in a buildout directory, the recipe
will reuse the eggs from this buildout instead of trying to install eggs
again.
parent 8ed0ee5a
...@@ -4,8 +4,6 @@ import unittest ...@@ -4,8 +4,6 @@ import unittest
from mock import patch from mock import patch
from slapos.recipe import free_port
class SocketMock(): class SocketMock():
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
self.args = args self.args = args
...@@ -28,31 +26,16 @@ class FreePortTest(unittest.TestCase): ...@@ -28,31 +26,16 @@ class FreePortTest(unittest.TestCase):
SocketMock.bind = SocketMock.close = SocketMock.nothing_happen SocketMock.bind = SocketMock.close = SocketMock.nothing_happen
def new_recipe(self, **kw): def new_recipe(self, **kw):
buildout = { from slapos.recipe import free_port
'buildout': { from slapos.test.utils import makeRecipe
'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': '',
}
}
options = { options = {
'ip': '127.0.0.1', 'ip': '127.0.0.1',
} }
options.update(kw) 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 @useMock
def test_ifNoBusyPortThenMinPortIsAlwaysReturned(self): def test_ifNoBusyPortThenMinPortIsAlwaysReturned(self):
......
...@@ -4,30 +4,15 @@ import unittest ...@@ -4,30 +4,15 @@ import unittest
from tempfile import mkdtemp from tempfile import mkdtemp
from shutil import rmtree from shutil import rmtree
from slapos.recipe import generic_cloudooo
class TestGenericCloudooo(unittest.TestCase): class TestGenericCloudooo(unittest.TestCase):
def new_recipe(self, options): def new_recipe(self, options):
buildout = { from slapos.recipe import generic_cloudooo
'buildout': { from slapos.test.utils import makeRecipe
'bin-directory': '', return makeRecipe(
'find-links': '', generic_cloudooo.Recipe,
'allow-hosts': '', options=options,
'develop-eggs-directory': '', name='generic_cloudooo')
'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)
def setUp(self): def setUp(self):
self.test_dir = mkdtemp() self.test_dir = mkdtemp()
......
...@@ -5,37 +5,19 @@ import sys ...@@ -5,37 +5,19 @@ import sys
import tempfile import tempfile
import unittest import unittest
from slapos.recipe import pbs
class PBSTest(unittest.TestCase): class PBSTest(unittest.TestCase):
def new_recipe(self): def new_recipe(self):
buildout = { from slapos.recipe import pbs
'buildout': { from slapos.test.utils import makeRecipe
'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': '',
}
}
options = { options = {
'rdiffbackup-binary': '' 'rdiffbackup-binary': ''
} }
return makeRecipe(
return pbs.Recipe(buildout=buildout, name='pbs', options=options) pbs.Recipe,
options=options,
name='pbs')
def test_push(self): def test_push(self):
recipe = self.new_recipe() recipe = self.new_recipe()
......
...@@ -6,8 +6,6 @@ import tempfile ...@@ -6,8 +6,6 @@ import tempfile
import unittest import unittest
from slapos.slap.slap import NotFoundError, ConnectionError from slapos.slap.slap import NotFoundError, ConnectionError
from slapos.recipe import re6stnet
class Re6stnetTest(unittest.TestCase): class Re6stnetTest(unittest.TestCase):
...@@ -47,31 +45,21 @@ class Re6stnetTest(unittest.TestCase): ...@@ -47,31 +45,21 @@ class Re6stnetTest(unittest.TestCase):
shutil.rmtree(path) shutil.rmtree(path)
def new_recipe(self): def new_recipe(self):
buildout = { from slapos.recipe import re6stnet
'buildout': { from slapos.test.utils import makeRecipe
'bin-directory': '', return makeRecipe(
'find-links': '', re6stnet.Recipe,
'allow-hosts': '', options=self.options,
'develop-eggs-directory': '', slap_connection={
'eggs-directory': '',
'python': 'testpython',
},
'testpython': {
'executable': sys.executable,
},
'slap-connection': {
'computer-id': 'comp-test', 'computer-id': 'comp-test',
'partition-id': 'slappart0', 'partition-id': 'slappart0',
'server-url': 'http://server.com', 'server-url': 'http://server.com',
'software-release-url': 'http://software.com', 'software-release-url': 'http://software.com',
'key-file': '/path/to/key', 'key-file': '/path/to/key',
'cert-file': '/path/to/cert' 'cert-file': '/path/to/cert'
} },
} name='re6stnet')
options = self.options
return re6stnet.Recipe(buildout=buildout, name='re6stnet', options=options)
def checkWrapper(self, path): def checkWrapper(self, path):
self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.exists(path))
......
"""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)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment