Skip to content
Snippets Groups Projects
Commit 14428242 authored by Carlos Natalino Da Silva's avatar Carlos Natalino Da Silva
Browse files

Improving tests for the OAD.

parent 444bee86
No related branches found
No related tags found
2 merge requests!142Release TeraFlowSDN 2.1,!138Fixing the tests of the optical cybersecurity components
...@@ -54,7 +54,7 @@ unit_test opticalattackdetector: ...@@ -54,7 +54,7 @@ unit_test opticalattackdetector:
script: script:
- export REDIS_PASSWORD=$(uuidgen) - export REDIS_PASSWORD=$(uuidgen)
- docker pull "redis:7.0-alpine" - docker pull "redis:7.0-alpine"
- docker run --name redis -d --network=teraflowbridge -p 6379:6379 -e REDIS_PASSWORD=${REDIS_PASSWORD} --rm redis:7.0-alpine redis-server --requirepass ${REDIS_PASSWORD} - docker run --name redis -d --network=teraflowbridge -p 16379:6379 -e REDIS_PASSWORD=${REDIS_PASSWORD} --rm redis:7.0-alpine redis-server --requirepass ${REDIS_PASSWORD}
- docker logs redis - docker logs redis
- REDIS_ADDRESS=$(docker inspect redis --format "{{.NetworkSettings.Networks.teraflowbridge.IPAddress}}") - REDIS_ADDRESS=$(docker inspect redis --format "{{.NetworkSettings.Networks.teraflowbridge.IPAddress}}")
- docker pull "$CI_REGISTRY_IMAGE/dbscanserving:$IMAGE_TAG" - docker pull "$CI_REGISTRY_IMAGE/dbscanserving:$IMAGE_TAG"
......
...@@ -13,13 +13,15 @@ ...@@ -13,13 +13,15 @@
# limitations under the License. # limitations under the License.
import logging import logging
import os import uuid
import queue
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
from common.proto import dbscanserving_pb2 as dbscan from common.proto import dbscanserving_pb2 as dbscan
from common.proto.optical_attack_detector_pb2 import DetectionRequest from common.proto.optical_attack_detector_pb2 import DetectionRequest
from common.tests.MockServicerImpl_Monitoring import MockServicerImpl_Monitoring
from opticalattackdetector.client.OpticalAttackDetectorClient import \ from opticalattackdetector.client.OpticalAttackDetectorClient import \
OpticalAttackDetectorClient OpticalAttackDetectorClient
...@@ -48,25 +50,30 @@ def optical_attack_detector_client(optical_attack_detector_service): ...@@ -48,25 +50,30 @@ def optical_attack_detector_client(optical_attack_detector_service):
def test_detect_attack( def test_detect_attack(
optical_attack_detector_service: OpticalAttackDetectorService,
optical_attack_detector_client: OpticalAttackDetectorClient, optical_attack_detector_client: OpticalAttackDetectorClient,
): ):
message = dbscan.DetectionResponse() message = dbscan.DetectionResponse()
message.cluster_indices.extend([0, 1, -1, -1, -1]) message.cluster_indices.extend([0, 1, -1, -1, -1])
monitoring_mock = MockServicerImpl_Monitoring(queue_samples = queue.Queue())
with patch( with patch(
"opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client" "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client"
) as mitigator, patch( ) as mitigator, patch(
"opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.monitoring_client" "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.monitoring_client",
monitoring_mock,
) as monitoring, patch( ) as monitoring, patch(
"opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client.Detect", "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client.Detect",
# TODO: return dumb object with "cluster_indices" attribute # TODO: return dumb object with "cluster_indices" attribute
# idea: create new response object # idea: create new response object
return_value=message, return_value=message,
) as dbscanserving: ) as dbscanserving:
request: DetectionRequest = DetectionRequest() for _ in range(10):
request.service_id.context_id.context_uuid.uuid = "" request: DetectionRequest = DetectionRequest()
request.service_id.service_uuid.uuid = str("") request.service_id.context_id.context_uuid.uuid = str(uuid.uuid4())
request.kpi_id.kpi_id.uuid = "" request.service_id.service_uuid.uuid = str(uuid.uuid4())
optical_attack_detector_client.DetectAttack(request) request.kpi_id.kpi_id.uuid = "1"
mitigator.NotifyAttack.assert_called_once() optical_attack_detector_client.DetectAttack(request)
monitoring.IncludeKpi.assert_called_once() dbscanserving.assert_called_once()
dbscanserving.assert_called_once() monitoring.IncludeKpi.assert_called_once()
mitigator.NotifyAttack.assert_called()
...@@ -17,7 +17,6 @@ import logging ...@@ -17,7 +17,6 @@ import logging
import traceback import traceback
from grpclib.client import Channel from grpclib.client import Channel
from prometheus_client import Counter
from common.proto.asyncio.optical_attack_detector_grpc import \ from common.proto.asyncio.optical_attack_detector_grpc import \
OpticalAttackDetectorServiceStub OpticalAttackDetectorServiceStub
......
...@@ -21,8 +21,7 @@ import threading ...@@ -21,8 +21,7 @@ import threading
import redis import redis
from kubernetes import client, config from kubernetes import client, config
from common.Constants import ServiceNameEnum from common.Settings import get_setting, wait_for_environment_variables
from common.Settings import get_service_host, get_setting, wait_for_environment_variables
# Configs can be set in Configuration class directly or using helper utility # Configs can be set in Configuration class directly or using helper utility
namespace = get_setting("TFS_K8S_NAMESPACE", default="tfs") namespace = get_setting("TFS_K8S_NAMESPACE", default="tfs")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment