Skip to content

Commit f264198

Browse files
committed
[NRL-863] Fixup unit tests for activate_stack.py. Add more validation around environment config
1 parent 3319040 commit f264198

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

scripts/activate_stack.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@
1818
VALID_LOCK_STATES = [STATE_LOCKED, STATE_OPEN]
1919

2020

21+
def _parse_env_config(raw_config: str) -> dict:
22+
env_config = json.loads(raw_config)
23+
if not all(
24+
key in env_config
25+
for key in [
26+
CONFIG_LOCK_STATE,
27+
CONFIG_INACTIVE_STACK,
28+
CONFIG_INACTIVE_VERSION,
29+
CONFIG_ACTIVE_STACK,
30+
CONFIG_ACTIVE_VERSION,
31+
CONFIG_DOMAIN_NAME,
32+
]
33+
):
34+
raise ValueError(
35+
f"Environment config must contain keys: {CONFIG_LOCK_STATE}, {CONFIG_INACTIVE_STACK}, {CONFIG_INACTIVE_VERSION}, {CONFIG_ACTIVE_STACK}, {CONFIG_ACTIVE_VERSION}, {CONFIG_DOMAIN_NAME}"
36+
)
37+
38+
if env_config[CONFIG_LOCK_STATE] not in VALID_LOCK_STATES:
39+
raise ValueError(
40+
f"Invalid lock state: {env_config[CONFIG_LOCK_STATE]}. Must be one of: {VALID_LOCK_STATES}"
41+
)
42+
43+
return env_config
44+
45+
2146
def _swap_config(config: dict[str, str], key1: str, key2: str):
2247
config[key1], config[key2] = config[key2], config[key1]
2348

@@ -96,7 +121,7 @@ def activate_stack(stack_name: str, env: str, session: any):
96121
parameters_key = f"nhsd-nrlf--{env}--env-config"
97122
response = sm.get_secret_value(SecretId=parameters_key)
98123

99-
environment_config = json.loads(response["SecretString"])
124+
environment_config = _parse_env_config(response["SecretString"])
100125
print(f"Got environment config for {env}: {environment_config}")
101126

102127
current_active_stack = environment_config[CONFIG_ACTIVE_STACK]

scripts/tests/test_activate_stack.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from scripts.activate_stack import activate_stack
6+
from scripts.activate_stack import _parse_env_config, activate_stack
77

88

99
@pytest.fixture
@@ -68,7 +68,9 @@ def _create_mock_env_config():
6868
return {
6969
"lock-state": "open",
7070
"active-stack": "test-stack-1",
71+
"active-version": "v1",
7172
"inactive-stack": "test-stack-2",
73+
"inactive-version": "v2",
7274
"domain-name": "test.domain.name",
7375
}
7476

@@ -88,16 +90,17 @@ def test_happy_path(mock_boto_session, mock_secretsmanager):
8890
expected_env_config = {
8991
**mock_env_config,
9092
"active-stack": "test-stack-2",
93+
"active-version": "v2",
9194
"inactive-stack": "test-stack-1",
95+
"inactive-version": "v1",
9296
}
9397
assert result["SecretString"] == json.dumps(expected_env_config)
9498

9599

96100
def test_lock_state_not_open(mock_boto_session, mock_secretsmanager):
97101
inital_env_config = {
102+
**_create_mock_env_config(),
98103
"lock-state": "locked",
99-
"inactive-stack": "test-stack-1",
100-
"active-stack": "test-stack-2",
101104
}
102105
mock_secretsmanager.create_secret(
103106
Name="nhsd-nrlf--locked--env-config", SecretString=json.dumps(inital_env_config)
@@ -115,7 +118,7 @@ def test_lock_state_not_open(mock_boto_session, mock_secretsmanager):
115118

116119
def test_stack_already_active(mock_boto_session, mock_secretsmanager):
117120
intial_env_config = {
118-
"lock-state": "open",
121+
**_create_mock_env_config(),
119122
"inactive-stack": "test-stack-1",
120123
"active-stack": "test-stack-2",
121124
}
@@ -132,3 +135,31 @@ def test_stack_already_active(mock_boto_session, mock_secretsmanager):
132135
SecretId="nhsd-nrlf--already-active--env-config" # pragma: allowlist secret
133136
)
134137
assert result["SecretString"] == json.dumps(intial_env_config)
138+
139+
140+
def test_parse_env_config_valid():
141+
valid_config = json.dumps(_create_mock_env_config())
142+
result = _parse_env_config(valid_config)
143+
assert result == _create_mock_env_config()
144+
145+
146+
def test_parse_env_config_empty():
147+
with pytest.raises(ValueError):
148+
_parse_env_config("")
149+
150+
151+
def test_parse_env_config_invalid_json():
152+
with pytest.raises(ValueError):
153+
_parse_env_config("this is not JSON!")
154+
155+
156+
def test_parse_env_config_missing_params():
157+
with pytest.raises(ValueError):
158+
_parse_env_config(json.dumps({"lock-state": "open"}))
159+
160+
161+
def test_parse_env_config_invalid_lock_state():
162+
with pytest.raises(ValueError):
163+
_parse_env_config(
164+
json.dumps({**_create_mock_env_config(), "lock-state": "invalid"})
165+
)

0 commit comments

Comments
 (0)