Skip to content

Commit

Permalink
Merge pull request #474 from ajkavanagh/switch-public-address-to-get-…
Browse files Browse the repository at this point in the history
…public-address

Switch public address to get public address and add fallback for unit.get_public_address()
  • Loading branch information
lourot authored Jan 14, 2022
2 parents a1edf18 + 41b736a commit 81416cb
Show file tree
Hide file tree
Showing 19 changed files with 563 additions and 28 deletions.
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ ignore:
- "unit_tests/*"
- "unit_tests/utilities/*"
- "unit_tests/**/*"
- "scripts/*"
- "zaza/charm_tests/**/*"
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pyparsing<3.0.0 # pin for aodhclient which is held for py35
aiounittest
async_generator
kubernetes<18.0.0; python_version < '3.6' # pined, as juju uses kubernetes
juju
juju_wait
PyYAML>=3.0
Expand All @@ -19,6 +20,7 @@ Jinja2>=2.6 # BSD License (3 clause)
six>=1.9.0
dnspython>=1.12.0
psutil>=1.1.1,<2.0.0
oslo.config<6.9.0;python_version < '3.6' # pin for py3.5 support
oslo.context<3.0.0;python_version < '3.6' # pin for py3.5 support
osprofiler<3.0.0;python_version < '3.6' # pin for py3.5 support
python-openstackclient>=3.14.0
Expand Down
5 changes: 5 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Scripts sub-directory

The scripts in this directory are for regression/manual testing of the
libjuju unit.get_public_address() function. The tox target 'third' can
also be used for regression testing.
15 changes: 15 additions & 0 deletions scripts/bash_tester.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

_dir="$( cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
runner="${_dir}/fetch1.py"

# loop 10 times and fetch an instance address
i=0
while [ $i -ne 10 ];
do
printf "\n\n\n!!!!!!"
printf "\n\n\nDoing number $i"
printf "\n\n"
$runner $i
i=$(($i+1))
done
98 changes: 98 additions & 0 deletions scripts/fetch1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python3

from juju.model import Model
import asyncio
import sys

# set to use something other than the current model
MODEL=None


async def get_units():
model = Model()
if MODEL is None:
await model.connect()
else:
await model.connect_model(MODEL)
units = sorted(model.applications['ubuntu'].units, key=lambda u: u.name)
await model.disconnect()
return units


async def get_address(unit):
model = Model()
await model.connect_model(MODEL)
print("{} Address: .public_address {}".format(unit.name, unit.public_address))
while True:
try:
print("{} Address: get_public_address() {}".format(unit.name, await unit.get_public_address()))
break
except Exception as e:
print("Exception was: %s", e)
await asyncio.sleep(.25)
await ensure_model_connected(model)
print("{} Address: .public_address {}".format(unit.name, unit.public_address))
print("\n")
await model.disconnect()


def run_it(step):
loop = asyncio.get_event_loop()
task = loop.create_task(step)
loop.run_until_complete(asyncio.wait([task], loop=loop))
result = task.result()
return result


async def get_unit(n):
units = await get_units()
print("units", units)
await get_address(units[n])


def is_model_disconnected(model):
"""Return True if the model is disconnected.
:param model: the model to check
:type model: :class:'juju.Model'
:returns: True if disconnected
:rtype: bool
"""
print("is_model_disconnected?: %s, %s", model.is_connected(), model.connection().is_open)
return not (model.is_connected() and model.connection().is_open)


async def ensure_model_connected(model):
"""Ensure that the model is connected.
If model is disconnected then reconnect it.
:param model: the model to check
:type model: :class:'juju.Model'
"""
if is_model_disconnected(model):
model_name = model.info.name
print(
"model: %s has disconnected, forcing full disconnection "
"and then reconnecting ...", model_name)
try:
await model.disconnect()
except Exception:
# We don't care if disconnect fails; we're much more
# interested in re-connecting, and this is just to clean up
# anything that might be left over (i.e.
# model.is_connected() might be true, but
# model.connection().is_open may be false
pass
print("Attempting to reconnect model %s", model_name)
await model.connect_model(model_name)


if __name__ == '__main__':
unit_num = 0
if len(sys.argv) > 1:
unit_num = int(sys.argv[1])

run_it(get_unit(unit_num))
asyncio.get_event_loop().close()

47 changes: 47 additions & 0 deletions scripts/tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

from juju.model import Model
import asyncio

# set to use something other than the current model
MODEL=None


async def get_units():
model = Model()
if MODEL is None:
await model.connect()
else:
await model.connect_model(MODEL)
units = sorted(model.applications['ubuntu'].units, key=lambda u: u.name)
await model.disconnect()
return units


async def get_address(unit):
model = Model()
await model.connect_model(MODEL)
print("{} Address: .public_address {}".format(unit.name, unit.public_address))
print("{} Address: get_public_address() {}".format(unit.name, await unit.get_public_address()))
print("{} Address: .public_address {}".format(unit.name, unit.public_address))
print("\n")
await model.disconnect()


def run_it(step):
loop = asyncio.get_event_loop()
task = loop.create_task(step)
loop.run_until_complete(asyncio.wait([task], loop=loop))
result = task.result()
return result


def get_all_units():
units = run_it(get_units())
print("units", units)
for unit in units:
run_it(get_address(unit))


get_all_units()
asyncio.get_event_loop().close()
1 change: 0 additions & 1 deletion tests-extended/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ tests_options:
log-to-stdout: false
log-to-python-logging: true
python-logging-level: info
logger-name: DEFAULT
raise-exceptions: true
upload:
- type: InfluxDB
Expand Down
2 changes: 1 addition & 1 deletion tests/bundles/first.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ applications:
num_units: 2
ubuntu:
charm: cs:ubuntu
num_units: 1
num_units: 3
2 changes: 1 addition & 1 deletion tests/bundles/second.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ applications:
num_units: 2
ubuntu:
charm: cs:ubuntu
num_units: 1
num_units: 3
5 changes: 5 additions & 0 deletions tests/bundles/third.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
applications:
ubuntu:
charm: cs:ubuntu
num_units: 10

3 changes: 3 additions & 0 deletions tests/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gate_bundles:
# destruction
- first
- second
- third
target_deploy_status:
magpie-xenial:
workload-status: active
Expand All @@ -24,7 +25,9 @@ configure:
- zaza.charm_tests.noop.setup.basic_setup
tests:
- zaza.charm_tests.noop.tests.NoopTest
- zaza.charm_tests.libjuju.tests.RegressionTest
tests_options:
force_deploy:
first
second
third
Loading

0 comments on commit 81416cb

Please sign in to comment.