def start(self, args="-it -d", command="/bin/bash"):
        """
        start the docker container

        :param args: Do not use it directly (It is defined in config.yaml)
        :param command: Do not use it directly (It is defined in config.yaml)
        :return: None
        """
        if not self.status():
            if self.info.get('start'):
                self.docker_id = self.runHost(
                    "%s -d %s %s" %
                    (self.info['start'], self.docker_static_name, self.name),
                    shell=True,
                    ignore_bg_processes=True,
                    verbose=core.is_not_silent()).stdout
            else:
                self.docker_id = self.runHost(
                    "docker run %s %s %s %s" %
                    (args, self.docker_static_name, self.name, command),
                    shell=True,
                    ignore_bg_processes=True,
                    verbose=core.is_not_silent()).stdout
            self.docker_id = self.docker_id.strip()
            # It installs packages in container is removed by default, in future maybe reconciled.
            # self.install_packages()
        if self.status() is False:
            raise mtfexceptions.ContainerExc(
                "Container %s (for module %s) is not running, probably DEAD immediately after start (ID: %s)"
                % (self.name, self.moduleName, self.docker_id))
            trans_dict["GUESTPACKAGER"] = self.get_packager()
    def __pullContainer(self):
        """
        Internal method, do not use it anyhow

        :return: None
        """
        if self.tarbased:
            self.runHost("docker import %s %s" % (self._icontainer, self.name),
                         verbose=core.is_not_silent())
        elif "docker=" in self._icontainer:
            pass
        else:
            self.runHost("docker pull %s" % self.name,
                         verbose=core.is_not_silent())
    def stop(self):
        """
        Stop the docker container

        :return: None
        """
        if self.status():
            try:
                self.runHost("docker stop %s" % self.docker_id,
                             verbose=core.is_not_silent())
                self.runHost("docker rm %s" % self.docker_id,
                             verbose=core.is_not_silent())
            except Exception as e:
                core.print_debug(e, "docker already removed")
                pass
    def __load_inspect_json(self):
        """
        Load json data from docker inspect command

        :return: dict
        """
        return json.loads(
            self.runHost("docker inspect %s" % self.name,
                         verbose=core.is_not_silent()).stdout)[0]["Config"]
    def copyFrom(self, src, dest):
        """
        Copy file from module

        :param src: str path of file inside module
        :param dest: str path of destination file
        :return: None
        """
        self.start()
        self.runHost("docker cp %s:%s %s" % (self.docker_id, src, dest),
                     verbose=core.is_not_silent())
    def copyTo(self, src, dest):
        """
        Copy file to module

        :param src: str path to source file
        :param dest: str path to file inside module
        :return: None
        """
        self.start()
        self.runHost("docker cp %s %s:%s" % (src, self.docker_id, dest),
                     verbose=core.is_not_silent())
Пример #7
0
    def getActualProfile(self):
        """
        Return actual profile set profile via env variable PROFILE, could be used for filtering tests with skipIf method
        Actually it returns list of packages, because profiles are not defined well

        :return: str
        """
        self.start()
        allpackages = self.run(r'rpm -qa --qf="%{{name}}\n"',
                               verbose=core.is_not_silent()).stdout.split('\n')
        return allpackages
 def __prepare_selinux(self):
     # disable selinux by default if not turned off
     if not os.environ.get('MTF_SKIP_DISABLING_SELINUX'):
         # https://github.com/fedora-modularity/meta-test-family/issues/53
         # workaround because systemd nspawn is now working well in F-26
         if not os.path.exists(selinux_state_file):
             core.print_info("Disabling selinux")
             actual_state = self.runHost("getenforce", ignore_status=True).stdout.strip()
             with open(selinux_state_file, 'w') as openfile:
                 openfile.write(actual_state)
             if setseto not in actual_state:
                 self.runHost("setenforce %s" % setseto,
                              verbose=core.is_not_silent(),
                              sudo=True)
 def __cleanup(self):
     if not os.environ.get('MTF_SKIP_DISABLING_SELINUX'):
         core.print_info("Turning back selinux to previous state")
         actual_state = self.runHost("getenforce", ignore_status=True).stdout.strip()
         if os.path.exists(selinux_state_file):
             core.print_info("Turning back selinux to previous state")
             with open(selinux_state_file, 'r') as openfile:
                 stored_state = openfile.readline()
                 if stored_state != actual_state:
                     self.runHost("setenforce %s" % stored_state,
                                  ignore_status=True,
                                  verbose=core.is_not_silent(),
                                  sudo=True)
             os.remove(selinux_state_file)
         else:
             core.print_info("Selinux state is not stored, skipping.")
    def status(self, command=None):
        """
        get status if container is running

        :return: bool
        """
        if not self.docker_id and common.get_if_reuse():
            result = self.runHost("docker ps -q --filter %s" %
                                  self.docker_static_name[2:],
                                  ignore_status=True,
                                  verbose=core.is_debug())
            # lenght of docker id  number is 12
            if result.exit_status == 0 and len(result.stdout) > 10:
                self.docker_id = result.stdout.strip()
                return True
        if self.docker_id and self.docker_id[:12] in self.runHost(
                "docker ps", shell=True, verbose=core.is_not_silent()).stdout:
            return True
        else:
            return False