Merge "Replace software upgrade sysinv data"

This commit is contained in:
Zuul 2024-04-15 14:52:45 +00:00 committed by Gerrit Code Review
commit 4b433c3d46
3 changed files with 231 additions and 0 deletions

View File

@ -198,3 +198,16 @@ class SoftwareAPIController(object):
@expose(method='GET', template='json')
def in_sync_controller(self):
return sc.in_sync_controller_api()
@expose(method='GET', template='json')
def software_upgrade(self):
return sc.get_software_upgrade()
@expose(method='GET', template='json')
def software_host_upgrade(self, *args):
args_list = list(args)
if not args_list:
return sc.get_all_software_host_upgrade()
hostname = args_list[0]
return sc.get_one_software_host_upgrade(hostname)

View File

@ -3062,6 +3062,75 @@ class PatchController(PatchService):
func(*args, **kwargs)
self._update_state_to_peer()
def _get_software_upgrade(self):
"""
Get the current software upgrade from/to versions and state
:return: dict of from_release, to_release and state
"""
all_deploy = self.db_api_instance.get_deploy_all()
if not all_deploy:
return None
deploy = all_deploy[0]
from_maj_min_release = utils.get_major_release_version(deploy.get("from_release"))
to_maj_min_release = utils.get_major_release_version(deploy.get("to_release"))
state = deploy.get("state")
return {
"from_release": from_maj_min_release,
"to_release": to_maj_min_release,
"state": state
}
def get_software_upgrade(self):
return self._get_software_upgrade()
def get_all_software_host_upgrade(self):
"""
Get all software host upgrade from/to versions and state
:return: list of dict of hostname, current_sw_version, target_sw_version and host_state
"""
deploy = self._get_software_upgrade()
deploy_hosts = self.db_api_instance.get_deploy_host()
if deploy is None or deploy_hosts is None:
return None
from_maj_min_release = deploy.get("from_release")
to_maj_min_release = deploy.get("to_release")
all_host_upgrades = []
for deploy_host in deploy_hosts:
all_host_upgrades.append({
"hostname": deploy_host.get("hostname"),
"current_sw_version": to_maj_min_release if deploy_host.get(
"state") == constants.DEPLOYED else from_maj_min_release,
"target_sw_version": to_maj_min_release,
"host_state": deploy_host.get("state")
})
return all_host_upgrades
def get_one_software_host_upgrade(self, hostname):
"""
Get the given software host upgrade from/to versions and state
:param hostname: hostname
:return: array of dict of hostname, current_sw_version, target_sw_version and host_state
"""
all_host_upgrades = self.get_all_software_host_upgrade()
if not all_host_upgrades:
return None
for host_upgrade in all_host_upgrades:
if host_upgrade.get("hostname") == hostname:
return [host_upgrade]
return None
class PatchControllerApiThread(threading.Thread):
def __init__(self):

View File

