Exemple #1
0
    def pre_start(self):
        """
        Create mandatory directories and install files from given templates
        using the drivers context before starting the application binary.
        """
        super(App, self).pre_start()

        self._make_dirs()

        if self.cfg.path_cleanup is True:
            name = os.path.basename(self.cfg.binary)
        else:
            name = "{}-{}".format(os.path.basename(self.cfg.binary),
                                  uuid.uuid4())

        self.binary = self._prepare_binary(self.cfg.binary)
        if os.path.isfile(self.binary):
            target = os.path.join(self._binpath, name)
            if self.cfg.binary_strategy == "copy":
                shutil.copyfile(self.binary, target)
                self.binary = target
            elif self.cfg.binary_strategy == "link" and not IS_WIN:
                os.symlink(os.path.abspath(self.binary), target)
                self.binary = target
            # else binary_strategy is noop then we don't do anything

        makedirs(self.app_path)
        self.std = StdFiles(self.app_path)

        if self.cfg.install_files:
            self._install_files()
Exemple #2
0
    def pre_start(self):
        """
        Create mandatory directories and install files from given templates
        using the drivers context before starting the application binary.
        """
        super(App, self).pre_start()

        self._make_dirs()
        if self.cfg.binary_copy:
            if self.cfg.path_cleanup is True:
                name = os.path.basename(self.cfg.binary)
            else:
                name = '{}-{}'.format(os.path.basename(self.cfg.binary),
                                      uuid.uuid4())

            target = os.path.join(self._binpath, name)
            shutil.copyfile(self.cfg.binary, target)
            self.binary = target
        else:
            self.binary = self.cfg.binary

        makedirs(self.app_path)
        self.std = StdFiles(self.app_path)

        if self.cfg.install_files:
            self._install_files()
Exemple #3
0
    def make_runpath_dirs(self):
        """
        Creates runpath related directories.
        """
        if self._runpath is None:
            raise RuntimeError(
                "{} runpath cannot be None".format(self.__class__.__name__)
            )

        self.logger.test_info(
            f"Testplan has runpath: {self._runpath} and pid {os.getpid()}"
        )

        self._scratch = os.path.join(self._runpath, "scratch")

        if self.cfg.path_cleanup is False:
            makedirs(self._runpath)
            makedirs(self._scratch)
        else:
            makeemptydirs(self._runpath)
            makeemptydirs(self._scratch)

        with open(
            os.path.join(self._runpath, self.runid_filename), "wb"
        ) as fp:
            pass
Exemple #4
0
 def _make_dirs(self):
     bin_dir = os.path.join(self.runpath, 'bin')
     etc_dir = os.path.join(self.runpath, 'etc')
     for directory in (bin_dir, etc_dir):
         makedirs(directory)
     self._binpath = bin_dir
     self._etcpath = etc_dir
Exemple #5
0
    def export(self, source):

        json_path = self.cfg.json_path

        if len(source):
            test_plan_schema = TestReportSchema(strict=True)
            data = test_plan_schema.dump(source).data
            attachments_dir = os.path.join(
                os.path.dirname(json_path), defaults.ATTACHMENTS
            )

            # Save the Testplan report.
            if self.cfg.split_json_report:
                (
                    structure_filename,
                    assertions_filename,
                ) = gen_attached_report_names(json_path)
                structure_filepath = os.path.join(
                    attachments_dir, structure_filename
                )
                assertions_filepath = os.path.join(
                    attachments_dir, assertions_filename
                )

                meta, structure, assertions = self.split_json_report(data)
                makedirs(attachments_dir)

                with open(structure_filepath, "w") as json_file:
                    json.dump(structure, json_file)
                with open(assertions_filepath, "w") as json_file:
                    json.dump(assertions, json_file)

                save_attachments(report=source, directory=attachments_dir)
                meta["version"] = 2
                # Modify dict ref may change the original `TestReport` object
                meta["attachments"] = copy.deepcopy(meta["attachments"])
                meta["attachments"][structure_filename] = structure_filepath
                meta["attachments"][assertions_filename] = assertions_filepath
                meta["structure_file"] = structure_filename
                meta["assertions_file"] = assertions_filename

                with open(json_path, "w") as json_file:
                    json.dump(meta, json_file)
            else:
                save_attachments(report=source, directory=attachments_dir)
                data["version"] = 1

                with open(json_path, "w") as json_file:
                    json.dump(data, json_file)

            self.logger.exporter_info(
                "JSON generated at %s", os.path.abspath(json_path)
            )
        else:
            self.logger.exporter_info(
                "Skipping JSON creation for empty report: %s", source.name
            )
