diff --git a/src/opticalattackdetector/.gitlab-ci.yml b/src/opticalattackdetector/.gitlab-ci.yml index 2a89b5c54f49434d9703f833e2c72486c2394f9f..9c064f118ed435ff1c2f7cad92c2f717b87969d8 100644 --- a/src/opticalattackdetector/.gitlab-ci.yml +++ b/src/opticalattackdetector/.gitlab-ci.yml @@ -72,7 +72,12 @@ unit_test opticalattackdetector: - sleep 5 - docker ps -a - docker logs $IMAGE_NAME - - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=DEBUG --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/home/${IMAGE_NAME}/results/${IMAGE_NAME}_report.xml; coverage xml -o /home/${IMAGE_NAME}/results/${IMAGE_NAME}_coverage.xml; coverage report --include='${IMAGE_NAME}/*' --show-missing" + - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=DEBUG --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/home/${IMAGE_NAME}/results/${IMAGE_NAME}_report.xml" + - docker logs redis + - docker logs dbscanserving + - docker logs $IMAGE_NAME + - docker exec -i $IMAGE_NAME bash -c "coverage xml -o /home/${IMAGE_NAME}/results/${IMAGE_NAME}_coverage.xml" + - docker exec -i $IMAGE_NAME bash -c "coverage report --include='${IMAGE_NAME}/*' --show-missing" coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/' after_script: - docker rm -f $IMAGE_NAME diff --git a/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py b/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py index 11016037ee36f3b63726f7cc9fe398609c4f451b..d8e8b649c2b1cccbe1071c6f9ff66504f757f1fd 100644 --- a/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py +++ b/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py @@ -221,7 +221,7 @@ class OpticalAttackDetectorServiceServicerImpl(OpticalAttackDetectorServiceServi monitoring_client.IncludeKpi(kpi) # if -1 in response.cluster_indices: # attack detected - if kpi.kpi_value.int32Val == -1: + if kpi.kpi_value.int32Val == 1: attack = AttackDescription() attack.cs_id.uuid = request.service_id.service_uuid.uuid with HISTOGRAM_DURATION.labels(step="mitigation", **METRIC_LABELS).time(): diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index 6c7579238a157a0f00e9eb0c9df53a04c1774ff6..0112ec75cac40e527390ed1991db7092461bdc1a 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -15,13 +15,13 @@ import logging import uuid import queue +import time from unittest.mock import patch import pytest from common.proto import dbscanserving_pb2 as dbscan from common.proto.optical_attack_detector_pb2 import DetectionRequest -from common.tests.MockServicerImpl_Monitoring import MockServicerImpl_Monitoring from opticalattackdetector.client.OpticalAttackDetectorClient import \ OpticalAttackDetectorClient @@ -37,6 +37,7 @@ LOGGER = logging.getLogger(__name__) def optical_attack_detector_service(): _service = OpticalAttackDetectorService() _service.start() + time.sleep(2) yield _service _service.stop() @@ -44,7 +45,7 @@ def optical_attack_detector_service(): @pytest.fixture(scope="session") def optical_attack_detector_client(optical_attack_detector_service: OpticalAttackDetectorService): _client = OpticalAttackDetectorClient( - host=optical_attack_detector_service.bind_address, + host="127.0.0.1", port=optical_attack_detector_service.bind_port, ) yield _client @@ -56,26 +57,20 @@ def test_detect_attack( optical_attack_detector_client: OpticalAttackDetectorClient, ): message = dbscan.DetectionResponse() - message.cluster_indices.extend([0, 1, -1, -1, -1]) + message.cluster_indices.extend([0, 1] * 5 + [-1] * 10) - monitoring_mock = MockServicerImpl_Monitoring(queue_samples = queue.Queue()) with patch( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client" ) as mitigator, patch( - "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.monitoring_client", - monitoring_mock, + "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.monitoring_client.IncludeKpi", ) as monitoring, patch( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client.Detect", - # TODO: return dumb object with "cluster_indices" attribute - # idea: create new response object return_value=message, ) as dbscanserving: - for _ in range(10): - request: DetectionRequest = DetectionRequest() - request.service_id.context_id.context_uuid.uuid = str(uuid.uuid4()) - request.service_id.service_uuid.uuid = str(uuid.uuid4()) - request.kpi_id.kpi_id.uuid = "1" - optical_attack_detector_client.DetectAttack(request) - dbscanserving.assert_called_once() - monitoring.IncludeKpi.assert_called_once() - mitigator.NotifyAttack.assert_called() + request: DetectionRequest = DetectionRequest() + request.service_id.context_id.context_uuid.uuid = str(uuid.uuid4()) + request.service_id.service_uuid.uuid = str(uuid.uuid4()) + request.kpi_id.kpi_id.uuid = "1" + optical_attack_detector_client.DetectAttack(request) + dbscanserving.assert_called() + monitoring.assert_called() diff --git a/src/webui/requirements.in b/src/webui/requirements.in index 2c638a223c6b45eecaf7479bdf249954e6bc8543..fd67c068858115260c962c5f139713107f00f84d 100644 --- a/src/webui/requirements.in +++ b/src/webui/requirements.in @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -Flask==2.1.3 -Flask-WTF==1.0.0 -flask-healthz==0.0.3 -flask-unittest==0.1.2 +Flask<3 +Flask-WTF<2 +flask-healthz<2 +flask-unittest==0.1.3 lorem-text==2.1 werkzeug==2.3.7