예제 #1
0
파일: behave.py 프로젝트: jmtd/ctf-cli
    def run(self):
        """
        Run Behave and pass some runtime arguments
        :return:
        """
        image = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_IMAGE)
        dockerfile = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_DOCKERFILE)
        junit = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_JUNIT)

        # configuration file for ansible
        if self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_EXEC_TYPE) == 'ansible':
            ansible_conf = self._working_dir_obj.exec_type_conf_path()
        else:
            ansible_conf = None

        command = [
            'behave',
            '-D', 'DOCKERFILE={0}'.format(dockerfile),
        ]

        if junit:
            command.append('--junit')
            command.append('--junit-directory={0}'.format(junit))

        if image:
            command.extend(['-D', 'IMAGE={0}'.format(image)])

        if ansible_conf:
            command.extend(['-D', 'ANSIBLE={0}'.format(ansible_conf)])

        logger.info("Running behave inside working directory '%s'", ' '.join(command))
        return call(command, cwd=self._working_dir_obj.path())
예제 #2
0
파일: application.py 프로젝트: jmtd/ctf-cli
    def run(self):
        """
        The main application execution method
        """
        logger.info("Running Containers Testing Framework cli")

        # If no Dockerfile passed on the cli, try to use one from the execution directory
        if not self._cli_conf.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_DOCKERFILE):
            local_file = os.path.join(self._execution_dir_path, 'Dockerfile')
            if not os.path.isfile(local_file):
                raise CTFCliError("No Dockerfile passed on the cli and no Dockerfile "
                                  "is present in the current directory!")
            logger.debug("Using Dockerfile from the current directory.")
            self._cli_conf.set(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_DOCKERFILE, local_file)

        # TODO: Remove this or rework, once more types are implemented
        if self._cli_conf.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_EXEC_TYPE) != 'ansible':
            raise CTFCliError("Wrong ExecType configured. Currently only 'ansible' is supported!")

        self._working_dir = BehaveWorkingDirectory(self._working_dir_path, self._cli_conf)

        # Setup Behave structure inside working directory
        # Clone common Features and steps into the working dir
        # Add the project specific Features and steps
        # Prepare the steps.py in the Steps dir that combines all the other
        self._working_dir.setup()

        # Execute Behave
        self._behave_runner = BehaveRunner(self._working_dir, self._cli_conf)
        return self._behave_runner.run()
예제 #3
0
    def run(self):
        """
        The main application execution method
        """
        logger.info("Running Containers Testing Framework cli")

        try:
            check_call("git submodule update --init", shell=True)
        except:
            pass

        # TODO: Remove this or rework, once more types are implemented
        if self._cli_conf.get(
                CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_EXEC_TYPE) != 'ansible':
            raise CTFCliError("Wrong ExecType configured. Currently only 'ansible' is supported!")

        self._working_dir = BehaveWorkingDirectory(self._working_dir_path, self._cli_conf)

        # Setup Behave structure inside working directory
        # Clone common Features and steps into the working dir
        # Add the project specific Features and steps
        # Prepare the steps.py in the Steps dir that combines all the other
        self._working_dir.setup()

        # Execute Behave
        self._behave_runner = BehaveRunner(self._working_dir, self._cli_conf)
        sys.exit(self._behave_runner.run())
예제 #4
0
    def update(self):
        """
        Update app submodules
        """
        logger.info("Updating remote test and features")

        check_call("git submodule foreach git pull --rebase origin master", shell=True)
예제 #5
0
    def run():
        try:
            # add application-wide debug log
            LoggerHelper.add_debug_log_file(os.getcwd())

            args = ArgumentsParser(sys.argv[1:])

            if args.verbose is True:
                LoggerHelper.add_stream_handler(logger,
                                                logging.Formatter('%(levelname)s:\t%(message)s'),
                                                logging.DEBUG)
            else:
                LoggerHelper.add_stream_handler(logger,
                                                logging.Formatter('%(levelname)s:\t%(message)s'),
                                                logging.INFO)

            app = Application(args)
            app.run()
        except KeyboardInterrupt:
            logger.info('Interrupted by user')
        except CTFCliError as e:
            logger.error('%s', str(e))
            sys.exit(1)
        else:
            sys.exit(0)
        finally:
            logger.debug('Exiting...')
