Example #1
0
def list_syntribos_opts():
    def wrap_try_except(func):
        def wrap(*args):
            try:
                func(*args)
            except IOError:
                msg = _(
                    "\nCan't open a file or directory specified in the "
                    "config file under the section `[syntribos]`; verify "
                    "if the path exists.\nFor more information please refer "
                    "the debug logs.")
                print(msg)
                exit(1)

        return wrap

    return [
        cfg.StrOpt("endpoint",
                   default="",
                   sample_default="http://localhost/app",
                   help=_("The target host to be tested")),
        cfg.IntOpt("threads",
                   default=16,
                   sample_default="16",
                   help=_("Maximum number of threads syntribos spawns "
                          "(experimental)")),
        cfg.Opt("templates",
                type=ContentType("r"),
                default="",
                sample_default="~/.syntribos/templates",
                help=_("A directory of template files, or a single "
                       "template file, to test on the target API")),
        cfg.StrOpt("payloads",
                   default="",
                   sample_default="~/.syntribos/data",
                   help=_("The location where we can find syntribos'"
                          "payloads")),
        cfg.MultiStrOpt("exclude_results",
                        default=[""],
                        sample_default=["500_errors", "length_diff"],
                        help=_("Defect types to exclude from the "
                               "results output")),
        cfg.Opt("custom_root",
                type=wrap_try_except(ExistingDirType()),
                short="c",
                sample_default="/your/custom/root",
                help=_("The root directory where the subfolders that make up"
                       " syntribos' environment (logs, templates, payloads, "
                       "configuration files, etc.)"),
                deprecated_for_removal=True),
        cfg.StrOpt("meta_vars",
                   sample_default="/path/to/meta.json",
                   help=_(
                       "The path to a meta variable definitions file, which "
                       "will be used when parsing your templates")),
    ]
Example #2
0
def list_syntribos_opts():
    def wrap_try_except(func):
        def wrap(*args):
            try:
                func(*args)
            except IOError:
                print("\nCan't open a file or directory specified in the "
                      "config file under the section `[syntribos]`; verify "
                      "if the path exists.\nFor more information please refer "
                      "the debug logs.")
                exit(1)

        return wrap

    return [
        cfg.StrOpt("endpoint",
                   default="",
                   sample_default="http://localhost/app",
                   help="The target host to be tested"),
        cfg.Opt("templates",
                type=ContentType("r", 0),
                default="",
                sample_default="~/.syntribos/templates",
                help="A directory of template files, or a single template "
                "file, to test on the target API"),
        cfg.StrOpt("payloads",
                   default="",
                   sample_default="~/.syntribos/data",
                   help="The location where we can find syntribos'"
                   "payloads"),
        cfg.MultiStrOpt("exclude_results",
                        default=[""],
                        sample_default=["500_errors", "length_diff"],
                        help="Defect types to exclude from the "
                        "results output"),
        cfg.Opt("custom_root",
                type=wrap_try_except(ExistingDirType()),
                short="c",
                sample_default="/your/custom/root",
                help="The root directory where the subfolders that make up"
                " syntribos' environment (logs, templates, payloads, "
                "configuration files, etc.)"),
    ]
Example #3
0
 def _get_strings(cls, file_name=None):
     payloads = CONF.syntribos.payloads
     if not payloads:
         payloads = remotes.get(CONF.remote.payloads_uri)
     content = ContentType('r', 0)(payloads)
     for file_path, _ in content:
         if file_path.endswith(".txt"):
             file_dir = os.path.split(file_path)[0]
             payloads = os.path.join(payloads, file_dir)
             break
     try:
         path = os.path.join(payloads, file_name or cls.data_key)
         with open(path, "rb") as fp:
             return fp.read().splitlines()
     except IOError as e:
         LOG.error("Exception raised: {}".format(e))
         print("\nNo file named {} found in the payloads dir, "
               "exiting..".format(file_name or cls.data_key))
         exit(1)
Example #4
0
 def _get_strings(cls, file_name=None):
     payloads = CONF.syntribos.payloads
     if not payloads:
         payloads = remotes.get(CONF.remote.payloads_uri)
     content = ContentType('r', 0)(payloads)
     for file_path, _ in content:
         if file_path.endswith(".txt"):
             file_dir = os.path.split(file_path)[0]
             payloads = os.path.join(payloads, file_dir)
             break
     try:
         if os.path.isfile(cls.data_key):
             path = cls.data_key
         else:
             path = os.path.join(payloads, file_name or cls.data_key)
         with open(path, "rb") as fp:
             return fp.read().splitlines()
     except (IOError, AttributeError, TypeError) as e:
         LOG.error("Exception raised: {}".format(e))
         print("\nPayload file for test '{}' not readable, "
               "exiting...".format(cls.test_name))
         exit(1)
