...

Text file src/github.com/datawire/ambassador/v2/python/tests/unit/test_mapping.py

Documentation: github.com/datawire/ambassador/v2/python/tests/unit

     1import json
     2
     3import pytest
     4
     5from tests.utils import compile_with_cachecheck
     6
     7
     8@pytest.mark.compilertest
     9def test_mapping_host_star_error():
    10    test_yaml = """
    11---
    12apiVersion: getambassador.io/v3alpha1
    13kind: Mapping
    14metadata:
    15  name: bad-mapping
    16  namespace: default
    17spec:
    18  host: "*"
    19  prefix: /star/
    20  service: star
    21"""
    22
    23    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
    24
    25    ir = r["ir"]
    26
    27    # print(json.dumps(ir.aconf.errors, sort_keys=True, indent=4))
    28
    29    errors = ir.aconf.errors["bad-mapping.default.1"]
    30    assert len(errors) == 1, f"Expected 1 error but got {len(errors)}"
    31
    32    assert errors[0]["ok"] == False
    33    assert errors[0]["error"] == "host exact-match * contains *, which cannot match anything."
    34
    35    for g in ir.groups.values():
    36        assert g.prefix != "/star/"
    37
    38    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
    39
    40
    41@pytest.mark.compilertest
    42def test_mapping_host_authority_star_error():
    43    test_yaml = """
    44---
    45apiVersion: getambassador.io/v3alpha1
    46kind: Mapping
    47metadata:
    48  name: bad-mapping
    49  namespace: default
    50spec:
    51  headers:
    52    ":authority": "*"
    53  prefix: /star/
    54  service: star
    55"""
    56
    57    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
    58
    59    ir = r["ir"]
    60
    61    # print(json.dumps(ir.aconf.errors, sort_keys=True, indent=4))
    62
    63    errors = ir.aconf.errors["bad-mapping.default.1"]
    64    assert len(errors) == 1, f"Expected 1 error but got {len(errors)}"
    65
    66    assert errors[0]["ok"] == False
    67    assert (
    68        errors[0]["error"] == ":authority exact-match '*' contains *, which cannot match anything."
    69    )
    70
    71    for g in ir.groups.values():
    72        assert g.prefix != "/star/"
    73
    74    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
    75
    76
    77@pytest.mark.compilertest
    78def test_mapping_host_ok():
    79    test_yaml = """
    80---
    81apiVersion: getambassador.io/v3alpha1
    82kind: Mapping
    83metadata:
    84  name: good-host-mapping
    85  namespace: default
    86spec:
    87  host: foo.example.com
    88  prefix: /wanted_group/
    89  service: star
    90"""
    91
    92    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
    93
    94    ir = r["ir"]
    95
    96    errors = ir.aconf.errors
    97    assert len(errors) == 0, "Expected no errors but got %s" % (
    98        json.dumps(errors, sort_keys=True, indent=4)
    99    )
   100
   101    found = 0
   102
   103    for g in ir.groups.values():
   104        if g.prefix == "/wanted_group/":
   105            assert g.host == "foo.example.com"
   106            found += 1
   107
   108    assert found == 1, "Expected 1 /wanted_group/ prefix, got %d" % found
   109
   110    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
   111
   112
   113@pytest.mark.compilertest
   114def test_mapping_host_authority_ok():
   115    test_yaml = """
   116---
   117apiVersion: getambassador.io/v3alpha1
   118kind: Mapping
   119metadata:
   120  name: good-host-mapping
   121  namespace: default
   122spec:
   123  headers:
   124    ":authority": foo.example.com
   125  prefix: /wanted_group/
   126  service: star
   127"""
   128
   129    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
   130
   131    ir = r["ir"]
   132
   133    errors = ir.aconf.errors
   134    assert len(errors) == 0, "Expected no errors but got %s" % (
   135        json.dumps(errors, sort_keys=True, indent=4)
   136    )
   137
   138    found = 0
   139
   140    for g in ir.groups.values():
   141        if g.prefix == "/wanted_group/":
   142            assert g.host == "foo.example.com"
   143            found += 1
   144
   145    assert found == 1, "Expected 1 /wanted_group/ prefix, got %d" % found
   146
   147    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
   148
   149
   150@pytest.mark.compilertest
   151def test_mapping_host_authority_and_host():
   152    test_yaml = """
   153---
   154apiVersion: getambassador.io/v3alpha1
   155kind: Mapping
   156metadata:
   157  name: good-host-mapping
   158  namespace: default
   159spec:
   160  headers:
   161    ":authority": bar.example.com
   162  host: foo.example.com
   163  prefix: /wanted_group/
   164  service: star
   165"""
   166
   167    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
   168
   169    ir = r["ir"]
   170
   171    errors = ir.aconf.errors
   172    assert len(errors) == 0, "Expected no errors but got %s" % (
   173        json.dumps(errors, sort_keys=True, indent=4)
   174    )
   175
   176    found = 0
   177
   178    for g in ir.groups.values():
   179        if g.prefix == "/wanted_group/":
   180            assert g.host == "foo.example.com"
   181            found += 1
   182
   183    assert found == 1, "Expected 1 /wanted_group/ prefix, got %d" % found
   184
   185    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
   186
   187
   188@pytest.mark.compilertest
   189def test_mapping_hostname_ok():
   190    test_yaml = """
   191---
   192apiVersion: getambassador.io/v3alpha1
   193kind: Mapping
   194metadata:
   195  name: good-hostname-mapping
   196  namespace: default
   197spec:
   198  hostname: "*.example.com"
   199  prefix: /wanted_group/
   200  service: star
   201"""
   202
   203    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
   204
   205    ir = r["ir"]
   206
   207    errors = ir.aconf.errors
   208    assert len(errors) == 0, "Expected no errors but got %s" % (
   209        json.dumps(errors, sort_keys=True, indent=4)
   210    )
   211
   212    found = 0
   213
   214    for g in ir.groups.values():
   215        if g.prefix == "/wanted_group/":
   216            assert g.host == "*.example.com"
   217            found += 1
   218
   219    assert found == 1, "Expected 1 /wanted_group/ prefix, got %d" % found
   220
   221    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
   222
   223
   224@pytest.mark.compilertest
   225def test_mapping_hostname_and_host():
   226    test_yaml = """
   227---
   228apiVersion: getambassador.io/v3alpha1
   229kind: Mapping
   230metadata:
   231  name: hostname-and-host-mapping
   232  namespace: default
   233spec:
   234  host: foo.example.com
   235  hostname: "*.example.com"
   236  prefix: /wanted_group/
   237  service: star
   238"""
   239
   240    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
   241
   242    ir = r["ir"]
   243
   244    errors = ir.aconf.errors
   245    assert len(errors) == 0, "Expected no errors but got %s" % (
   246        json.dumps(errors, sort_keys=True, indent=4)
   247    )
   248
   249    found = 0
   250
   251    for g in ir.groups.values():
   252        if g.prefix == "/wanted_group/":
   253            assert g.host == "*.example.com"
   254            found += 1
   255
   256    assert found == 1, "Expected 1 /wanted_group/ prefix, got %d" % found
   257
   258    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
   259
   260
   261@pytest.mark.compilertest
   262def test_mapping_hostname_and_authority():
   263    test_yaml = """
   264---
   265apiVersion: getambassador.io/v3alpha1
   266kind: Mapping
   267metadata:
   268  name: hostname-and-host-mapping
   269  namespace: default
   270spec:
   271  headers:
   272    ":authority": foo.example.com
   273  hostname: "*.example.com"
   274  prefix: /wanted_group/
   275  service: star
   276"""
   277
   278    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
   279
   280    ir = r["ir"]
   281
   282    errors = ir.aconf.errors
   283    assert len(errors) == 0, "Expected no errors but got %s" % (
   284        json.dumps(errors, sort_keys=True, indent=4)
   285    )
   286
   287    found = 0
   288
   289    for g in ir.groups.values():
   290        if g.prefix == "/wanted_group/":
   291            assert g.host == "*.example.com"
   292            found += 1
   293
   294    assert found == 1, "Expected 1 /wanted_group/ prefix, got %d" % found
   295
   296    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))
   297
   298
   299@pytest.mark.compilertest
   300def test_mapping_hostname_and_host_and_authority():
   301    test_yaml = """
   302---
   303apiVersion: getambassador.io/v3alpha1
   304kind: Mapping
   305metadata:
   306  name: hostname-and-host-mapping
   307  namespace: default
   308spec:
   309  headers:
   310    ":authority": bar.example.com
   311  host: foo.example.com
   312  hostname: "*.example.com"
   313  prefix: /wanted_group/
   314  service: star
   315"""
   316
   317    r = compile_with_cachecheck(test_yaml, envoy_version="v3", errors_ok=True)
   318
   319    ir = r["ir"]
   320
   321    errors = ir.aconf.errors
   322    assert len(errors) == 0, "Expected no errors but got %s" % (
   323        json.dumps(errors, sort_keys=True, indent=4)
   324    )
   325
   326    found = 0
   327
   328    for g in ir.groups.values():
   329        if g.prefix == "/wanted_group/":
   330            assert g.host == "*.example.com"
   331            found += 1
   332
   333    assert found == 1, "Expected 1 /wanted_group/ prefix, got %d" % found
   334
   335    # print(json.dumps(ir.as_dict(), sort_keys=True, indent=4))

View as plain text