Skip to content

Commit

Permalink
[crmsh-4.6] Dev: utils: Load CIB_file env before some readonly comman…
Browse files Browse the repository at this point in the history
…ds (#1541)

backport #1540
  • Loading branch information
liangxin1300 authored Sep 4, 2024
2 parents 4abc54a + 69a5321 commit 3a178c6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crmsh/ui_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ def do_assist(self):
def do_show(self, context, *args):
"usage: show [xml] [<id>...]"
from .utils import obscure
utils.load_cib_file_env()
osargs = [arg[8:] for arg in args if arg.startswith('obscure:')]
if not osargs and config.core.obscure_pattern:
# obscure_pattern could be
Expand All @@ -585,6 +586,7 @@ def do_show(self, context, *args):
@command.completers_repeating(compl.call(ra.get_properties_list))
def do_get_property(self, context, *args):
"usage: get-property [-t|--true [<name>...]"
utils.load_cib_file_env()
properties = [a for a in args if a not in ('-t', '--true')]
truth = any(a for a in args if a in ('-t', '--true'))

Expand Down Expand Up @@ -743,6 +745,7 @@ def _verify(self, set_obj_semantic, set_obj_all):
@command.skill_level('administrator')
def do_verify(self, context):
"usage: verify"
utils.load_cib_file_env()
cib_factory.ensure_cib_updated()
set_obj_all = mkset_obj("xml")
return self._verify(set_obj_all, set_obj_all)
Expand Down
2 changes: 2 additions & 0 deletions crmsh/ui_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def requires(self):
@command.completers(compl.nodes)
def do_show(self, context, node=None):
'usage: show [<node>]'
utils.load_cib_file_env()
cib = xmlutil.cibdump2elem()
if cib is None:
return False
Expand Down Expand Up @@ -616,6 +617,7 @@ def do_server(self, context, *nodes):
server -- print server hostname / address for each node
server <node> ... -- print server hostname / address for node
"""
utils.load_cib_file_env()
cib = xmlutil.cibdump2elem()
if cib is None:
return False
Expand Down
11 changes: 11 additions & 0 deletions crmsh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3176,4 +3176,15 @@ def ssh_command():
if config.core.no_ssh:
raise NoSSHError(constants.NO_SSH_ERROR_MSG)
return "ssh"


def load_cib_file_env():
if options.regression_tests or ServiceManager().service_is_active("pacemaker.service"):
return
cib_file = os.environ.setdefault('CIB_file', constants.CIB_RAW_FILE)
logger.warning("Cluster is not running, loading the CIB file from %s", cib_file)
if not os.path.exists(cib_file):
raise ValueError(f"Cannot find cib file: {cib_file}")


# vim:ts=4:sw=4:et:
16 changes: 16 additions & 0 deletions test/features/configure_bugs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ Feature: Functional test for configure sub level
Tag @clean means need to stop cluster service if the service is available
Need nodes: hanode1 hanode2

@clean
Scenario: Load CIB_file env before read-only commands
Given Cluster service is "stopped" on "hanode1"
# Should put this scenario at the beginning of the test suite
And File "/var/lib/pacemaker/cib/cib.xml" not exist on "hanode1"
When Try "crm configure show" on "hanode1"
Then Except "Cannot find cib file: /var/lib/pacemaker/cib/cib.xml" in stderr
And Expected return code is "1"
When Run "crm cluster init -y" on "hanode1"
Then Cluster service is "started" on "hanode1"
When Run "crm cluster stop" on "hanode1"
Then Cluster service is "stopped" on "hanode1"
When Try "crm configure show" on "hanode1"
Then Except "Cluster is not running, loading the CIB file from /var/lib/pacemaker/cib/cib.xml" in stderr
And Expected return code is "0"

@clean
Scenario: Replace sensitive data by default(bsc#1163581)
Given Cluster service is "stopped" on "hanode1"
Expand Down
7 changes: 7 additions & 0 deletions test/features/steps/step_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,13 @@ def step_impl(context, path, node):
assert rc == 0


@given('File "{path}" not exist on "{node}"')
def step_impl(context, path, node):
cmd = '[ ! -f {} ]'.format(path)
rc, _, stderr = behave_agent.call(node, 1122, cmd, user='root')
assert rc == 0


@then('File "{path}" not exist on "{node}"')
def step_impl(context, path, node):
cmd = '[ ! -f {} ]'.format(path)
Expand Down

0 comments on commit 3a178c6

Please sign in to comment.