Exemple #6
0
 def pre_start(self):
     super(KafkaStandalone, self).pre_start()
     self.log_path = os.path.join(self.runpath, "log")
     self.etc_path = os.path.join(self.runpath, "etc")
     for directory in (self.log_path, self.etc_path):
         if self.cfg.path_cleanup is False:
             makedirs(directory)
         else:
             makeemptydirs(directory)
     self.config = os.path.join(self.runpath, "etc", "server.properties")
     instantiate(self.cfg.cfg_template, self.context_input(), self.config)
Exemple #7
0
    def _pull_files(self):
        """Pull custom files from remote host."""

        pull_dst = os.path.join(self.runpath, "pulled_files")
        makedirs(pull_dst)

        for entry in self.cfg.pull:
            self._transfer_data(
                source=entry,
                remote_source=True,
                target=pull_dst,
                exclude=self.cfg.pull_exclude,
            )
Exemple #8
0
def save_attachments(report, directory):
    """
    Save the report attachments to the given directory.
    :param report: Testplan report.
    :type report: ``testplan.testing.report.TestReport``
    :param directory: Directory to save attachments in.
    :type directory: ``str``
    """
    attachments = getattr(report, 'attachments', None)
    if attachments:
        for dst, src in attachments.items():
            dst_path = os.path.join(directory, dst)
            makedirs(os.path.dirname(dst_path))
            copyfile(src=src, dst=dst_path)
Exemple #9
0
    def starting(self):
        """
        Create mandatory directories, install files from given templates
        using the drivers context and starts the application binary.
        """
        super(App, self).starting()
        self._make_dirs()
        if self.cfg.binary_copy:
            if self.cfg.path_cleanup is True:
                name = os.path.basename(self.cfg.binary)
            else:
                name = '{}-{}'.format(os.path.basename(self.cfg.binary),
                                      uuid.uuid4())

            target = os.path.join(self._binpath, name)
            shutil.copyfile(self.cfg.binary, target)
            self.binary = target
        else:
            self.binary = self.cfg.binary

        makedirs(self.app_path)
        self.std = StdFiles(self.app_path)

        if self.cfg.install_files:
            self._install_files()

        cmd = ' '.join(self.cmd) if self.cfg.shell else self.cmd
        cwd = self.cfg.working_dir or self.runpath
        try:
            self.logger.debug('{driver} driver command: {cmd},{linesep}'
                              '\trunpath: {runpath}{linesep}'
                              '\tout/err files {out} - {err}'.format(
                                  driver=self.uid(),
                                  cmd=cmd,
                                  runpath=self.runpath,
                                  linesep=os.linesep,
                                  out=self.std.out_path,
                                  err=self.std.err_path))
            self.proc = subprocess.Popen(cmd,
                                         shell=self.cfg.shell,
                                         stdout=self.std.out,
                                         stderr=self.std.err,
                                         cwd=cwd,
                                         env=self.cfg.env)
        except Exception:
            TESTPLAN_LOGGER.error(
                'Error while App[%s] driver executed command: %s',
                self.cfg.name, cmd if self.cfg.shell else ' '.join(cmd))
            raise
Exemple #10
0
 def _pull_files(self):
     """Push custom files to be available on remotes."""
     for entry in [itm.rstrip('/') for itm in self.cfg.pull]:
         # Prepare target path for possible windows usage.
         dirname = os.sep.join(os.path.dirname(entry).split('/'))
         try:
             makedirs(dirname)
         except Exception as exc:
             self.logger.error('Cound not create {} directory - {}'.format(
                 dirname, exc))
         else:
             self._transfer_data(source=entry,
                                 remote_source=True,
                                 target=dirname,
                                 exclude=self.cfg.pull_exclude)
Exemple #11
0
def save_attachments(report: TestReport, directory: str) -> Dict[str, str]:
    """
    Saves the report attachments to the given directory.

    :param report: Testplan report.
    :param directory: directory to save attachments in
    :return: dictionary of destination paths
    """
    moved_attachments = {}
    attachments = getattr(report, "attachments", None)
    if attachments:
        for dst, src in attachments.items():
            dst_path = os.path.join(directory, dst)
            makedirs(os.path.dirname(dst_path))
            copyfile(src=src, dst=dst_path)
            moved_attachments[dst] = dst_path

    return moved_attachments