Example #5
0
    def run(cls):
        """Method sets up logger and decides on Syntribos control flow

        This is the method where control flow of Syntribos is decided
        based on the commands entered. Depending upon commands such
        as ```list_tests``` or ```run``` the respective method is called.
        """
        global result

        cli.print_symbol()

        # If we are initializing, don't look for a default config file
        if "init" in sys.argv:
            cls.setup_config()
        else:
            cls.setup_config(use_file=True)
        try:
            if CONF.sub_command.name == "init":
                ENV.initialize_syntribos_env()
                exit(0)

            elif CONF.sub_command.name == "list_tests":
                cls.list_tests()
                exit(0)

            elif CONF.sub_command.name == "download":
                ENV.download_wrapper()
                exit(0)
        except AttributeError:
            print(
                _(
                    "Not able to run the requested sub command, please check "
                    "the debug logs for more information, exiting..."))
            exit(1)

        if not ENV.is_syntribos_initialized():
            print(_("Syntribos was not initialized. Please run the 'init'"
                    " command or set it up manually. See the README for"
                    " more information about the installation process."))
            exit(1)

        cls.setup_runtime_env()

        decorator = unittest.runner._WritelnDecorator(cls.output)
        result = syntribos.result.IssueTestResult(decorator, True, verbosity=1)

        cls.start_time = time.time()
        if CONF.sub_command.name == "run":
            list_of_tests = list(
                cls.get_tests(CONF.test_types, CONF.excluded_types))
        elif CONF.sub_command.name == "dry_run":
            dry_run_output = {"failures": [], "successes": []}
            list_of_tests = list(cls.get_tests(dry_run=True))

        print(_("\nRunning Tests...:"))
        templates_dir = CONF.syntribos.templates
        if templates_dir is None:
            print(_("Attempting to download templates from {}").format(
                CONF.remote.templates_uri))
            templates_path = remotes.get(CONF.remote.templates_uri)
            try:
                templates_dir = ContentType("r", 0)(templates_path)
            except IOError:
                print(_("Not able to open `%s`; please verify path, "
                        "exiting...") % templates_path)
                exit(1)

        print(_("\nPress Ctrl-C to pause or exit...\n"))
        meta_vars = None
        templates_dir = list(templates_dir)
        cls.meta_dir_dict = {}
        for file_path, file_content in templates_dir:
            if os.path.basename(file_path) == "meta.json":
                meta_path = os.path.dirname(file_path)
                try:
                    cls.meta_dir_dict[meta_path] = json.loads(file_content)
                except Exception:
                    print("Unable to parse %s, skipping..." % file_path)
        for file_path, req_str in templates_dir:
            if "meta.json" in file_path:
                continue
            meta_vars = cls.get_meta_vars(file_path)
            LOG = cls.get_logger(file_path)
            CONF.log_opt_values(LOG, logging.DEBUG)
            if not file_path.endswith(".template"):
                LOG.warning('file.....:%s (SKIPPED - not a .template file)',
                            file_path)
                continue

            test_names = [t for (t, i) in list_of_tests]  # noqa
            log_string = ''.join([
                '\n{0}\nTEMPLATE FILE\n{0}\n'.format('-' * 12),
                'file.......: {0}\n'.format(file_path),
                'tests......: {0}\n'.format(test_names)
            ])
            LOG.debug(log_string)
            print(syntribos.SEP)
            print("Template File...: {}".format(file_path))
            print(syntribos.SEP)

            if CONF.sub_command.name == "run":
                cls.run_given_tests(list_of_tests, file_path,
                                    req_str, meta_vars)
            elif CONF.sub_command.name == "dry_run":
                cls.dry_run(list_of_tests, file_path,
                            req_str, dry_run_output, meta_vars)

        if CONF.sub_command.name == "run":
            result.print_result(cls.start_time)
            cleanup.delete_temps()
        elif CONF.sub_command.name == "dry_run":
            cls.dry_run_report(dry_run_output)