From a20a9a94e2bd314bb40ba32a3b65a401c98c6116 Mon Sep 17 00:00:00 2001 From: Romy <35330373+romayalon@users.noreply.github.com> Date: Sun, 5 Jan 2025 17:04:06 +0200 Subject: [PATCH] NC | Online Upgrade | Add config_dir_upgrade unit tests Signed-off-by: Romy <35330373+romayalon@users.noreply.github.com> --- .../test_nc_upgrade_manager.test.js | 160 +++++++++++++++++- 1 file changed, 154 insertions(+), 6 deletions(-) diff --git a/src/test/unit_tests/jest_tests/test_nc_upgrade_manager.test.js b/src/test/unit_tests/jest_tests/test_nc_upgrade_manager.test.js index 925d0eb2a7..7e2a89175b 100644 --- a/src/test/unit_tests/jest_tests/test_nc_upgrade_manager.test.js +++ b/src/test/unit_tests/jest_tests/test_nc_upgrade_manager.test.js @@ -57,6 +57,7 @@ module.exports = { description: 'dummy upgrade script file 1' }; `; + const old_expected_system_json = { [hostname]: { current_version: '5.17.0', @@ -72,25 +73,77 @@ const old_expected_system_json = { const old_expected_system_json_has_config_directory = { [hostname]: { - current_version: '5.18.1', + current_version: pkg.version + '.1', upgrade_history: { successful_upgrades: [{ timestamp: 1724687496424, - from_version: '5.18.0', - to_version: '5.18.1' + from_version: pkg.version, + to_version: pkg.version + '.1' }] }, }, config_directory: { - config_dir_version: '1.0.0', - upgrade_package_version: '5.18.0', + config_dir_version: config_fs.config_dir_version, + upgrade_package_version: pkg.version, phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED, upgrade_history: { successful_upgrades: [{ timestamp: 1724687496424, completed_scripts: [], package_from_version: '5.17.0', - package_to_version: '5.18.0' + package_to_version: pkg.version + }] + } + } +}; + +const failed_expected_system_json_has_config_directory = { + [hostname]: { + current_version: pkg.version, + upgrade_history: { + successful_upgrades: [{ + timestamp: 1724687496424, + from_version: pkg.version, + to_version: pkg.version + }] + }, + }, + config_directory: { + config_dir_version: '0.0.0', + upgrade_package_version: '5.17.0', + phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED, + upgrade_history: { + successful_upgrades: [{ // mock successful upgrade + timestamp: 1724687496424, + completed_scripts: [], + package_from_version: '5.16.0', + package_to_version: '5.17.0' + }] + } + } +}; + +const newer_expected_system_json_has_config_directory = { + [hostname]: { + current_version: pkg.version + '.1', + upgrade_history: { + successful_upgrades: [{ + timestamp: 1724687496424, + from_version: pkg.version, + to_version: pkg.version + '.1' + }] + }, + }, + config_directory: { + config_dir_version: config_fs.config_dir_version + '.1', + upgrade_package_version: pkg.version + '.1', + phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED, + upgrade_history: { + successful_upgrades: [{ + timestamp: 1724687496424, + completed_scripts: [], + package_from_version: '5.17.0', + package_to_version: pkg.version + '.1' }] } } @@ -673,6 +726,101 @@ describe('nc upgrade manager - upgrade config directory', () => { assert_upgrade_finish_or_fail_data(system_json_data, expected_data); }); }); + + describe('nc upgrade manager - upgrade_config_dir', () => { + beforeEach(async () => { + await create_config_dir(config.NSFS_NC_DEFAULT_CONF_DIR); + await create_config_dir(config_root); + await create_redirect_file(config_fs, config_root); + }); + + afterEach(async () => { + await clean_config_dir(config_fs, config_root); + }, TEST_TIMEOUT); + + it('upgrade_config_dir - empty system_data', async () => { + const expected_version = pkg.version; + const hosts_list = [hostname]; + const expected_err_msg = 'system does not exist'; + await expect(nc_upgrade_manager.upgrade_config_dir(expected_version, hosts_list)) + .rejects.toThrow(expected_err_msg); + }); + + it('upgrade_config_dir - config_directory property is missing in system.json', async () => { + const system_data = _.cloneDeep(current_expected_system_json); + const expected_version = pkg.version; + const hosts_list = [hostname]; + await config_fs.create_system_config_file(JSON.stringify(system_data)); + const res = await nc_upgrade_manager.upgrade_config_dir(expected_version, hosts_list); + const system_json_data = await config_fs.get_system_config_file(); + const expected_data = { + ...system_data, + config_directory: { + phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED, + config_dir_version: config_fs.config_dir_version, + upgrade_package_version: expected_version, + upgrade_history: { + successful_upgrades: [res] + } + } + }; + assert_upgrade_finish_or_fail_data(system_json_data, expected_data); + }); + + it('upgrade_config_dir - system.json and package.json have the same version', async () => { + const system_data = _.cloneDeep(old_expected_system_json_has_config_directory); + const expected_version = pkg.version; + const hosts_list = [hostname]; + await config_fs.create_system_config_file(JSON.stringify(system_data)); + const expected_msg = { "message": "config_dir_version on system.json and config_fs.config_dir_version match, nothing to upgrade" }; + const res = await nc_upgrade_manager.upgrade_config_dir(expected_version, hosts_list); + expect(res).toStrictEqual(expected_msg); + }); + + it('upgrade_config_dir - config_dir_version in system.json is newer than the hosts current config_dir_version', async () => { + const system_data = _.cloneDeep(newer_expected_system_json_has_config_directory); + const expected_version = pkg.version; + const hosts_list = [hostname]; + await config_fs.create_system_config_file(JSON.stringify(system_data)); + const expected_err_msg = 'attempt to run old container version with newer server version'; + await expect(nc_upgrade_manager.upgrade_config_dir(expected_version, hosts_list)) + .rejects.toThrow(expected_err_msg); + }); + + it('upgrade_config_dir - host RPM is not upgraded to the expectced version', async () => { + const system_data = _.cloneDeep(old_expected_system_json); + const expected_version = pkg.version; + const hosts_list = [hostname]; + await config_fs.create_system_config_file(JSON.stringify(system_data)); + const expected_err_msg = `config dir upgrade can not be started until all expected hosts have the expected version=${pkg.version}, host=${hostname} host's current_version=${system_data[hostname].current_version}`; + await expect(nc_upgrade_manager.upgrade_config_dir(expected_version, hosts_list)) + .rejects.toThrow(expected_err_msg); + }); + + it('upgrade_config_dir - old successful upgrades is not empty', async () => { + const system_data = _.cloneDeep(failed_expected_system_json_has_config_directory); + const expected_version = pkg.version; + const hosts_list = [hostname]; + await config_fs.create_system_config_file(JSON.stringify(system_data)); + const res = await nc_upgrade_manager.upgrade_config_dir(expected_version, hosts_list); + const system_json_data = await config_fs.get_system_config_file(); + const expected_data = { + ...system_data, + config_directory: { + phase: CONFIG_DIR_PHASES.CONFIG_DIR_UNLOCKED, + config_dir_version: config_fs.config_dir_version, + upgrade_package_version: expected_version, + upgrade_history: { + successful_upgrades: [ + res, + ...failed_expected_system_json_has_config_directory.config_directory.upgrade_history.successful_upgrades, + ] + } + } + }; + assert_upgrade_finish_or_fail_data(system_json_data, expected_data); + }); + }); }); /**