def test_walk_tree():
    files = walk_tree(here)
    assert files

    list_of_files = walk_tree(here, ".py")
    print(f"Detected {len(list_of_files)} .py files found in directory: {here}")
    assert len(list_of_files) > 0
Exemple #2
0
def test_build_buildspecs():
    buildspec_paths = os.path.join(test_root, "buildsystem", "valid_buildspecs")

    #  testing buildtest build --buildspec tests/buildsystem/valid_buildspecs
    cmd = BuildTest(config_file=DEFAULT_SETTINGS_FILE, buildspecs=[buildspec_paths])
    cmd.build()

    excluded_buildspecs = walk_tree(buildspec_paths, ".yml")
    assert len(excluded_buildspecs) > 0
    #  testing buildtest build --buildspec tests/buildsystem/valid_buildspecs and exclude the first buildspec
    cmd = BuildTest(
        config_file=DEFAULT_SETTINGS_FILE,
        buildspecs=[buildspec_paths],
        exclude_buildspecs=[excluded_buildspecs[0]],
    )
    cmd.build()

    #  testing buildtest build --buildspec tests/examples/buildspecs --exclude tests/examples/buildspecs
    # this results in no buildspecs built
    with pytest.raises(SystemExit):
        cmd = BuildTest(
            config_file=DEFAULT_SETTINGS_FILE,
            buildspecs=[buildspec_paths],
            exclude_buildspecs=[buildspec_paths],
        )
        cmd.build()
Exemple #3
0
def test_BuildspecParser(tmp_path):

    # Invalid path to buildspec file should exit
    with pytest.raises(BuildTestError):
        BuildspecParser("")

    # Passing 'None' will raise an error
    with pytest.raises(BuildTestError):
        BuildspecParser(None)

    directory = os.path.join(here, "invalid_buildspecs")
    builders = []
    for buildspec in walk_tree(directory, ".yml"):
        buildspecfile = os.path.join(directory, buildspec)
        print("Processing buildspec: ", buildspecfile)
        with pytest.raises(BuildTestError):
            BuildspecParser(buildspecfile)

    # Examples folder
    valid_buildspecs_directory = os.path.join(here, "valid_buildspecs")

    # A directory is not allowed either, this will raise an error.
    with pytest.raises(BuildTestError):
        BuildspecParser(valid_buildspecs_directory)

    # Test loading Buildspec files
    for buildspec in walk_tree(valid_buildspecs_directory, ".yml"):
        buildspecfile = os.path.join(valid_buildspecs_directory, buildspec)
        bp = BuildspecParser(buildspecfile)
        assert bp.recipe
        assert bp.buildspec
        assert bp.executors

        filters = {"tags": None, "executors": None}

        builders = Builder(bp, filters=filters, testdir=tmp_path)
        builders = builders.get_builders()
        assert builders

        for builder in builders:

            # Builders (on init) set up metadata attribute
            assert builder.metadata

            # Invoking build will build the test script
            # and write test
            builder.build()
Exemple #4
0
def test_valid_config_schemas():

    valid_schema_dir = os.path.join(pytest_root, "examples", "config_schemas",
                                    "valid")
    schema_config = load_schema(DEFAULT_SETTINGS_SCHEMA)
    for schema in walk_tree(valid_schema_dir, ".yml"):
        example = load_recipe(os.path.abspath(schema))
        custom_validator(recipe=example, schema=schema_config)
Exemple #5
0
def test_walk_tree_invalid_dir(tmp_path):
    # we want to test an invalid directory so we remove temporary directory created by tmp_path
    shutil.rmtree(tmp_path)
    print(
        f"Removing directory: {tmp_path} first before doing directory traversal on invalid directory"
    )
    list_of_files = walk_tree(tmp_path, ".py")
    print(
        f"Returned following files: {list_of_files} with .py extension for path: {tmp_path}"
    )
    assert not list_of_files
