diff --git a/sunbeam-python/sunbeam/features/pro/feature.py b/sunbeam-python/sunbeam/features/pro/feature.py index caaa290ca..592310cd7 100644 --- a/sunbeam-python/sunbeam/features/pro/feature.py +++ b/sunbeam-python/sunbeam/features/pro/feature.py @@ -16,7 +16,7 @@ from sunbeam.clusterd.client import Client from sunbeam.core.common import BaseStep, Result, ResultType, StepContext, run_plan from sunbeam.core.deployment import Deployment -from sunbeam.core.juju import JujuHelper, JujuStepHelper +from sunbeam.core.juju import JujuException, JujuHelper, JujuStepHelper from sunbeam.core.manifest import ( AddManifestStep, CharmManifest, @@ -86,11 +86,11 @@ def is_skip(self, context: StepContext) -> Result: def run(self, context: StepContext) -> Result: """Apply terraform configuration to deploy ubuntu-pro.""" - extra_tfvars = { - "machine_model_uuid": self.jhelper.get_model_uuid(self.model), - "token": self.token, - } try: + extra_tfvars = { + "machine_model_uuid": self.jhelper.get_model_uuid(self.model), + "token": self.token, + } self.tfhelper.update_tfvars_and_apply_tf( self.client, self.manifest, @@ -98,7 +98,7 @@ def run(self, context: StepContext) -> Result: override_tfvars=extra_tfvars, reporter=context.reporter, ) - except (TerraformException, TerraformStateLockedException) as e: + except (JujuException, TerraformException, TerraformStateLockedException) as e: return Result(ResultType.FAILED, str(e)) # Note(gboutry): application is in state unknown when it's deployed @@ -137,12 +137,16 @@ def __init__( self, client: Client, tfhelper: TerraformHelper, + jhelper: JujuHelper, manifest: Manifest, + model: str, ): super().__init__("Disable Ubuntu Pro", "Disabling Ubuntu Pro support") self.client = client self.tfhelper = tfhelper + self.jhelper = jhelper self.manifest = manifest + self.model = model def has_prompts(self) -> bool: """Returns true if the step has prompts that it can ask the user.""" @@ -158,8 +162,13 @@ def is_skip(self, context: StepContext) -> Result: def run(self, context: StepContext) -> Result: """Apply terraform configuration to disable ubuntu-pro.""" - extra_tfvars = {"token": ""} + # The tfvars are not persisted between runs, so every variable the + # plan requires must be passed again, not only the emptied token. try: + extra_tfvars = { + "machine_model_uuid": self.jhelper.get_model_uuid(self.model), + "token": "", + } self.tfhelper.update_tfvars_and_apply_tf( self.client, self.manifest, @@ -167,7 +176,7 @@ def run(self, context: StepContext) -> Result: override_tfvars=extra_tfvars, reporter=context.reporter, ) - except TerraformException as e: + except (JujuException, TerraformException, TerraformStateLockedException) as e: return Result(ResultType.FAILED, str(e)) return Result(ResultType.COMPLETED) @@ -258,12 +267,15 @@ def run_enable_plans( def run_disable_plans(self, deployment: Deployment, show_hints: bool): """Run the disablement plans.""" tfhelper = deployment.get_tfhelper(self.tfplan) + jhelper = JujuHelper(deployment.juju_controller) plan = [ TerraformInitStep(tfhelper), DisableUbuntuProApplicationStep( deployment.get_client(), tfhelper, + jhelper, self.manifest, + deployment.openstack_machines_model, ), ] diff --git a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py index 6aa37084b..e7e1b03a9 100644 --- a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py +++ b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py @@ -9,11 +9,13 @@ import yaml from sunbeam.core.common import ResultType +from sunbeam.core.juju import JujuException, ModelNotFoundException from sunbeam.core.manifest import AddManifestStep, FeatureManifest, Manifest from sunbeam.core.terraform import ( TerraformException, TerraformHelper, TerraformInitStep, + TerraformStateLockedException, ) from sunbeam.features.pro import feature as pro_feature from sunbeam.features.pro.feature import ( @@ -88,6 +90,22 @@ def test_enable_tf_apply_failed(self, enable_step, basic_tfhelper, step_context) assert result.result_type == ResultType.FAILED assert result.message == "apply failed..." + @pytest.mark.parametrize( + "error", + [ModelNotFoundException("Model 'test-model' not found"), JujuException("boom")], + ) + def test_enable_model_lookup_failed( + self, enable_step, basic_jhelper, basic_tfhelper, step_context, error + ): + basic_jhelper.get_model_uuid.side_effect = error + + result = enable_step.run(step_context) + + basic_tfhelper.update_tfvars_and_apply_tf.assert_not_called() + basic_jhelper.wait_application_ready.assert_not_called() + assert result.result_type == ResultType.FAILED + assert result.message == str(error) + def test_enable_waiting_timed_out(self, enable_step, basic_jhelper, step_context): basic_jhelper.wait_application_ready.side_effect = TimeoutError("timed out") @@ -133,10 +151,12 @@ def test_enable_blocked_without_stderr( class TestDisableUbuntuProApplicationStep: @pytest.fixture - def disable_step(self, basic_client, basic_tfhelper, basic_manifest): + def disable_step( + self, basic_client, basic_tfhelper, basic_jhelper, basic_manifest, test_model + ): """Disable Ubuntu Pro step instance for testing.""" return DisableUbuntuProApplicationStep( - basic_client, basic_tfhelper, basic_manifest + basic_client, basic_tfhelper, basic_jhelper, basic_manifest, test_model ) def test_is_skip(self, disable_step, step_context): @@ -147,28 +167,61 @@ def test_has_prompts(self, disable_step): assert not disable_step.has_prompts() def test_disable( - self, disable_step, basic_client, basic_tfhelper, basic_manifest, step_context + self, + disable_step, + basic_client, + basic_tfhelper, + basic_jhelper, + basic_manifest, + test_model, + step_context, ): + basic_jhelper.get_model_uuid.return_value = "model-uuid" + result = disable_step.run(step_context) + + basic_jhelper.get_model_uuid.assert_called_once_with(test_model) basic_tfhelper.update_tfvars_and_apply_tf.assert_called_with( basic_client, basic_manifest, tfvar_config=None, - override_tfvars={"token": ""}, + override_tfvars={"machine_model_uuid": "model-uuid", "token": ""}, reporter=step_context.reporter, ) assert result.result_type == ResultType.COMPLETED - def test_disable_tf_apply_failed(self, disable_step, basic_tfhelper, step_context): - basic_tfhelper.update_tfvars_and_apply_tf.side_effect = TerraformException( - "apply failed..." - ) + @pytest.mark.parametrize( + "error", + [ + TerraformException("apply failed..."), + TerraformStateLockedException("state locked..."), + ], + ) + def test_disable_tf_apply_failed( + self, disable_step, basic_tfhelper, step_context, error + ): + basic_tfhelper.update_tfvars_and_apply_tf.side_effect = error result = disable_step.run(step_context) basic_tfhelper.update_tfvars_and_apply_tf.assert_called_once() assert result.result_type == ResultType.FAILED - assert result.message == "apply failed..." + assert result.message == str(error) + + @pytest.mark.parametrize( + "error", + [ModelNotFoundException("Model 'test-model' not found"), JujuException("boom")], + ) + def test_disable_model_lookup_failed( + self, disable_step, basic_jhelper, basic_tfhelper, step_context, error + ): + basic_jhelper.get_model_uuid.side_effect = error + + result = disable_step.run(step_context) + + basic_tfhelper.update_tfvars_and_apply_tf.assert_not_called() + assert result.result_type == ResultType.FAILED + assert result.message == str(error) class TestTerraformPlan: @@ -304,3 +357,69 @@ def test_no_user_manifest_stores_nothing(self, run_plan): TerraformInitStep, EnableUbuntuProApplicationStep, ] + + +def _required_tf_variables() -> set[str]: + """Return the deploy-pro variables that have no default value. + + A regex scan that only handles flat variable blocks, as deploy-pro has + today. Nested blocks (e.g. validation {}), a "}" inside a string or a + heredoc line starting with "default =" would confuse it. + """ + variables_tf = ( + Path(pro_feature.__file__).parent / "etc" / "deploy-pro" / "variables.tf" + ) + blocks = re.findall( + r'variable\s+"([^"]+)"\s*\{([^}]*)\}', variables_tf.read_text(), re.DOTALL + ) + assert blocks, "no variables found in variables.tf" + return { + name for name, body in blocks if not re.search(r"^\s*default\s*=", body, re.M) + } + + +class TestRequiredTerraformVariables: + """The tfvars are not persisted, so each step must pass every required var.""" + + def _override_tfvars(self, basic_tfhelper) -> set[str]: + call = basic_tfhelper.update_tfvars_and_apply_tf.call_args + return set(call.kwargs["override_tfvars"]) + + def test_required_variables_detected(self): + assert "machine_model_uuid" in _required_tf_variables() + + def test_enable_passes_required_variables( + self, + basic_client, + basic_tfhelper, + basic_jhelper, + basic_manifest, + test_model, + test_token, + step_context, + ): + EnableUbuntuProApplicationStep( + basic_client, + basic_tfhelper, + basic_jhelper, + basic_manifest, + test_token, + test_model, + ).run(step_context) + + assert _required_tf_variables() <= self._override_tfvars(basic_tfhelper) + + def test_disable_passes_required_variables( + self, + basic_client, + basic_tfhelper, + basic_jhelper, + basic_manifest, + test_model, + step_context, + ): + DisableUbuntuProApplicationStep( + basic_client, basic_tfhelper, basic_jhelper, basic_manifest, test_model + ).run(step_context) + + assert _required_tf_variables() <= self._override_tfvars(basic_tfhelper)