def test_reads_default_values_for_empty_dict(self): self.assertEqual( update_result.Result( success=False, error='', timestamp=datetime.datetime.utcfromtimestamp(0), ), update_result.read(io.StringIO('{}')))
def test_returns_success_when_no_process_is_running_and_last_run_was_ok( self, mock_read_update_result, mock_check_output): mock_check_output.return_value = """ USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 224928 8612 ? Ss Apr03 0:01 /sbin/dummy-a root 51 0.0 0.0 103152 21264 ? Ss Apr03 0:00 /lib/dummy-b """.lstrip().encode('utf-8') mock_read_update_result.return_value = update_result.Result( success=True, error=None, timestamp='2021-02-10T085735Z') status_actual, error_actual = update.get_current_state() self.assertEqual(update.Status.DONE, status_actual) self.assertIsNone(error_actual)
def test_returns_latest_if_it_is_in_the_future(self, mock_now, mock_glob): """Due to NTP updates, the latest result might be from a later time.""" mock_glob.return_value = [ self.make_mock_file( '2021-01-01T000000Z-update-result.json', """ { "success": true, "error": "", "timestamp": "2021-01-01T000000Z" } """), self.make_mock_file( '2021-01-01T000300Z-update-result.json', """ { "success": true, "error": "", "timestamp": "2021-01-01T000300Z" } """), self.make_mock_file( '2021-01-01T000600Z-update-result.json', """ { "success": true, "error": "", "timestamp": "2021-01-01T000600Z" } """) ] # Set the current time to one minute *behind* the latest result # timestamp to simulate a time adjustment after, e.g., an NTP update. mock_now.return_value = datetime.datetime(year=2021, month=1, day=1, hour=0, minute=5, second=0, tzinfo=datetime.timezone.utc) self.assertEqual( update_result.Result(success=True, error='', timestamp=datetime.datetime( year=2021, month=1, day=1, hour=0, minute=6, second=0, tzinfo=datetime.timezone.utc)), update_result_reader.read())
def test_ignores_update_result_if_update_is_running( self, mock_read_update_result, mock_check_output): """If update is running, last update result does not matter.""" mock_check_output.return_value = """ USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 224928 8612 ? Ss Apr03 0:01 /sbin/dummy-a root 51 0.0 0.0 103152 21264 ? Ss Apr03 0:00 /opt/tinypilot-privileged/update """.lstrip().encode('utf-8') # get_current_state should ignore this result because an update process # is currently running, which takes priority over the previous result. mock_read_update_result.return_value = update_result.Result( success=True, error=None, timestamp='2021-02-10T085735Z') status_actual, error_actual = update.get_current_state() self.assertEqual(update.Status.IN_PROGRESS, status_actual) self.assertIsNone(error_actual)
def test_returns_latest_if_it_is_within_last_eight_minutes( self, mock_now, mock_glob): mock_glob.return_value = [ self.make_mock_file( '2020-12-31T000000Z-update-result.json', """ { "success": true, "error": "", "timestamp": "2020-12-31T000000Z" } """), self.make_mock_file( '2021-01-01T000000Z-update-result.json', """ { "success": true, "error": "", "timestamp": "2021-01-01T000000Z" } """), self.make_mock_file( '2021-01-01T000300Z-update-result.json', """ { "success": true, "error": "", "timestamp": "2021-01-01T000300Z" } """) ] mock_now.return_value = datetime.datetime(year=2021, month=1, day=1, hour=0, minute=5, second=0, tzinfo=datetime.timezone.utc) self.assertEqual( update_result.Result(success=True, error='', timestamp=datetime.datetime( year=2021, month=1, day=1, hour=0, minute=3, second=0, tzinfo=datetime.timezone.utc)), update_result_reader.read())
def test_writes_error_result_accurately(self): mock_file = io.StringIO() update_result.write( update_result.Result( success=False, error='dummy update error', timestamp=datetime.datetime(2021, 2, 10, 8, 57, 35, tzinfo=datetime.timezone.utc), ), mock_file) self.assertEqual(('{"success": false, "error": "dummy update error", ' '"timestamp": "2021-02-10T085735Z"}'), mock_file.getvalue())
def test_reads_correct_values_for_successful_result(self): self.assertEqual( update_result.Result( success=True, error='', timestamp=datetime.datetime(2021, 2, 10, 8, 57, 35, tzinfo=datetime.timezone.utc), ), update_result.read( io.StringIO(""" { "success": true, "error": "", "timestamp": "2021-02-10T085735Z" } """)))