Skip to content

Commit

Permalink
Merge pull request pytest-dev#111 from manahl/xfang-fix-bin-names
Browse files Browse the repository at this point in the history
Fix binary names
  • Loading branch information
javefang committed Jan 18, 2019
2 parents 8f3cb9b + 73b6714 commit b10faa3
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 62 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
@@ -1,7 +1,10 @@

## Changelog

### 1.4.0 (Unreleased)
### 1.4.1 (Unreleased)
* pytest-server-fixtures: server fixture binary path specified in ENV now only affect server class 'thread'

### 1.4.0 (2019-01-15)
* Fixing python 3 compatibility in Simple HTTP Server fixture
* Fixed broken tests in pytest-profiling
* Pinned pytest<4.0.0 until all deprecation warnings are fixed.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.5.0
1.4.1
45 changes: 41 additions & 4 deletions pytest-server-fixtures/pytest_server_fixtures/base2.py
Expand Up @@ -49,9 +49,12 @@ def start(self):
self._server = create_server(
server_class=CONFIG.server_class,
server_type=self.__class__.__name__,
get_cmd=self.get_cmd,
cmd=self.cmd,
cmd_local=self.cmd_local,
get_args=self.get_args,
env=self.env,
image=self.image,
labels=self.labels,
workspace=self.workspace,
cwd=self._cwd,
random_hostname=self.random_hostname,
Expand Down Expand Up @@ -119,24 +122,58 @@ def cwd(self):
def image(self):
"""
Get the Docker image of the server.
Only used when SERVER_FIXTURE_SERVER_CLASS is 'docker' or 'kubernetes'.
"""
raise NotImplementedError("Concret class should implement this")

@property
def labels(self):
"""
Extra labels to be added to the server fixture container.
Only used when SERVER_FIXTURE_SERVER_CLASS is 'docker' or 'kubernetes'.
"""
return dict()

@property
def env(self):
"""
Get the environment variables for running the server fixture.
"""
return dict()

def get_cmd(self, **kwargs):
@property
def cmd(self):
"""
Get the command to run the server fixtures.
Get the command to run the server fixture.
"""
raise NotImplementedError("Concrete class should implement this")

@property
def cmd_local(self):
"""
Get the local command to run the server fixture.
Only used when SERVER_FIXTURES_SERVER_CLASS is 'thread'.
"""
return self.get_cmd()

def get_args(self, hostname=None, workspace=None):
"""
Get the arguments to run the server fixtures.
@param hostname: hostname of the server
@param workspace: workspace of the server
"""
raise NotImplementedError("Concrete class should implement this")

def pre_setup(self):
"""DEPRECATED: only used if serverclass=thread"""
"""
DEPRECATED
Only used when SERVER_FIXTURE_SERVER_CLASS is 'thread'
"""
pass

def post_setup(self):
Expand Down
11 changes: 9 additions & 2 deletions pytest-server-fixtures/pytest_server_fixtures/mongo.py
Expand Up @@ -83,9 +83,16 @@ def __init__(self, delete=True, **kwargs):
self._port = self._get_port(27017)
self.api = None

def get_cmd(self, **kwargs):
@property
def cmd(self):
return 'mongod'

@property
def cmd_local(self):
return CONFIG.mongo_bin

def get_args(self, **kwargs):
cmd = [
CONFIG.mongo_bin,
'--port=%s' % self.port,
'--nounixsocket',
'--syncdelay=0',
Expand Down
11 changes: 9 additions & 2 deletions pytest-server-fixtures/pytest_server_fixtures/redis.py
Expand Up @@ -68,9 +68,16 @@ def api(self):
self._api = redis.Redis(host=self.hostname, port=self.port, db=self.db)
return self._api

def get_cmd(self, **kwargs):
@property
def cmd(self):
return "redis-server"

@property
def cmd_local(self):
return CONFIG.redis_executable

def get_args(self, **kwargs):
cmd = [
CONFIG.redis_executable,
"--port", str(self.port),
"--timeout", "0",
"--loglevel", "notice",
Expand Down
12 changes: 10 additions & 2 deletions pytest-server-fixtures/pytest_server_fixtures/rethink.py
Expand Up @@ -127,9 +127,17 @@ def __init__(self, **kwargs):
self._http_port = self._get_port(8080)
self.db = None

def get_cmd(self, **kwargs):

@property
def cmd(self):
return "rethindb"

@property
def cmd_local(self):
return CONFIG.rethink_executable

def get_args(self, **kwargs):
cmd = [
CONFIG.rethink_executable,
'--driver-port', str(self.port),
'--http-port', str(self.http_port),
'--cluster-port', str(self.cluster_port),
Expand Down
Expand Up @@ -8,27 +8,32 @@ def create_server(server_class, **kwargs):
if server_class == 'thread':
from .thread import ThreadServer
return ThreadServer(
kwargs["get_cmd"],
kwargs["env"],
kwargs["workspace"],
cmd=kwargs["cmd_local"],
get_args=kwargs["get_args"],
env=kwargs["env"],
workspace=kwargs["workspace"],
cwd=kwargs["cwd"],
random_hostname=kwargs["random_hostname"],
)

if server_class == 'docker':
from .docker import DockerServer
return DockerServer(
kwargs["server_type"],
kwargs["get_cmd"],
kwargs["env"],
kwargs["image"],
server_type=kwargs["server_type"],
cmd=kwargs["cmd"],
get_args=kwargs["get_args"],
env=kwargs["env"],
image=kwargs["image"],
labels=kwargs["labels"],
)

if server_class == 'kubernetes':
from .kubernetes import KubernetesServer
return KubernetesServer(
kwargs["server_type"],
kwargs["get_cmd"],
kwargs["env"],
kwargs["image"],
server_type=kwargs["server_type"],
cmd=kwargs["cmd"],
get_args=kwargs["get_args"],
env=kwargs["env"],
image=kwargs["image"],
labels=kwargs["labels"],
)
Expand Up @@ -33,8 +33,12 @@ class ServerFixtureNotTerminatedException(Exception):
class ServerClass(threading.Thread):
"""Example interface for ServerClass."""

def __init__(self, get_cmd, env, hostname=None):
"""Initialise the server class.
def __init__(self,
cmd,
get_args,
env):
"""
Initialise the server class.
Server fixture will be started here.
"""
super(ServerClass, self).__init__()
Expand All @@ -43,9 +47,9 @@ def __init__(self, get_cmd, env, hostname=None):
self.daemon = True

self._id = get_random_id(SERVER_ID_LEN)
self._get_cmd = get_cmd
self._cmd = cmd
self._get_args = get_args
self._env = env or {}
self._hostname = hostname

def run(self):
"""In a new thread, wait for the server to return."""
Expand All @@ -61,12 +65,13 @@ def teardown(self):

@property
def is_running(self):
"""Tell if the server is running."""
raise NotImplementedError("Concrete class should implement this")

@property
def hostname(self):
"""Get server's hostname."""
return self._hostname
raise NotImplementedError("Concrete class should implement this")

@property
def name(self):
Expand Down
Expand Up @@ -19,8 +19,14 @@
class DockerServer(ServerClass):
"""Docker server class."""

def __init__(self, server_type, get_cmd, env, image, labels={}):
super(DockerServer, self).__init__(get_cmd, env)
def __init__(self,
server_type,
cmd,
get_args,
env,
image,
labels={}):
super(DockerServer, self).__init__(cmd, get_args, env)

self._image = image
self._labels = merge_dicts(labels, {
Expand All @@ -38,7 +44,7 @@ def launch(self):
self._container = self._client.containers.run(
image=self._image,
name=self.name,
command=self._get_cmd(),
command=[self._cmd] + self._get_args(),
environment=self._env,
labels=self._labels,
detach=True,
Expand Down
Expand Up @@ -43,8 +43,15 @@ class NotRunningInKubernetesException(Exception):
class KubernetesServer(ServerClass):
"""Kubernetes server class."""

def __init__(self, server_type, get_cmd, env, image, labels={}):
super(KubernetesServer, self).__init__(get_cmd, env)
def __init__(self,
server_type,
cmd,
get_args,
env,
image,
labels={}):
super(KubernetesServer, self).__init__(cmd, get_args, env)

if not fixture_namespace:
raise NotRunningInKubernetesException()

Expand Down
40 changes: 21 additions & 19 deletions pytest-server-fixtures/pytest_server_fixtures/serverclass/thread.py
Expand Up @@ -58,39 +58,39 @@ def _kill_proc_tree(pid, sig=signal.SIGKILL, timeout=None):
class ThreadServer(ServerClass):
"""Thread server class."""

def __init__(self, get_cmd, env, workspace, cwd=None, random_hostname=True):
super(ThreadServer, self).__init__(
get_cmd,
env,
hostname=(get_ephemeral_host() if random_hostname else CONFIG.fixture_hostname),
)
def __init__(self,
cmd,
get_args,
env,
workspace,
cwd=None,
random_hostname=True):
super(ThreadServer, self).__init__(cmd, get_args, env)

self.exit = False
self._hostname = get_ephemeral_host() if random_hostname else CONFIG.fixture_hostname
self._workspace = workspace
self._cwd = cwd
self._proc = None
self._run_cmd = []

def launch(self):
log.debug("Launching thread server.")

self._run_cmd = self._get_cmd(
hostname=self._hostname,
run_cmd = [self._cmd] + self._get_args(
hostname=self.hostname,
workspace=self._workspace,
)

args = dict(
env=self._env,
cwd=self._cwd,
)

debug = is_debug()

extra_args = dict()
if debug:
args['stdout'] = subprocess.PIPE
args['stderr'] = subprocess.PIPE
extra_args['stdout'] = subprocess.PIPE
extra_args['stderr'] = subprocess.PIPE

self._proc = subprocess.Popen(self._run_cmd, **args)
self._proc = subprocess.Popen(run_cmd, env=self._env, cwd=self._cwd, **extra_args)
log.debug("Running server: %s", ' '.join(run_cmd))
log.debug("CWD: %s", self._cwd)

if debug:
ProcessReader(self._proc, self._proc.stdout, False).start()
Expand All @@ -100,8 +100,6 @@ def launch(self):

def run(self):
"""Run in thread"""
log.debug("Running server: %s", ' '.join(self._run_cmd))
log.debug("CWD: %s", self._cwd)
try:
self._proc.wait()
except OSError:
Expand All @@ -117,6 +115,10 @@ def is_running(self):
# return False if there is a return code from the main process
return self._proc.poll() is None

@property
def hostname(self):
return self._hostname

def teardown(self):
if not self._proc:
log.warning("No process is running, skip teardown.")
Expand Down
Expand Up @@ -5,9 +5,12 @@
@patch('pytest_server_fixtures.serverclass.docker.ServerClass.__init__')
def test_init(mock_init):
s = DockerServer(sentinel.server_type,
sentinel.get_cmd,
sentinel.cmd,
sentinel.get_args,
sentinel.env,
sentinel.image)

mock_init.assert_called_with(sentinel.get_cmd, sentinel.env)
mock_init.assert_called_with(sentinel.cmd,
sentinel.get_args,
sentinel.env)

Expand Up @@ -8,9 +8,12 @@
@patch('pytest_server_fixtures.serverclass.docker.ServerClass.__init__')
def test_init(mock_init):
s = KubernetesServer(sentinel.server_type,
sentinel.get_cmd,
sentinel.env,
sentinel.image)
sentinel.cmd,
sentinel.get_args,
sentinel.env,
sentinel.image)

mock_init.assert_called_with(sentinel.get_cmd, sentinel.env)
mock_init.assert_called_with(sentinel.cmd,
sentinel.get_args,
sentinel.env)

0 comments on commit b10faa3

Please sign in to comment.