def test_register_system_fail_interactive(self): # Check the function tries to register multiple times without # critical log. tool_opts.credentials_thru_cli = False subscription.register_system() self.assertEqual(utils.run_subprocess.called, 3) self.assertEqual(len(subscription.logging.getLogger.critical_msgs), 0)
def test_register_system_fail_non_interactive(self, tool_opts, monkeypatch, caplog): """Check the critical severity is logged when the credentials are given on the cmdline but registration fails.""" monkeypatch.setattr(subscription, "MAX_NUM_OF_ATTEMPTS_TO_SUBSCRIBE", 1) monkeypatch.setattr(subscription, "sleep", mock.Mock()) fake_spawn = mock.Mock() fake_spawn.before.decode = mock.Mock(return_value="nope") fake_spawn.exitstatus = 1 monkeypatch.setattr(utils, "PexpectSizedWindowSpawn", fake_spawn) tool_opts.username = "******" tool_opts.password = "******" tool_opts.credentials_thru_cli = True with pytest.raises(SystemExit): subscription.register_system() assert caplog.records[-1].levelname == "CRITICAL"
def test_register_system_fail_interactive(self, tool_opts, monkeypatch, caplog): """Test that the three attempts work: fail to register two times and succeed the third time.""" tool_opts.credentials_thru_cli = False monkeypatch.setattr(subscription, "sleep", mock.Mock()) fake_from_tool_opts = mock.Mock( return_value=subscription.RegistrationCommand(username="******", password="******")) monkeypatch.setattr(subscription.RegistrationCommand, "from_tool_opts", fake_from_tool_opts) # We may want to move this to the toplevel if we have other needs to # test pexpect driven code. If we do so, though, we would want to # make it a bit more generic: # * Be able to set iterations before success # * Allow setting both exitstatus and signalstatus # * Allow setting stdout output # * Probably make the output and status settable per invocation rather # than using a count class FakeProcess(mock.Mock): called_count = 0 @property def exitstatus(self): self.called_count += 1 return self.called_count % 3 fake_process = FakeProcess() fake_process.before.decode = mock.Mock(side_effect=("nope", "nope", "Success")) fake_spawn = mock.Mock(return_value=fake_process) monkeypatch.setattr(utils, "PexpectSizedWindowSpawn", fake_spawn) subscription.register_system() assert len(fake_spawn.call_args_list) == 3 assert "CRITICAL" not in [rec.levelname for rec in caplog.records]
def test_register_system_fail_with_keyboardinterrupt(self, monkeypatch): monkeypatch.setattr(subscription.RegistrationCommand, "from_tool_opts", RegistrationCmdFromTooloptsMocked()) with pytest.raises(KeyboardInterrupt) as err: subscription.register_system()