1771.1.1
by Seman Said
Added model change tests. |
1 |
#!/usr/bin/env python3
|
1751.1.1
by Seman Said
Added assess model change test. |
2 |
|
3 |
import argparse |
|
1771.1.1
by Seman Said
Added model change tests. |
4 |
import asyncio |
1751.1.1
by Seman Said
Added assess model change test. |
5 |
import logging |
1771.1.1
by Seman Said
Added model change tests. |
6 |
import os |
1751.1.1
by Seman Said
Added assess model change test. |
7 |
import sys |
8 |
||
1771.1.1
by Seman Said
Added model change tests. |
9 |
from juju.client.connection import Connection |
10 |
from juju.client import watcher |
|
11 |
||
1751.1.1
by Seman Said
Added assess model change test. |
12 |
from deploy_stack import ( |
13 |
BootstrapManager, |
|
1771.1.1
by Seman Said
Added model change tests. |
14 |
until_timeout, |
1751.1.1
by Seman Said
Added assess model change test. |
15 |
)
|
1751.1.14
by Aaron Bentley
Use Juju 2 compatible deploy. |
16 |
from jujucharm import local_charm_path |
1751.1.1
by Seman Said
Added assess model change test. |
17 |
from utility import ( |
18 |
add_basic_testing_arguments, |
|
19 |
configure_logging, |
|
1771.1.1
by Seman Said
Added model change tests. |
20 |
JujuAssertionError, |
1751.1.1
by Seman Said
Added assess model change test. |
21 |
)
|
22 |
||
23 |
||
24 |
log = logging.getLogger("assess_model_change_watcher") |
|
1771.1.1
by Seman Said
Added model change tests. |
25 |
TOKEN = "1234asdf" |
26 |
||
27 |
||
28 |
def is_config_change_in_event(event): |
|
29 |
message = _get_message(event) |
|
30 |
return all([ |
|
31 |
"application" in event, |
|
32 |
"change" in event, |
|
33 |
is_in_dict("config", {"token": TOKEN}, message), |
|
34 |
])
|
|
35 |
||
36 |
||
37 |
def _get_message(event): |
|
38 |
for message in event: |
|
39 |
if isinstance(message, dict): |
|
40 |
return message |
|
41 |
return None |
|
42 |
||
43 |
||
44 |
def is_in_dict(key, value, items): |
|
45 |
return items.get(key) == value |
|
46 |
||
47 |
||
1771.1.2
by Seman Said
test working |
48 |
async def listen_to_watcher(event_found, conn, future): |
1771.1.1
by Seman Said
Added model change tests. |
49 |
logging.info("Starting to listen for the watcher.") |
1771.1.3
by Seman Said
Updated makefile. |
50 |
all_watcher = watcher.AllWatcher() |
51 |
all_watcher.connect(conn) |
|
1771.1.2
by Seman Said
test working |
52 |
for _ in until_timeout(120): |
53 |
logging.info("Listening for events...") |
|
1771.1.3
by Seman Said
Updated makefile. |
54 |
change = await all_watcher.Next() |
1771.1.1
by Seman Said
Added model change tests. |
55 |
for delta in change.deltas: |
56 |
logging.info("Event received: {}".format(str(delta.deltas))) |
|
1771.1.2
by Seman Said
test working |
57 |
if event_found(delta.deltas) is True: |
1771.1.3
by Seman Said
Updated makefile. |
58 |
await all_watcher.Stop() |
1771.1.2
by Seman Said
test working |
59 |
await conn.close() |
1771.1.1
by Seman Said
Added model change tests. |
60 |
logging.info("Event found: {}".format(str(delta.deltas))) |
61 |
future.set_result(True) |
|
62 |
return
|
|
1771.1.3
by Seman Said
Updated makefile. |
63 |
|
64 |
await all_watcher.Stop() |
|
1771.1.2
by Seman Said
test working |
65 |
await conn.close() |
66 |
logging.warning("Event not found.") |
|
67 |
future.set_result(False) |
|
1771.1.1
by Seman Said
Added model change tests. |
68 |
|
69 |
||
70 |
def run_listener(client, event, juju_bin): |
|
1771.1.3
by Seman Said
Updated makefile. |
71 |
logging.info("Running listener.") |
1771.1.1
by Seman Said
Added model change tests. |
72 |
loop = asyncio.get_event_loop() |
73 |
future = asyncio.Future() |
|
74 |
||
75 |
logging.info("Connect to the current model.") |
|
76 |
os.environ['JUJU_DATA'] = client.env.juju_home |
|
1771.1.2
by Seman Said
test working |
77 |
os.environ['PATH'] = "{}{}{}".format( |
78 |
juju_bin, os.pathsep, os.environ.get('PATH', '')) |
|
79 |
conn = loop.run_until_complete(Connection.connect_current()) |
|
1771.1.1
by Seman Said
Added model change tests. |
80 |
logging.info("Connected to the current model.") |
81 |
||
82 |
asyncio.ensure_future(listen_to_watcher(event, conn, future)) |
|
1771.1.3
by Seman Said
Updated makefile. |
83 |
return loop, future |
1771.1.1
by Seman Said
Added model change tests. |
84 |
|
85 |
||
86 |
def assess_model_change_watcher(client, charm_series, juju_bin): |
|
1771.1.3
by Seman Said
Updated makefile. |
87 |
charm = local_charm_path( |
88 |
charm='dummy-source', juju_ver=client.version, series=charm_series, |
|
89 |
platform='ubuntu') |
|
90 |
client.deploy(charm) |
|
91 |
client.wait_for_started() |
|
92 |
||
93 |
loop, future = run_listener(client, is_config_change_in_event, juju_bin) |
|
94 |
||
95 |
logging.info("Making config change.") |
|
96 |
client.set_config('dummy-source', {'token': TOKEN}) |
|
1771.1.1
by Seman Said
Added model change tests. |
97 |
|
98 |
loop.run_until_complete(future) |
|
99 |
result = future.result() |
|
100 |
if result is not True: |
|
1771.1.3
by Seman Said
Updated makefile. |
101 |
raise JujuAssertionError("Config change event was not sent.") |
1771.1.1
by Seman Said
Added model change tests. |
102 |
loop.close() |
1751.1.1
by Seman Said
Added assess model change test. |
103 |
|
104 |
||
105 |
def parse_args(argv): |
|
1771.1.3
by Seman Said
Updated makefile. |
106 |
parser = argparse.ArgumentParser(description="Assess config change.") |
1751.1.1
by Seman Said
Added assess model change test. |
107 |
add_basic_testing_arguments(parser) |
108 |
return parser.parse_args(argv) |
|
109 |
||
110 |
||
111 |
def main(argv=None): |
|
112 |
args = parse_args(argv) |
|
113 |
configure_logging(args.verbose) |
|
114 |
bs_manager = BootstrapManager.from_args(args) |
|
115 |
with bs_manager.booted_context(args.upload_tools): |
|
1771.1.3
by Seman Said
Updated makefile. |
116 |
assess_model_change_watcher( |
117 |
bs_manager.client, bs_manager.series, args.juju_bin) |
|
1751.1.1
by Seman Said
Added assess model change test. |
118 |
return 0 |
119 |
||
120 |
||
121 |
if __name__ == '__main__': |
|
1771.1.3
by Seman Said
Updated makefile. |
122 |
sys.exit(main()) |