Exemple #6
0
def func_schema(args):
    """This method implements command ``buildtest schema`` which shows a list
    of schemas, their json content and list of schema examples. The input
    ``args`` is an instance of argparse class that contains
    user selection via command line. This method can do the following

    ``buildtest schema`` - Show all schema names
    ``buildtest schema --name <NAME> -j ``. View json content of a specified schema
    ``buildtest schema --name <NAME> -e``. Show schema examples
    Parameters:

    :param args: instance of argparse class
    :type args: <class 'argparse.Namespace'>
    :result: output of json schema on console
    """

    # the default behavior when "buildtest schema" is executed is to show list of all
    # schemas
    if not args.json and not args.example:
        for schema in schema_table["names"]:
            print(schema)
        return

    # -n option is required when using schema options
    if not args.name:
        raise SystemExit("Please specify a schema name with -n option")

    if args.json:
        print(json.dumps(schema_table[args.name]["recipe"], indent=2))
        return

    # There are no examples for definitions schema
    if args.name == "definitions.schema.json":
        if args.example or args.validate:
            raise SystemExit(
                "There are no examples for definitions.schema.json")

    examples = os.path.join(here, "examples", args.name)

    # get all examples for specified schema. We validate all examples and
    # and print content of all examples. If there is an error during validation
    # we show the error message.

    schema_examples = walk_tree(examples, ".yml")
    for example in schema_examples:

        if args.example:
            content = read_file(example)
            print(f"File: {example}")
            print("{:_<80}".format(""))
            print(content)
def test_cori():
    # This test must run on Cori Login nodes which are cori[01-20].nersc.gov.
    hostname = socket.getfqdn()
    if not hostname.startswith("cori"):
        pytest.skip("This test runs on Cori Login nodes ('cori*')")
    here = os.path.dirname(os.path.abspath(__file__))
    settings_file = os.path.join(here, "settings", "cori.yml")

    buildspec_files = walk_tree(
        os.path.join(os.getenv("BUILDTEST_ROOT"), "tests", "examples", "cori"),
        ".yml")
    cmd = BuildTest(config_file=settings_file, buildspecs=buildspec_files)
    cmd.build()

    # testing buildtest config compilers find
    bc = BuildtestCompilers(settings_file=settings_file)
    bc.find_compilers()
    def _discover_buildspecs(self):
        """This method retrieves buildspecs based on ``self.paths`` which is a
        list of directory paths to search. If ``--root`` is specified
        we process each argument and recursively find all .yml files
        """

        buildspecs = []
        # add all buildspecs from each repo. walk_tree will find all .yml files
        # recursively and add them to list

        if not self.paths:
            raise BuildTestError(
                "Unable to search any buildspecs, please specify a directory")

        if self.paths:
            for path in self.paths:
                buildspec = walk_tree(path, ".yml")
                buildspecs += buildspec

        print(f"\nBuildspec Paths: {self.paths} \n")

        return buildspecs
Exemple #9
0
def test_Builder(tmp_path):
    directory = os.path.join(here, "invalid_builds")
    filters = {"tags": None, "executors": None}
    print(os.listdir(directory))
    for buildspec in walk_tree(directory, ".yml"):
        buildspecfile = os.path.join(directory, buildspec)
        print("file:", buildspecfile)
        bp = BuildspecParser(buildspecfile)
        builder = Builder(bp=bp, testdir=tmp_path, filters=filters)
        builders = builder.get_builders()
        for builder in builders:
            with pytest.raises(BuildTestError):
                builder.build()

    directory = os.path.join(here, "valid_builds")
    filters = {"tags": None, "executors": None}
    for buildspec in os.listdir(directory):
        buildspec = os.path.join(directory, buildspec)
        bp = BuildspecParser(buildspec)
        builder = Builder(bp=bp, testdir=tmp_path, filters=filters)
        builders = builder.get_builders()
        for builder in builders:
            builder.build()
