mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-03-26 21:33:02 +00:00
Add helm dependency update (#208)
Add helm dependency update SUMMARY Execute the helm dependency update under the hood when found dependencies block in Chart.yaml file. Support the execution of: Standalone dependency update by executing: helm dependency update CHART Inline dependency update when specifying the helm chart_repo_url by adding --dependency-update to the helm install command. ISSUE TYPE Feature Pull Request #191 COMPONENT NAME helm, helm_template ADDITIONAL INFORMATION There is a doc generated for history_max option for the helm module. I think that is not generated in the previous PR #164. There is others changes affect the docs/ folder when I run the collection_prep_add_docs -p . command. These changes are added in the last commit 64eab40. I let you decide rather we keep the commit or remove it. The --dependency-update insertion option is tested used a local helm chart repository create via docker. So here are the tasks that test this feature. Maybe if we create a GitHub repository for the helm chart, we can add this test code in the CI pipeline. # Test The update dependency with chart_repo_url - name: "Test chart without dependencies block and chart_repo_url defined" block: - name: "Test chart without dependencies block and chart_repo_url defined" helm: binary_path: "{{ helm_binary }}" name: test chart_ref: "ingress-nginx" chart_repo_url: https://kubernetes.github.io/ingress-nginx chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" create_namespace: yes register: release - assert: that: - "'--dependency-update' not in release.command" - "'upgrade' in release.command" success_msg: "Command does not contains '--dependency-update' options" fail_msg: "Command contains '--dependency-update' options" - name: "Test chart with dependencies block and chart_repo_url defined and replace True" block: - name: "Test chart with dependencies block and chart_repo_url defined and replace True" helm: binary_path: "{{ helm_binary }}" name: test1 chart_ref: "dep_up" chart_repo_url: http://repo:8080/charts chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" create_namespace: yes replace: true register: release - debug: var=release - assert: that: - "'--dependency-update' in release.command" - "'install' in release.command" success_msg: "Command contains '--dependency-update' options with helm install command" fail_msg: "Command not contains '--dependency-update' with helm install command" - name: "Test chart with dependencies block and chart_repo_url defined and replace False fails" block: - name: "Test chart with dependencies block and chart_repo_url defined and replace False fails" helm: binary_path: "{{ helm_binary }}" name: test2 chart_ref: "dep_up" chart_repo_url: http://repo:8080/charts chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" create_namespace: yes replace: false register: release ignore_errors: true - assert: that: - release.failed - release.msg == "'--dependency-update' hasn't been supported yet with 'helm upgrade'. Please use 'helm install' instead by adding 'replace' option" success_msg: "Command build fail when adding '--dependency-update' with the helm upgrade command" Reviewed-by: Mike Graves <mgraves@redhat.com> Reviewed-by: Wissem BEN CHAABANE <benchaaben.wissem@gmail.com> Reviewed-by: Bikouo Aubin <None>
This commit is contained in:
committed by
GitHub
parent
f2f4b66d77
commit
4fa1fb966b
103
tests/unit/modules/test_helm_template_module.py
Normal file
103
tests/unit/modules/test_helm_template_module.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible_collections.kubernetes.core.plugins.modules import helm_template
|
||||
from ansible_collections.kubernetes.core.tests.unit.utils.ansible_module_mock import (
|
||||
AnsibleFailJson,
|
||||
AnsibleExitJson,
|
||||
exit_json,
|
||||
fail_json,
|
||||
get_bin_path,
|
||||
set_module_args,
|
||||
)
|
||||
|
||||
|
||||
class TestDependencyUpdateWithoutChartRepoUrlOption(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(
|
||||
basic.AnsibleModule,
|
||||
exit_json=exit_json,
|
||||
fail_json=fail_json,
|
||||
get_bin_path=get_bin_path,
|
||||
)
|
||||
self.mock_module_helper.start()
|
||||
|
||||
# Stop the patch after test execution
|
||||
# like tearDown but executed also when the setup failed
|
||||
self.addCleanup(self.mock_module_helper.stop)
|
||||
|
||||
def test_module_fail_when_required_args_missing(self):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
set_module_args({})
|
||||
helm_template.main()
|
||||
|
||||
def test_dependency_update_option_not_defined(self):
|
||||
set_module_args({"chart_ref": "/tmp/path"})
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm_template.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm template /tmp/path", environ_update={}
|
||||
)
|
||||
assert result.exception.args[0]["command"] == "/usr/bin/helm template /tmp/path"
|
||||
|
||||
def test_dependency_update_option_false(self):
|
||||
set_module_args(
|
||||
{
|
||||
"chart_ref": "test",
|
||||
"chart_repo_url": "https://charts.com/test",
|
||||
"dependency_update": False,
|
||||
}
|
||||
)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm_template.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm template test --repo=https://charts.com/test",
|
||||
environ_update={},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm template test --repo=https://charts.com/test"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_true(self):
|
||||
set_module_args(
|
||||
{"chart_ref": "https://charts/example.tgz", "dependency_update": True}
|
||||
)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm_template.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm template https://charts/example.tgz --dependency-update",
|
||||
environ_update={},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm template https://charts/example.tgz --dependency-update"
|
||||
)
|
||||
497
tests/unit/modules/test_module_helm.py
Normal file
497
tests/unit/modules/test_module_helm.py
Normal file
@@ -0,0 +1,497 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
|
||||
from unittest.mock import MagicMock, patch, call
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible_collections.kubernetes.core.plugins.modules import helm
|
||||
from ansible_collections.kubernetes.core.tests.unit.utils.ansible_module_mock import (
|
||||
AnsibleFailJson,
|
||||
AnsibleExitJson,
|
||||
exit_json,
|
||||
fail_json,
|
||||
get_bin_path,
|
||||
set_module_args,
|
||||
)
|
||||
|
||||
|
||||
class TestDependencyUpdateWithoutChartRepoUrlOption(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(
|
||||
basic.AnsibleModule,
|
||||
exit_json=exit_json,
|
||||
fail_json=fail_json,
|
||||
get_bin_path=get_bin_path,
|
||||
)
|
||||
self.mock_module_helper.start()
|
||||
|
||||
# Stop the patch after test execution
|
||||
# like tearDown but executed also when the setup failed
|
||||
self.addCleanup(self.mock_module_helper.stop)
|
||||
|
||||
self.chart_info_without_dep = {
|
||||
"apiVersion": "v2",
|
||||
"appVersion": "default",
|
||||
"description": "A chart used in molecule tests",
|
||||
"name": "test-chart",
|
||||
"type": "application",
|
||||
"version": "0.1.0",
|
||||
}
|
||||
|
||||
self.chart_info_with_dep = {
|
||||
"apiVersion": "v2",
|
||||
"appVersion": "default",
|
||||
"description": "A chart used in molecule tests",
|
||||
"name": "test-chart",
|
||||
"type": "application",
|
||||
"version": "0.1.0",
|
||||
"dependencies": [
|
||||
{
|
||||
"name": "test",
|
||||
"version": "0.1.0",
|
||||
"repository": "file://../test-chart",
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
def test_module_fail_when_required_args_missing(self):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
set_module_args({})
|
||||
helm.main()
|
||||
|
||||
def test_dependency_update_option_not_defined(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "/tmp/path",
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
helm.run_dep_update = MagicMock()
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
helm.run_dep_update.assert_not_called()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm upgrade -i --reset-values test /tmp/path",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm upgrade -i --reset-values test /tmp/path"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_false(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "/tmp/path",
|
||||
"dependency_update": False,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
helm.run_dep_update = MagicMock()
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
helm.run_dep_update.assert_not_called()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm upgrade -i --reset-values test /tmp/path",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm upgrade -i --reset-values test /tmp/path"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_true(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "/tmp/path",
|
||||
"dependency_update": True,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_with_dep)
|
||||
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = 0, "configuration updated", ""
|
||||
with patch.object(basic.AnsibleModule, "warn") as mock_warn:
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_warn.assert_not_called()
|
||||
mock_run_command.assert_has_calls(
|
||||
[
|
||||
call(
|
||||
"/usr/bin/helm upgrade -i --reset-values test /tmp/path",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
]
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm upgrade -i --reset-values test /tmp/path"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_true_without_dependencies_block(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "/tmp/path",
|
||||
"dependency_update": True,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with patch.object(basic.AnsibleModule, "warn") as mock_warn:
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_warn.assert_called_once()
|
||||
mock_run_command.assert_has_calls(
|
||||
[
|
||||
call(
|
||||
"/usr/bin/helm upgrade -i --reset-values test /tmp/path",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
]
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm upgrade -i --reset-values test /tmp/path"
|
||||
)
|
||||
|
||||
|
||||
class TestDependencyUpdateWithChartRepoUrlOption(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(
|
||||
basic.AnsibleModule,
|
||||
exit_json=exit_json,
|
||||
fail_json=fail_json,
|
||||
get_bin_path=get_bin_path,
|
||||
)
|
||||
self.mock_module_helper.start()
|
||||
|
||||
# Stop the patch after test execution
|
||||
# like tearDown but executed also when the setup failed
|
||||
self.addCleanup(self.mock_module_helper.stop)
|
||||
|
||||
self.chart_info_without_dep = {
|
||||
"apiVersion": "v2",
|
||||
"appVersion": "default",
|
||||
"description": "A chart used in molecule tests",
|
||||
"name": "test-chart",
|
||||
"type": "application",
|
||||
"version": "0.1.0",
|
||||
}
|
||||
|
||||
self.chart_info_with_dep = {
|
||||
"apiVersion": "v2",
|
||||
"appVersion": "default",
|
||||
"description": "A chart used in molecule tests",
|
||||
"name": "test-chart",
|
||||
"type": "application",
|
||||
"version": "0.1.0",
|
||||
"dependencies": [
|
||||
{
|
||||
"name": "test",
|
||||
"version": "0.1.0",
|
||||
"repository": "file://../test-chart",
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
def test_dependency_update_option_not_defined(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "chart1",
|
||||
"chart_repo_url": "http://repo.example/charts",
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm --repo=http://repo.example/charts upgrade -i --reset-values test chart1",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm --repo=http://repo.example/charts upgrade -i --reset-values test chart1"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_False(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "chart1",
|
||||
"chart_repo_url": "http://repo.example/charts",
|
||||
"dependency_update": False,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm --repo=http://repo.example/charts upgrade -i --reset-values test chart1",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm --repo=http://repo.example/charts upgrade -i --reset-values test chart1"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_True_and_replace_option_disabled(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "chart1",
|
||||
"chart_repo_url": "http://repo.example/charts",
|
||||
"dependency_update": True,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_with_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
helm.main()
|
||||
# mock_run_command.assert_called_once_with('/usr/bin/helm --repo=http://repo.example/charts upgrade -i --reset-values test chart1',
|
||||
# environ_update={'HELM_NAMESPACE': 'test'})
|
||||
assert result.exception.args[0]["msg"] == (
|
||||
"'--dependency-update' hasn't been supported yet with 'helm upgrade'. "
|
||||
"Please use 'helm install' instead by adding 'replace' option"
|
||||
)
|
||||
assert result.exception.args[0]["failed"]
|
||||
|
||||
def test_dependency_update_option_True_and_replace_option_enabled(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "chart1",
|
||||
"chart_repo_url": "http://repo.example/charts",
|
||||
"dependency_update": True,
|
||||
"replace": True,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm --repo=http://repo.example/charts install --dependency-update --replace test chart1",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm --repo=http://repo.example/charts install --dependency-update --replace test chart1"
|
||||
)
|
||||
|
||||
|
||||
class TestDependencyUpdateWithChartRefIsUrl(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(
|
||||
basic.AnsibleModule,
|
||||
exit_json=exit_json,
|
||||
fail_json=fail_json,
|
||||
get_bin_path=get_bin_path,
|
||||
)
|
||||
self.mock_module_helper.start()
|
||||
|
||||
# Stop the patch after test execution
|
||||
# like tearDown but executed also when the setup failed
|
||||
self.addCleanup(self.mock_module_helper.stop)
|
||||
|
||||
self.chart_info_without_dep = {
|
||||
"apiVersion": "v2",
|
||||
"appVersion": "default",
|
||||
"description": "A chart used in molecule tests",
|
||||
"name": "test-chart",
|
||||
"type": "application",
|
||||
"version": "0.1.0",
|
||||
}
|
||||
|
||||
self.chart_info_with_dep = {
|
||||
"apiVersion": "v2",
|
||||
"appVersion": "default",
|
||||
"description": "A chart used in molecule tests",
|
||||
"name": "test-chart",
|
||||
"type": "application",
|
||||
"version": "0.1.0",
|
||||
"dependencies": [
|
||||
{
|
||||
"name": "test",
|
||||
"version": "0.1.0",
|
||||
"repository": "file://../test-chart",
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
def test_dependency_update_option_not_defined(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "http://repo.example/charts/application.tgz",
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm upgrade -i --reset-values test http://repo.example/charts/application.tgz",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm upgrade -i --reset-values test http://repo.example/charts/application.tgz"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_False(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "http://repo.example/charts/application.tgz",
|
||||
"dependency_update": False,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm upgrade -i --reset-values test http://repo.example/charts/application.tgz",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm upgrade -i --reset-values test http://repo.example/charts/application.tgz"
|
||||
)
|
||||
|
||||
def test_dependency_update_option_True_and_replace_option_disabled(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "http://repo.example/charts/application.tgz",
|
||||
"dependency_update": True,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_with_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleFailJson) as result:
|
||||
helm.main()
|
||||
# mock_run_command.assert_called_once_with('/usr/bin/helm --repo=http://repo.example/charts upgrade -i --reset-values test chart1',
|
||||
# environ_update={'HELM_NAMESPACE': 'test'})
|
||||
assert result.exception.args[0]["msg"] == (
|
||||
"'--dependency-update' hasn't been supported yet with 'helm upgrade'. "
|
||||
"Please use 'helm install' instead by adding 'replace' option"
|
||||
)
|
||||
assert result.exception.args[0]["failed"]
|
||||
|
||||
def test_dependency_update_option_True_and_replace_option_enabled(self):
|
||||
set_module_args(
|
||||
{
|
||||
"release_name": "test",
|
||||
"release_namespace": "test",
|
||||
"chart_ref": "http://repo.example/charts/application.tgz",
|
||||
"dependency_update": True,
|
||||
"replace": True,
|
||||
}
|
||||
)
|
||||
helm.get_release_status = MagicMock(return_value=None)
|
||||
helm.fetch_chart_info = MagicMock(return_value=self.chart_info_without_dep)
|
||||
with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
|
||||
mock_run_command.return_value = (
|
||||
0,
|
||||
"configuration updated",
|
||||
"",
|
||||
) # successful execution
|
||||
with self.assertRaises(AnsibleExitJson) as result:
|
||||
helm.main()
|
||||
mock_run_command.assert_called_once_with(
|
||||
"/usr/bin/helm install --dependency-update --replace test http://repo.example/charts/application.tgz",
|
||||
environ_update={"HELM_NAMESPACE": "test"},
|
||||
)
|
||||
assert (
|
||||
result.exception.args[0]["command"]
|
||||
== "/usr/bin/helm install --dependency-update --replace test http://repo.example/charts/application.tgz"
|
||||
)
|
||||
59
tests/unit/utils/ansible_module_mock.py
Normal file
59
tests/unit/utils/ansible_module_mock.py
Normal file
@@ -0,0 +1,59 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# This module maock the AnsibleModule class for more information please visite
|
||||
# https://docs.ansible.com/ansible/latest/dev_guide/testing_units_modules.html#module-argument-processing
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils.common.text.converters import to_bytes
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
"""prepare arguments so that they will be picked up during module creation"""
|
||||
args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args)
|
||||
|
||||
|
||||
class AnsibleExitJson(Exception):
|
||||
"""Exception class to be raised by module.exit_json and caught by the test case"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
"""Exception class to be raised by module.fail_json and caught by the test case"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def exit_json(*args, **kwargs):
|
||||
"""function to patch over exit_json; package return data into an exception"""
|
||||
if "changed" not in kwargs:
|
||||
kwargs["changed"] = False
|
||||
raise AnsibleExitJson(kwargs)
|
||||
|
||||
|
||||
def fail_json(*args, **kwargs):
|
||||
"""function to patch over fail_json; package return data into an exception"""
|
||||
kwargs["failed"] = True
|
||||
raise AnsibleFailJson(kwargs)
|
||||
|
||||
|
||||
def get_bin_path(self, arg, required=False):
|
||||
"""Mock AnsibleModule.get_bin_path"""
|
||||
if arg.endswith("helm"):
|
||||
return "/usr/bin/helm"
|
||||
else:
|
||||
if required:
|
||||
fail_json(msg="%r not found !" % arg)
|
||||
|
||||
|
||||
# def warn(self,msg):
|
||||
# return msg
|
||||
Reference in New Issue
Block a user