Skip to content

Commit

Permalink
feat(reana_dev): prevent Helm values string wrapping by PyYAML (reana…
Browse files Browse the repository at this point in the history
  • Loading branch information
jlemesh committed Oct 14, 2024
1 parent d054739 commit 4ea8810
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 3 deletions.
9 changes: 6 additions & 3 deletions reana/reana_dev/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def job_mounts_to_config(job_mounts):

values_dict = {}
with open(os.path.join(get_srcdir("reana"), values)) as f:
values_dict = yaml.safe_load(f.read())
values_dict = yaml.safe_load(f.read()) or {}

job_mount_config = job_mounts_to_config(job_mounts)
if job_mount_config:
Expand All @@ -365,9 +365,12 @@ def job_mounts_to_config(job_mounts):
find_standard_component_name(c) for c in exclude_components.split(",")
]
if "reana-ui" in standard_named_exclude_components:
values_dict["components"]["reana_ui"]["enabled"] = False
values_dict.setdefault("components", {}).setdefault("reana_ui", {})[
"enabled"
] = False

values_yaml = yaml.dump(values_dict) if values_dict else ""
# set arbitrary big value for `width` to prevent PyYAML from wrapping long lines
values_yaml = yaml.dump(values_dict, width=100000) if values_dict else ""
helm_install = f"cat <<EOF | helm install {instance_name} helm/reana -n {namespace} --create-namespace --wait -f -\n{values_yaml}\nEOF"

cmds = []
Expand Down
174 changes: 174 additions & 0 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
#
# This file is part of REANA
# Copyright (C) 2024 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""REANA CLI cluster command tests."""

from __future__ import absolute_import, print_function

import pytest

from click.testing import CliRunner
from mock import patch, mock_open
from unittest.mock import call


helm_command = """cat <<EOF | helm install reana helm/reana -n default --create-namespace --wait -f -
components:
reana_ui:
enabled: false
debug:
enabled: true
EOF"""


@pytest.mark.parametrize(
"options, run_command_calls, run_command_side_effects, exit_code, open_calls, open_mock_read_value",
[
(
[
"--admin-email",
"[email protected]",
"--admin-password",
"admin",
"--values",
"alternative-values-dev.yaml",
"--mode",
"debug",
"--exclude-components",
"reana-ui,reana-workflow-controller",
],
[
call("reana-dev python-install-eggs", "reana"),
call("reana-dev git-submodule --update", "reana"),
call("helm dep update helm/reana", "reana"),
call(helm_command, "reana"),
call(
"kubectl config set-context --current --namespace=default", "reana"
),
call(
"/code/src/reana/scripts/create-admin-user.sh default reana [email protected] admin",
"reana",
),
],
[None] * 6,
0,
[call("/code/src/reana/alternative-values-dev.yaml")],
"",
),
(
[
"--admin-email",
"[email protected]",
"--admin-password",
"admin",
],
[
call("helm dep update helm/reana", "reana"),
call(
"cat <<EOF | helm install reana helm/reana -n default --create-namespace --wait -f -\ncomponents:\n reana_workflow_controller:\n environment:\n REANA_OPENSEARCH_ENABLED: true\ndebug:\n enabled: true\n\nEOF",
"reana",
),
call(
"kubectl config set-context --current --namespace=default", "reana"
),
call(
"/code/src/reana/scripts/create-admin-user.sh default reana [email protected] admin",
"reana",
),
],
[None] * 4,
0,
[call("/code/src/reana/helm/configurations/values-dev.yaml")],
"debug:\n enabled: true\ncomponents:\n reana_workflow_controller:\n environment:\n REANA_OPENSEARCH_ENABLED: true\n",
),
(
[
"--admin-email",
"[email protected]",
"--admin-password",
"admin",
"--mode",
"releasehelm",
],
[
call("helm dep update helm/reana", "reana"),
call(
"cat <<EOF | helm install reana helm/reana -n default --create-namespace --wait -f -\n\nEOF",
"reana",
),
call(
"kubectl config set-context --current --namespace=default", "reana"
),
call(
"/code/src/reana/scripts/create-admin-user.sh default reana [email protected] admin",
"reana",
),
],
[None] * 4,
0,
[call("/code/src/reana/helm/reana/values.yaml")],
"",
),
(
[
"--admin-email",
"[email protected]",
"--admin-password",
"admin",
],
[
call("helm dep update helm/reana", "reana"),
call(
"cat <<EOF | helm install reana helm/reana -n default --create-namespace --wait -f -\n\nEOF",
"reana",
),
call(
"kubectl config set-context --current --namespace=default", "reana"
),
call(
"/code/src/reana/scripts/create-admin-user.sh default reana [email protected] admin",
"reana",
),
],
[None, None, None, ValueError()],
1,
[call("/code/src/reana/helm/configurations/values-dev.yaml")],
"",
),
],
)
@patch("reana.reana_dev.cluster.get_srcdir")
@patch("reana.reana_dev.cluster.run_command")
@patch("builtins.open", new_callable=mock_open)
def test_cluster_deploy(
open_mock,
run_command_mock,
get_srcdir_mock,
options,
run_command_calls,
run_command_side_effects,
exit_code,
open_calls,
open_mock_read_value,
):
"""Test cluster-deploy command."""
from reana.reana_dev.cluster import cluster_deploy

run_command_count = len(run_command_calls)
open_mock.return_value.read.return_value = open_mock_read_value

run_command_mock.side_effect = run_command_side_effects
get_srcdir_mock.return_value = "/code/src/reana"
runner = CliRunner()
result = runner.invoke(cluster_deploy, options)
assert run_command_mock.call_count == run_command_count
assert run_command_mock.call_args_list == run_command_calls
assert result.exit_code == exit_code
assert open_mock.call_count == len(open_calls)
assert open_mock.call_args_list == open_calls

0 comments on commit 4ea8810

Please sign in to comment.