@ -201,3 +201,152 @@ class TestSoftwareController(unittest.TestCase):
mock_isfile.side_effect = [False, True]
result = controller.in_sync_controller_api()
self.assertEqual(result, {"in_sync": False})
@patch('software.software_controller.json.load')
@patch('software.software_controller.open', new_callable=mock_open)
def test_get_software_host_upgrade_deployed(self,
mock_dummy, # pylint: disable=unused-argument
mock_json_load, # pylint: disable=unused-argument
):
controller = PatchController()
controller._get_software_upgrade = MagicMock(return_value={ # pylint: disable=protected-access
"from_release": "1.0.0",
"to_release": "2.0.0"
})
controller.db_api_instance.get_deploy_host = MagicMock(return_value=[
{"hostname": "host1", "state": constants.DEPLOYED},
{"hostname": "host2", "state": constants.DEPLOYING}
])
# Test when the host is deployed
result = controller.get_one_software_host_upgrade("host1")
self.assertEqual(result, [{
"hostname": "host1",
"current_sw_version": "2.0.0",
"target_sw_version": "2.0.0",
"host_state": constants.DEPLOYED
}])
@patch('software.software_controller.json.load')
@patch('software.software_controller.open', new_callable=mock_open)
def test_get_software_host_upgrade_deploying(self,
mock_dummy, # pylint: disable=unused-argument
mock_json_load, # pylint: disable=unused-argument
):
controller = PatchController()
controller._get_software_upgrade = MagicMock(return_value={ # pylint: disable=protected-access
"from_release": "1.0.0",
"to_release": "2.0.0"
})
controller.db_api_instance.get_deploy_host = MagicMock(return_value=[
{"hostname": "host1", "state": constants.DEPLOYED},
{"hostname": "host2", "state": constants.DEPLOYING}
])
# Test when the host is deploying
result = controller.get_one_software_host_upgrade("host2")
self.assertEqual(result, [{
"hostname": "host2",
"current_sw_version": "1.0.0",
"target_sw_version": "2.0.0",
"host_state": constants.DEPLOYING
}])
@patch('software.software_controller.json.load')
@patch('software.software_controller.open', new_callable=mock_open)
def test_get_all_software_host_upgrade_deploying(self,
mock_dummy, # pylint: disable=unused-argument
mock_json_load, # pylint: disable=unused-argument
):
controller = PatchController()
controller._get_software_upgrade = MagicMock(return_value={ # pylint: disable=protected-access
"from_release": "1.0.0",
"to_release": "2.0.0"
})
controller.db_api_instance.get_deploy_host = MagicMock(return_value=[
{"hostname": "host1", "state": constants.DEPLOYED},
{"hostname": "host2", "state": constants.DEPLOYING}
])
# Test when the host is deploying
result = controller.get_all_software_host_upgrade()
self.assertEqual(result, [{
"hostname": "host1",
"current_sw_version": "2.0.0",
"target_sw_version": "2.0.0",
"host_state": constants.DEPLOYED
}, {
"hostname": "host2",
"current_sw_version": "1.0.0",
"target_sw_version": "2.0.0",
"host_state": constants.DEPLOYING
}])
@patch('software.software_controller.json.load')
@patch('software.software_controller.open', new_callable=mock_open)
def test_get_software_host_upgrade_none_state(self,
mock_dummy, # pylint: disable=unused-argument
mock_json_load, # pylint: disable=unused-argument
):
controller = PatchController()
# Test when the deploy or deploy_hosts is None
controller._get_software_upgrade = MagicMock(return_value=None) # pylint: disable=protected-access
controller.db_api_instance.get_deploy_host.return_value = None
result = controller.get_one_software_host_upgrade("host1")
self.assertIsNone(result)
@patch('software.software_controller.json.load')
@patch('software.software_controller.open', new_callable=mock_open)
def test_get_software_upgrade_get_deploy_all(self,
mock_dummy, # pylint: disable=unused-argument
mock_json_load, # pylint: disable=unused-argument
):
controller = PatchController()
# Create a mock instance of the db_api
db_api_instance_mock = MagicMock()
controller.db_api_instance = db_api_instance_mock
# Create a mock return value for the get_deploy_all method
deploy_all_mock = [{"from_release": "1.0.0", "to_release": "2.0.0", "state": "start"}]
db_api_instance_mock.get_deploy_all.return_value = deploy_all_mock
# Call the method being tested
result = controller._get_software_upgrade() # pylint: disable=protected-access
# Verify that the expected methods were called
db_api_instance_mock.get_deploy_all.assert_called_once()
# Verify the expected result
expected_result = {
"from_release": "1.0",
"to_release": "2.0",
"state": "start"
}
self.assertEqual(result, expected_result)
@patch('software.software_controller.json.load')
@patch('software.software_controller.open', new_callable=mock_open)
def test_get_software_upgrade_get_deploy_all_none(self,
mock_dummy, # pylint: disable=unused-argument
mock_json_load, # pylint: disable=unused-argument
):
controller = PatchController()
# Create a mock instance of the db_api
db_api_instance_mock = MagicMock()
controller.db_api_instance = db_api_instance_mock
# Create a mock return value for the get_deploy_all method
db_api_instance_mock.get_deploy_all.return_value = None
# Call the method being tested
result = controller._get_software_upgrade() # pylint: disable=protected-access
# Verify that the expected methods were called
db_api_instance_mock.get_deploy_all.assert_called_once()
self.assertEqual(result, None)