예제 #6
0
파일: behave.py 프로젝트: vpavlin/ctf-cli
    def run(self):
        """
        Run Behave and pass some runtime arguments
        :return:
        """
        image = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_IMAGE)
        dockerfile = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_DOCKERFILE)

        # configuration file for ansible
        if self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_EXEC_TYPE) == 'ansible':
            ansible_conf = self._working_dir_obj.exec_type_conf_path()
        else:
            ansible_conf = None

        command = [
            'behave',
            '-D', 'DOCKERFILE={0}'.format(dockerfile),
        ]

        if image:
            command.extend(['-D', 'IMAGE={0}'.format(image)])

        if ansible_conf:
            command.extend(['-D', 'ANSIBLE={0}'.format(ansible_conf)])

        logger.info("Running behave inside working directory '%s'", ' '.join(command))
        return call(command, cwd=self._working_dir_obj.path())
예제 #7
0
파일: behave.py 프로젝트: SirEOF/ctf-cli
    def run(self):
        """
        Run Behave and pass some runtime arguments
        :return:
        """

        behave_data = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME,
                                             CTFCliConfig.CONFIG_BEHAVE_DATA)
        behave_tags = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME,
                                             CTFCliConfig.CONFIG_BEHAVE_TAGS)
        junit = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME,
                                       CTFCliConfig.CONFIG_JUNIT)
        verbose = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME,
                                         CTFCliConfig.CONFIG_VERBOSE)

        # configuration file for ansible
        if self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME,
                                  CTFCliConfig.CONFIG_EXEC_TYPE) == 'ansible':
            ansible_conf = self._working_dir_obj.exec_type_conf_path()
        else:
            ansible_conf = None

        command = ['behave']
        if verbose == "yes":
            command.append('-v')

        if junit:
            command.append('--junit')
            command.append('--junit-directory={0}'.format(junit))

        if behave_data:
            #FIXME hacky otherwise it return string - seems like wierd buig in config.py
            if type(behave_data) is str:
                behave_data = behave_data.split('\n')
            for userdata in behave_data:
                command.extend(['-D', '{0}'.format(userdata)])

        if behave_tags:
            #FIXME hacky otherwise it return string - seems like wierd buig in config.py
            if type(behave_tags) is str:
                behave_tags = behave_tags.split('\n')
            for tag in behave_tags:
                command.extend(['-t', '{0}'.format(tag)])
            if verbose != "yes":
                command.append('--no-skipped')

        if ansible_conf:
            command.extend(['-D', 'ANSIBLE={0}'.format(ansible_conf)])

        logger.info("Running behave inside working directory '%s'",
                    ' '.join(command))
        return call(command, cwd=self._working_dir_obj.path())
예제 #8
0
 def _add_remote_features(self):
     """
     Add all remote features
     :return:
     """
     for test in self._tests_conf.get_tests():
         remote_repo = self._tests_conf.get_test_features(test)
         local_dir = os.path.join(self._features_dir, '{0}_features'.format(test).replace('-', '_'))
         logger.info("Using remote Features from '%s'", remote_repo)
         logger.debug("Cloning remote test Features from '%s' to '%s'", remote_repo, local_dir)
         try:
             check_call(['git', 'clone', '-q', remote_repo, local_dir])
         except CalledProcessError as e:
             raise CTFCliError("Cloning of {0} failed!\n{1}".format(remote_repo, str(e)))
예제 #9
0
파일: behave.py 프로젝트: jmtd/ctf-cli
 def _add_remote_features(self):
     """
     Add all remote features
     :return:
     """
     for test in self._tests_conf.get_tests():
         remote_repo = self._tests_conf.get_test_features(test)
         local_dir = os.path.join(self._features_dir, '{0}_features'.format(test).replace('-', '_'))
         logger.info("Using remote Features from '%s'", remote_repo)
         logger.debug("Cloning remote test Features from '%s' to '%s'", remote_repo, local_dir)
         try:
             check_call(['git', 'clone', '-q', remote_repo, local_dir])
         except CalledProcessError as e:
             raise CTFCliError("Cloning of {0} failed!\n{1}".format(remote_repo, str(e)))
