|
| 1 | +"""Minimal reproducible example script. |
| 2 | +
|
| 3 | +This script is for you to use to reproduce a bug or demonstrate a feature. |
| 4 | +""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from os import getenv |
| 8 | +import json |
| 9 | +import time |
| 10 | + |
| 11 | +import docker |
| 12 | +from docker.errors import NotFound |
| 13 | +from docker.models.containers import Container |
| 14 | +from docker.models.networks import Network |
| 15 | + |
| 16 | +from acapy_controller import Controller |
| 17 | +from acapy_controller.logging import logging_to_stdout |
| 18 | +from acapy_controller.protocols import ( |
| 19 | + connection, |
| 20 | + didexchange, |
| 21 | + indy_anoncred_credential_artifacts, |
| 22 | + indy_anoncred_onboard, |
| 23 | + indy_anoncreds_publish_revocation, |
| 24 | + indy_anoncreds_revoke, |
| 25 | + indy_issue_credential_v2, |
| 26 | + indy_present_proof_v2, |
| 27 | +) |
| 28 | + |
| 29 | +ALICE = getenv("ALICE", "http://alice:3001") |
| 30 | +BOB = getenv("BOB", "http://bob:3001") |
| 31 | + |
| 32 | + |
| 33 | +def healthy(container: Container) -> bool: |
| 34 | + """Check if container is healthy.""" |
| 35 | + inspect_results = container.attrs |
| 36 | + return inspect_results["State"]["Running"] and inspect_results["State"]["Health"]["Status"] == "healthy" |
| 37 | + |
| 38 | + |
| 39 | +def unhealthy(container: Container) -> bool: |
| 40 | + """Check if container is unhealthy.""" |
| 41 | + inspect_results = container.attrs |
| 42 | + return not inspect_results["State"]["Running"] |
| 43 | + |
| 44 | + |
| 45 | +def wait_until_healthy(client, container_id: str, attempts: int = 350, is_healthy=True): |
| 46 | + """Wait until container is healthy.""" |
| 47 | + container = client.containers.get(container_id) |
| 48 | + print((container.name, container.status)) |
| 49 | + for _ in range(attempts): |
| 50 | + if (is_healthy and healthy(container)) or unhealthy(container): |
| 51 | + return |
| 52 | + else: |
| 53 | + time.sleep(1) |
| 54 | + container = client.containers.get(container_id) |
| 55 | + raise TimeoutError("Timed out waiting for container") |
| 56 | + |
| 57 | + |
| 58 | +async def main(): |
| 59 | + """Test Controller protocols.""" |
| 60 | + async with Controller(base_url=ALICE) as alice, Controller(base_url=BOB) as bob: |
| 61 | + # connect the 2 agents |
| 62 | + print(">>> connecting agents ...") |
| 63 | + (alice_conn, bob_conn) = await didexchange(alice, bob) |
| 64 | + |
| 65 | + # setup alice as an issuer |
| 66 | + print(">>> setting up alice as issuer ...") |
| 67 | + await indy_anoncred_onboard(alice) |
| 68 | + schema, cred_def = await indy_anoncred_credential_artifacts( |
| 69 | + alice, |
| 70 | + ["firstname", "lastname"], |
| 71 | + support_revocation=True, |
| 72 | + ) |
| 73 | + |
| 74 | + # Issue a credential |
| 75 | + print(">>> issue credential ...") |
| 76 | + alice_cred_ex, _ = await indy_issue_credential_v2( |
| 77 | + alice, |
| 78 | + bob, |
| 79 | + alice_conn.connection_id, |
| 80 | + bob_conn.connection_id, |
| 81 | + cred_def.credential_definition_id, |
| 82 | + {"firstname": "Bob", "lastname": "Builder"}, |
| 83 | + ) |
| 84 | + |
| 85 | + # Present the the credential's attributes |
| 86 | + print(">>> present proof ...") |
| 87 | + await indy_present_proof_v2( |
| 88 | + bob, |
| 89 | + alice, |
| 90 | + bob_conn.connection_id, |
| 91 | + alice_conn.connection_id, |
| 92 | + requested_attributes=[{"name": "firstname"}], |
| 93 | + ) |
| 94 | + print(">>> Done!") |
| 95 | + |
| 96 | + # play with docker |
| 97 | + client = docker.from_env() |
| 98 | + containers = client.containers.list(all=True) |
| 99 | + docker_containers = {} |
| 100 | + for container in containers: |
| 101 | + if 'com.docker.compose.service' in container.attrs['Config']['Labels']: |
| 102 | + container_name = container.attrs['Config']['Labels']['com.docker.compose.service'] |
| 103 | + container_id = container.attrs['Id'] |
| 104 | + container_is_running = container.attrs['State']['Running'] |
| 105 | + docker_containers[container_name] = {'Id': container_id, 'Running': container_is_running} |
| 106 | + print(">>> container:", container_name, docker_containers[container_name]) |
| 107 | + |
| 108 | + # try to restart a container (stop alice and start alice-upgrade) |
| 109 | + alice_docker_container = docker_containers['alice'] |
| 110 | + alice_container = client.containers.get(alice_docker_container['Id']) |
| 111 | + |
| 112 | + print(">>> shut down alice ...") |
| 113 | + alice_container.stop() |
| 114 | + |
| 115 | + print(">>> waiting for alice container to exit ...") |
| 116 | + alice_id = alice_container.attrs['Id'] |
| 117 | + wait_until_healthy(client, alice_id, is_healthy=False) |
| 118 | + alice_container.remove() |
| 119 | + |
| 120 | + print(">>> start new alice container ...") |
| 121 | + new_alice_container = client.containers.run( |
| 122 | + 'acapy-test', |
| 123 | + command=alice_container.attrs['Config']['Cmd'], |
| 124 | + detach=True, |
| 125 | + environment={'RUST_LOG': 'aries-askar::log::target=error'}, |
| 126 | + healthcheck=alice_container.attrs['Config']['Healthcheck'], |
| 127 | + name='alice', |
| 128 | + network=alice_container.attrs['HostConfig']['NetworkMode'], |
| 129 | + ports=alice_container.attrs['NetworkSettings']['Ports'], |
| 130 | + ) |
| 131 | + print(">>> new container:", 'alice', json.dumps(new_alice_container.attrs)) |
| 132 | + alice_id = new_alice_container.attrs['Id'] |
| 133 | + |
| 134 | + try: |
| 135 | + wait_until_healthy(client, alice_id) |
| 136 | + print(">>> new alice container is healthy") |
| 137 | + |
| 138 | + # run some more tests ... alice should still be connected to bob for example ... |
| 139 | + async with Controller(base_url=ALICE) as alice, Controller(base_url=BOB) as bob: |
| 140 | + # Present the the credential's attributes |
| 141 | + print(">>> present proof ... again ...") |
| 142 | + await indy_present_proof_v2( |
| 143 | + bob, |
| 144 | + alice, |
| 145 | + bob_conn.connection_id, |
| 146 | + alice_conn.connection_id, |
| 147 | + requested_attributes=[{"name": "firstname"}], |
| 148 | + ) |
| 149 | + print(">>> Done! (again)") |
| 150 | + finally: |
| 151 | + # cleanup - shut down alice agent (not part of docker compose) |
| 152 | + print(">>> shut down alice ...") |
| 153 | + alice_container = client.containers.get(alice_id) |
| 154 | + alice_container.stop() |
| 155 | + wait_until_healthy(client, alice_id, is_healthy=False) |
| 156 | + alice_container.remove() |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + logging_to_stdout() |
| 161 | + asyncio.run(main()) |
0 commit comments