def test_custom_message(self, os_system): with self.assertRaisesRegexp(RuntimeError, r"Custom mesage '\*\*secret\*\*'"): cmd(command="mycommand", secret=True, error_msg="Custom mesage '{command}'") os_system.assert_called_once_with('mycommand')
def copy(self, origin, tgt): # TODO: check if it is running? command = "docker cp {origin} {name}:{tgt}".format(origin=origin, name=self.name, tgt=tgt) cmd(command=command, error_msg="Error copying file to container: {command}")
def upload(recipe, username, channel, dry_run=False): # TODO: Check requirements in a free function so it can be called before it all starts. # Check requirements: LOGIN_USERNAME = get_env("CONAN_LOGIN_USERNAME", get_env("CONAN_USERNAME", None)) REMOTE = get_env("CONAN_UPLOAD", None) PASSWORD = get_env("CONAN_PASSWORD", None) if not REMOTE: log.error("No remote provided in 'CONAN_UPLOAD' env variable") return if not PASSWORD or not LOGIN_USERNAME: raise ValueError( "No password or username provided for remote '{}'. Use env variable 'CONAN_PASSWORD' and 'CONAN_LOGIN_USERNAME' to provide them." .format(REMOTE)) # Add remote and upload log.info("Add conan remote: {url}".format(url=REMOTE)) with conan.remote(url=REMOTE) as remote_name: log.info("Authenticate user '{username}' in remote {url}".format( username=LOGIN_USERNAME, url=REMOTE)) conan.remote_auth(remote_name, LOGIN_USERNAME, PASSWORD) # Upload command package_ref = "{}/{}@{}/{}".format(recipe.name, recipe.version, username, channel) command = "conan upload -r {} --all --force --confirm {}".format( remote_name, package_ref ) # TODO: Need 'sudo' because the packages may have been created using another user (inside docker). Fix this, how? if not dry_run: cmd(command)
def _run(self): if self._is_running: self._stop() with temporary_env_file() as env_file: env_file_param = "--env-file={}".format(env_file) cmd(command=self._running.format(env_file=env_file_param), error_msg="Error running container: {command}") self._is_running = True
def remote(url, name=None, insert_first=True): name = name or uuid.uuid4() command = "conan remote add {name} {url}".format(name=name, url=url) if insert_first: command += " --insert 0" cmd(command) yield name cmd("conan remote remove {name}".format(name=name))
def remote_auth(remote, username, password): # pragma: no cover command = "conan user -p {password} -r {remote} {username}".format( username=username, remote=remote, password=password) cmd(command, secret=True)
def run_in_docker(self, command, sudo=True): # TODO: check if docker is already running? sudoer = "sudo " if sudo else '' return cmd( command="docker exec {name} /bin/sh -c \"{sudoer}{command}\"". format(name=self.name, command=command, sudoer=sudoer))
def _stop(self): cmd(command="docker stop {name}".format(name=self.name), exception=None) self._is_running = False
def pull(self): command = "docker pull {}".format(self.image) cmd(command=command, error_msg="Error pulling docker image '{}': {{command}}".format( self.image))
def test_custom_exception(self, os_system): with self.assertRaises(IndexError): cmd(command="mycommand", exception=IndexError) os_system.assert_called_once_with('mycommand')
def test_no_exception(self, os_system): r = cmd(command="mycommand", exception=None) os_system.assert_called_once_with('mycommand') self.assertEqual(r, -1)
def test_fail_secret(self, os_system): with self.assertRaisesRegexp( RuntimeError, r"Error running command '\*\*secret\*\*'"): cmd(command="mycommand", secret=True) os_system.assert_called_once_with('mycommand')
def test_fail(self, os_system): with self.assertRaisesRegexp(RuntimeError, "Error running command 'mycommand'"): cmd(command="mycommand") os_system.assert_called_once_with('mycommand')
def test_success_secret(self, os_system): cmd(command="mycommand", secret=True) os_system.assert_called_once_with('mycommand')
def test_success(self, os_system): cmd(command="mycommand") os_system.assert_called_once_with('mycommand')
def cmd(self, command): log.info("command to run: {}".format(command)) if not self.dry_run: ret = cmd(command=command, exception=None) return SUCCESS if ret == 0 else FAIL return DRY_RUN