Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify args creation #377

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions modules/aws/sonar-upgrader/python_upgrader/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
import argparse
import json
from upgrade.main import main, fill_args_defaults, set_global_variables
from upgrade.main import main, set_global_variables, get_argument_parser
from upgrade.upgrade_status_service import OverallUpgradeStatus
from upgrade.upgrade_exception import UpgradeException

Expand Down Expand Up @@ -51,19 +51,11 @@

@pytest.fixture
def setup_for_each_test(mocker):
default_args = argparse.Namespace(
agentless_gws=[],
dsf_hubs=[],
target_version="4.13",
connection_timeout=None,
test_connection=None,
run_preflight_validations=None,
run_upgrade=None,
run_postflight_validations=None,
stop_on_failure=None,
tarball_location=None,
)
fill_args_defaults(default_args)
args = get_argument_parser().parse_args([
'--agentless_gws', '',
'--dsf_hubs', '',
'--target_version', '4.13',
])
set_global_variables(100)

# mock UpgradeStatusService class functions
Expand All @@ -83,7 +75,7 @@ def setup_for_each_test(mocker):

test_connection_mock = mocker.patch('upgrade.main.test_connection')

yield default_args, upgrade_status_service_mock, test_connection_mock
yield args, upgrade_status_service_mock, test_connection_mock


def test_main_all_flags_disabled(setup_for_each_test, mocker):
Expand Down
37 changes: 11 additions & 26 deletions modules/aws/sonar-upgrader/python_upgrader/upgrade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,51 +898,37 @@ def verify_successful_run_by_configuration_options(args, upgrade_status_service)
# Preparation functions


def parse_args():
def get_argument_parser():
parser = argparse.ArgumentParser(description="Upgrade script for DSF Hub and Agentless Gateway")
parser.add_argument("--agentless_gws", required=True, help="JSON-encoded Agentless Gateway list")
parser.add_argument("--dsf_hubs", required=True, help="JSON-encoded DSF Hub list")
parser.add_argument("--target_version", required=True, help="Target version to upgrade")
parser.add_argument("--connection_timeout",
default=90,
help="Client connection timeout in seconds used for the SSH connections between the "
"installer machine and the DSF nodes being upgraded. Its purpose is to ensure a "
"uniform behavior across different platforms. Note that the SSH server in the DSF nodes "
"may have its own timeout configurations which may override this setting.")
parser.add_argument("--test_connection", type=str_to_bool,
default=True,
help="Whether to test the SSH connection to all DSF nodes being upgraded "
"before starting the upgrade")
parser.add_argument("--run_preflight_validations", type=str_to_bool,
default=True,
help="Whether to run preflight validations")
parser.add_argument("--run_upgrade", type=str_to_bool, help="Whether to run the upgrade")
parser.add_argument("--run_upgrade", type=str_to_bool, default=True, help="Whether to run the upgrade")
parser.add_argument("--run_postflight_validations", type=str_to_bool,
default=True,
help="Whether to run postflight validations")
# parser.add_argument("--clean_old_deployments", type=str_to_bool, help="Whether to clean old deployments")
parser.add_argument("--clean_old_deployments", type=str_to_bool, default=False, help="Whether to clean old deployments")
parser.add_argument("--stop_on_failure", type=str_to_bool,
default=True,
help="Whether to stop or continue to upgrade the next DSF nodes in case of failure "
"on a DSF node")
parser.add_argument("--tarball_location",
default='{"s3_bucket": "1ef8de27-ed95-40ff-8c08-7969fc1b7901", "s3_region": "us-east-1"}',
help="JSON-encoded S3 bucket location of the DSF installation software")
args = parser.parse_args()
return args


def fill_args_defaults(args):
if args.connection_timeout is None:
args.connection_timeout = 90
if args.test_connection is None:
args.test_connection = True
if args.run_preflight_validations is None:
args.run_preflight_validations = True
if args.run_upgrade is None:
args.run_upgrade = True
if args.run_postflight_validations is None:
args.run_postflight_validations = True
if args.stop_on_failure is None:
args.stop_on_failure = True
if args.tarball_location is None:
args.tarball_location = '{"s3_bucket": "1ef8de27-ed95-40ff-8c08-7969fc1b7901", "s3_region": "us-east-1"}'

args.clean_old_deployments = False
return parser


def set_global_variables(connection_timeout):
Expand All @@ -951,8 +937,7 @@ def set_global_variables(connection_timeout):


if __name__ == "__main__":
args = parse_args()
fill_args_defaults(args)
args = get_argument_parser().parse_args()
set_global_variables(args.connection_timeout)

main(args)