|
| 1 | +import pytest |
| 2 | + |
| 3 | +from juju.application import ExposedEndpoint |
| 4 | + |
| 5 | +from .. import base |
| 6 | + |
| 7 | + |
| 8 | +@base.bootstrapped |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_expose_unexpose(event_loop): |
| 11 | + async with base.CleanModel() as model: |
| 12 | + app = await model.deploy( |
| 13 | + "cs:~jameinel/ubuntu-lite-7", |
| 14 | + ) |
| 15 | + |
| 16 | + if not app.supports_granular_expose_parameters(): |
| 17 | + pytest.skip("this test requires a 2.9 or greated controller") |
| 18 | + |
| 19 | + # Expose all opened port ranges |
| 20 | + await app.expose() |
| 21 | + exposed_endpoints = await get_exposed_endpoints(model, app) |
| 22 | + assert exposed_endpoints == { |
| 23 | + "": { |
| 24 | + "expose-to-cidrs": ["0.0.0.0/0", "::/0"], |
| 25 | + "expose-to-spaces": None |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + # Expose all opened port ranges to the CIDRs that correspond to a list |
| 30 | + # of spaces. |
| 31 | + await app.expose(exposed_endpoints={ |
| 32 | + "": ExposedEndpoint(to_spaces=["alpha"]) |
| 33 | + }) |
| 34 | + exposed_endpoints = await get_exposed_endpoints(model, app) |
| 35 | + assert exposed_endpoints == { |
| 36 | + "": { |
| 37 | + "expose-to-cidrs": None, |
| 38 | + "expose-to-spaces": ["alpha"] |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + # Expose all opened port ranges to a list of CIDRs. |
| 43 | + await app.expose(exposed_endpoints={ |
| 44 | + "": ExposedEndpoint(to_cidrs=["10.0.0.0/24"]) |
| 45 | + }) |
| 46 | + exposed_endpoints = await get_exposed_endpoints(model, app) |
| 47 | + assert exposed_endpoints == { |
| 48 | + "": { |
| 49 | + "expose-to-cidrs": ["10.0.0.0/24"], |
| 50 | + "expose-to-spaces": None |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + # Expose all opened port ranges to a list of spaces and CIDRs. |
| 55 | + await app.expose(exposed_endpoints={ |
| 56 | + "": ExposedEndpoint(to_spaces=["alpha"], to_cidrs=["10.0.0.0/24"]) |
| 57 | + }) |
| 58 | + exposed_endpoints = await get_exposed_endpoints(model, app) |
| 59 | + assert exposed_endpoints == { |
| 60 | + "": { |
| 61 | + "expose-to-spaces": ["alpha"], |
| 62 | + "expose-to-cidrs": ["10.0.0.0/24"] |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + # Expose individual endpoints to different space/CIDR combinations |
| 67 | + await app.expose(exposed_endpoints={ |
| 68 | + "": ExposedEndpoint(to_spaces=["alpha"], to_cidrs=["10.0.0.0/24"]), |
| 69 | + "ubuntu": ExposedEndpoint(to_cidrs=["10.42.42.0/24"]) |
| 70 | + }) |
| 71 | + exposed_endpoints = await get_exposed_endpoints(model, app) |
| 72 | + assert exposed_endpoints == { |
| 73 | + "": { |
| 74 | + "expose-to-spaces": ["alpha"], |
| 75 | + "expose-to-cidrs": ["10.0.0.0/24"] |
| 76 | + }, |
| 77 | + "ubuntu": { |
| 78 | + "expose-to-cidrs": ["10.42.42.0/24"], |
| 79 | + "expose-to-spaces": None |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + # Unexpose individual endpoints (other endpoints remain exposed). |
| 84 | + await app.unexpose(exposed_endpoints=["ubuntu"]) |
| 85 | + exposed_endpoints = await get_exposed_endpoints(model, app) |
| 86 | + assert exposed_endpoints == { |
| 87 | + "": { |
| 88 | + "expose-to-spaces": ["alpha"], |
| 89 | + "expose-to-cidrs": ["10.0.0.0/24"] |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + # Unexpose application |
| 94 | + await app.unexpose() |
| 95 | + exposed_endpoints = await get_exposed_endpoints(model, app) |
| 96 | + assert exposed_endpoints == {} |
| 97 | + |
| 98 | + |
| 99 | +async def get_exposed_endpoints(model, app): |
| 100 | + app_name = app.name |
| 101 | + status = await model.get_status(filters=[app_name]) |
| 102 | + exposed_endpoints = status.applications[app_name].exposed_endpoints |
| 103 | + return {k: v.serialize() for k, v in exposed_endpoints.items()} |
0 commit comments