Add get-admin-password action with unit test
Implemented a new action to provide users the possibility of retrieving Keystone service's admin password via juju action. The result of this action is equivalent to running “juju run --unit {keystone unit} leader-get admin_passwd”. Closes-Bug: #1858657 Change-Id: I231c4b73016f7e7b4ba7f06219dd8e212402a339
This commit is contained in:
parent
6f4894ea13
commit
4949830cea
|
@ -14,4 +14,8 @@ openstack-upgrade:
|
|||
Perform openstack upgrades. Config option action-managed-upgrade must be
|
||||
set to True.
|
||||
security-checklist:
|
||||
description: Validate the running configuration against the OpenStack security guides checklist
|
||||
description: |
|
||||
Validate the running configuration against the OpenStack security guides
|
||||
checklist.
|
||||
get-admin-password:
|
||||
description: Retrieve the admin password for the Keystone service.
|
|
@ -0,0 +1,53 @@
|
|||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright 2022 Canonical Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append('.')
|
||||
|
||||
from charmhelpers.core.hookenv import (
|
||||
function_set,
|
||||
function_fail,
|
||||
leader_get,
|
||||
)
|
||||
|
||||
|
||||
def get_admin_password(arg):
|
||||
"""Implementation of 'get-admin-password' action."""
|
||||
function_set({'admin-password': leader_get("admin_passwd")})
|
||||
|
||||
|
||||
ACTIONS = {
|
||||
"get-admin-password": get_admin_password
|
||||
}
|
||||
|
||||
|
||||
def main(args):
|
||||
action_name = os.path.basename(args[0])
|
||||
try:
|
||||
action = ACTIONS[action_name]
|
||||
except KeyError:
|
||||
return "Action {} undefined".format(action_name)
|
||||
else:
|
||||
try:
|
||||
action(args)
|
||||
except Exception as e:
|
||||
function_fail("Action {} failed: {}".format(action_name, str(e)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv))
|
|
@ -0,0 +1 @@
|
|||
admin_password.py
|
|
@ -0,0 +1,72 @@
|
|||
# Copyright 2022 Canonical Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from unittest import mock
|
||||
from test_utils import CharmTestCase
|
||||
|
||||
from actions import admin_password
|
||||
|
||||
|
||||
TO_PATCH = [
|
||||
'leader_get',
|
||||
'function_set',
|
||||
]
|
||||
|
||||
|
||||
class GetAdminPasswordTestCase(CharmTestCase):
|
||||
def setUp(self):
|
||||
super(GetAdminPasswordTestCase, self).setUp(
|
||||
admin_password, TO_PATCH)
|
||||
|
||||
def test_get_admin_password(self):
|
||||
admin_password.get_admin_password([])
|
||||
self.function_set.assert_called_with({'admin-password':
|
||||
self.leader_get("admin_passwd")})
|
||||
|
||||
|
||||
class MainTestCase(CharmTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(MainTestCase, self).setUp(admin_password, ["function_fail"])
|
||||
|
||||
def test_invokes_action(self):
|
||||
dummy_calls = []
|
||||
|
||||
def dummy_action(arg):
|
||||
dummy_calls.append(True)
|
||||
|
||||
with mock.patch.dict(admin_password.ACTIONS, {"foo": dummy_action}):
|
||||
admin_password.main(["foo"])
|
||||
self.assertEqual(dummy_calls, [True])
|
||||
|
||||
def test_unknown_action(self):
|
||||
"""Unknown actions aren't a traceback."""
|
||||
exit_string = admin_password.main(["foo"])
|
||||
self.assertEqual("Action foo undefined", exit_string)
|
||||
|
||||
def assert_function_fail_msg(self, action_name, msg):
|
||||
"""Shortcut for asserting error with default structure"""
|
||||
admin_password.function_fail.assert_called_with("Action {} "
|
||||
"failed: {}"
|
||||
.format(action_name,
|
||||
msg))
|
||||
|
||||
def test_failing_action(self):
|
||||
"""Actions which traceback trigger function_fail() calls."""
|
||||
def dummy_action(arg):
|
||||
raise ValueError("uh oh")
|
||||
|
||||
with mock.patch.dict(admin_password.ACTIONS, {"foo": dummy_action}):
|
||||
admin_password.main(["foo"])
|
||||
self.assert_function_fail_msg("foo", "uh oh")
|
Loading…
Reference in New Issue