예제 #10
0
    def _add_project_specific_environment_py(self):
        """
        Adds project specific environment.py from execution_dir/test/ into the
        working directory. It is renamed and included in the common environment.py

        :return:
        """
        project_environment_py = os.path.join(self._project_tests_dir, 'environment.py')
        if os.path.exists(project_environment_py):
            logger.info("Using project specific environment.py from '%s'", project_environment_py.replace(
                self._execution_dir + os.sep, ''))
            shutil.copy(project_environment_py, self._working_dir)
        else:
            logger.warning("Not using project specific environment.py. '%s' does not exist!", project_environment_py)
예제 #11
0
    def _add_project_specific_features(self):
        """
        Adds project specific features from execution_dir/test/features into the
        features in working directory

        :return:
        """
        project_features_dir = os.path.join(self._project_tests_dir, 'features')
        if os.path.exists(project_features_dir):
            logger.info("Using project specific Features from '%s'",
                        project_features_dir.replace(self._execution_dir + os.sep, ''))
            shutil.copytree(project_features_dir, self._features_dir)
        else:
            logger.warning("Not using project specific Features. '%s' does not exist!", project_features_dir)
예제 #12
0
파일: behave.py 프로젝트: goldmann/ctf-cli
    def run(self):
        """
        Run Behave and pass some runtime arguments
        :return:
        """

        behave_data = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_BEHAVE_DATA)
        behave_tags = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_BEHAVE_TAGS)
        junit = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_JUNIT)
        verbose = self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_VERBOSE)

        # configuration file for ansible
        if self._cli_conf_obj.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_EXEC_TYPE) == 'ansible':
            ansible_conf = self._working_dir_obj.exec_type_conf_path()
        else:
            ansible_conf = None

        command = [
            'behave'
        ]
        if verbose == "yes":
            command.append('-v')

        if junit:
            command.append('--junit')
            command.append('--junit-directory={0}'.format(junit))

        if behave_data:
            #FIXME hacky otherwise it return string - seems like wierd buig in config.py
            if type(behave_data) is str:
                behave_data = behave_data.split('\n')
            for userdata in behave_data:
                command.extend(['-D', '{0}'.format(userdata)])

        if behave_tags:
            #FIXME hacky otherwise it return string - seems like wierd buig in config.py
            if type(behave_tags) is str:
                behave_tags = behave_tags.split('\n')
            for tag in behave_tags:
                command.extend(['-t', '{0}'.format(tag)])
            if verbose != "yes":
                command.append('--no-skipped')

        if ansible_conf:
            command.extend(['-D', 'ANSIBLE={0}'.format(ansible_conf)])

        logger.info("Running behave inside working directory '%s'", ' '.join(command))
        return call(command, cwd=self._working_dir_obj.path())
예제 #13
0
파일: behave.py 프로젝트: jmtd/ctf-cli
    def _add_project_specific_steps(self):
        """
        Adds project specific steps from execution_dir/test/steps into the
        steps in working directory.

        :return:
        """
        project_steps_dir = os.path.join(self._project_tests_dir, 'steps')
        if os.path.exists(project_steps_dir):
            logger.info("Using project specific Steps from '%s'", project_steps_dir.replace(self._execution_dir
                                                                                            + os.sep, ''))
            shutil.copytree(project_steps_dir, os.path.join(self._steps_dir,
                                                            '{0}_steps'.format(os.path.basename(
                                                                self._execution_dir).replace('-', '_'))))

        else:
            logger.warning("Not using project specific Steps. '%s' does not exist!", project_steps_dir)
예제 #14
0
파일: behave.py 프로젝트: vpavlin/ctf-cli
    def _add_project_specific_steps(self):
        """
        Adds project specific steps from execution_dir/test/steps into the
        steps in working directory.

        :return:
        """
        project_steps_dir = os.path.join(self._project_tests_dir, 'steps')
        if os.path.exists(project_steps_dir):
            logger.info("Using project specific Steps from '%s'", project_steps_dir.replace(self._execution_dir
                                                                                            + os.sep, ''))
            shutil.copytree(project_steps_dir, os.path.join(self._steps_dir,
                                                            '{0}_steps'.format(os.path.basename(
                                                                self._execution_dir).replace('-', '_'))))

        else:
            logger.warning("Not using project specific Steps. '%s' does not exist!", project_steps_dir)
