def test_device_status_success_response(self): responses.add( responses.GET, TEST_SUPERVISOR_DEVICE_STATUS_URL, status=200, json={'appState': 'applied'} ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) assert bs.get_device_status('appState') == 'applied'
def test_shutdown_gateway_success_response(self): responses.add( responses.POST, TEST_SUPERVISOR_SHUTDOWN_URL, status=200, json={"Data": "OK", "Error": ""} ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) resp = bs.shutdown() assert resp['Data'] == 'OK'
def test_device_status_error_on_connection_timeout(self): responses.add( responses.GET, TEST_SUPERVISOR_DEVICE_STATUS_URL, body=ConnectTimeout('Timout trying to make connection') ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) with self.assertRaises(RuntimeError) as exp: bs.get_device_status('appState') assert str(exp.exception).startswith("Device status request failed")
def test_shutdown_gateway_error_on_connection_timeout(self): responses.add( responses.POST, TEST_SUPERVISOR_SHUTDOWN_URL, body=ConnectTimeout('Timout trying to make connection') ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) with self.assertRaises(Exception) as exp: bs.shutdown() assert str(exp.exception) == 'supervisor API not accessible'
def test_shutdown_gateway_error_on_connection_error(self): responses.add( responses.POST, TEST_SUPERVISOR_SHUTDOWN_URL, body=ConnectionError('Unable to connect') ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) with self.assertRaises(Exception) as exp: bs.shutdown() assert str(exp.exception) == "supervisor API not accessible"
def test_device_status_empty_response(self): responses.add( responses.GET, TEST_SUPERVISOR_DEVICE_STATUS_URL, body='', status=200 ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) with self.assertRaises(RuntimeError) as exp: bs.get_device_status('appState') assert str(exp.exception).startswith("Supervisor API did not return valid json response")
def test_shutdown_gateway_empty_response(self): responses.add( responses.POST, TEST_SUPERVISOR_SHUTDOWN_URL, body='', status=200 ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) with self.assertRaises(Exception) as exp: bs.shutdown() assert str(exp.exception) == 'shutdown failed due to supervisor API issue'
def use_verified_json(self, diagnostics_report: DiagnosticsReport) -> None: if SHUTDOWN_GATEWAY_KEY not in self.verified_json: diagnostics_report.record_failure(self.NO_SHUTDOWN_GATEWAY_KEY_MSG, self) return try: balena_supervisor = BalenaSupervisor.new_from_env() shutdown_response = balena_supervisor.shutdown() diagnostics_report.record_result(shutdown_response, self) except Exception as e: diagnostics_report.record_failure(e, self)
def perform_test(self, diagnostics_report: DiagnosticsReport) -> None: try: balena_supervisor = BalenaSupervisor.new_from_env() device_status = balena_supervisor.get_device_status('appState') if device_status == 'applied': diagnostics_report.record_result("device_ready", self) else: diagnostics_report.record_failure( f"appState is {device_status}", self) except Exception as e: diagnostics_report.record_failure(e, self)
def test_creation_from_env(self): bs = BalenaSupervisor.new_from_env() assert bs.supervisor_address == TEST_SUPERVISOR_ADDRESS assert bs.supervisor_api_key == TEST_SUPERVISOR_API_KEY assert bs.headers == {'Content-type': 'application/json'}