Ejemplo n.º 1
0
def test_load_not_arm_file():
    # given
    file_path = EXAMPLES_DIR / "json/normal.json"

    # when
    template, file_lines = load(file_path)

    # then
    assert template == {}
    assert file_lines == []
Ejemplo n.º 2
0
def test_load_mariadb():
    # given
    file_path = EXAMPLES_DIR / "json/mariadb.json"

    # when
    template, file_lines = load(file_path)

    # then
    assert template[
        "$schema"] == "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#"
    assert template["contentVersion"] == "1.0.0.0"
    assert len(file_lines) == 25
Ejemplo n.º 3
0
def parse(filename):
    """
        Decode filename into an object
    """
    template = None
    template_lines = None
    try:
        (template, template_lines) = cfn_yaml.load(filename)
    except IOError as e:
        if e.errno == 2:
            LOGGER.error('Template file not found: %s', filename)
        elif e.errno == 21:
            LOGGER.error('Template references a directory, not a file: %s',
                         filename)
        elif e.errno == 13:
            LOGGER.error('Permission denied when accessing template file: %s',
                         filename)
    except UnicodeDecodeError as err:
        LOGGER.error('Cannot read file contents: %s', filename)
    except cfn_yaml.CfnParseError as err:
        pass
    except ScannerError as err:
        if err.problem in [
                'found character \'\\t\' that cannot start any token',
                'found unknown escape character'
        ]:
            try:
                (template, template_lines) = cfn_json.load(filename)
            except cfn_json.JSONDecodeError:
                pass
            except JSONDecodeError:
                pass
            except Exception as json_err:  # pylint: disable=W0703
                LOGGER.error('Template %s is malformed: %s', filename,
                             err.problem)
                LOGGER.error('Tried to parse %s as JSON but got error: %s',
                             filename, str(json_err))
    except YAMLError as err:
        pass

    if template is not None and isinstance(
            template,
            dict_node) and '$schema' in template and 'resources' in template:
        return template, template_lines
    return None, None