Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev workflows #10

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
23bb1a4
Add `httpretty~=1.1.4`
yotamloe Mar 12, 2024
cd62914
Update workflows
yotamloe Mar 12, 2024
eb1e57d
Update workflows
yotamloe Mar 12, 2024
16cf53c
Update `tests` workflow
yotamloe Mar 12, 2024
40be8ec
Update workflows
yotamloe Mar 13, 2024
1950b66
Add dev deps
yotamloe Mar 13, 2024
f283b58
add pytest debug logs
yotamloe Mar 13, 2024
91f4e16
fix test config files
8naama Mar 18, 2024
525002d
try fix deadlock in `test_sending_data` test
8naama Mar 21, 2024
55fd527
try another approach to fix tests
8naama Mar 24, 2024
a2103ca
revert attempts
8naama Mar 24, 2024
d0a6c19
try fix issue
8naama Mar 24, 2024
535fd25
try killing process different way
8naama Mar 24, 2024
65ae906
try another approach
8naama Mar 24, 2024
dd716ce
add print for process state
8naama Mar 24, 2024
b3ee7b2
add return at ApisManager
8naama Mar 24, 2024
6a7ccd3
try new approach
8naama Mar 24, 2024
462202b
add missing param for new approach
8naama Mar 24, 2024
458e355
try False on queue get block
8naama Mar 24, 2024
1943bcb
verify deadlock in q.get
8naama Mar 24, 2024
8d4892f
update logger test to debug
8naama Mar 25, 2024
553e9fc
add checks to the functions that fill the queue
8naama Mar 25, 2024
e2a47ac
add test param to `run()`func in api manager to try stop the process …
8naama Mar 26, 2024
d555573
add test=True to the relevant tests
8naama Mar 26, 2024
bddecbd
add extra test log and reduce sleep before killing process
8naama Mar 26, 2024
7f7f76f
run ApisManager().run as process to kill it
8naama Mar 26, 2024
5c11a7e
test another URL and restore prev api_managers
8naama Mar 26, 2024
f449376
revert oath_api
8naama Mar 26, 2024
76ef30d
add test parameter to API manager to see if it will solve issue
8naama Mar 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build and Deploy to dockerhub
on:
workflow_dispatch:
release:
types: [published]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: logzio/logzio-api-fetcher
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
33 changes: 33 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Automatic tests and code-coverage
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Add credentials to config files
run: |
cd tests
grep -rli '<<CISCO_SECURE_X_API_ID>>' * | xargs -i@ sed -i 's/<<CISCO_SECURE_X_API_ID>>/${{ secrets.CISCO_SECURE_X_API_ID }}/g' @
grep -rli '<<CISCO_SECURE_X_API_KEY>>' * | xargs -i@ sed -i 's/<<CISCO_SECURE_X_API_KEY>>/${{ secrets.CISCO_SECURE_X_API_KEY }}/g' @
grep -rli '<<AZURE_AD_SECRET_ID>>' * | xargs -i@ sed -i 's/<<AZURE_AD_SECRET_ID>>/${{ secrets.AZURE_AD_SECRET_ID }}/g' @
grep -rli '<<AZURE_AD_SECRET_VALUE>>' * | xargs -i@ sed -i 's/<<AZURE_AD_SECRET_VALUE>>/${{ secrets.AZURE_AD_SECRET_VALUE }}/g' @
grep -rli '<<CLIENT_ID>>' * | xargs -i@ sed -i 's/<<CLIENT_ID>>/${{ secrets.AZURE_AD_CLIENT_ID }}/g' @
grep -rli '<<AZURE_AD_TENANT_ID>>' * | xargs -i@ sed -i 's/<<AZURE_AD_TENANT_ID>>/${{ secrets.AZURE_AD_TENANT_ID }}/g' @
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pytest-cov
pip install pytest
- name: Run unit tests
run: |
pytest --log-cli-level=debug tests/*_tests.py
64 changes: 0 additions & 64 deletions .github/workflows/workflow.yaml

This file was deleted.

11 changes: 6 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pyyaml
requests
urllib3
python-dateutil
jsonpath-ng
pyyaml~=6.0.1
requests~=2.31.0
urllib3~=2.2.1
python-dateutil~=2.9.0.post0
jsonpath-ng
httpretty~=1.1.4
7 changes: 5 additions & 2 deletions src/apis_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import signal
import threading

import requests

from typing import Optional
Expand Down Expand Up @@ -33,12 +34,13 @@ class ApisManager:
AUTH_API_TYPES = [API_GENERAL_TYPE, API_CISCO_SECURE_X_TYPE]
OAUTH_API_TYPES = [API_GENERAL_TYPE, API_AZURE_GRAPH_TYPE, API_AZURE_MAIL_REPORTS_TYPE]

def __init__(self) -> None:
def __init__(self, test=False) -> None:
self._apis: list[Api] = []
self._logzio_connection: Optional[LogzioConnection] = None
self._threads = []
self._event = threading.Event()
self._lock = threading.Lock()
self.test = test

def run(self) -> None:
if not self._read_data_from_config():
Expand Down Expand Up @@ -107,7 +109,8 @@ def _run_api_scheduled_task(self, api: Api) -> None:
thread.start()
thread.join()

if self._event.wait(timeout=api.get_api_time_interval()):
if self._event.wait(timeout=api.get_api_time_interval()) or self.test:
logger.info("TEST: stopping shipping due to test or timeout")
break

def _send_data_to_logzio(self, api: Api, logzio_shipper: LogzioShipper) -> None:
Expand Down
1 change: 0 additions & 1 deletion src/oauth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,3 @@ def get_token_request(self):
def _set_current_data_last_date(self, date):
if date:
self._current_data_last_date = date

17 changes: 10 additions & 7 deletions tests/azure_graph_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ def test_sending_data(self) -> None:
self.tests_utils.run_oauth_api_process,
status=200,
sleep_time=10)

requests_num, sent_logs_num, sent_bytes = queue.get()
if queue.empty():
logger.info("TEST: queue empty")
else:
logger.info("TEST: queue not empty")
requests_num, sent_logs_num, sent_bytes = queue.get(False)
data_bytes, data_num = self.tests_utils.get_api_data_bytes_and_num_from_json_data(
self.azure_graph_json_body[AzureGraph.DEFAULT_GRAPH_DATA_LINK])

Expand All @@ -105,7 +108,7 @@ def test_sending_data_iterations(self) -> None:
status=200,
sleep_time=70)

requests_num, sent_logs_num, sent_bytes = queue.get()
requests_num, sent_logs_num, sent_bytes = queue.get(False)
data_bytes, data_num = self.tests_utils.get_api_data_bytes_and_num_from_json_data(
self.azure_graph_json_body[AzureGraph.DEFAULT_GRAPH_DATA_LINK])

Expand All @@ -121,7 +124,7 @@ def test_sending_data_multiple_azure_graph(self) -> None:
status=200,
sleep_time=10)

requests_num, sent_logs_num, sent_bytes = queue.get()
requests_num, sent_logs_num, sent_bytes = queue.get(False)
data_bytes, data_num = self.tests_utils.get_api_data_bytes_and_num_from_json_data(
self.azure_graph_json_body[AzureGraph.DEFAULT_GRAPH_DATA_LINK])

Expand All @@ -137,7 +140,7 @@ def test_sending_data_with_custom_fields(self) -> None:
status=200,
sleep_time=10)

requests_num, sent_logs_num, sent_bytes = queue.get()
requests_num, sent_logs_num, sent_bytes = queue.get(False)
data_bytes, data_num = self.tests_utils.get_api_data_bytes_and_num_from_json_data(
self.azure_graph_json_body[AzureGraph.DEFAULT_GRAPH_DATA_LINK])
custom_fields_azure_graph = self.tests_utils.get_first_api(AzureGraphApiTests.CUSTOM_FIELDS_CONFIG_FILE,
Expand All @@ -156,7 +159,7 @@ def test_time_interval(self) -> None:
status=200,
sleep_time=70)

requests_num, sent_logs_num, sent_bytes = queue.get()
requests_num, sent_logs_num, sent_bytes = queue.get(False)
data_bytes, data_num = self.tests_utils.get_api_data_bytes_and_num_from_json_data(
self.azure_graph_json_body[AzureGraph.DEFAULT_GRAPH_DATA_LINK])

Expand Down Expand Up @@ -188,7 +191,7 @@ def test_bad_config(self) -> None:
status=200,
sleep_time=1)

requests_num, sent_logs_num, sent_bytes = queue.get()
requests_num, sent_logs_num, sent_bytes = queue.get(False)

self.assertEqual(0, requests_num)
self.assertEqual(0, sent_logs_num)
Expand Down
1 change: 1 addition & 0 deletions tests/config/azure_graph/base_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ oauth_apis:
time_interval: 1
days_back_fetch: 30
start_date_name: createdDateTime
end_date_name: EndDate
1 change: 1 addition & 0 deletions tests/config/azure_graph/custom_fields_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ oauth_apis:
url: https://graph.microsoft.com/v1.0/auditLogs/signIns
method: GET
start_date_name: createdDateTime
end_date_name: EndDate
json_paths:
data_date: createdDateTime
settings:
Expand Down
1 change: 1 addition & 0 deletions tests/config/azure_graph/days_back_fetch_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ oauth_apis:
url: https://graph.microsoft.com/v1.0/auditLogs/signIns
method: GET
start_date_name: createdDateTime
end_date_name: EndDate
json_paths:
data_date: createdDateTime
additional_filters:
Expand Down
1 change: 1 addition & 0 deletions tests/config/azure_graph/filters_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ oauth_apis:
url: https://graph.microsoft.com/v1.0/auditLogs/signIns
method: GET
start_date_name: createdDateTime
end_date_name: EndDate
json_paths:
data_date: createdDateTime
filters:
Expand Down
21 changes: 15 additions & 6 deletions tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

class TestUtils:
LOGZIO_HTTPPRETTY_URL = 'https://listener.logz.io:8071/?token=123456789a&type=api_fetcher'
LOGZIO_HTTPPRETTY_URL2 = 'https://listener.logz.io:8071/?token=123456789a'
LOGZIO_URL = 'https://listener.logz.io:8071'
LOGZIO_TOKEN = '123456789a'
LAST_START_DATES_FILE = 'tests/last_start_dates.txt'
Expand Down Expand Up @@ -76,9 +77,7 @@ def run_oauth_api_process(self, config_file: str, status: int, queue: multiproce
is_multi_test: bool) -> None:
from tests.azure_graph_api_tests import AzureGraphApiTests
httpretty.register_uri(httpretty.POST, TestUtils.LOGZIO_URL, status=status)
httpretty.register_uri(self.token_http_method,
self.token_url,
body=json.dumps(self.token_body))
httpretty.register_uri(self.token_http_method, self.token_url, body=json.dumps(self.token_body))
httpretty.register_uri(self.api_http_method, self.api_url, body=json.dumps(self.api_body), status=200,
headers={AzureGraph.OAUTH_AUTHORIZATION_HEADER:
AzureGraphApiTests.AZURE_GRAPH_TEST_TOKEN})
Expand All @@ -90,14 +89,22 @@ def run_oauth_api_process(self, config_file: str, status: int, queue: multiproce
ApisManager.LAST_START_DATES_FILE = TestUtils.LAST_START_DATES_FILE
logzio_requests = []

ApisManager().run()
# logger.info("TEST: starting API Manager")
# p = multiprocessing.Process(target=ApisManager().run)
ApisManager(True).run() # test_sending_data is stuck here, never reaching after this line
# p.start()
# time.sleep(2)
# p.kill()
logger.info("TEST: Finished API Manager!")

# this now doesn't work, because the requests are made in a separate process
for request in httpretty.latest_requests():
logger.info("TEST: reading requests")
if request.url.startswith(self.api_url):
continue

logzio_requests.append(request)

logger.info("TEST: putting data in the queue")
queue.put(self._get_sending_data_results(logzio_requests))

def get_first_api(self, config_file: str, is_auth_api: bool) -> Api:
Expand Down Expand Up @@ -214,7 +221,8 @@ def _get_sending_data_results(self, latest_requests: list) -> tuple[int, int, in
sent_bytes = 0

for request in latest_requests:
if request.url == self.LOGZIO_HTTPPRETTY_URL:
logger.info(f"TEST: checking request {request}")
if request.url in (self.LOGZIO_HTTPPRETTY_URL, self.LOGZIO_HTTPPRETTY_URL2):
requests_num += 1

try:
Expand All @@ -226,4 +234,5 @@ def _get_sending_data_results(self, latest_requests: list) -> tuple[int, int, in
sent_logs_num += 1
sent_bytes += len(log)

logger.info(f"TEST: _get_sending_data_results params {requests_num}, {sent_logs_num}, {sent_bytes}")
return int(requests_num / 2), int(sent_logs_num / 2), int(sent_bytes / 2)