From: Michael Fritch Date: Mon, 24 May 2021 13:52:53 +0000 (-0600) Subject: cephadm: always require a CephadmContext X-Git-Tag: v16.2.5~72^2~14 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=4cddd90f9376fd9e60a71faba9a5e17e874cd911;p=ceph.git cephadm: always require a CephadmContext it's not possible for the CephadmContext to be a NoneType Signed-off-by: Michael Fritch (cherry picked from commit 054f555afb6c0081f7b37a23932ff51d901092c7) --- diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index a7b664cd631e..fb3723ee7570 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -8131,18 +8131,15 @@ def _parse_args(av): return args -def cephadm_init_ctx(args: List[str]) -> Optional[CephadmContext]: - +def cephadm_init_ctx(args: List[str]) -> CephadmContext: ctx = CephadmContext() ctx.set_args(_parse_args(args)) return ctx -def cephadm_init(args: List[str]) -> Optional[CephadmContext]: - +def cephadm_init(args: List[str]) -> CephadmContext: global logger ctx = cephadm_init_ctx(args) - assert ctx is not None # Logger configuration if not os.path.exists(LOG_DIR): @@ -8167,10 +8164,6 @@ def cephadm_init(args: List[str]) -> Optional[CephadmContext]: if handler.name == 'console': handler.setLevel(logging.DEBUG) - if not ctx.has_function(): - sys.stderr.write('No command specified; pass -h or --help for usage\n') - return None - return ctx @@ -8185,7 +8178,8 @@ def main(): av = sys.argv[1:] ctx = cephadm_init(av) - if not ctx: # error, exit + if not ctx.has_function(): + sys.stderr.write('No command specified; pass -h or --help for usage\n') sys.exit(1) try: diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index ae857955cabd..ec804c2ff423 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -410,19 +410,17 @@ default proto ra metric 100 # test normal valid login with url, username and password specified call_throws.return_value = '', '', 0 - ctx: Optional[cd.CephadmContext] = cd.cephadm_init_ctx( + ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['registry-login', '--registry-url', 'sample-url', '--registry-username', 'sample-user', '--registry-password', 'sample-pass']) ctx.container_engine = self.mock_docker() - assert ctx retval = cd.command_registry_login(ctx) assert retval == 0 # test bad login attempt with invalid arguments given - ctx: Optional[cd.CephadmContext] = cd.cephadm_init_ctx( + ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['registry-login', '--registry-url', 'bad-args-url']) - assert ctx with pytest.raises(Exception) as e: assert cd.command_registry_login(ctx) assert str(e.value) == ('Invalid custom registry arguments received. To login to a custom registry include ' @@ -430,18 +428,16 @@ default proto ra metric 100 # test normal valid login with json file get_parm.return_value = {"url": "sample-url", "username": "sample-username", "password": "sample-password"} - ctx: Optional[cd.CephadmContext] = cd.cephadm_init_ctx( + ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['registry-login', '--registry-json', 'sample-json']) ctx.container_engine = self.mock_docker() - assert ctx retval = cd.command_registry_login(ctx) assert retval == 0 # test bad login attempt with bad json file get_parm.return_value = {"bad-json": "bad-json"} - ctx: Optional[cd.CephadmContext] = cd.cephadm_init_ctx( + ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['registry-login', '--registry-json', 'sample-json']) - assert ctx with pytest.raises(Exception) as e: assert cd.command_registry_login(ctx) assert str(e.value) == ("json provided for custom registry login did not include all necessary fields. " @@ -454,11 +450,10 @@ default proto ra metric 100 # test login attempt with valid arguments where login command fails call_throws.side_effect = Exception - ctx: Optional[cd.CephadmContext] = cd.cephadm_init_ctx( + ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['registry-login', '--registry-url', 'sample-url', '--registry-username', 'sample-user', '--registry-password', 'sample-pass']) - assert ctx with pytest.raises(Exception) as e: cd.command_registry_login(ctx) assert str(e.value) == "Failed to login to custom registry @ sample-url as sample-user with given password"