Skip to content

Commit b29ae95

Browse files
committed
Initial WIP for changes using --existing.
1 parent 3b44382 commit b29ae95

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

acceptancetests/deploy_stack.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ class BootstrapManager:
699699
def __init__(self, temp_env_name, client, tear_down_client, bootstrap_host,
700700
machines, series, agent_url, agent_stream, region, log_dir,
701701
keep_env, permanent, jes_enabled, controller_strategy=None,
702-
logged_exception_exit=True):
702+
logged_exception_exit=True, existing_controller=None):
703703
"""Constructor.
704704
705705
Please see see `BootstrapManager` for argument descriptions.
@@ -728,6 +728,10 @@ def __init__(self, temp_env_name, client, tear_down_client, bootstrap_host,
728728
self.has_controller = False
729729
self.resource_details = None
730730

731+
# We can probably just make this just 1 state.
732+
self.existing_controller = existing_controller is not None
733+
self.controller_id = existing_controller
734+
731735
def ensure_cleanup(self):
732736
"""
733737
Ensure any required cleanup for the current substrate is done.
@@ -775,6 +779,9 @@ def tear_down_client(self):
775779

776780
@classmethod
777781
def from_args(cls, args):
782+
if 'existing' in args and args.existing:
783+
return cls._from_existing_controller(args)
784+
778785
if not args.logs:
779786
args.logs = generate_default_clean_dir(args.temp_env_name)
780787

@@ -791,7 +798,11 @@ def from_args(cls, args):
791798
return cls.from_client(args, client)
792799

793800
@classmethod
794-
def from_existing_controller(cls, args):
801+
def _from_existing_controller(cls, args):
802+
if 'existing' not in args:
803+
raise RuntimeError(
804+
'Attempting to use existing controller without providing '
805+
'controller id.')
795806
try:
796807
juju_home = os.environ['JUJU_DATA']
797808
except KeyError:
@@ -817,17 +828,18 @@ def from_existing_controller(cls, args):
817828
controller_name=controller,
818829
model_name=model)
819830
client.has_controller = True
820-
return cls.from_client_existing(args, client)
831+
return cls.from_client_existing(args, client, args.existing)
821832

822833
@classmethod
823-
def from_client_existing(cls, args, client):
834+
def from_client_existing(cls, args, client, existing_controller):
824835
jes_enabled = client.is_jes_enabled()
825836
controller_strategy = ExistingController(client)
826837
return cls(
827838
args.temp_env_name, client, client, args.bootstrap_host,
828839
args.machine, args.series, args.agent_url, args.agent_stream,
829840
args.region, args.logs, args.keep_env, permanent=jes_enabled,
830-
jes_enabled=jes_enabled, controller_strategy=controller_strategy)
841+
jes_enabled=jes_enabled, controller_strategy=controller_strategy,
842+
existing_controller=existing_controller)
831843

832844
@classmethod
833845
def from_client(cls, args, client):
@@ -1108,22 +1120,26 @@ def booted_context(self, upload_tools, **kwargs):
11081120
:param **kwargs: All remaining keyword arguments are passed to the
11091121
client's bootstrap.
11101122
"""
1111-
try:
1112-
with self.top_context() as machines:
1113-
with self.bootstrap_context(
1114-
machines, omit_config=self.client.bootstrap_replaces):
1115-
self.controller_strategy.create_initial_model(
1116-
upload_tools, self.series, kwargs)
1117-
with self.runtime_context(machines):
1118-
self.client.list_controllers()
1119-
self.client.list_models()
1120-
for m_client in self.client.iter_model_clients():
1121-
m_client.show_status()
1122-
yield machines
1123-
except LoggedException:
1124-
if self.logged_exception_exit:
1125-
sys.exit(1)
1126-
raise
1123+
if self.existing_controller:
1124+
yield self.existing_context(upload_tools, **kwargs)
1125+
else:
1126+
try:
1127+
with self.top_context() as machines:
1128+
with self.bootstrap_context(
1129+
machines,
1130+
omit_config=self.client.bootstrap_replaces):
1131+
self.controller_strategy.create_initial_model(
1132+
upload_tools, self.series, kwargs)
1133+
with self.runtime_context(machines):
1134+
self.client.list_controllers()
1135+
self.client.list_models()
1136+
for m_client in self.client.iter_model_clients():
1137+
m_client.show_status()
1138+
yield machines
1139+
except LoggedException:
1140+
if self.logged_exception_exit:
1141+
sys.exit(1)
1142+
raise
11271143

11281144
@contextmanager
11291145
def existing_booted_context(self, upload_tools, **kwargs):
@@ -1142,13 +1158,15 @@ def existing_booted_context(self, upload_tools, **kwargs):
11421158
sys.exit(1)
11431159

11441160
@contextmanager
1145-
def existing_context(self, upload_tools, controller_id):
1161+
def existing_context(self, upload_tools):
1162+
if controller_id is None:
1163+
raise RuntimeError() # Lets make it so this isn't possible.
11461164
try:
11471165
with self.top_context() as machines:
11481166
with self.runtime_context(machines):
11491167
self.has_controller = True
1150-
if controller_id != 'current':
1151-
self.controller_strategy.prepare(controller_id)
1168+
if self.controller_id != 'current':
1169+
self.controller_strategy.prepare(self.controller_id)
11521170
self.controller_strategy.create_initial_model()
11531171
yield machines
11541172
except LoggedException:
@@ -1265,15 +1283,3 @@ def wait_for_state_server_to_shutdown(host, client, instance_id, timeout=60):
12651283
else:
12661284
raise Exception(
12671285
'{} was not deleted:'.format(instance_id))
1268-
1269-
1270-
def test_on_controller(test, args):
1271-
if args.existing:
1272-
bs_manager = BootstrapManager.from_existing_controller(args)
1273-
with bs_manager.existing_context(args.upload_tools,
1274-
args.existing):
1275-
test(bs_manager.client)
1276-
else:
1277-
bs_manager = BootstrapManager.from_args(args)
1278-
with bs_manager.booted_context(args.upload_tools):
1279-
test(bs_manager.client)

acceptancetests/utility.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ def add_arg_juju_bin(parser):
247247
default=None)
248248

249249

250-
def add_basic_testing_arguments(parser, using_jes=False, deadline=True,
251-
env=True):
250+
def add_basic_testing_arguments(
251+
parser, using_jes=False, deadline=True, env=True, existing=False):
252252
"""Returns the parser loaded with basic testing arguments.
253253
254254
The basic testing arguments, used in conjuction with boot_context ensures
@@ -317,9 +317,10 @@ def add_basic_testing_arguments(parser, using_jes=False, deadline=True,
317317
parser.add_argument('--keep-env', action='store_true',
318318
help='Keep the Juju environment after the test'
319319
' completes.')
320-
parser.add_argument('--existing', action='store',
321-
help='Existing controller to test against. Using '
322-
'"current" selects the current controller')
320+
if existing:
321+
parser.add_argument('--existing', action='store',
322+
help='Existing controller to test against. Using '
323+
'"current" selects the current controller')
323324
if deadline:
324325
parser.add_argument('--timeout', dest='deadline', type=_to_deadline,
325326
help="The script timeout, in seconds.")

0 commit comments

Comments
 (0)