Skip to content

Commit

Permalink
Merged list function tests into end2end tests; fixed zhmc_partition_l…
Browse files Browse the repository at this point in the history
…ist module

Details:

* Merged function tests of list modules into end2end tests to remove duplicate
  test cases.

* Fixed KeyError in zhmc_partition_list module by removing incorrect property
  'activation_mode' from result partitions.

Signed-off-by: Andreas Maier <[email protected]>
  • Loading branch information
andy-maier committed Jun 30, 2022
1 parent 018126e commit ebe9778
Show file tree
Hide file tree
Showing 10 changed files with 554 additions and 659 deletions.
6 changes: 0 additions & 6 deletions docs/source/modules/zhmc_partition_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ partitions
[
{
"activation_mode": "linux",
"cpc_name": "CPC1",
"has_unacceptable_status": false,
"name": "partition1",
Expand Down Expand Up @@ -190,9 +189,4 @@ partitions

| **type**: bool
activation_mode
The activation mode of the partition. For details, see the description of the 'activation-mode' property in the data model of the 'Logical Partition' resource (see :term:`HMC API`).

| **type**: str

5 changes: 5 additions & 0 deletions docs/source/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Availability: `AutomationHub`_, `Galaxy`_, `GitHub`_

**Bug fixes:**

* Fixed KeyError in zhmc_partition_list module by removing incorrect property
'activation_mode' from result partitions.

**Enhancements:**

* Added a new 'zhmc_partition_list' Ansible module for listing partitions on
Expand Down Expand Up @@ -67,6 +70,8 @@ Availability: `AutomationHub`_, `Galaxy`_, `GitHub`_
* Added a new module 'zhmc_user_role_list' that supports listing the names
of user roles on the HMC. (issue #362)

* Merged function tests into end2end tests to remove duplicate test cases.

**Cleanup:**

**Known issues:**
Expand Down
7 changes: 0 additions & 7 deletions plugins/modules/zhmc_partition_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@
description: Indicates whether the current status of the partition is
unacceptable, based on its 'acceptable-status' property.
type: bool
activation_mode:
description: The activation mode of the partition. For details, see the
description of the 'activation-mode' property in the data model of the
'Logical Partition' resource (see :term:`HMC API`).
type: str
sample:
[
{
Expand All @@ -176,7 +171,6 @@
"se_version": "2.15.0",
"status": "active",
"has_unacceptable_status": False,
"activation_mode": 'linux'
}
]
"""
Expand Down Expand Up @@ -265,7 +259,6 @@ def perform_list(params):
"status": partition.get_property('status'),
"has_unacceptable_status": partition.get_property(
'has-unacceptable-status'),
"activation_mode": partition.get_property('activation-mode'),
}
partition_list.append(partition_properties)

Expand Down
2 changes: 2 additions & 0 deletions tests/end2end/mocked_hmc_z14.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ hmc_definition:
activation-mode: "linux"
next-activation-profile-name: "LPAR1"
last-used-activation-profile: "LPAR1"
has-unacceptable-status: false
- properties:
object-id: lpar2
name: "LPAR2"
Expand All @@ -482,6 +483,7 @@ hmc_definition:
activation-mode: "not-set"
next-activation-profile-name: "LPAR2"
last-used-activation-profile: "LPAR2"
has-unacceptable-status: true
reset_activation_profiles:
- properties:
name: "CPC1"
Expand Down
183 changes: 183 additions & 0 deletions tests/end2end/test_zhmc_cpc_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# Copyright 2022 IBM Corp. All Rights Reserved.
#
# 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.

"""
End2end tests for zhmc_cpc_list module.
"""

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import pytest
import mock
import requests.packages.urllib3
import zhmcclient
# pylint: disable=line-too-long,unused-import
from zhmcclient.testutils import hmc_definition, hmc_session # noqa: F401, E501
# pylint: enable=line-too-long,unused-import

from plugins.modules import zhmc_cpc_list
from .utils import mock_ansible_module, get_failure_msg

requests.packages.urllib3.disable_warnings()

# Print debug messages
DEBUG = False

LOG_FILE = 'zhmc_cpc_list.log' if DEBUG else None


def get_module_output(mod_obj):
"""
Return the module output as a tuple (changed, user_properties) (i.e.
the arguments of the call to exit_json()).
If the module failed, return None.
"""

def func(changed, cpcs):
return changed, cpcs

if not mod_obj.exit_json.called:
return None
call_args = mod_obj.exit_json.call_args

# The following makes sure we get the arguments regardless of whether they
# were specified as positional or keyword arguments:
return func(*call_args[0], **call_args[1])


def assert_cpc_list(
cpc_list, include_unmanaged_cpcs, exp_cpc_dict, exp_um_cpc_dict):
"""
Assert the output of the zhmc_cpc_list module
"""

assert isinstance(cpc_list, list)

exp_len = len(exp_cpc_dict)
if include_unmanaged_cpcs:
exp_len += len(exp_um_cpc_dict)
assert len(cpc_list) == exp_len

for cpc_item in cpc_list:

assert 'name' in cpc_item, \
"Returned CPC {ri!r} does not have a 'name' property". \
format(ri=cpc_item)
cpc_name = cpc_item['name']

assert 'is_managed' in cpc_item, \
"Returned CPC {ri!r} does not have a 'is_managed' property". \
format(ri=cpc_item)
is_managed = cpc_item['is_managed']

for pname, pvalue in cpc_item.items():

if is_managed:
assert cpc_name in exp_cpc_dict, \
"Result contains unexpected managed CPC: {rn!r}". \
format(rn=cpc_name)

exp_cpc = exp_cpc_dict[cpc_name]

# Handle artificial properties
if pname == 'is_managed':
continue

# Verify normal properties
pname_hmc = pname.replace('_', '-')
assert pname_hmc in exp_cpc.properties, \
"Unexpected property {pn!r} in CPC {rn!r}". \
format(pn=pname, rn=cpc_name)
exp_value = exp_cpc.properties[pname_hmc]
assert pvalue == exp_value, \
"Incorrect value for property {pn!r} of CPC {rn!r}". \
format(pn=pname, rn=cpc_name)

else:
assert cpc_name in exp_um_cpc_dict, \
"Result contains unexpected unmanaged CPC: {rn!r}". \
format(rn=cpc_name)


@pytest.mark.parametrize(
"check_mode", [
pytest.param(False, id="check_mode=False"),
pytest.param(True, id="check_mode=True"),
]
)
@pytest.mark.parametrize(
"include_unmanaged_cpcs", [
pytest.param(False, id="include_unmanaged_cpcs=False"),
pytest.param(True, id="include_unmanaged_cpcs=True"),
]
)
@mock.patch("plugins.modules.zhmc_cpc_list.AnsibleModule", autospec=True)
def test_user_cpc_list(
ansible_mod_cls, include_unmanaged_cpcs, check_mode, hmc_session): # noqa: F811, E501
"""
Test listing of CPCs of the HMC.
"""

hd = hmc_session.hmc_definition
hmc_host = hd.host
hmc_auth = dict(userid=hd.userid, password=hd.password,
ca_certs=hd.ca_certs, verify=hd.verify)

client = zhmcclient.Client(hmc_session)

faked_session = hmc_session if hd.mock_file else None

# Determine the expected managed CPCs on the HMC
exp_cpcs = client.cpcs.list()
exp_cpcs_dict = {}
for cpc in exp_cpcs:
cpc.pull_full_properties()
exp_cpcs_dict[cpc.name] = cpc

# Determine the expected unmanaged CPCs on the HMC
exp_um_cpcs = client.consoles.console.list_unmanaged_cpcs()
exp_um_cpcs_dict = {}
for cpc in exp_um_cpcs:
cpc.pull_full_properties()
exp_um_cpcs_dict[cpc.name] = cpc

# Prepare module input parameters (must be all required + optional)
params = {
'hmc_host': hmc_host,
'hmc_auth': hmc_auth,
'include_unmanaged_cpcs': include_unmanaged_cpcs,
'log_file': LOG_FILE,
'_faked_session': faked_session,
}

# Prepare mocks for AnsibleModule object
mod_obj = mock_ansible_module(ansible_mod_cls, params, check_mode)

# Exercise the code to be tested
with pytest.raises(SystemExit) as exc_info:
zhmc_cpc_list.main()
exit_code = exc_info.value.args[0]

# Assert module exit code
assert exit_code == 0, \
"Module failed with exit code {e} and message:\n{m}". \
format(e=exit_code, m=get_failure_msg(mod_obj))

# Assert module output
changed, cpc_list = get_module_output(mod_obj)
assert changed is False

assert_cpc_list(
cpc_list, include_unmanaged_cpcs, exp_cpcs_dict, exp_um_cpcs_dict)
Loading

0 comments on commit ebe9778

Please sign in to comment.