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
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):
......
......@@ -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()
......
......@@ -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()
......
......@@ -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))
......
"""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