Exemple #12
0
    def make_runpath_dirs(self):
        """
        Creates runpath related directories.
        """
        self._runpath = self.generate_runpath()
        self._scratch = os.path.join(self._runpath, "scratch")
        if self.runpath is None:
            raise RuntimeError("{} runpath cannot be None".format(
                self.__class__.__name__))
        self.logger.debug("{} has {} runpath and pid {}".format(
            self.__class__.__name__, self.runpath, os.getpid()))

        if self.cfg.path_cleanup is False:
            makedirs(self._runpath)
            makedirs(self._scratch)
        else:
            makeemptydirs(self._runpath)
            makeemptydirs(self._scratch)
    def setup(self, env, result):
        makedirs(os.path.dirname(self._tmp_file))
        if not os.path.exists(self._tmp_file):
            self._iteration = 0
        else:
            with open(self._tmp_file, "r") as fp:
                try:
                    self._iteration = int(fp.read())
                except Exception:
                    self._iteration = 0

        if self._iteration == self._max_retries:  # iter starts from zero
            os.remove(self._tmp_file)
        else:
            with open(self._tmp_file, "w") as fp:
                fp.write(str(self._iteration + 1))

        result.log("Suite setup in iteration {}".format(self._iteration))
Exemple #14
0
    def setup(self, env, result):
        """
        Create a tmp text file which record how many times the suite
        has been executed, should remove it after the last rerun.
        """
        makedirs(os.path.dirname(self._tmp_file))
        if not os.path.exists(self._tmp_file):
            self._iteration = 0
        else:
            with open(self._tmp_file, "r") as fp:
                self._iteration = int(fp.read())

        if self._iteration == self._max_rerun:
            os.remove(self._tmp_file)
        else:
            with open(self._tmp_file, "w") as fp:
                fp.write(str(self._iteration + 1))

        result.log("Suite setup in iteration {}".format(self._iteration))
Exemple #15
0
 def pre_start(self):
     """
     Create mandatory directories and install files from given templates
     using the drivers context before starting zookeeper.
     """
     super(ZookeeperStandalone, self).pre_start()
     self.zkdata_path = os.path.join(self.runpath, "zkdata")
     self.zklog_path = os.path.join(self.runpath, "zklog")
     self.etc_path = os.path.join(self.runpath, "etc")
     self.env["ZOO_LOG_DIR"] = self.zklog_path
     for directory in (self.zkdata_path, self.zklog_path, self.etc_path):
         if self.cfg.path_cleanup is False:
             makedirs(directory)
         else:
             makeemptydirs(directory)
     self.config = os.path.join(self.runpath, "etc", "zookeeper.cfg")
     if self.port == 0:
         raise RuntimeError("Zookeeper doesn't support random port")
     instantiate(self.cfg.cfg_template, self.context_input(), self.config)
Exemple #16
0
    def make_runpath_dirs(self):
        """
        Creates runpath related directories.
        """
        self.define_runpath()
        if self._runpath is None:
            raise RuntimeError(
                f"{self.__class__.__name__} runpath cannot be None")

        self._scratch = os.path.join(self._runpath, "scratch")

        self.logger.debug("%s has %s runpath and pid %d", self, self.runpath,
                          os.getpid())

        if self.cfg.path_cleanup is False:
            makedirs(self._runpath)
            makedirs(self._scratch)
        else:
            makeemptydirs(self._runpath)
            makeemptydirs(self._scratch)
Exemple #17
0
    def __init__(
        self,
        dirpath,
        description=None,
        ignore=None,
        only=None,
        recursive=False,
        dst_path=None,
        scratch_path=None,
    ):

        self.source_path = dirpath
        self.ignore = ignore
        self.only = only
        self.recursive = recursive
        self.dst_path = (dst_path or hashlib.md5(
            self.source_path.encode("utf-8")).hexdigest())
        self.file_list = traverse_dir(
            self.source_path,
            topdown=True,
            ignore=ignore,
            only=only,
            recursive=recursive,
            include_subdir=False,
        )

        if scratch_path:
            # will best effort make a copy of the file
            for fpath in self.file_list:
                src_path = os.path.join(self.source_path, fpath)
                dst_path = os.path.join(scratch_path, self.dst_path, fpath)
                makedirs(os.path.dirname(dst_path))
                try:
                    shutil.copyfile(src_path, dst_path)
                except Exception:
                    break
            else:
                self.source_path = os.path.join(scratch_path, self.dst_path)

        super(Directory, self).__init__(description=description)