From ac6bdf870d0c4727128ce38cf818ee93ce510ce8 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Tue, 18 Feb 2014 19:00:53 +0000 Subject: Split out 'util' and 'config' modules from test_trove_upgrades --- config.py | 45 +++++++++++++++++ test_trove_upgrades.py | 130 +++++++++---------------------------------------- util.py | 90 ++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 108 deletions(-) create mode 100644 config.py create mode 100644 util.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..e53b66a --- /dev/null +++ b/config.py @@ -0,0 +1,45 @@ +# Copyright (C) 2014 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +'''Baserock system-test configuration.''' + +import os + +# The test host must have passwordless access to this machine. The tests set +# set StrictHostKeyChecking=no for SSH connections so it does not need to be in +# '~/.ssh/known_hosts'. +DEPLOY_URL = 'kvm+ssh://sam@landfill.ducie.codethink.co.uk/' +DEPLOY_PATH = '/home/VIRT-IMAGES/' + +# Seconds to wait for machine to appear on network before assuming it didn't +# boot or connect to network successfully. +BOOT_TIMEOUT=20 + +# FIXME: building should automatically use the version of Morph from the system +# branch, really ... but for now, if the installed Morph can't build +# baserock:baserock/morphs 'master' branch, you can change this! +os.environ['PYTHONPATH'] = '/src/morph' +MORPH_COMMAND = ['/src/morph/morph', '--no-git-update'] +#MORPH_COMMAND = '/src/morph/morph' +#MORPH_COMMAND = 'morph' + +BUILD_TEMPDIR = '/src/tmp' + +#BRANCH = 'master' +BRANCH = 'baserock/sam/trove-upgrades' + +# For debugging. FIXME: would be better if cliapp's logging mechanism supported +# logging to stdout, but .... :( +VERBOSE = True diff --git a/test_trove_upgrades.py b/test_trove_upgrades.py index 111bde4..001f5f3 100644 --- a/test_trove_upgrades.py +++ b/test_trove_upgrades.py @@ -64,90 +64,13 @@ import yaml import morphlib import morphlib.plugins.branch_and_merge_plugin -# The test host must have passwordless access to this machine. The tests set -# set StrictHostKeyChecking=no for SSH connections so it does not need to be in -# '~/.ssh/known_hosts'. -DEPLOY_URL = 'kvm+ssh://sam@landfill.ducie.codethink.co.uk/' -DEPLOY_PATH = '/home/VIRT-IMAGES/' +import config +import util -# Seconds to wait for machine to appear on network before assuming it didn't -# boot or connect to network successfully. -BOOT_TIMEOUT=20 - -# FIXME: building should automatically use the version of Morph from the system -# branch, really ... but for now, if the installed Morph can't build -# baserock:baserock/morphs 'master' branch, you can change this! -os.environ['PYTHONPATH'] = '/src/morph' -MORPH_COMMAND = ['/src/morph/morph', '--no-git-update'] -#MORPH_COMMAND = '/src/morph/morph' -#MORPH_COMMAND = 'morph' - -BUILD_TEMPDIR = '/src/tmp' - -#BRANCH = 'master' -BRANCH = 'baserock/sam/trove-upgrades' - -# For debugging. FIXME: would be better if cliapp's logging mechanism supported -# logging to stdout, but .... :( -VERBOSE = True - - -def remote_runcmd(url, command, **kwargs): - ''' - Execute a command on machine 'url'. - - Command must be a list of arguments, not a single string. - - FIXME: perhaps this functionality should be merged into cliapp.ssh_runcmd() - so that we can use that instead. - ''' - if VERBOSE: - print "%s: %s" % (url, ' '.join(command)) - url = urlparse.urlsplit(url) - if url[0] in ['ssh', 'kvm+ssh']: - ssh_host = url[1] - - ssh_cmd = ['ssh'] - - # The identity of the newly-created test machine will never be in - # '~/.ssh/known_hosts'; this switch avoids seeing the 'do you want to - # connect' prompt that SSH would normally present in this situation. - ssh_cmd.extend(['-o', 'StrictHostKeyChecking=no']) - - return cliapp.runcmd(ssh_cmd + [ssh_host, ' '.join(command)], **kwargs) - else: - raise NotImplementedError("Remote machine must be an ssh:// URL") - - -def run_morph(args, **kwargs): - ''' - Run Morph on the current machine. - ''' - morph_command = MORPH_COMMAND - if isinstance(morph_command, str): - morph_command = morph_command.split(' ') - cmd = morph_command + args - if VERBOSE: - print ' '.join(cmd) - if 'stdout' not in kwargs: - kwargs['stdout'] = sys.stdout - if 'stderr' not in kwargs: - kwargs['stderr'] = sys.stdout - return cliapp.runcmd(cmd, **kwargs) - - -def run_git(args, **kwargs): - return cliapp.runcmd(['git'] + args, **kwargs) - - -def read_file(file_path): - with open(file_path, 'r') as f: - return f.read() - - -def write_file(file_path, text): - with open(file_path, 'w') as f: - f.write(text) +from util import read_file, write_file +from util import remote_runcmd +from util import run_git, run_morph +from util import set_directory class Deployment(object): @@ -235,8 +158,8 @@ class TroveInitialDeployment(Deployment): if initial_deploy_type == 'kvm': deploy_location = ''.join( - [DEPLOY_URL, self.vm_name, DEPLOY_PATH, '%s.img' % - self.vm_name]) + [config.DEPLOY_URL, self.vm_name, config.DEPLOY_PATH, + '%s.img' % self.vm_name]) else: raise NotImplementedError() @@ -396,7 +319,7 @@ class SystemTestBranch(object): object_name = original_ref + ':' + chunk_morph_name with open(chunk_morph_name, 'w') as f: run_git(['cat-file', 'blob', object_name], stdout=f) - message = 'Add lighttpd.morph from branch %s' % BRANCH + message = 'Add lighttpd.morph from branch %s' % config.BRANCH run_git(['add', chunk_morph_name]) run_git(['commit', '-m', message]) @@ -493,7 +416,7 @@ class BaseTestSuite(object): (hostname, timeout)) time.sleep(0.5) - def wait_for_ssh(self, host_url, timeout=BOOT_TIMEOUT, **kwargs): + def wait_for_ssh(self, host_url, timeout=config.BOOT_TIMEOUT, **kwargs): print "Waiting for machine to respond over SSH ..." start_time = time.time() while True: @@ -512,8 +435,8 @@ class BaseTestSuite(object): def wait_for_machine_to_boot(self, instance): wait_time = self.wait_for_hostname_to_appear( - instance.vm_name, timeout=BOOT_TIMEOUT) - if VERBOSE: + instance.vm_name, timeout=config.BOOT_TIMEOUT) + if config.VERBOSE: print "Host %s appeared after %0.1f seconds" % \ (instance.vm_name, wait_time) @@ -523,28 +446,15 @@ class BaseTestSuite(object): test_url = 'ssh://root@%s/' % instance.vm_name self.wait_for_ssh( - test_url, timeout=BOOT_TIMEOUT-wait_time) + test_url, timeout=config.BOOT_TIMEOUT-wait_time) - def create_system_branch(self, workspace_dir, name, parent=BRANCH): + def create_system_branch(self, workspace_dir, name, parent=config.BRANCH): run_morph( ['branch', 'baserock:baserock/morphs', name, parent], cwd=workspace_dir) return SystemTestBranch(workspace_dir, name) -@contextlib.contextmanager -def set_directory(path): - ''' - Context manager to set current working directory of a script. - ''' - old_path = os.getcwd() - os.chdir(path) - try: - yield - finally: - os.chdir(old_path) - - class TestUpgrades(BaseTestSuite): @contextlib.contextmanager def given_out_of_date_trove_instance(self, vm_name, fixture_dir, reuse=False): @@ -796,7 +706,7 @@ class SimpleTestRunner(cliapp.Application): def check_access_to_deploy_host(self): # From: https://stackoverflow.com/questions/3830508/check-if-passwordless-access-has-been-setup - deploy_url = urlparse.urlsplit(DEPLOY_URL) + deploy_url = urlparse.urlsplit(config.DEPLOY_URL) assert deploy_url[0] == 'kvm+ssh' try: cliapp.runcmd( @@ -813,7 +723,9 @@ class SimpleTestRunner(cliapp.Application): # it, rather than just ignoring exceptions. def run_virsh(args): try: - remote_runcmd(DEPLOY_URL, ['virsh', '-c', 'qemu:///system'] + args) + remote_runcmd( + config.DEPLOY_URL, + ['virsh', '-c', 'qemu:///system'] + args) except cliapp.AppException as e: pass run_virsh(['destroy', self.vm_name]) @@ -844,12 +756,14 @@ class SimpleTestRunner(cliapp.Application): if self.settings['reuse-fixture'] is not None: fixture_dir = self.settings['reuse-fixture'] else: - fixture_dir = cliapp.runcmd(['mktemp', '-d', '-p', BUILD_TEMPDIR]).strip() + fixture_dir = cliapp.runcmd( + ['mktemp', '-d', '-p', config.BUILD_TEMPDIR]).strip() run_morph(['init', fixture_dir]) try: print "Running %s" % test - workspace_dir = cliapp.runcmd(['mktemp', '-d', '-p', BUILD_TEMPDIR]).strip() + workspace_dir = cliapp.runcmd( + ['mktemp', '-d', '-p', config.BUILD_TEMPDIR]).strip() try: run_morph(['init', workspace_dir]) diff --git a/util.py b/util.py new file mode 100644 index 0000000..8974674 --- /dev/null +++ b/util.py @@ -0,0 +1,90 @@ +# Copyright (C) 2014 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +'''Baserock system-test utility functions.''' + +import cliapp +import contextlib +import sys +import urlparse + +import config + + +def run_morph(args, **kwargs): + '''Run Morph on the current machine.''' + morph_command = config.MORPH_COMMAND + if isinstance(morph_command, str): + morph_command = morph_command.split(' ') + cmd = morph_command + args + if config.VERBOSE: + print ' '.join(cmd) + if 'stdout' not in kwargs: + kwargs['stdout'] = sys.stdout + if 'stderr' not in kwargs: + kwargs['stderr'] = sys.stdout + return cliapp.runcmd(cmd, **kwargs) + + +def run_git(args, **kwargs): + return cliapp.runcmd(['git'] + args, **kwargs) + + +def remote_runcmd(url, command, **kwargs): + ''' + Execute a command on machine 'url'. + + Command must be a list of arguments, not a single string. + + FIXME: perhaps this functionality should be merged into cliapp.ssh_runcmd() + so that we can use that instead. + ''' + if config.VERBOSE: + print "%s: %s" % (url, ' '.join(command)) + url = urlparse.urlsplit(url) + if url[0] in ['ssh', 'kvm+ssh']: + ssh_host = url[1] + + ssh_cmd = ['ssh'] + + # The identity of the newly-created test machine will never be in + # '~/.ssh/known_hosts'; this switch avoids seeing the 'do you want to + # connect' prompt that SSH would normally present in this situation. + ssh_cmd.extend(['-o', 'StrictHostKeyChecking=no']) + + return cliapp.runcmd(ssh_cmd + [ssh_host, ' '.join(command)], **kwargs) + else: + raise NotImplementedError("Remote machine must be an ssh:// URL") + + +def read_file(file_path): + with open(file_path, 'r') as f: + return f.read() + + +def write_file(file_path, text): + with open(file_path, 'w') as f: + f.write(text) + + +@contextlib.contextmanager +def set_directory(path): + '''Context manager to set current working directory of a script.''' + old_path = os.getcwd() + os.chdir(path) + try: + yield + finally: + os.chdir(old_path) -- cgit v1.2.1