Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions sunbeam-python/sunbeam/features/pro/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -86,19 +86,19 @@ 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,
tfvar_config=None,
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
Expand Down Expand Up @@ -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."""
Expand All @@ -158,16 +162,21 @@ 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": "",
Comment thread
vsz-work marked this conversation as resolved.
Resolved
}
self.tfhelper.update_tfvars_and_apply_tf(
self.client,
self.manifest,
tfvar_config=None,
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)
Expand Down Expand Up @@ -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)
Comment thread
vsz-work marked this conversation as resolved.
Resolved
plan = [
TerraformInitStep(tfhelper),
DisableUbuntuProApplicationStep(
deployment.get_client(),
tfhelper,
jhelper,
self.manifest,
deployment.openstack_machines_model,
),
]

Expand Down
137 changes: 128 additions & 9 deletions sunbeam-python/tests/unit/sunbeam/features/test_pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Comment thread
vsz-work marked this conversation as resolved.
Resolved
r'variable\s+"([^"]+)"\s*\{([^}]*)\}', variables_tf.read_text(), re.DOTALL
Comment thread
vsz-work marked this conversation as resolved.
Resolved
)
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)
Loading