def test_timeout_update_available(self): """ Make sure that we get a status update if the selected updater indicates that an update is available. """ mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(StableUpdater, "__init__") self.mox.StubOutWithMock(StableUpdater, "check") self.mox.StubOutWithMock(StableUpdater, "get_update_version") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(Client, "change_status") StableUpdater.__init__(self.__repo, self.__remote_url) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() notifyer = UpdateNotifyer(self.__repo, self.__remote_url) notifyer.start() notifyer.timeout()
def test_duplicated_command_output(self): """ Make sure that StatusProvider only updates the status if the command yields a different result than it did previously. """ self.__setup_parser() mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(SafeConfigParser, "has_section") self.mox.StubOutWithMock(SafeConfigParser, "get") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(subprocess.Popen, "__init__") self.mox.StubOutWithMock(subprocess.Popen, "communicate") self.mox.StubOutWithMock(Client, "change_status") SafeConfigParser.has_section("status").AndReturn(True) SafeConfigParser.get("status", "command").AndReturn("foobar") SafeConfigParser.get("status", "interval").AndReturn(3) threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("result", "")) Client.change_status("result") threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("result", "")) # This time we expect no status change, since this is the same result as # returned previously.. #Client.change_status("result") threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("another result", "")) Client.change_status("another result") self.mox.ReplayAll() provider = StatusProvider() provider.start() # We'll have to emulate a timeout here.. provider.timeout() provider.timeout() provider.timeout()
def test_toggle_update_availability(self): """ Make sure that the update indicaiton is reset if an update all of a sudden is not available. """ mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(StableUpdater, "__init__") self.mox.StubOutWithMock(StableUpdater, "check") self.mox.StubOutWithMock(StableUpdater, "get_update_version") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(Client, "change_status") StableUpdater.__init__(self.__repo, self.__remote_url) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(False) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() notifyer = UpdateNotifyer(self.__repo, self.__remote_url) notifyer.start() notifyer.timeout() notifyer.timeout() notifyer.timeout() notifyer.timeout()
def timeout(self): """ Called upon by the Timer when self.__interval has elapsed. """ had_update = self.has_update self.has_update = self.__updater.check() if self.has_update and not had_update: cli = Client() new_version = self.__updater.get_update_version() if new_version: cli.change_status(u"update available: %s" % new_version[:7]) self.start()
def timeout(self): """ This method is responsible for executing the configured command. """ try: subproc = subprocess.Popen(self.__command, stdout = subprocess.PIPE) stdout = subproc.communicate()[0] except OSError: logger = logging.getLogger() logger.error("%s: error executing status command.") return if self.__previous_result != stdout: client = Client() client.change_status(stdout) self.start() self.__previous_result = stdout
def do_command(self, command, args=None): """ Overridden in order to provide the restricted command set feature. """ if "bye" == command: client = Client() client.change_status(u"terminating session", False) client.disconnect() return u"terminating" cmd = [command] if args: cmd.extend(args) try: body = self.make_syscall(cmd) except OSError as ex: body = "%s: %s (%d)" % (type(ex), ex.strerror, ex.errno) return body
def test_existing_status_config_section(self): """ If we have an existing, properly configured status section, StatusProvider will, after having been started, execute the configured command after the configured timeout has elapsed, by starting a threading.Timer. """ self.__setup_parser() mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(SafeConfigParser, "has_section") self.mox.StubOutWithMock(SafeConfigParser, "get") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(subprocess.Popen, "__init__") self.mox.StubOutWithMock(subprocess.Popen, "communicate") self.mox.StubOutWithMock(Client, "change_status") SafeConfigParser.has_section("status").AndReturn(True) SafeConfigParser.get("status", "command").AndReturn("foobar") SafeConfigParser.get("status", "interval").AndReturn(3) threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE) subprocess.Popen.communicate().AndReturn(("result", "")) Client.change_status("result") threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() provider = StatusProvider() provider.start() # We'll have to emulate a timeout here.. provider.timeout()
def test_timeout_update_still_available(self): """ Make sure that we do not spam with software update notices. Once there has been an update notification, updates found thereafter should not be indicated. """ mock_timer = self.mox.CreateMockAnything() self.mox.StubOutWithMock(StableUpdater, "__init__") self.mox.StubOutWithMock(StableUpdater, "check") self.mox.StubOutWithMock(StableUpdater, "get_update_version") self.mox.StubOutWithMock(threading, "Timer") self.mox.StubOutWithMock(threading.Thread, "start") self.mox.StubOutWithMock(Client, "change_status") StableUpdater.__init__(self.__repo, self.__remote_url) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) Client.change_status(mox.IgnoreArg()) threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() StableUpdater.check().AndReturn(True) StableUpdater.get_update_version().AndReturn("1.0") threading.Timer(3600, mox.IgnoreArg()).AndReturn(mock_timer) mock_timer.start() self.mox.ReplayAll() notifyer = UpdateNotifyer(self.__repo, self.__remote_url) notifyer.start() notifyer.timeout() notifyer.timeout()