diff --git a/merlin/examples/generator.py b/merlin/examples/generator.py index c45cfb9c..725448be 100644 --- a/merlin/examples/generator.py +++ b/merlin/examples/generator.py @@ -146,5 +146,4 @@ def setup_example(name, outdir): LOG.info(f"Copying example '{name}' to {outdir}") write_example(src_path, outdir) - print(f"example: {example}") return example diff --git a/merlin/server/server_config.py b/merlin/server/server_config.py index f4d5d517..b0b91f89 100644 --- a/merlin/server/server_config.py +++ b/merlin/server/server_config.py @@ -92,8 +92,8 @@ def generate_password(length, pass_command: str = None) -> str: :return:: string value with given length """ if pass_command: - process = subprocess.run(pass_command.split(), shell=True, stdout=subprocess.PIPE) - return process.stdout + process = subprocess.run(pass_command, shell=True, capture_output=True, text=True) + return process.stdout.strip() characters = list(string.ascii_letters + string.digits + "!@#$%^&*()") diff --git a/tests/unit/server/test_server_config.py b/tests/unit/server/test_server_config.py new file mode 100644 index 00000000..058e77fc --- /dev/null +++ b/tests/unit/server/test_server_config.py @@ -0,0 +1,43 @@ +""" +Tests for the `server_config.py` module. +""" + +import string + +from merlin.server.server_config import ( + PASSWORD_LENGTH, + check_process_file_format, + config_merlin_server, + create_server_config, + dump_process_file, + generate_password, + get_server_status, + parse_redis_output, + pull_process_file, + pull_server_config, + pull_server_image, +) + + +def test_generate_password_no_pass_command(): + """ + Test the `generate_password` function with no password command. + This should generate a password of 256 (PASSWORD_LENGTH) random ASCII characters. + """ + generated_password = generate_password(PASSWORD_LENGTH) + assert len(generated_password) == PASSWORD_LENGTH + valid_ascii_chars = string.ascii_letters + string.digits + "!@#$%^&*()" + for ch in generated_password: + assert ch in valid_ascii_chars + + +def test_generate_password_with_pass_command(): + """ + Test the `generate_password` function with no password command. + This should generate a password of 256 (PASSWORD_LENGTH) random ASCII characters. + """ + test_pass = "test-password" + generated_password = generate_password(0, pass_command=f"echo {test_pass}") + assert generated_password == test_pass + +