Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
mharding-hpe committed Sep 19, 2024
1 parent ba82916 commit aeaf1ec
Showing 1 changed file with 123 additions and 67 deletions.
190 changes: 123 additions & 67 deletions cray/tests/test_modules/test_bos_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,30 @@
# pylint: disable=unused-argument
# pylint: disable=invalid-name

from itertools import chain, combinations
import json

from cray.tests.utils import compare_dicts

from cray.tests.test_modules.test_bos import bos_url

# helpers

def verify_commands_equal(runner, cli, expected_result_data, command_list):
""" Verifies that all of the specified commands pass and give the same result """
for command in command_list:
result = runner.invoke(cli, command)
assert result.exit_code == 0
data = json.loads(result.output)
compare_dicts(expected_result_data, data)

# From Python 3 docs: https://docs.python.org/3/library/itertools.html
def powerset(iterable):
"""powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"""
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

# tests

def test_cray_bos_components_base(cli_runner, rest_mock):
""" Test cray bos components base command """
Expand Down Expand Up @@ -76,108 +94,146 @@ def test_cray_bos_v2_components_update_basic(cli_runner, rest_mock):
def test_cray_bos_v2_components_update_clear(cli_runner, rest_mock):
""" Test cray bos v2 components update"""
runner, cli, config = cli_runner
common_command_prefix = ['bos', 'v2', 'components', 'update']
common_command_suffix = ['--enabled', 'False', 'fakexname']
command_arguments_list = [
['--staged-state-session', '',
'--staged-state-configuration', '',
'--staged-state-boot-artifacts-initrd', '',
'--staged-state-boot-artifacts-kernel-parameters', '',
'--staged-state-boot-artifacts-kernel', '',
'--desired-state-bss-token', '',
'--desired-state-configuration', '',
'--desired-state-boot-artifacts-initrd', '',
'--desired-state-boot-artifacts-kernel-parameters', '',
'--desired-state-boot-artifacts-kernel', ''
],
['--clear-staged-state', '--clear-desired-state'],
['--clear-pending-state']
]
result = runner.invoke(
cli,
['bos', 'v2', 'components', 'update', '--staged-state-session', "",
'--staged-state-configuration', "",
'--staged-state-boot-artifacts-initrd', "",
'--staged-state-boot-artifacts-kernel-parameters', "",
'--staged-state-boot-artifacts-kernel', "",
'--desired-state-bss-token', "",
'--desired-state-configuration', "",
'--desired-state-boot-artifacts-initrd', "",
'--desired-state-boot-artifacts-kernel-parameters', "",
'--desired-state-boot-artifacts-kernel', "",
'--enabled', 'False',
'fakexname']
common_command_prefix + command_arguments_list[0] + common_command_suffix
)
assert result.exit_code == 0
data = json.loads(result.output)
assert data['method'] == 'PATCH'
assert data['url'] == bos_url(config, ver="v2", uri='/components/fakexname')
result2 = runner.invoke(
cli,
['bos', 'v2', 'components', 'update', '--clear-staged-state',
'--clear-desired-state', '--enabled', 'False',
'fakexname']
)
assert result2.exit_code == 0
data2 = json.loads(result2.output)
compare_dicts(data, data2)
result3 = runner.invoke(
cli,
['bos', 'v2', 'components', 'update', '--clear-staged-state',
'--clear-desired-state', '--enabled', 'False',
'fakexname']
compare_dicts(
{
'enabled': False,
'staged_state': {
"session": "",
"configuration": "",
"boot_artifacts": {
"initrd": "",
"kernel_parameters": "",
"kernel": ""
}
},
'desired_state': {
"bss_token": "",
"configuration": "",
"boot_artifacts": {
"initrd": "",
"kernel_parameters": "",
"kernel": ""
}
}
},
data['body']
)
data3 = json.loads(result3.output)
compare_dicts(data, data3)
# Now make sure that the other versions of the command produce identical results
verify_commands_equal(runner, cli, data,
[common_command_prefix + command_args + common_command_suffix
for command_args in command_arguments_list[1:]])


def test_cray_bos_v2_components_updatemany_by_ids(cli_runner, rest_mock):
""" Test cray bos v2 components updatemany"""
runner, cli, config = cli_runner
result = runner.invoke(
cli,
['bos', 'v2', 'components', 'updatemany', '--filter-ids',
'test1,test2', '--enabled', 'False']
)
common_command_prefix = ['bos', 'v2', 'components', 'updatemany', '--filter-ids', 'id1,id2']
command_arguments_list = [
['--enabled', 'False'],
['--enabled', 'False', '--patch', '{}'],
['--patch', '{ "enabled": false }']
]
result = runner.invoke(cli, common_command_prefix + command_arguments_list[0])
assert result.exit_code == 0
data = json.loads(result.output)
assert data['method'] == 'PATCH'
assert data['url'] == bos_url(config, ver="v2", uri='/components')
compare_dicts(
{
'patch': { 'enabled': False },
'filters': { 'ids': 'test1,test2' }
'filters': { 'ids': 'id1,id2' }
},
data['body']
)
# Now make sure that the other versions of the command produce identical results
verify_commands_equal(runner, cli, data,
[common_command_prefix + command_args
for command_args in command_arguments_list[1:]])


def test_cray_bos_v2_components_updatemany_by_session(cli_runner, rest_mock):
""" Test cray bos v2 components updatemany"""
runner, cli, config = cli_runner
result = runner.invoke(
cli,
['bos', 'v2', 'components', 'updatemany', '--filter-session',
'test1', '--clear-pending-state', '--patch', '{ "enabled": true }']
)
common_command_prefix = ['bos', 'v2', 'components', 'updatemany', '--filter-session', 'test1']
full_patch_data = {
'enabled': True,
'staged_state': {
'session': '',
'configuration': '',
'boot_artifacts': {
'initrd': '',
'kernel_parameters': '',
'kernel': ''
}
},
'desired_state': {
'bss_token': '',
'configuration': '',
'boot_artifacts': {
'initrd': '',
'kernel_parameters': '',
'kernel': ''
}
}
}
def json_patch(**fields):
return json.dumps({ k: v for k, v in full_patch_data.items() if k in fields })

command_arguments_list = []
all_arguments = [ '--retry-policy', '--clear-staged-state', '--clear-desired-state', '--clear-pending-state' ]
for explicit_args in powerset(all_arguments):
args=list(explicit_args)
patch_fields=[]
if '--retry-policy' in explicit_args:
args.insert(args.index('--retry-policy')+1, '57')
else:
patch_fields.append('retry_policy')
if {'--clear-staged-state', '--clear-pending-state'}.isdisjoint(explicit_args):
patch_fields.append('staged_state')
if {'--clear-desired-state', '--clear-pending-state'}.isdisjoint(explicit_args):
patch_fields.append('desired_state')
command_arguments_list.append(args + ['--patch', json_patch(patch_fields)])
if not patch_fields:
# This means we can equivalently omit the --patch argument
command_arguments_list.append(args)
result = runner.invoke(cli, common_command_prefix + command_arguments_list[0])
assert result.exit_code == 0
data = json.loads(result.output)
assert data['method'] == 'PATCH'
assert data['url'] == bos_url(config, ver="v2", uri='/components')
compare_dicts(
{
'patch': {
'enabled': True,
'staged_state': {
"session": "",
"configuration": "",
"boot_artifacts": {
"initrd": "",
"kernel_parameters": "",
"kernel": "" }
},
'desired_state': {
"bss_token": "",
"configuration": "",
"boot_artifacts": {
"initrd": "",
"kernel_parameters": "",
"kernel": "" }
}
},
'patch': full_patch_data,
'filters': { 'session': 'test1' }
},
data['body']
)
result2 = runner.invoke(
cli,
['bos', 'v2', 'components', 'updatemany', '--filter-session',
'test1', '--clear-desired-state', '--clear-staged-state',
'--enabled', 'True']
)
assert result2.exit_code == 0
data2 = json.loads(result2.output)
compare_dicts(data, data2)
# Now make sure that the other versions of the command produce identical results
verify_commands_equal(runner, cli, data,
[common_command_prefix + command_args
for command_args in command_arguments_list[1:]])

0 comments on commit aeaf1ec

Please sign in to comment.