mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 20:01:25 -07:00
Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9838)
Make set_module_args() a context manager, and remove copies of set_module_args(). Prepares for Data Tagging.
This commit is contained in:
parent
402f725424
commit
a1781d09dd
84 changed files with 4043 additions and 4302 deletions
|
@ -6,16 +6,14 @@ from __future__ import absolute_import, division, print_function
|
|||
__metaclass__ = type
|
||||
|
||||
import jenkins
|
||||
import json
|
||||
|
||||
from xml.etree import ElementTree as et
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils.common.text.converters import to_bytes
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch, call
|
||||
from ansible_collections.community.general.plugins.modules import jenkins_node
|
||||
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, set_module_args, exit_json, fail_json
|
||||
from pytest import fixture, raises, mark, param
|
||||
|
||||
|
||||
|
@ -62,35 +60,6 @@ def assert_xml_equal(x, y):
|
|||
raise AssertionError("{} != {}".format(x, y))
|
||||
|
||||
|
||||
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):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self.value[item]
|
||||
|
||||
|
||||
def exit_json(*args, **kwargs):
|
||||
if 'changed' not in kwargs:
|
||||
kwargs['changed'] = False
|
||||
raise AnsibleExitJson(kwargs)
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def fail_json(*args, **kwargs):
|
||||
kwargs['failed'] = True
|
||||
raise AnsibleFailJson(kwargs)
|
||||
|
||||
|
||||
@fixture(autouse=True)
|
||||
def module():
|
||||
with patch.multiple(
|
||||
|
@ -120,16 +89,16 @@ def get_instance(instance):
|
|||
def test_get_jenkins_instance_with_user_and_token(instance):
|
||||
instance.node_exists.return_value = False
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
"url": "https://localhost:8080",
|
||||
"user": "admin",
|
||||
"token": "password",
|
||||
})
|
||||
}):
|
||||
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
jenkins_node.main()
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.call_args == call("https://localhost:8080", "admin", "password")
|
||||
|
||||
|
@ -137,15 +106,15 @@ def test_get_jenkins_instance_with_user_and_token(instance):
|
|||
def test_get_jenkins_instance_with_user(instance):
|
||||
instance.node_exists.return_value = False
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
"url": "https://localhost:8080",
|
||||
"user": "admin",
|
||||
})
|
||||
}):
|
||||
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
jenkins_node.main()
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.call_args == call("https://localhost:8080", "admin")
|
||||
|
||||
|
@ -153,14 +122,14 @@ def test_get_jenkins_instance_with_user(instance):
|
|||
def test_get_jenkins_instance_with_no_credential(instance):
|
||||
instance.node_exists.return_value = False
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
"url": "https://localhost:8080",
|
||||
})
|
||||
}):
|
||||
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
jenkins_node.main()
|
||||
with pytest.raises(AnsibleExitJson):
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.call_args == call("https://localhost:8080")
|
||||
|
||||
|
@ -173,18 +142,18 @@ def test_state_present_when_absent(get_instance, instance, state):
|
|||
instance.node_exists.return_value = False
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": state,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.create_node.call_args == call("my-node", launcher=jenkins.LAUNCHER_SSH)
|
||||
|
||||
assert result.value["created"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["created"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
@mark.parametrize(["state"], [param(state) for state in PRESENT_STATES])
|
||||
|
@ -192,19 +161,19 @@ def test_state_present_when_absent_check_mode(get_instance, instance, state):
|
|||
instance.node_exists.return_value = False
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": state,
|
||||
"_ansible_check_mode": True,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.create_node.called
|
||||
|
||||
assert result.value["created"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["created"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
@mark.parametrize(["state"], [param(state) for state in PRESENT_STATES])
|
||||
|
@ -215,18 +184,18 @@ def test_state_present_when_absent_redirect_auth_error_handled(
|
|||
instance.get_node_config.return_value = "<slave />"
|
||||
instance.create_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": state,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.create_node.call_args == call("my-node", launcher=jenkins.LAUNCHER_SSH)
|
||||
|
||||
assert result.value["created"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["created"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
@mark.parametrize(["state"], [param(state) for state in PRESENT_STATES])
|
||||
|
@ -235,72 +204,72 @@ def test_state_present_when_absent_other_error_raised(get_instance, instance, st
|
|||
instance.get_node_config.return_value = "<slave />"
|
||||
instance.create_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": state,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.create_node.call_args == call("my-node", launcher=jenkins.LAUNCHER_SSH)
|
||||
|
||||
assert "Create node failed" in str(result.value)
|
||||
assert "Create node failed" in str(result.value.args[0])
|
||||
|
||||
|
||||
def test_state_present_when_present(get_instance, instance):
|
||||
instance.node_exists.return_value = True
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.create_node.called
|
||||
|
||||
assert result.value["created"] is False
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["created"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
||||
|
||||
def test_state_absent_when_present(get_instance, instance):
|
||||
instance.node_exists.return_value = True
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.delete_node.call_args == call("my-node")
|
||||
|
||||
assert result.value["deleted"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["deleted"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_absent_when_present_check_mode(get_instance, instance):
|
||||
instance.node_exists.return_value = True
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
"_ansible_check_mode": True,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.delete_node.called
|
||||
|
||||
assert result.value["deleted"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["deleted"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_absent_when_present_redirect_auth_error_handled(get_instance, instance):
|
||||
|
@ -308,18 +277,18 @@ def test_state_absent_when_present_redirect_auth_error_handled(get_instance, ins
|
|||
instance.get_node_config.return_value = "<slave />"
|
||||
instance.delete_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.delete_node.call_args == call("my-node")
|
||||
|
||||
assert result.value["deleted"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["deleted"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_absent_when_present_other_error_raised(get_instance, instance):
|
||||
|
@ -327,35 +296,35 @@ def test_state_absent_when_present_other_error_raised(get_instance, instance):
|
|||
instance.get_node_config.return_value = "<slave />"
|
||||
instance.delete_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.delete_node.call_args == call("my-node")
|
||||
|
||||
assert "Delete node failed" in str(result.value)
|
||||
assert "Delete node failed" in str(result.value.args[0])
|
||||
|
||||
|
||||
def test_state_absent_when_absent(get_instance, instance):
|
||||
instance.node_exists.return_value = False
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "absent",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.delete_node.called
|
||||
|
||||
assert result.value["deleted"] is False
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["deleted"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
||||
|
||||
def test_state_enabled_when_offline(get_instance, instance):
|
||||
|
@ -363,18 +332,18 @@ def test_state_enabled_when_offline(get_instance, instance):
|
|||
instance.get_node_config.return_value = "<slave />"
|
||||
instance.get_node_info.return_value = {"offline": True}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "enabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.enable_node.call_args == call("my-node")
|
||||
|
||||
assert result.value["enabled"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["enabled"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_enabled_when_offline_check_mode(get_instance, instance):
|
||||
|
@ -382,19 +351,19 @@ def test_state_enabled_when_offline_check_mode(get_instance, instance):
|
|||
instance.get_node_config.return_value = "<slave />"
|
||||
instance.get_node_info.return_value = {"offline": True}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "enabled",
|
||||
"_ansible_check_mode": True,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.enable_node.called
|
||||
|
||||
assert result.value["enabled"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["enabled"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_enabled_when_offline_redirect_auth_error_handled(get_instance, instance):
|
||||
|
@ -403,18 +372,18 @@ def test_state_enabled_when_offline_redirect_auth_error_handled(get_instance, in
|
|||
instance.get_node_info.side_effect = [{"offline": True}, {"offline": False}]
|
||||
instance.enable_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "enabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.enable_node.call_args == call("my-node")
|
||||
|
||||
assert result.value["enabled"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["enabled"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_enabled_when_offline_other_error_raised(get_instance, instance):
|
||||
|
@ -423,17 +392,17 @@ def test_state_enabled_when_offline_other_error_raised(get_instance, instance):
|
|||
instance.get_node_info.side_effect = [{"offline": True}, {"offline": True}]
|
||||
instance.enable_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "enabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.enable_node.call_args == call("my-node")
|
||||
|
||||
assert "Enable node failed" in str(result.value)
|
||||
assert "Enable node failed" in str(result.value.args[0])
|
||||
|
||||
|
||||
def test_state_enabled_when_not_offline(get_instance, instance):
|
||||
|
@ -441,18 +410,18 @@ def test_state_enabled_when_not_offline(get_instance, instance):
|
|||
instance.get_node_config.return_value = "<slave />"
|
||||
instance.get_node_info.return_value = {"offline": False}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "enabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.enable_node.called
|
||||
|
||||
assert result.value["enabled"] is False
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["enabled"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
||||
|
||||
def test_state_disabled_when_not_offline(get_instance, instance):
|
||||
|
@ -463,18 +432,18 @@ def test_state_disabled_when_not_offline(get_instance, instance):
|
|||
"offlineCauseReason": "",
|
||||
}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.disable_node.call_args == call("my-node", "")
|
||||
|
||||
assert result.value["disabled"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["disabled"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_disabled_when_not_offline_redirect_auth_error_handled(
|
||||
|
@ -494,18 +463,18 @@ def test_state_disabled_when_not_offline_redirect_auth_error_handled(
|
|||
]
|
||||
instance.disable_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.disable_node.call_args == call("my-node", "")
|
||||
|
||||
assert result.value["disabled"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["disabled"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_disabled_when_not_offline_other_error_raised(get_instance, instance):
|
||||
|
@ -523,17 +492,17 @@ def test_state_disabled_when_not_offline_other_error_raised(get_instance, instan
|
|||
]
|
||||
instance.disable_node.side_effect = jenkins.JenkinsException
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleFailJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.disable_node.call_args == call("my-node", "")
|
||||
|
||||
assert "Disable node failed" in str(result.value)
|
||||
assert "Disable node failed" in str(result.value.args[0])
|
||||
|
||||
|
||||
def test_state_disabled_when_not_offline_check_mode(get_instance, instance):
|
||||
|
@ -544,19 +513,19 @@ def test_state_disabled_when_not_offline_check_mode(get_instance, instance):
|
|||
"offlineCauseReason": "",
|
||||
}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
"_ansible_check_mode": True,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.disable_node.called
|
||||
|
||||
assert result.value["disabled"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["disabled"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_state_disabled_when_offline(get_instance, instance):
|
||||
|
@ -567,32 +536,32 @@ def test_state_disabled_when_offline(get_instance, instance):
|
|||
"offlineCauseReason": "",
|
||||
}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.disable_node.called
|
||||
|
||||
assert result.value["disabled"] is False
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["disabled"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
||||
|
||||
def test_configure_num_executors_when_not_configured(get_instance, instance):
|
||||
instance.node_exists.return_value = True
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
"num_executors": 3,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.reconfig_node.call_args[0][0] == "my-node"
|
||||
assert_xml_equal(instance.reconfig_node.call_args[0][1], """
|
||||
|
@ -601,8 +570,8 @@ def test_configure_num_executors_when_not_configured(get_instance, instance):
|
|||
</slave>
|
||||
""")
|
||||
|
||||
assert result.value["configured"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["configured"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_configure_num_executors_when_not_equal(get_instance, instance):
|
||||
|
@ -613,14 +582,14 @@ def test_configure_num_executors_when_not_equal(get_instance, instance):
|
|||
</slave>
|
||||
"""
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
"num_executors": 2,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert_xml_equal(instance.reconfig_node.call_args[0][1], """
|
||||
<slave>
|
||||
|
@ -628,8 +597,8 @@ def test_configure_num_executors_when_not_equal(get_instance, instance):
|
|||
</slave>
|
||||
""")
|
||||
|
||||
assert result.value["configured"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["configured"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_configure_num_executors_when_equal(get_instance, instance):
|
||||
|
@ -640,26 +609,26 @@ def test_configure_num_executors_when_equal(get_instance, instance):
|
|||
</slave>
|
||||
"""
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
"num_executors": 2,
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.reconfig_node.called
|
||||
|
||||
assert result.value["configured"] is False
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["configured"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
||||
|
||||
def test_configure_labels_when_not_configured(get_instance, instance):
|
||||
instance.node_exists.return_value = True
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
"labels": [
|
||||
|
@ -667,10 +636,10 @@ def test_configure_labels_when_not_configured(get_instance, instance):
|
|||
"b",
|
||||
"c",
|
||||
],
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.reconfig_node.call_args[0][0] == "my-node"
|
||||
assert_xml_equal(instance.reconfig_node.call_args[0][1], """
|
||||
|
@ -679,8 +648,8 @@ def test_configure_labels_when_not_configured(get_instance, instance):
|
|||
</slave>
|
||||
""")
|
||||
|
||||
assert result.value["configured"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["configured"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_configure_labels_when_not_equal(get_instance, instance):
|
||||
|
@ -691,7 +660,7 @@ def test_configure_labels_when_not_equal(get_instance, instance):
|
|||
</slave>
|
||||
"""
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
"labels": [
|
||||
|
@ -699,10 +668,10 @@ def test_configure_labels_when_not_equal(get_instance, instance):
|
|||
"z",
|
||||
"c",
|
||||
],
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.reconfig_node.call_args[0][0] == "my-node"
|
||||
assert_xml_equal(instance.reconfig_node.call_args[0][1], """
|
||||
|
@ -711,8 +680,8 @@ def test_configure_labels_when_not_equal(get_instance, instance):
|
|||
</slave>
|
||||
""")
|
||||
|
||||
assert result.value["configured"] is True
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["configured"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
def test_configure_labels_when_equal(get_instance, instance):
|
||||
|
@ -723,7 +692,7 @@ def test_configure_labels_when_equal(get_instance, instance):
|
|||
</slave>
|
||||
"""
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
"labels": [
|
||||
|
@ -731,45 +700,45 @@ def test_configure_labels_when_equal(get_instance, instance):
|
|||
"b",
|
||||
"c",
|
||||
],
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.reconfig_node.called
|
||||
|
||||
assert result.value["configured"] is False
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["configured"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
||||
|
||||
def test_configure_labels_fail_when_contains_space(get_instance, instance):
|
||||
instance.node_exists.return_value = True
|
||||
instance.get_node_config.return_value = "<slave />"
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "present",
|
||||
"labels": [
|
||||
"a error",
|
||||
],
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleFailJson):
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleFailJson):
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.reconfig_node.called
|
||||
|
||||
|
||||
@mark.parametrize(["state"], [param(state) for state in ["enabled", "present", "absent"]])
|
||||
def test_raises_error_if_offline_message_when_state_not_disabled(get_instance, instance, state):
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": state,
|
||||
"offline_message": "This is a message...",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleFailJson):
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleFailJson):
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.disable_node.called
|
||||
|
||||
|
@ -782,18 +751,18 @@ def test_set_offline_message_when_equal(get_instance, instance):
|
|||
"offlineCauseReason": "This is an old message...",
|
||||
}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
"offline_message": "This is an old message...",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.disable_node.called
|
||||
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
||||
|
||||
def test_set_offline_message_when_not_equal_not_offline(get_instance, instance):
|
||||
|
@ -804,18 +773,18 @@ def test_set_offline_message_when_not_equal_not_offline(get_instance, instance):
|
|||
"offlineCauseReason": "This is an old message...",
|
||||
}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
"offline_message": "This is a new message...",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert instance.disable_node.call_args == call("my-node", "This is a new message...")
|
||||
|
||||
assert result.value["changed"] is True
|
||||
assert result.value.args[0]["changed"] is True
|
||||
|
||||
|
||||
# Not calling disable_node when already offline seems like a sensible thing to do.
|
||||
|
@ -829,15 +798,15 @@ def test_set_offline_message_when_not_equal_offline(get_instance, instance):
|
|||
"offlineCauseReason": "This is an old message...",
|
||||
}
|
||||
|
||||
set_module_args({
|
||||
with set_module_args({
|
||||
"name": "my-node",
|
||||
"state": "disabled",
|
||||
"offline_message": "This is a new message...",
|
||||
})
|
||||
}):
|
||||
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
with raises(AnsibleExitJson) as result:
|
||||
jenkins_node.main()
|
||||
|
||||
assert not instance.disable_node.called
|
||||
|
||||
assert result.value["changed"] is False
|
||||
assert result.value.args[0]["changed"] is False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue