generated from canonical/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow multiple apps to write safely to etc/environment (#95)
- Loading branch information
1 parent
42fa693
commit bb494cc
Showing
4 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2023 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
from unittest.mock import patch | ||
|
||
from src.utils import map_env | ||
|
||
from utils import get_env, update_env | ||
|
||
|
||
def test_map_env_populated(): | ||
example_env = [ | ||
"KAFKA_OPTS=orcs -Djava=wargs -Dkafka=goblins", | ||
"SERVER_JVMFLAGS=dwarves -Djava=elves -Dzookeeper=men", | ||
] | ||
env = map_env(env=example_env) | ||
|
||
assert len(env) == 2 | ||
assert sorted(env.keys()) == sorted(["KAFKA_OPTS", "SERVER_JVMFLAGS"]) | ||
|
||
for value in env.values(): | ||
assert type(value) == str | ||
# checks handles multiple equals signs in value | ||
assert len(value.split()) == 3 | ||
|
||
|
||
def test_get_env_empty(): | ||
with patch("utils.safe_get_file", return_value=[]): | ||
assert not get_env() | ||
assert get_env() == {} | ||
|
||
|
||
def test_update_env(): | ||
example_get_env = { | ||
"KAFKA_OPTS": "orcs -Djava=wargs -Dkafka=goblins", | ||
"SERVER_JVMFLAGS": "dwarves -Djava=elves -Dzookeeper=men", | ||
} | ||
example_update_env = { | ||
"SERVER_JVMFLAGS": "gimli -Djava=legolas -Dzookeeper=aragorn", | ||
} | ||
|
||
with ( | ||
patch("utils.get_env", return_value=example_get_env), | ||
patch("utils.safe_write_to_file") as safe_write, | ||
): | ||
update_env(env=example_update_env) | ||
|
||
assert all( | ||
updated in safe_write.call_args.kwargs["content"] | ||
for updated in ["gimli", "legolas", "aragorn"] | ||
) | ||
assert "KAFKA_OPTS" in safe_write.call_args.kwargs["content"] | ||
assert safe_write.call_args.kwargs["path"] == "/etc/environment" | ||
assert safe_write.call_args.kwargs["mode"] == "w" |