def is_module_installed(self, module): from infi.execute import execute_assert_success, ExecutionError try: execute_assert_success("bin/python -c import {}".format(module).split()) except (OSError, ExecutionError): # pragma: no cover return False return True
def test_jish_and_file(self): with unstaged_files_context(): execute_assert_success(["git", "add", "foo"]) with open("foo", "w"): pass self.assertEquals(jissue(["commit", "just a message", "--file=foo"], dict(JISSUE_ISSUE="HOSTDEV-955")), 0) self.assertIn("just a message", execute_assert_success("git log", shell=True).get_stdout())
def test_homepage__old_setup_in(self): from os.path import abspath, dirname, exists from shutil import copy with temporary_directory_context(): execute_assert_success("projector repository init infi.test https://github.com/Infinidat/infi.test short long".split()) execute_assert_success("projector devenv build --no-scripts".split()) exists(abspath("./setup.py"))
def easy_install(self, package_name, release_version=None): from infi.execute import execute_assert_success from os import path easy_install = path.join(self._bindir, "easy_install") requiremement = package_name if release_version is None else "{0}=={1}".format(package_name, release_version) logger.info("installing {0}, version {1}".format(package_name, release_version)) execute_assert_success([easy_install, "-i", "http://pypi.python.org/simple", "-U", requiremement])
def restart_cinder(cinder_volume_only=True): if not is_devstack(): execute_assert_success(["openstack-service", "restart", "cinder-volume" if cinder_volume_only else "cinder"]) sleep(10 if cinder_volume_only else 60) # give time for the volume drive to come up, no APIs to checking this else: cmdline = execute_assert_success(RESTART_CINDER_DEVSTACK_CMDLINE, shell=True) sleep(120)
def setUpClass(cls): if not path.exists("/usr/bin/cinder"): raise SkipTest("openstack not installed") if 'centos' not in get_platform_string() and 'redhat' not in get_platform_string(): raise SkipTest("not centos or redhat") execute_assert_success(["yum", "install", "-y", "python-devel"]) execute_assert_success(["rm", "-rf", "dist"])
def test_via_buildout(self): pid = self.start_python() time.sleep(1) self.assertFalse(pid.is_finished()) execute_assert_success([os.path.join('bin', 'buildout' + EXTENSION), 'install', 'close-application']) pid.poll() self.assertTrue(pid.is_finished())
def init_branches(self): from infi.execute import execute_assert_success from os import curdir from gitpy import LocalRepository repository = LocalRepository(curdir) branches = [branch.name for branch in repository.getBranches()] remotes = repository.getRemotes() remote_branches = [] if len(remotes) > 0: remote_branches = [ branch.name for branch in remotes[0].getBranches() ] if 'master' not in branches: if 'master' in remote_branches: execute_assert_success( "git branch --track master origin/master", shell=True) else: execute_assert_success( "git symbolic-ref HEAD refs/heads/master", shell=True) execute_assert_success( "git commit --allow-empty -m \"Initial commit\"", shell=True) if 'develop' not in branches: if 'develop' in remote_branches: execute_assert_success( "git branch --track develop origin/develop", shell=True) else: execute_assert_success("git branch --no-track develop master", shell=True)
def with_new_devlopment_config_file(self): with with_tempfile() as tempfile: cmd = "bin/app_repo dump defaults --development > {}".format(tempfile) execute_assert_success(cmd, shell=True) cmd = "bin/app_repo -f {} remote set repo.lab.il.infinidat.com beats me".format(tempfile) execute_assert_success(cmd, shell=True) yield tempfile
def _release_version_in_git(version_tag): from infi.execute import execute_assert_success execute_assert_success("git checkout master", shell=True) execute_assert_success("git merge develop --no-ff -m \"Finished Release {}\"".format(version_tag), shell=True) execute_assert_success("git tag -a {0} -m {0}".format(version_tag), shell=True) execute_assert_success("git checkout develop", shell=True) execute_assert_success("git merge master", shell=True)
def test_homepage__github(self): execute_assert_success("git init .".split()) execute_assert_success( "git remote add origin git://github.com/Infinidat/infi.recipe.template.version.git".split() ) homepage = Recipe.get_homepage() self.assertEqual(homepage, "https://github.com/Infinidat/infi.recipe.template.version")
def virtualenv(): from infi.execute import execute_assert_success from ..mirror.mirror_build import tempdir with tempdir() as path: logger.info("Creating virtualenv in {0}".format(path)) execute_assert_success(["virtualenv", "--no-site-packages", path]) yield Env.from_virtualenv(path)
def build(): from sys import maxsize from os import environ environ = environ.copy() buildout_file = 'buildout-build.cfg' if system() == 'Linux': from platform import dist, linux_distribution _, version, distid = linux_distribution() dist_name = dist()[0].lower() if dist_name == 'ubuntu': if version == '16.04': buildout_file = 'buildout-build-ubuntu-16.04.cfg' else: buildout_file = 'buildout-build-ubuntu.cfg' if dist_name in ['redhat', 'centos'] and maxsize > 2**32: arch = execute_assert_success(["uname", "-i"]).get_stdout().lower() if 'ppc64le' in arch: buildout_file = 'buildout-build-redhat-ppc64le.cfg' elif 'ppc64' in arch: buildout_file = 'buildout-build-redhat-ppc64.cfg' else: buildout_file = 'buildout-build-redhat-64bit.cfg' if dist_name in ['suse'] and version in ['10']: buildout_file = 'buildout-build-suse-10.cfg' elif system() == 'Darwin': from platform import mac_ver environ["MACOSX_DEPLOYMENT_TARGET"] = '.'.join(mac_ver()[0].split( '.', 2)[:2]) gcc_version = execute_assert_success(["gcc", "--version"]).get_stdout() if 'version 5.' in gcc_version: buildout_file = 'buildout-build-osx-xcode-5.cfg' elif 'version 6.' in gcc_version: buildout_file = 'buildout-build-osx-xcode-6.cfg' elif 'version 7.' in gcc_version: buildout_file = 'buildout-build-osx-xcode-7.cfg' elif 'version 8.' in gcc_version: buildout_file = 'buildout-build-osx-xcode-8.cfg' else: buildout_file = 'buildout-build-osx.cfg' elif system() == 'Windows': if maxsize > 2**32: buildout_file = 'buildout-build-windows-64bit.cfg' else: buildout_file = 'buildout-build-windows.cfg' elif system() == "SunOS": if 'sparc' in execute_assert_success(["isainfo"]).get_stdout().lower(): buildout_file = 'buildout-build-solaris-sparc.cfg' elif '64' in execute_assert_success(["isainfo", "-b"]).get_stdout(): buildout_file = 'buildout-build-solaris-64bit.cfg' else: pass # TODO support 32 bit elif system() == "AIX": from os import uname aix_version = "{0[3]}.{0[2]}".format(uname()) if aix_version == "7.1": buildout_file = 'buildout-build-aix.cfg' elif aix_version == "7.2": buildout_file = 'buildout-build-aix-7.2.cfg' execte_buildout(buildout_file, environ)
def start(self): process = execute_assert_success(['iscsicli', 'listtargets']) output = process.get_stdout().splitlines() for line in output: if "infinibox" in line: execute_assert_success( ['iscsicli', 'QLoginTarget', line.strip()])
def test_vesion_tag_in_release_branch(self): execute_assert_success('git init .'.split()) execute_assert_success('git commit --allow-empty -m empty'.split()) execute_assert_success('git tag -a v0.0.alpha -m tag'.split()) execute_assert_success('git commit --allow-empty -m empty'.split()) execute_assert_success('git commit --allow-empty -m empty'.split()) version = Recipe.extract_version_tag() self.assertTrue('v0.0.alpha.post2' in version)
def install_context(self, package): from glob import glob non_infinidat_packages = [i for i in glob("parts/*deb") if "python-infinidat-openstack" not in i] execute_assert_success(["dpkg", "-i", package] + non_infinidat_packages) try: yield finally: execute_assert_success(["dpkg", "-r", "python-infinidat-openstack"])
def platform_specific_cleanup(cls): env = os.environ.copy() env['NO_CUSTOM_ACTIONS'] = '1' execute_assert_success(['dpkg', '-r', PACKAGE_NAME], env=env, allowed_return_codes=[0, 1, 2]) if os.path.exists(PREFIX): shutil.rmtree(PREFIX)
def execute_with_buildout(commandline_or_args, env=None): from os import name, path, environ _env = environ.copy() if env: _env.update(env) args = parse_args(commandline_or_args) execute_assert_success([path.join('bin', 'buildout{}'.format('.exe' if name == 'nt' else ''))] + BUILDOUT_PARAMETERS + args, env=_env)
def install_readline(self): from infi.execute import execute_assert_success, ExecutionError module = self.get_readline_module() if not module or self.is_module_installed(module): # pragma: no cover return try: execute_assert_success("bin/easy_install {}".format(module).split()) except (OSError, ExecutionError): # pragma: no cover logger.warn("distribute is not a requirements, not installing readline support")
def execute_with_isolated_python(commandline_or_args): import os from ..assertions import is_windows args = parse_args(commandline_or_args) executable = [get_isolated_executable('python')] with open_buildout_configfile() as buildout: if buildout.get('buildout', 'relative-paths') in ['True', 'true']: [executable] = os.path.abspath(executable[0]) execute_assert_success(executable + args)
def execute_with_isolated_python(commandline_or_args): import os from ..assertions import is_windows args = parse_args(commandline_or_args) executable = [os.path.join('parts', 'python', 'bin', 'python{}'.format('.exe' if is_windows() else ''))] with open_buildout_configfile() as buildout: if buildout.get('buildout', 'relative-paths') in ['True', 'true']: [executable] = os.path.abspath(executable[0]) execute_assert_success(executable + args)
def test_homepage__github(self): execute_assert_success('git init .'.split()) execute_assert_success( "git remote add origin git://github.com/Infinidat/infi.recipe.template.version.git" .split()) homepage = Recipe.get_homepage() self.assertEqual( homepage, "https://github.com/Infinidat/infi.recipe.template.version")
def set_source_iqn(self, iqn): '''receives a string, validates it's an iqn then set it to the host NOTE: this restart the iscsi service and may fail active sessions ! in Solaris, this doesn't save a copy of the old IQN ''' IQN(iqn) # checks iqn is valid old_iqn = self.get_source_iqn() # check file exist and valid execute_assert_success(['iscsiadm', 'modify', 'initiator-node', '-N', iqn]) logger.info("iqn was replaced from {} to {}".format(old_iqn, iqn))
def test_022_iscsiapi_reset_source_iqn_windows(self): from infi.execute import execute_assert_success from infi.dtypes.iqn import IQN, InvalidIQN if not get_platform_string().startswith('windows'): raise SkipTest("windows test skipping other platforms") execute_assert_success(['iscsicli', 'NodeName', 'invalid.iqn']) self.assertRaises(InvalidIQN, self.iscsiapi.get_source_iqn) self.iscsiapi.reset_source_iqn() self.assertEqual(IQN, type(self.iscsiapi.get_source_iqn()))
def build_two_packages(self): first = self.build() execute_assert_success(["git", "commit", "--allow-empty", "--message", "testing package upgrade"]) execute_assert_success(["bin/buildout", "buildout:develop=", "install", "setup.py", "__version__.py"]) def _revert(): execute_assert_success(["git", "reset", "--hard", "HEAD^"]) execute_assert_success(["bin/buildout", "buildout:develop=", "install", "setup.py", "__version__.py"]) self.addCleanup(_revert) second = self.build() return first, second
def test_homepage__old_setup_in(self): from os.path import abspath, dirname, exists from shutil import copy with temporary_directory_context(): execute_assert_success( "projector repository init infi.test https://github.com/Infinidat/infi.test short long" .split()) execute_assert_success( "projector devenv build --no-scripts".split()) exists(abspath("./setup.py"))
def get_script(self, config, script_name, index_name="main-stable", package=None, version=None): url = "http://localhost:%s/%s/%s" % (config.webserver.port, script_name, index_name) if package: url += "/%s" % package if version: url += "/%s" % version filename = str(uuid4()) logger.debug(url) execute_assert_success(["wget", url, "-O", filename]) return path.abspath(filename)
def set_source_iqn(self, iqn): '''receives a string, validates it's an iqn then set it to the host NOTE: this restart the iscsi service and may fail active sessions ! in Solaris, this doesn't save a copy of the old IQN ''' _ = IQN(iqn) # checks iqn is valid old_iqn = self.get_source_iqn() # check file exist and valid execute_assert_success( ['iscsiadm', 'modify', 'initiator-node', '-N', iqn]) logger.info("iqn was replaced from {} to {}".format(old_iqn, iqn))
def _remove_missing_multipath_devices(self): devices = self._get_all_devices(True) for device in devices: try: # try to send an IO to make the OS refresh the state path device.get_scsi_standard_inquiry() except DeviceError: pass if all(path.get_state() == "down" for path in device.get_paths()): execute_assert_success(["rmdev", "-dl", device.get_display_name()])
def get_hctl(self): """Returns a `infi.dtypes.hctl.HCTL` object""" proc = execute_assert_success(["/usr/sbin/lsdev", "-F", "parent", "-l", self._name]) driver = proc.get_stdout().decode().strip() host = self._get_host_by_driver(driver) proc = execute_assert_success(["/usr/sbin/lsattr", "-F", "value", "-a", "ww_name", "-a", "lun_id", "-E", "-l", self._name]) target, lun = proc.get_stdout().decode().strip().split("\n") target = int(target, 16) lun = int(lun, 16) >> 48 return HCTL(host, 0, target, lun)
def _release_version_in_git(version_tag): from infi.execute import execute_assert_success execute_assert_success("git checkout master", shell=True) execute_assert_success( "git merge develop --no-ff -m \"Finished Release {}\"".format( version_tag), shell=True) execute_assert_success("git tag -a {0} -m {0}".format(version_tag), shell=True) execute_assert_success("git checkout develop", shell=True) execute_assert_success("git merge master", shell=True)
def _is_vol_mapped(volume_serial, timeout=3): from time import sleep for n in range(0, timeout): execute_assert_success(['powershell', '-c', 'Update-HostStorageCache']) try: execute_assert_success(['powershell', '-c', 'Get-Disk', '-SerialNumber', str(volume_serial)]) return True except: sleep(1) continue return False
def rescan_method(self): res = execute_command("cfgadm -lao show_SCSI_LUN".split(), check_returncode=False) if res.get_returncode() not in (0, 2): raise ExecutionError(res) execute_assert_success("devfsadm -vC".split()) execute_assert_success("devfsadm -r / -p /etc/path_to_inst".split()) try: execute("vxdctl enable".split()) # TODO execute only if veritas is installed except OSError: pass return 0
def _execute_discover(self, ip_address, port): from .iscsi_exceptions import DiscoveryFailed try: execute_assert_success(['iscsicli', 'AddTargetPortal', str(ip_address), str(port)]) except ExecutionError as e: msg = "couldn't connect to ip_address {!r}, error: {!r}" + \ "This could be due to one of the following reasons:" + \ "1. There is no IP connectivity between your host and {!r}" + \ "2. The iSCSI server is down" formated_msg = msg.format(ip_address, e, ip_address) logger.error(formated_msg) raise DiscoveryFailed(formated_msg)
def new_repository_context(self, origin_url, expected_homepage): with temporary_directory_context(): execute_assert_success("projector repository init infi.test {0} short long".format(origin_url).split()) with open("setup.in") as fd: setup_in = fd.read() with open("setup.in", "w") as fd: fd.write(setup_in.replace("url = 'http://www.infinidat.com'", "url = ${infi.recipe.template.version:homepage}")) yield execute_assert_success("projector devenv build --no-scripts".split()) with open("setup.py") as fd: actual_homepath = "url = {0},".format(None if expected_homepage is None else repr(expected_homepage)) self.assertIn(actual_homepath, fd.read())
def get_hctl(self): """Returns a `infi.dtypes.hctl.HCTL` object""" driver = execute_assert_success( ["/usr/sbin/lsdev", "-F", "parent", "-l", self._name]).get_stdout().strip() host = self._get_host_by_driver(driver) target, lun = execute_assert_success([ "/usr/sbin/lsattr", "-F", "value", "-a", "ww_name", "-a", "lun_id", "-E", "-l", self._name ]).get_stdout().strip().split("\n") target = int(target, 16) lun = int(lun, 16) >> 48 return HCTL(host, 0, target, lun)
def rescan_method(self): res = execute_command("cfgadm -lao show_SCSI_LUN".split(), check_returncode=False) if res.get_returncode() not in (0, 2): raise ExecutionError(res) execute_assert_success("devfsadm -vC".split()) execute_assert_success("devfsadm -r / -p /etc/path_to_inst".split()) try: execute("vxdctl enable".split() ) # TODO execute only if veritas is installed except OSError: pass return 0
def execute_with_buildout(commandline_or_args, env=None, stripped=True): from os import name, path, environ _env = environ.copy() if env: _env.update(env) args = parse_args(commandline_or_args) python = path.join('bin', 'python{}'.format('.exe' if name == 'nt' else '')) buildout = path.join('bin', 'buildout{}'.format('.exe' if name == 'nt' else '')) buildout_script = path.join('bin', 'buildout{}'.format('-script.py' if name == 'nt' else '')) if path.exists(python) and not stripped: execute_assert_success([python, buildout_script] + BUILDOUT_PARAMETERS + args, env=_env) else: execute_assert_success([buildout] + BUILDOUT_PARAMETERS + args, env=_env)
def unstaged_files_context(): from tempfile import mkdtemp from os import path, chdir curdir = path.abspath(path.curdir) tempdir = mkdtemp() chdir(tempdir) execute_assert_success(["git", "init", "."]) with open("foo", "w") as fd: fd.write("bar") try: yield finally: chdir(curdir)
def pull_package(remote_fqdn, base_directory, packge_uri): from infi.execute import execute_assert_success from os import path, makedirs from shutil import move with temporary_directory_context(): filename = path.basename(packge_uri) if path.exists(filename): return url = "ftp://{0}/{1}".format(remote_fqdn, packge_uri.strip('/')) execute_assert_success(["wget", url]) dst = path.join(base_directory, "incoming", filename) if not path.exists(path.dirname(dst)): makedirs(path.dirname(dst)) move(filename, dst)
def _execute_discover(self, ip_address, port): from .iscsi_exceptions import DiscoveryFailed try: execute_assert_success( ['iscsicli', 'AddTargetPortal', str(ip_address), str(port)]) except ExecutionError as e: msg = "couldn't connect to ip_address {!r}, error: {!r}" + \ "This could be due to one of the following reasons:" + \ "1. There is no IP connectivity between your host and {!r}" + \ "2. The iSCSI server is down" formated_msg = msg.format(ip_address, e, ip_address) logger.error(formated_msg) raise DiscoveryFailed(formated_msg)
def create_console_scripts(): from infi.execute import execute_assert_success for name in CONSOLE_SCRIPTS: execute_assert_success( [ os.path.join("bin", "projector"), "console-scripts", "add", name, "infi.recipe.application_packager.scripts:{0}".format(name), "--commit-changes", ] ) execute_assert_success([os.path.join("bin", "projector"), "devenv", "build", "--no-scripts"])
def test_01_fs_create(self): for fs in fs_names: # size = self._get_random_size() size = "1GB" cmd = [ 'smbmgr', 'fs', 'create', '--name={}'.format(fs), '--size={}'.format(size) ] result = execute_assert_success(cmd).get_stdout() if outputs.fs_delete in result: raise cmd = ['smbmgr', 'fs', 'query'] result_out = execute_assert_success(cmd).get_stdout() for fs in fs_names: self.assertIn('{}'.format(fs), result_out)
def mirror_git_repository(from_url, to_url): from infi.execute import execute_assert_success with with_tempdir(): execute_assert_success(command=["git", "clone", "--bare", from_url, "clone"]) with chdir("clone"): execute_assert_success(command=["git", "remote", "add", "github", to_url]) execute_assert_success(command=["git", "push", "github", "--all"]) execute_assert_success(command=["git", "push", "github", "--tags"])
def _get_dev_by_class(self, cls_name): proc = execute_assert_success( ["/usr/sbin/lsdev", "-c", cls_name, "-F", "name"]) output = proc.get_stdout().strip() if not output: return [] return [line.strip() for line in output.split("\n")]
def execute_with_python(commandline_or_args): import os import sys from ..assertions import is_windows args = parse_args(commandline_or_args) executable = [get_python_interpreter()] if not is_running_inside_virtualenv(): executable.append('-S') try: execute_assert_success(executable + args) except PrettyExecutionError: if '-S' not in executable: raise logger.warning("Command failed with -S, trying without") executable.remove('-S') execute_assert_success(executable + args)
def _run_prep_vol_to_cluster_script(fs): # TODO: move to run # TODO: handle the cases where fs.get_winid is None from smb import PROJECTROOT from os import path vol_to_cluster_script = path.realpath( path.join(PROJECTROOT, 'src', 'smb', 'cli', 'powershell', 'prep_vol_to_cluster.ps1')) if fs.get_winid() is None: log_n_raise(logger, "Can't prepare volume {}".format(fs.get_name()), level=ERROR, color="red") try: cmd = execute_assert_success([ 'powershell', '.', '"' + vol_to_cluster_script.replace('\\', '/') + '"' + " -DiskNumber {} -MountPath {}".format( fs.get_winid(), fs.get_mountpoint()) ]) except: error = sys.exc_info()[1] log_n_raise(logger, "{} failed with error: {}".format(vol_to_cluster_script, error), disable_print=True)
def test_05_share_query(self): cmd = ['smbmgr', 'share', 'query'] result = execute_assert_success(cmd).get_stdout() self.assertIn(outputs.share_query_header, result) self.assertIn('share1', result) self.assertIn('share 2', result) self.assertIn('long_share_3...', result)
def file_type_contains(filepath, output): try: return output in execute_assert_success(['file', filepath]).get_stdout() except ExecutionError: logger.exception('failed to determine file type: {0}'.format(filepath)) return False
def am_I_master(): from platform import node config = config_get(silent=True) cmd = execute_assert_success(['powershell', '-c', 'Get-ClusterGroup', '-name', config['FSRoleName'], '|', 'Select-Object', '-ExpandProperty', 'OwnerNode', '|', 'Select-Object', '-ExpandProperty', 'name']) if cmd.get_stdout().strip() == node(): return True else: log_n_raise(logger, "The Node you are running on is NOT the Active Cluster Node")
def _execute_assert_n_log(self, cmd, log_prefix='running: ', log_level='debug'): try: getattr(logger, str(log_level))(log_prefix + "{}".format( cmd if isinstance(cmd, basestring) else ' '.join(cmd))) except AttributeError as e: logger.error("logger.{} doesn't exist, {!r}".format(log_level, e)) return execute_assert_success(cmd)
def test_03_fs_detach_attach(self): cmd = [ 'smbmgr', 'fs', 'create', '--name=detachable_fs', '--size={}'.format(self._get_random_size()) ] execute_assert_success(cmd) cmd = ['smbmgr', 'fs', 'detach', '--name=detachable_fs', '--yes'] execute_assert_success(cmd) cmd = ['smbmgr', 'fs', 'attach', '--name=detachable_fs', '--yes'] execute_assert_success(cmd) cmd = ['smbmgr', 'fs', 'delete', '--name=detachable_fs', '--yes'] execute_assert_success(cmd)
def execute_with_buildout(commandline_or_args, env=None, stripped=True): from os import name, path, environ _env = environ.copy() if env: _env.update(env) args = parse_args(commandline_or_args) python = path.join('bin', 'python{}'.format('.exe' if name == 'nt' else '')) buildout = path.join('bin', 'buildout{}'.format('.exe' if name == 'nt' else '')) buildout_script = path.join( 'bin', 'buildout{}'.format('-script.py' if name == 'nt' else '')) if path.exists(python) and not stripped: execute_assert_success([python, buildout_script] + BUILDOUT_PARAMETERS + args, env=_env) else: execute_assert_success([buildout] + BUILDOUT_PARAMETERS + args, env=_env)