def test_bad_request_no_event(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) body = { "type": "aviso", "data": { "request": { "class": "od", "date": "20190810", "destination": "MACI", "domain": "g", "expver": "1", "step": "1", "stream": "enfo", "time": "0", "target": "E1" }, "location": "s3://data.ecmwf.int/diss/foo/bar/20190810/xyz", }, "datacontenttype": "application/json", "id": "0c02fdc5-148c-43b5-b2fa-cb1f590369ff", "source": "/host/user", "specversion": "1.0", "time": "2020-03-02T13:34:40.245Z", } resp = requests.post(f"{frontend_url_api}/notification", json=body) assert resp.status_code == 400 assert resp.text message = json.loads(resp.text) assert message.get( "message") == 'Invalid notification, \'event\' could not be located'
def test_valid_mars_notification(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) body = { "type": "aviso", "data": { "event": "mars", "request": { "class": "od", "date": "20190810", "domain": "g", "expver": "1", "step": "1", "stream": "enfo", "time": "0", } }, "datacontenttype": "application/json", "id": "0c02fdc5-148c-43b5-b2fa-cb1f590369ff", "source": "/host/user", "specversion": "1.0", "time": "2020-03-02T13:34:40.245Z", } resp = requests.post(f"{frontend_url_api}/notification", json=body) assert resp.ok assert resp.status_code == 200 assert resp.text message = json.loads(resp.text) assert message.get("message") assert message.get("message") == "Notification successfully submitted"
def test_send_notification(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) body = { "type": "aviso", "data": { "event": "dissemination", "request": { "class": "od", "date": "20190810", "destination": "MACI", "domain": "g", "expver": "1", "step": "1", "stream": "enfo", "time": "0", "target": "E1" }, "location": "s3://data.ecmwf.int/diss/foo/bar/20190810/xyz", }, "datacontenttype": "application/json", "id": "0c02fdc5-148c-43b5-b2fa-cb1f590369ff", "source": "/host/user", "specversion": "1.0", "time": "2020-03-02T13:34:40.245Z", } resp = requests.post(f"http://localhost:30003/api/v1/notification", json=body) assert resp.ok assert resp.status_code == 200 assert resp.text message = json.loads(resp.text) assert message.get("message") assert message.get("message") == "Notification successfully submitted"
def test_bad_request_no_id(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) body = { "type": "aviso", "data": { "event": "dissemination", "request": { "class": "od", "date": "20190810", "destination": "MACI", "domain": "g", "expver": "1", "step": "1", "stream": "enfo", "time": "0", "target": "E1" }, "location": "s3://data.ecmwf.int/diss/foo/bar/20190810/xyz", }, "datacontenttype": "application/json", "source": "/host/user", "specversion": "1.0", "time": "2020-03-02T13:34:40.245Z", } resp = requests.post(f"{frontend_url_api}/notification", json=body) assert resp.status_code == 400 assert resp.text message = json.loads(resp.text) assert message.get("message") == 'Missing required attributes: {\'id\'}'
def test_bad_request_no_body(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) resp = requests.post(f"{frontend_url_api}/notification") assert resp.status_code == 400 assert resp.text message = json.loads(resp.text) assert message.get( "message") == "Invalid notification, Body cannot be empty"
def post_worker_init(self, worker): """ This method is called just after a worker has initialized the application. This method is a Gunicorn server hook. Gunicorn spawns this app over multiple workers as processes. This method ensures that there is only one timer and one transmitter thread running per worker. Without this hook a transmitter thread is created at application level but not at worker level and then at every request a timer will be created detached from the transmitter thread. This would result in no telemetry collected. """ logger.debug("Initialising a tlm collector per worker") self.init_timer()
def test_notify_ttl(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) body = { "type": "aviso", # aviso type indicates how to interpret the "data" payload "data": { # this is aviso specific "event": "dissemination", "request": { "target": "E1", "class": "od", "date": "20191112", "destination": "FOO", "domain": "g", "expver": "1", "step": "1", "stream": "enfo", "time": "0", }, "location": "xxx", # location on ceph or s3 "ttl": "1" }, "datacontenttype": "application/json", "id": "0c02fdc5-148c-43b5-b2fa-cb1f590369ff", # UUID random generated by client (maybe reused if request is the same) "source": "/host/user", # together with 'id', uniquely identifies a request "specversion": "1.0", "time": "2020-03-02T13:34:40.245Z", # optional, but recommended } resp = requests.post(f"{frontend_url_api}/notification", json=body) assert resp.ok assert resp.status_code == 200 message = resp.json() assert message.get("message") == "Notification successfully submitted" # now retrieve it ps = _parse_inline_params( "event=dissemination,target=E1,class=od,date=20191112,destination=FOO,domain=g,expver=1,step=1,stream=enfo,time=0" ) result = NotificationManager().value(ps, config=config.aviso) assert "xxx" in result # wait for it to expire time.sleep(3) # now test the value command ps = _parse_inline_params( "event=dissemination,target=E1,class=od,date=20191112,destination=FOO,domain=g,expver=1,step=1,stream=enfo,time=0" ) result = NotificationManager().value(ps, config=config.aviso) assert result is None
def test_bad_request_no_type(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) body = { "type": "aviso", "datacontenttype": "application/json", "id": "0c02fdc5-148c-43b5-b2fa-cb1f590369ff", "source": "/host/user", "specversion": "1.0", "time": "2020-03-02T13:34:40.245Z", } resp = requests.post(f"{frontend_url_api}/notification", json=body) assert resp.status_code == 400 assert resp.text message = json.loads(resp.text) assert message.get( "message") == 'Invalid notification, \'data\' could not be located'
def test_valid_dissemination_notification(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) body = { "type": "aviso", # aviso type indicates how to interpret the "data" payload "data": { # this is aviso specific "event": "dissemination", "request": { "target": "E1", "class": "od", "date": "20190810", "destination": "MACI", "domain": "g", "expver": "1", "step": "1", "stream": "enfo", "time": "0", }, "location": "s3://data.ecmwf.int/diss/foo/bar/20190810/xyz", # location on ceph or s3 }, "datacontenttype": "application/json", "id": "0c02fdc5-148c-43b5-b2fa-cb1f590369ff", # UUID random generated by client (maybe reused if request is the same) "source": "/host/user", # together with 'id', uniquely identifies a request "specversion": "1.0", "time": "2020-03-02T13:34:40.245Z", # optional, but recommended } resp = requests.post(f"{frontend_url_api}/notification", json=body) assert resp.ok assert resp.status_code == 200 assert resp.text message = json.loads(resp.text) assert message.get("message") assert message.get("message") == "Notification successfully submitted"
def notify(): logger.debug("New notification received") # we expect only JSON body body = request.json if body is None: return bad_request("Invalid notification, Body cannot be empty") logger.debug(body) try: # parse the body as cloud event notification = self._parse_cloud_event(request) logger.info(f"New event received: {notification}") # send the notification and time it self.timed_notify(notification, config=self.config.aviso) except InvalidInputError as e: return bad_request(e) logger.debug("Notification successfully submitted") return ok("Notification successfully submitted")
def test_homepage(): time.sleep(1) logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) assert requests.get(frontend_url_home).status_code == 500
def test_method_not_allowed_405(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) resp = requests.get(f"{frontend_url_api}/notification") assert resp.status_code == 405
def test_not_found_404(): logger.debug( os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]) resp = requests.post(f"{frontend_url_api}/notFound") assert resp.status_code == 404