Skip to content

Commit

Permalink
Adding unit tests for MetaDefender service
Browse files Browse the repository at this point in the history
  • Loading branch information
cccs-kevin committed Aug 12, 2021
1 parent 0ba8915 commit b42b208
Show file tree
Hide file tree
Showing 6 changed files with 648 additions and 5 deletions.
12 changes: 7 additions & 5 deletions metadefender.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def execute(self, request: ServiceRequest) -> None:
f"{self.nodes[self.current_node]['newest_dat']}")

# Compare queue time of current node with new random node after a minimum run time on current node
elapsed_time = self.start_time - time.time()
elapsed_time = time.time() - self.start_time
if elapsed_time >= self.config.get("max_node_time"):
self.new_node(force=True)
elif elapsed_time >= self.config.get("min_node_time"):
Expand Down Expand Up @@ -380,16 +380,18 @@ def parse_results(self, response: Dict[str, Any]) -> Result:
scan_results = response.get('scan_results', response)
virus_name = ""
process_results = response.get('process_info', response)
hit = False
fail = False
processed = {}
if scan_results is not None and scan_results.get('progress_percentage') == 100:
hit = False
fail = False
no_threat_detected = []
av_hits = ResultSection('AV Detections as Infected or Suspicious')
av_fails = ResultSection('Failed to Scan or No Threats Detected')

scans = scan_results.get('scan_details', scan_results)
av_scan_times = []
for majorkey, subdict in sorted(scans.items()):
modified_scans = {key: value for key, value in scans.items() if key not in ["progress_percentage"]}
for majorkey, subdict in sorted(modified_scans.items()):
if majorkey in self.blocklist:
continue
heur_id = None
Expand Down Expand Up @@ -451,7 +453,6 @@ def parse_results(self, response: Dict[str, Any]) -> Result:
if process_results is not None and process_results.get('progress_percentage') == 100:
hit = False
fail = False
cdr_fails = ResultSection('CDR Failed or No Malicious Files Found')
processed = process_results.get('post_processing', process_results)
if processed['actions_failed']:
fail = True
Expand All @@ -463,6 +464,7 @@ def parse_results(self, response: Dict[str, Any]) -> Result:
body=json.dumps(processed))
res.add_section(cdr_json_section)
if fail:
cdr_fails = ResultSection('CDR Failed or No Malicious Files Found')
res.add_section(cdr_fails)

return res
36 changes: 36 additions & 0 deletions pipelines/azure-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: tests

trigger: ["*"]
pr: ["*"]

pool:
vmImage: "ubuntu-20.04"

jobs:
- job: run_test
strategy:
matrix:
python3_7:
python.version: "3.7"
Python3_8:
python.version: "3.8"

timeoutInMinutes: 10

steps:
- task: UsePythonVersion@0
displayName: Set python version
inputs:
versionSpec: "$(python.version)"
- script: |
sudo apt-get update
sudo apt-get install -y qemu-utils libfuzzy-dev libfuzzy2
sudo rm -rf /var/lib/apt/lists/*
sudo env "PATH=$PATH" python -m pip install -U --no-cache-dir assemblyline assemblyline_v4_service
sudo env "PATH=$PATH" python -m pip install -U --no-cache-dir -r `pwd`/test/requirements.txt
sudo rm -rf /tmp/* /var/lib/apt/lists/* ~/.cache/pip
displayName: Setup environment
- script: python -m pytest --durations=10 -rsx -vv --cov-report=xml --cov=metadefender
displayName: Test
- script: python -m codecov
displayName: Upload Coverage
5 changes: 5 additions & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pytest
requests-mock
pytest-mock
pytest-cov
codecov
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"classification": "TLP:W", "response": {"milestones": {"service_started": null, "service_completed": null}, "service_version": null, "service_name": "metadefender", "service_tool_version": null, "supplementary": [], "extracted": [], "service_context": null, "service_debug_info": null}, "result": {"score": 0, "sections": []}, "sha256": "dadc624d4454e10293dbd1b701b9ee9f99ef83b4cd07b695111d37eb95abcff8", "drop_file": false, "temp_submission_data": {}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a text file
Loading

0 comments on commit b42b208

Please sign in to comment.