Exemple #10
0
def discover_by_buildspecs(buildspec):
    """Given a buildspec file specified by the user with ``buildtest build --buildspec``,
    discover one or more files and return a list for buildtest to process.
    This method is called once per argument of ``--buildspec`` or ``--exclude``
    option. If its a directory path we recursively find all buildspecs with
    .yml extension. If filepath doesn't exist or file extension is not .yml we
    return None and capture error in log.

    # file path
    buildtest build --buildspec tutorials/hello.sh.yml

    # directory path
    buildtest build --buildspec tutorials

    :param buildspec: Input argument from ``buildtest build --buildspec``
    :type buildspec: str
    :return: A list of discovered buildspec with resolved path, if its invalid we return None
    :rtype: list or None
    """

    buildspecs = []
    # if buildspec doesn't exist print message and log error and return
    if not os.path.exists(os.path.abspath(buildspec)):
        msg = (
            f"Unable to find any buildspecs with name: {os.path.abspath(buildspec)} "
            +
            "Please provide an absolute or relative path to a directory or file relative to current directory."
        )
        print(msg)
        logger.error(msg)
        return

    # Now handle path based on being a directory or file path
    if os.path.isdir(buildspec):
        logger.debug(
            f"Buildspec File: {buildspec} is a directory so traversing directory tree to find all Buildspec files with .yml extension"
        )
        buildspecs = walk_tree(buildspec, ".yml")
    elif os.path.isfile(buildspec):
        # if buildspec doesn't end in .yml extension we print message and return None
        if not re.search(".yml$", buildspec):
            msg = f"{buildspec} does not end in file extension .yml"
            print(msg)
            logger.error(msg)
            return

        buildspecs = [buildspec]
        logger.debug(f"BuildSpec: {buildspec} is a file")

    # If we don't have any files discovered
    if not buildspecs:
        msg = "No Buildspec files found with input: %s." % buildspec
        print(msg)
        logger.error(msg)
        return

    # return all buildspec by resolving path, this gets the real canonical path and address shell expansion and user expansion
    buildspecs = [resolve_path(file) for file in buildspecs]

    logger.info(f"Found the following config files: {buildspecs}")
    return buildspecs
Exemple #11
0
def test_walk_tree_no_files(tmp_path):
    list_of_files = walk_tree(tmp_path, ".py")
    print(
        f"Detected {len(list_of_files)} .py files found in directory: {tmp_path}"
    )
    assert 0 == len(list_of_files)
def test_BuildspecParser(tmp_path):
    config = BuildtestConfiguration(DEFAULT_SETTINGS_FILE)
    executors = BuildExecutor(config)
    # Invalid path to buildspec file should exit
    with pytest.raises(BuildTestError):
        BuildspecParser("", executors)

    # Passing 'None' will raise an error
    with pytest.raises(BuildTestError):
        BuildspecParser(None, executors)

    directory = os.path.join(here, "invalid_buildspecs")
    builders = []
    for buildspec in walk_tree(directory, ".yml"):
        buildspecfile = os.path.join(directory, buildspec)
        print("Processing buildspec: ", buildspecfile)
        with pytest.raises(BuildTestError):
            BuildspecParser(buildspecfile, executors)

    directory = os.path.join(here, "invalid_builds")
    # invalid builds for compiler schema tests. These tests will raise BuildTestError exception upon building
    # even though they are valid buildspecs.\
    for buildspec in walk_tree(directory, ".yml"):
        buildspecfile = os.path.join(directory, buildspec)
        print("Processing buildspec", buildspecfile)
        bp = BuildspecParser(buildspecfile, executors)

        with pytest.raises(BuildTestError):
            builder = Builder(bp=bp,
                              buildexecutor=executors,
                              filters=[],
                              testdir="/tmp")
            builders = builder.get_builders()
            for test in builders:
                test.build()

    # Examples folder
    valid_buildspecs_directory = os.path.join(here, "valid_buildspecs")

    # A directory is not allowed either, this will raise an error.
    with pytest.raises(BuildTestError):
        BuildspecParser(valid_buildspecs_directory, executors)

    # Test loading Buildspec files
    for buildspec in walk_tree(valid_buildspecs_directory, ".yml"):
        buildspecfile = os.path.join(valid_buildspecs_directory, buildspec)
        bp = BuildspecParser(buildspecfile, executors)
        assert hasattr(bp, "recipe")
        assert hasattr(bp, "buildspec")
        assert hasattr(bp, "buildexecutors")

        filters = []

        builders = Builder(bp=bp,
                           buildexecutor=executors,
                           filters=filters,
                           testdir=tmp_path)
        builders = builders.get_builders()
        assert builders

        for builder in builders:

            # Builders (on init) set up metadata attribute
            assert hasattr(builder, "metadata")

            # Invoking build will build the test script
            # and write test
            builder.build()