Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --autodelete option to resource move command #349

Merged
merged 7 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: add check for ENV in CommandRunner to the testing framework
  • Loading branch information
ondrejmular committed Jul 29, 2021
commit ee86098eba78eed0fbb3127c1ab9e8b5c8598f69
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path
from unittest import TestCase

from pcs_test.tools import fixture
Expand Down Expand Up @@ -67,8 +68,8 @@ def test_failure_pcmk_running(self):
)

def test_sucess_pcmk_not_running(self):
cmd_env = dict(CIB_file=os.path.join(settings.cib_dir, "cib.xml"))
self.config.services.is_running("pacemaker", return_value=False)
# TODO: we do not test environment variables in runner
for node in self.nodes:
self.config.runner.place(
[
Expand All @@ -78,24 +79,27 @@ def test_sucess_pcmk_not_running(self):
f"--xpath=/cib/configuration/nodes/node[@uname='{node}']",
],
name=f"remove_node.{node}",
env=cmd_env,
)
cluster.remove_nodes_from_cib(self.env_assist.get_env(), self.nodes)

def test_failure_pcmk_not_running(self):
err_msg = "an error"
cmd_env = dict(CIB_file=os.path.join(settings.cib_dir, "cib.xml"))
cmd = [settings.cibadmin, "--delete-all", "--force"]
cmd_xpath = "--xpath=/cib/configuration/nodes/node[@uname='{}']"
self.config.services.is_running("pacemaker", return_value=False)
# TODO: we do not test environment variables in runner
self.config.runner.place(
cmd + [cmd_xpath.format(self.nodes[0])],
name="remove_node_success",
env=cmd_env,
)
self.config.runner.place(
cmd + [cmd_xpath.format(self.nodes[1])],
returncode=1,
stderr=err_msg,
name="remove_node_failure",
env=cmd_env,
)
self.env_assist.assert_raise_library_error(
lambda: cluster.remove_nodes_from_cib(
Expand Down
14 changes: 9 additions & 5 deletions pcs_test/tier0/lib/commands/cluster/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ class CibIsMocked(TestCase, AssertInvalidCibMixin):
def setUp(self):
self.env_assist, self.config = get_env_tools(test_case=self)
self.cib_tempfile = "/fake/tmp/file"
self.cmd_env = dict(CIB_file=self.cib_tempfile)
self.config.env.set_cib_data("<cib/>", cib_tempfile=self.cib_tempfile)

def test_success_on_valid_cib(self):
(
self.config.runner.pcmk.verify(cib_tempfile=self.cib_tempfile)
.runner.cib.load()
.runner.pcmk.load_state()
self.config.runner.pcmk.verify(
cib_tempfile=self.cib_tempfile, env=self.cmd_env
)
.runner.cib.load(env=self.cmd_env)
.runner.pcmk.load_state(env=self.cmd_env)
)
verify(self.env_assist.get_env())

Expand All @@ -142,9 +145,10 @@ def test_fail_on_invalid_cib(self):
self.config.runner.pcmk.verify(
stderr="".join(CRM_VERIFY_ERROR_REPORT_LINES),
cib_tempfile=self.cib_tempfile,
env=self.cmd_env,
)
.runner.cib.load()
.runner.pcmk.load_state()
.runner.cib.load(env=self.cmd_env)
.runner.pcmk.load_state(env=self.cmd_env)
)
self.assert_raises_invalid_cib_content(CRM_VERIFY_ERROR_REPORT_LINES[0])

Expand Down
30 changes: 16 additions & 14 deletions pcs_test/tier0/lib/commands/remote_node/test_node_add_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def node_add_guest(


class LocalConfig(EnvConfigMixin):
def load_cib(self):
self.config.runner.cib.load(resources=FIXTURE_RESOURCES)
def load_cib(self, env=None):
self.config.runner.cib.load(resources=FIXTURE_RESOURCES, env=env)

def push_cib(self, wait=False, meta_attributes=FIXTURE_META_ATTRIBUTES):
self.config.env.push_cib(
Expand Down Expand Up @@ -636,6 +636,8 @@ def test_all_node_names_missing(self):

class NotLive(TestCase):
def setUp(self):
self.tmp_file = "/fake/tmp_file"
self.cmd_env = dict(CIB_file=self.tmp_file)
self.env_assist, self.config = get_env_tools(self)
self.config.env.set_known_hosts_dests(KNOWN_HOSTS_DESTS)
cib_xml_man = XmlManipulation.from_file(rc("cib-empty.xml"))
Expand All @@ -649,12 +651,15 @@ def setUp(self):
VIRTUAL_MACHINE_ID
),
)
self.config.env.set_cib_data(str(cib_xml_man))
self.config.env.set_cib_data(
str(cib_xml_man), cib_tempfile=self.tmp_file
)

def test_addr_specified(self):
# Instance of 'Config' has no 'local' member
# pylint: disable=no-member
self.config.local.load_cib().local.push_cib()
self.config.local.load_cib(env=self.cmd_env)
self.config.local.push_cib()
node_add_guest(self.env_assist.get_env())
self.env_assist.assert_reports(fixture_reports_not_live_cib(NODE_NAME))

Expand All @@ -676,9 +681,8 @@ def test_addr_not_specified(self):
/>
</meta_attributes>
"""
self.config.local.load_cib().local.push_cib(
meta_attributes=meta_attributes
)
self.config.local.load_cib(env=self.cmd_env)
self.config.local.push_cib(meta_attributes=meta_attributes)
node_add_guest(self.env_assist.get_env(), options={"remote-port": "99"})
self.env_assist.assert_reports(
[
Expand Down Expand Up @@ -713,9 +717,8 @@ def test_unknown_host_addr_not_specified(self):
</meta_attributes>
"""
self.config.env.set_known_hosts_dests(dict())
self.config.local.load_cib().local.push_cib(
meta_attributes=meta_attributes
)
self.config.local.load_cib(env=self.cmd_env)
self.config.local.push_cib(meta_attributes=meta_attributes)
node_add_guest(self.env_assist.get_env(), options={"remote-port": "99"})
self.env_assist.assert_reports(
[
Expand Down Expand Up @@ -746,16 +749,15 @@ def test_unknown_host_addr_specified(self):
</meta_attributes>
"""
self.config.env.set_known_hosts_dests(dict())
self.config.local.load_cib().local.push_cib(
meta_attributes=meta_attributes
)
self.config.local.load_cib(env=self.cmd_env)
self.config.local.push_cib(meta_attributes=meta_attributes)
node_add_guest(self.env_assist.get_env(), options={"remote-addr": "aa"})
self.env_assist.assert_reports(fixture_reports_not_live_cib(NODE_NAME))

def test_validate_values(self):
# Instance of 'Config' has no 'local' member
# pylint: disable=no-member
self.config.local.load_cib()
self.config.local.load_cib(env=self.cmd_env)
self.env_assist.assert_raise_library_error(
lambda: node_add_guest(
self.env_assist.get_env(),
Expand Down
32 changes: 22 additions & 10 deletions pcs_test/tier0/lib/commands/remote_node/test_node_add_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,24 +680,32 @@ def test_all_node_names_missing(self):

class NotLive(TestCase):
def setUp(self):
self.tmp_file = "/fake/tmp_file"
self.cmd_env = dict(CIB_file=self.tmp_file)
self.env_assist, self.config = get_env_tools(self)
self.config.env.set_known_hosts_dests(KNOWN_HOSTS_DESTS)
with open(rc("cib-empty.xml")) as cib_file:
self.config.env.set_cib_data(cib_file.read())
self.config.env.set_cib_data(
cib_file.read(), cib_tempfile=self.tmp_file
)

def test_addr_specified(self):
(
self.config.runner.cib.load()
.runner.pcmk.load_agent(agent_name="ocf:pacemaker:remote")
self.config.runner.cib.load(env=self.cmd_env)
.runner.pcmk.load_agent(
agent_name="ocf:pacemaker:remote", env=self.cmd_env
)
.env.push_cib(resources=FIXTURE_RESOURCES)
)
node_add_remote(self.env_assist.get_env())
self.env_assist.assert_reports(fixture_reports_not_live_cib(NODE_NAME))

def test_addr_not_specified(self):
(
self.config.runner.cib.load()
.runner.pcmk.load_agent(agent_name="ocf:pacemaker:remote")
self.config.runner.cib.load(env=self.cmd_env)
.runner.pcmk.load_agent(
agent_name="ocf:pacemaker:remote", env=self.cmd_env
)
.env.push_cib(
resources=FIXTURE_RESOURCES_TEMPLATE.format(
server=NODE_ADDR_PCSD, onfail=""
Expand All @@ -722,12 +730,14 @@ def test_addr_not_specified(self):
def test_unknown_host_addr_not_specified(self):
self.config.env.set_known_hosts_dests(dict())
(
self.config.runner.cib.load()
.runner.pcmk.load_agent(agent_name="ocf:pacemaker:remote")
self.config.runner.cib.load(env=self.cmd_env)
.runner.pcmk.load_agent(
agent_name="ocf:pacemaker:remote", env=self.cmd_env
)
.env.push_cib(
resources=FIXTURE_RESOURCES_TEMPLATE.format(
server=NODE_NAME, onfail=""
)
),
)
)
node_add_remote(self.env_assist.get_env(), no_node_addr=True)
Expand All @@ -748,8 +758,10 @@ def test_unknown_host_addr_not_specified(self):
def test_unknown_host_addr_specified(self):
self.config.env.set_known_hosts_dests(dict())
(
self.config.runner.cib.load()
.runner.pcmk.load_agent(agent_name="ocf:pacemaker:remote")
self.config.runner.cib.load(env=self.cmd_env)
.runner.pcmk.load_agent(
agent_name="ocf:pacemaker:remote", env=self.cmd_env
)
.env.push_cib(
resources=FIXTURE_RESOURCES_TEMPLATE.format(
server="addr", onfail=""
Expand Down
16 changes: 12 additions & 4 deletions pcs_test/tier0/lib/commands/test_booth.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,9 +1796,13 @@ def test_success_custom_instance(self):
)

def test_success_not_live_cib(self):
tmp_file = "/fake/tmp_file"
env = dict(CIB_file=tmp_file)
(
self.config.env.set_cib_data(open(rc("cib-empty.xml")).read())
.runner.cib.load()
self.config.env.set_cib_data(
open(rc("cib-empty.xml")).read(), cib_tempfile=tmp_file
)
.runner.cib.load(env=env)
.raw_file.read(
file_type_codes.BOOTH_CONFIG,
self.fixture_cfg_path(),
Expand All @@ -1807,10 +1811,12 @@ def test_success_not_live_cib(self):
.runner.pcmk.load_agent(
agent_name="ocf:heartbeat:IPaddr2",
name="runner.pcmk.load_agent.ipaddr2",
env=env,
)
.runner.pcmk.load_agent(
agent_name="ocf:pacemaker:booth-site",
name="runner.pcmk.load_agent.booth-site",
env=env,
)
.env.push_cib(resources=self.fixture_cib_booth_group())
)
Expand Down Expand Up @@ -2056,16 +2062,18 @@ def test_success_custom_instance(self):
)

def test_success_not_live_cib(self):
tmp_file = "/fake/tmp_file"
env = dict(CIB_file=tmp_file)
cib_xml_man = XmlManipulation.from_file(rc("cib-empty.xml"))
cib_xml_man.append_to_first_tag_name(
"resources", self.fixture_cib_booth_group(wrap_in_resources=False)
)
(
self.config
# This makes env.is_cib_live return False
.env.set_cib_data(str(cib_xml_man))
.env.set_cib_data(str(cib_xml_man), cib_tempfile=tmp_file)
# This instructs the runner to actually return our mocked cib
.runner.cib.load_content(str(cib_xml_man))
.runner.cib.load_content(str(cib_xml_man), env=env)
)
commands.remove_from_cluster(
self.env_assist.get_env(),
Expand Down
25 changes: 19 additions & 6 deletions pcs_test/tier0/lib/commands/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,18 +383,22 @@ def test_success_live_remote_node_verbose(self):
)

def test_succes_mocked(self):
tmp_file = "/fake/tmp_file"
env = dict(CIB_file=tmp_file)
(
self.config.env.set_corosync_conf_data(rc_read("corosync.conf"))
.env.set_cib_data("<cib/>")
.env.set_cib_data("<cib/>", cib_tempfile=tmp_file)
.runner.pcmk.load_state_plaintext(
stdout="crm_mon cluster status",
env=env,
)
.runner.cib.load(
resources="""
<resources>
<primitive id="S" class="stonith" type="fence_dummy" />
</resources>
"""
""",
env=env,
)
)
self.assertEqual(
Expand All @@ -407,22 +411,31 @@ def test_succes_mocked(self):
)

def test_succes_mocked_verbose(self):
tmp_file = "/fake/tmp_file"
env = dict(CIB_file=tmp_file)
(
self.config.env.set_corosync_conf_data(rc_read("corosync.conf"))
.env.set_cib_data("<cib/>")
.runner.pcmk.can_fence_history_status(stderr="not supported")
.env.set_cib_data("<cib/>", cib_tempfile=tmp_file)
.runner.pcmk.can_fence_history_status(
stderr="not supported",
env=env,
)
.runner.pcmk.load_state_plaintext(
verbose=True,
stdout="crm_mon cluster status",
env=env,
)
.runner.cib.load(
resources="""
<resources>
<primitive id="S" class="stonith" type="fence_dummy" />
</resources>
"""
""",
env=env,
)
.runner.pcmk.load_ticket_state_plaintext(
stdout="ticket status", env=env
)
.runner.pcmk.load_ticket_state_plaintext(stdout="ticket status")
)
self.assertEqual(
status.full_cluster_status_plaintext(
Expand Down
20 changes: 16 additions & 4 deletions pcs_test/tier0/lib/test_env_cib.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_not_satisfied_nice_to_have_greater_than_required(self):
class GetCib(TestCase, ManageCibAssertionMixin):
def setUp(self):
self.env_assist, self.config = get_env_tools(test_case=self)
self.tmp_file = "/fake/tmp/file"

def test_raise_library_error_when_cibadmin_failed(self):
stderr = "cibadmin: Connection to local file failed..."
Expand All @@ -156,8 +157,10 @@ def test_raise_library_error_when_cibadmin_failed(self):
# into tempfile when the runner is not mocked. And content is then
# loaded from tempfile by `cibadmin --local --query`. Runner is
# mocked in tests so the value of cib_data is not in the fact used.
.env.set_cib_data("whatever").runner.cib.load(
returncode=203, stderr=stderr
.env.set_cib_data(
"whatever", cib_tempfile=self.tmp_file
).runner.cib.load(
returncode=203, stderr=stderr, env=dict(CIB_file=self.tmp_file)
)
)

Expand All @@ -172,7 +175,11 @@ def test_returns_cib_from_cib_data(self):
(
self.config
# Value of cib_data is unimportant here. See details in sibling test
.env.set_cib_data("whatever").runner.cib.load(filename=cib_filename)
.env.set_cib_data(
"whatever", cib_tempfile=self.tmp_file
).runner.cib.load(
filename=cib_filename, env=dict(CIB_file=self.tmp_file)
)
)
with open(rc(cib_filename)) as cib_file:
assert_xml_equal(
Expand Down Expand Up @@ -435,7 +442,12 @@ def setUp(self):
self.env_assist, self.config = get_env_tools(test_case=self)

def test_wait_not_suported_for_mocked_cib(self):
(self.config.env.set_cib_data("<cib/>").runner.cib.load())
tmp_file = "/fake/tmp/file"
(
self.config.env.set_cib_data(
"<cib/>", cib_tempfile=tmp_file
).runner.cib.load(env=dict(CIB_file=tmp_file))
)

env = self.env_assist.get_env()
env.get_cib()
Expand Down
Loading