예제 #15
0
    def run(self):
        """
        The main application execution method
        """
        logger.info("Running Containers Testing Framework cli")

        self._working_dir = BehaveWorkingDirectory(self._working_dir_path,
                                                   self._cli_conf)

        # Setup Behave structure inside working directory
        # Clone common Features and steps into the working dir
        # Add the project specific Features and steps
        # Prepare the steps.py in the Steps dir that combines all the other
        self._working_dir.setup()

        # Execute Behave
        self._behave_runner = BehaveRunner(self._working_dir, self._cli_conf)
        return self._behave_runner.run()
    def run():
        try:
            # add application-wide debug log
            LoggerHelper.add_debug_log_file(os.getcwd())
            args = ArgumentsParser(sys.argv[1:])
            if args.verbose is True:
                LoggerHelper.add_stream_handler(logger,
                                                logging.Formatter('%(levelname)s:\t%(message)s'),
                                                logging.DEBUG)
            else:
                LoggerHelper.add_stream_handler(logger,
                                                logging.Formatter('%(levelname)s:\t%(message)s'),
                                                logging.INFO)

            app = Application(args)
            if 'init' in args.cli_action:
                app.init()
            if 'run' in args.cli_action:
                app.run()
            if 'update' in args.cli_action:
                app.update()
            if 'remote' in args.cli_action:
                if 'add' in args.remote_action:
                    app.add_remote()
                if 'remove' in args.remote_action:
                    app.remove_remote()
                if 'list' in args.remote_action:
                    app.list_remotes()

        except KeyboardInterrupt:
            logger.info('Interrupted by user')
        except CTFCliError as e:
            logger.error('%s', str(e))
            sys.exit(1)
        else:
            sys.exit(0)
        finally:
            logger.debug('Exiting...')
예제 #17
0
파일: application.py 프로젝트: jmtd/ctf-cli
    def update(self):
        """
        Update app submodules
        """
        logger.info("Updating Containers Testing Framework common steps and features")

        common_steps_dir = os.path.join(self._execution_dir_path, "tests", "steps", "common_steps")
        common_features_dir = os.path.join(self._execution_dir_path, "tests", "features", "common-features")
        for directory in [common_steps_dir, common_features_dir]:
            logger.info("Updating %s" % directory)
            check_call("git fetch origin", shell=True, cwd=directory)
            check_call("git checkout origin/master", shell=True, cwd=directory)

        # Check that steps are not contradicting with each other
        logger.info("Checking project steps sanity")
        check_call("behave tests -d", shell=True)
예제 #18
0
파일: application.py 프로젝트: jmtd/ctf-cli
    def init(self):
        """
        Initialize default app test structure
        """
        logger.info("Initialize default directory structure")

        # Make sure we're in a directory under git control
        try:
            check_call('git rev-parse', shell=True)
        except CalledProcessError:
            logger.info("Directory is not under git control, running git init")
            check_call("git init", shell=True)

        # Create test dir if it is missing
        tests_dir = os.path.join(self._execution_dir_path, "tests")
        if os.path.exists(tests_dir):
            logger.info("Directory tests already exists")
        else:
            logger.info("Creating tests directory")
            os.mkdir(tests_dir)
            check_call("git add %s" % tests_dir, shell=True)

        env_py_path = os.path.join(tests_dir, "environment.py")
        if os.path.exists(env_py_path):
            logger.info("File tests/environment.py already exists")
        else:
            logger.info("Creating environment.py")
            # Create environment.py
            with open(env_py_path, "w") as f:
                f.write(common_environment_py_content)
            check_call("git add %s" % env_py_path, shell=True)

        features_dir = os.path.join(tests_dir, "features")
        if os.path.exists(features_dir):
            logger.info("Directory tests/features already exists")
        else:
            logger.info("Creating tests/features directory")
            os.mkdir(features_dir)
            check_call("git add %s" % features_dir, shell=True)

        steps_dir = os.path.join(tests_dir, "steps")
        if os.path.exists(steps_dir):
            logger.info("Directory tests/steps already exists")
        else:
            logger.info("Creating tests/steps directory")
            os.mkdir(steps_dir)
            check_call("git add %s" % steps_dir, shell=True)

        # TODO: check that this file is actually necessary
        steps_init_file = os.path.join(steps_dir, "__init__.py")
        if os.path.exists(steps_init_file):
            logger.info("File tests/steps/__init__.py already exists")
        else:
            logger.info("Creating tests/steps/__init__.py file")
            open(steps_init_file, "a").close()
            check_call("git add %s" % steps_init_file, shell=True)

        steps_py_file = os.path.join(steps_dir, "steps.py")
        if os.path.exists(steps_py_file):
            logger.info("File tests/steps/steps.py already exists")
        else:
            logger.info("Creating tests/steps/steps.py file")
            with open(steps_py_file, "w") as f:
                f.write(common_steps_py_content)
            check_call("git add %s" % steps_py_file, shell=True)

        # Add common-features and common-steps as submodules
        # TODO:  make this generic when a different type of container is specified
        common_features_dir = os.path.join(features_dir, "common-features")
        if os.path.exists(common_features_dir):
            logger.info("Directory tests/features/common-features already exists")
        else:
            logger.info("Adding tests/features/common-features as a submodule")
            check_call('git submodule add https://github.com/Containers-Testing-Framework/common-features.git tests/features/common-features', shell=True)

        common_steps_dir = os.path.join(steps_dir, "common_steps")
        if os.path.exists(common_steps_dir):
            logger.info("Directory tests/steps/common_steps already exists")
        else:
            logger.info("Adding tests/steps/common_steps as a submodule")
            check_call('git submodule add https://github.com/Containers-Testing-Framework/common-steps.git tests/steps/common_steps', shell=True)

        # Copy sample configuration
        ctf_conf_file = os.path.join(self._execution_dir_path, "ctf.conf")
        if os.path.exists(ctf_conf_file):
            logger.info("File ctf.conf already exists")
        else:
            logger.info("Creating ctf.conf file")
            # Create environment.py
            with open(ctf_conf_file, "w") as f:
                f.write(sample_ctl_ctf_config)
            check_call("git add %s" % ctf_conf_file, shell=True)
예제 #19
0
    def init(self):
        """
        Initialize default app test structure
        """
        logger.info("Initialize default directory structure")

        # Make sure we're in a directory under git control
        try:
            check_call('git rev-parse &> /dev/null', shell=True)
        except CalledProcessError:
            logger.info("Directory is not under git control, running git init")
            check_call("git init &> /dev/null", shell=True)

        # Create test dir if it is missing
        tests_dir = os.path.join(self._execution_dir_path, "tests")
        if os.path.exists(tests_dir):
            logger.info("Directory tests already exists")
        else:
            logger.info("Creating tests directory")
            os.mkdir(tests_dir)
            check_call("git add %s" % tests_dir, shell=True)

        features_dir = os.path.join(tests_dir, "features")
        if os.path.exists(features_dir):
            logger.info("Directory tests/features already exists")
        else:
            logger.info("Creating tests/features directory")
            os.mkdir(features_dir)
            check_call("git add %s" % features_dir, shell=True)

        steps_dir = os.path.join(tests_dir, "steps")
        if os.path.exists(steps_dir):
            logger.info("Directory tests/steps already exists")
        else:
            logger.info("Creating tests/steps directory")
            os.mkdir(steps_dir)
            check_call("git add %s" % steps_dir, shell=True)

        # TODO: check that this file is actually necessary
        steps_init_file = os.path.join(steps_dir, "__init__.py")
        if os.path.exists(steps_init_file):
            logger.info("File tests/steps/__init__.py already exists")
        else:
            logger.info("Creating tests/steps/__init__.py file")
            open(steps_init_file, "a").close()
            check_call("git add %s" % steps_init_file, shell=True)

        steps_py_file = os.path.join(steps_dir, "steps.py")
        if os.path.exists(steps_py_file):
            logger.info("File tests/steps/steps.py already exists")
        else:
            logger.info("Creating tests/steps/steps.py file")
            with open(steps_py_file, "w") as f:
                f.write(common_steps_py_content)
            check_call("git add %s" % steps_py_file, shell=True)

        # Copy sample configuration
        ctf_conf_file = os.path.join(self._execution_dir_path, "ctf.conf")
        if os.path.exists(ctf_conf_file):
            logger.info("File ctf.conf already exists")
        else:
            logger.info("Creating ctf.conf file")
            # Create environment.py
            with open(ctf_conf_file, "w") as f:
                f.write(sample_ctl_ctf_config)
            check_call("git add %s" % ctf_conf_file, shell=True)