Ejemplo n.º 1
0
def test_config(testname, dirpath, configdir):
    errors = []

    if not os.path.isdir(configdir):
        errors.append("configdir %s is not a directory" % configdir)

    print("==== checking intermediate output")

    ambassador = shell(['ambassador', 'dump', configdir])

    if ambassador.code != 0:
        errors.append('ambassador dump failed! %s' % ambassador.code)
    else:
        current_raw = ambassador.output(raw=True)
        current = None
        gold = None

        try:
            current = sanitize_errors(json.loads(current_raw))
        except json.decoder.JSONDecodeError as e:
            errors.append("current intermediate was unparseable?")

        if current:
            current['envoy_config'] = filtered_overview(
                current['envoy_config'])

            current_path = os.path.join(dirpath, "intermediate.json")
            json.dump(current,
                      open(current_path, "w"),
                      sort_keys=True,
                      indent=4)

            gold_path = os.path.join(dirpath, "gold.intermediate.json")

            if os.path.exists(gold_path):
                udiff = unified_diff(gold_path, current_path)

                if udiff:
                    errors.append(
                        "gold.intermediate.json and intermediate.json do not match!\n\n%s"
                        % "\n".join(udiff))

    print("==== checking config generation")

    envoy_json_out = os.path.join(dirpath, "envoy.json")

    try:
        os.unlink(envoy_json_out)
    except OSError as e:
        if e.errno != errno.ENOENT:
            raise

    ambassador = shell(
        ['ambassador', 'config', '--check', configdir, envoy_json_out])

    print(ambassador.errors(raw=True))

    if ambassador.code != 0:
        errors.append('ambassador failed! %s' % ambassador.code)
    else:
        envoy = shell([
            'docker', 'run', '--rm', '-v',
            '%s:/etc/ambassador-config' % dirpath, VALIDATOR_IMAGE,
            '/usr/local/bin/envoy', '--base-id', '1', '--mode', 'validate',
            '--service-cluster', 'test', '-c',
            '/etc/ambassador-config/envoy.json'
        ],
                      verbose=True)

        envoy_succeeded = (envoy.code == 0)

        if not envoy_succeeded:
            errors.append('envoy failed! %s' % envoy.code)

        envoy_output = list(envoy.output())

        if envoy_succeeded:
            if not envoy_output[-1].strip().endswith(' OK'):
                errors.append('envoy validation failed!')

        gold_path = os.path.join(dirpath, "gold.json")

        if os.path.exists(gold_path):
            udiff = unified_diff(gold_path, envoy_json_out)

            if udiff:
                errors.append("gold.json and envoy.json do not match!\n\n%s" %
                              "\n".join(udiff))

    print("==== checking short-circuit with existing config")

    ambassador = shell(
        ['ambassador', 'config', '--check', configdir, envoy_json_out])

    print(ambassador.errors(raw=True))

    if ambassador.code != 0:
        errors.append('ambassador repeat check failed! %s' % ambassador.code)

    if 'Output file exists' not in ambassador.errors(raw=True):
        errors.append('ambassador repeat check did not short circuit??')

    if errors:
        print("---- ERRORS")
        print("%s" % "\n".join(errors))

    assert not errors, ("failing, errors: %d" % len(errors))
Ejemplo n.º 2
0
def test_config(testname, dirpath, configdir):
    # pytest.xfail("old V1 tests are disabled")
    # return
    
    global logger 
    errors = []

    if not os.path.isdir(configdir):
        errors.append("configdir %s is not a directory" % configdir)

    print("==== loading resources")

    aconf = Config()
    fetcher = ResourceFetcher(logger, aconf)
    fetcher.load_from_filesystem(configdir, recurse=True)
    aconf.load_all(fetcher.sorted())

    ir = IR(aconf, file_checker=file_always_exists, secret_reader=atest_secret_reader)
    v1config = V1Config(ir)

    print("==== checking IR")

    current = get_old_intermediate(aconf, ir, v1config)
    current['envoy_config'] = filtered_overview(current['envoy_config'])
    current = sanitize_errors(current)

    current_path = os.path.join(dirpath, "intermediate.json")
    json.dump(current, open(current_path, "w"), sort_keys=True, indent=4)

    # Check the IR against its gold file, if that gold file exists.
    gold_path = os.path.join(dirpath, "gold.intermediate.json")

    if os.path.exists(gold_path):
        gold_parsed = None

        try:
            gold_parsed = json.load(open(gold_path, "r"))
        except json.decoder.JSONDecodeError as e:
            errors.append("%s was unparseable?" % gold_path)

        gold_no_yaml = normalize_gold(gold_parsed)
        gold_no_yaml_path = os.path.join(dirpath, "gold.no_yaml.json")
        json.dump(gold_no_yaml, open(gold_no_yaml_path, "w"), sort_keys=True, indent=4)

        udiff = unified_diff(gold_no_yaml_path, current_path)

        if udiff:
            errors.append("gold.intermediate.json and intermediate.json do not match!\n\n%s" % "\n".join(udiff))

    print("==== checking V1")

    # Check the V1 config against its gold file, if it exists (and it should).
    gold_path = os.path.join(dirpath, "gold.json")

    if os.path.exists(gold_path):
        v1path = os.path.join(dirpath, "v1.json")
        json.dump(v1config.as_dict(), open(v1path, "w"), sort_keys=True, indent=4)

        udiff = unified_diff(gold_path, v1path)

        if udiff:
            errors.append("gold.json and v1.json do not match!\n\n%s" % "\n".join(udiff))

    # if ambassador.code != 0:
    #     errors.append('ambassador failed! %s' % ambassador.code)
    # else:
    #     envoy = shell([ 'docker', 'run',
    #                         '--rm',
    #                         '-v', '%s:/etc/ambassador-config' % dirpath,
    #                         VALIDATOR_IMAGE,
    #                         '/usr/local/bin/envoy',
    #                            '--base-id', '1',
    #                            '--mode', 'validate',
    #                            '--service-cluster', 'test',
    #                            '-c', '/etc/ambassador-config/envoy.json' ],
    #                   verbose=True)
    #
    #     envoy_succeeded = (envoy.code == 0)
    #
    #     if not envoy_succeeded:
    #         errors.append('envoy failed! %s' % envoy.code)
    #
    #     envoy_output = list(envoy.output())
    #
    #     if envoy_succeeded:
    #         if not envoy_output[-1].strip().endswith(' OK'):
    #             errors.append('envoy validation failed!')
    #
    # print("==== checking short-circuit with existing config")
    #
    # ambassador = shell([ 'ambassador', 'config', '--check', configdir, envoy_json_out ])
    #
    # print(ambassador.errors(raw=True))
    #
    # if ambassador.code != 0:
    #     errors.append('ambassador repeat check failed! %s' % ambassador.code)
    #
    # if 'Output file exists' not in ambassador.errors(raw=True):
    #     errors.append('ambassador repeat check did not short circuit??')

    if errors:
        print("---- ERRORS")
        print("%s" % "\n".join(errors))

    assert not errors, ("failing, _errors: %d" % len(errors))