Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_test_model(foo_unit=True, foo_status='idle'):
juju_model = Model()
state = juju_model.state
state.apply_delta(ApplicationDelta(
('application', 'type2', {'name': 'foo'})))
if foo_unit:
state.apply_delta(UnitDelta(('unit', 'type1', {
'name': 'steve',
'application': 'foo',
# Add some status to fake out chaos's "wait 'til the
# cloud is idle" check.
'agent-status': {'current': foo_status}
})))
return juju_model
async def _set_model():
model = Model()
await model.connect_current()
self.model = model
def make_test_model(foo_unit=True, foo_status='idle'):
juju_model = Model()
state = juju_model.state
state.apply_delta(ApplicationDelta(
('application', 'type2', {'name': 'foo'})))
if foo_unit:
state.apply_delta(UnitDelta(('unit', 'type1', {
'name': 'steve',
'application': 'foo',
# Add some status to fake out chaos's "wait 'til the
# cloud is idle" check.
'agent-status': {'current': foo_status}
})))
return juju_model
def make_test_model(foo_unit=True, foo_status='idle'):
juju_model = Model()
state = juju_model.state
state.apply_delta(ApplicationDelta(
('application', 'type2', {'name': 'foo'})))
if foo_unit:
state.apply_delta(UnitDelta(('unit', 'type1', {
'name': 'steve',
'application': 'foo',
# Add some status to fake out chaos's "wait 'til the
# cloud is idle" check.
'agent-status': {'current': foo_status}
})))
return juju_model
def run_listener(client, event, juju_bin):
logging.info("Running listener.")
loop = asyncio.get_event_loop()
future = asyncio.Future()
logging.info("Connect to the current model.")
os.environ['JUJU_DATA'] = client.env.juju_home
os.environ['PATH'] = "{}{}{}".format(
juju_bin, os.pathsep, os.environ.get('PATH', ''))
conn = loop.run_until_complete(Connection.connect_current())
logging.info("Connected to the current model.")
asyncio.ensure_future(listen_to_watcher(event, conn, future))
return loop, future
def test_run_in_model(self):
self.patch_object(model, 'Model')
self.Model.return_value = self.Model_mock
async def _wrapper():
async with model.run_in_model('modelname') as mymodel:
return mymodel
self.assertEqual(loop.run(_wrapper()), self.Model_mock)
self.Model_mock.connect_model.assert_called_once_with('modelname')
self.Model_mock.disconnect.assert_called_once_with()
def test_run_in_model_exception(self):
self.patch_object(model, 'Model')
self.Model.return_value = self.Model_mock
async def _wrapper():
async with model.run_in_model('modelname'):
raise Exception
with self.assertRaises(Exception):
loop.run(_wrapper())
self.Model_mock.connect_model.assert_called_once_with('modelname')
self.Model_mock.disconnect.assert_called_once_with()
async def listen_to_watcher(event_found, conn, future):
logging.info("Starting to listen for the watcher.")
all_watcher = watcher.AllWatcher()
all_watcher.connect(conn)
for _ in until_timeout(120):
logging.info("Listening for events...")
change = await all_watcher.Next()
for delta in change.deltas:
logging.info("Event received: {}".format(str(delta.deltas)))
if event_found(delta.deltas) is True:
await all_watcher.Stop()
await conn.close()
logging.info("Event found: {}".format(str(delta.deltas)))
future.set_result(True)
return
await all_watcher.Stop()
await conn.close()
logging.warning("Event not found.")
def submit(self):
if self.constraints_copy:
try:
parsed = set(parse_constraints(self.constraints_copy))
except ValueError:
return self.set_constraints_error()
has_valid_constraints = parsed.issubset(
{normalize_key(field) for field in consts.ALLOWED_CONSTRAINTS})
if not has_valid_constraints:
return self.set_constraints_error()
self.application.options = self.options_copy
self.application.num_units = self.num_units_copy
self.application.constraints = self.constraints_copy
# Replace application data with updated info
new_data = self.application.to_dict()
app.current_bundle['applications'][self.application.name] = new_data
self.prev_screen()
async def run_in_model(model_name):
"""Context manager for executing code inside a libjuju model.
Example of using run_in_model:
async with run_in_model(model_name) as model:
model.do_something()
:param model_name: Name of model to run function in
:type model_name: str
:returns: The juju Model object correcsponding to model_name
:rtype: Iterator[:class:'juju.Model()']
"""
model = Model()
if not model_name:
model_name = await async_get_juju_model()
await model.connect_model(model_name)
try:
await yield_(model)
finally:
await model.disconnect()