From a84e9ef1165092b747e367228acf8445d21105a4 Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 02:14:10 +0000 Subject: [PATCH 1/9] First attempt at e2e tests on github --- .github/workflows/e2e.yml | 28 +++++++++++++++++++ tests/e2e.py | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 .github/workflows/e2e.yml create mode 100644 tests/e2e.py diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml new file mode 100644 index 0000000..9ada8c8 --- /dev/null +++ b/.github/workflows/e2e.yml @@ -0,0 +1,28 @@ +name: e2e tests + +on: [pull_request] + +jobs: + unittest: + runs-on: ubuntu-latest + strategy: + matrix: + os: [ubuntu-latest] + python-version: ["3.10"] + steps: + - uses: actions/checkout@v4 + - run: + sudo apt-get update && sudo apt-get install -y python3-pip git && sudo + pip3 install poetry + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + cache: poetry + - name: Install a Yeti prod deployment + run: git clone https://github.com/yeti-platform/yeti-docker && cd yeti-docker/prod && ./init.sh + - name: Install Python dependencies + run: poetry install --no-root + - name: e2e testing + run: | + poetry run python -m unittest tests/e2e.py diff --git a/tests/e2e.py b/tests/e2e.py new file mode 100644 index 0000000..7d762f5 --- /dev/null +++ b/tests/e2e.py @@ -0,0 +1,58 @@ +import os +import time +import unittest +from unittest.mock import MagicMock, patch + +import requests + +from yeti import errors +from yeti.api import YetiApi + +os.environ["YETI_ENDPOINT"] = "http://dev-frontend-1:3000" +os.environ["YETI_API_KEY"] = ( + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdGluZyIsInN1YiI6InlldGkiLCJzY29wZXMiOlsiYWxsIl0sImNyZWF0ZWQiOiIyMDI1LTAzLTExVDIzOjQ1OjU5Ljg3OTQ1OVoiLCJleHAiOm51bGwsImxhc3RfdXNlZCI6bnVsbCwiZW5hYmxlZCI6dHJ1ZSwiZXhwaXJlZCI6ZmFsc2V9.yTidlJ5r8mURLpV9ER3APpO5MlPoG30Z0PqtMLbY1Vg" +) + + +class YetiEndToEndTest(unittest.TestCase): + def setUp(self): + self.api = YetiApi(os.getenv("YETI_ENDPOINT")) + + def test_auth_api_key(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + self.api.search_indicators(name="test") + + def test_no_auth(self): + with self.assertRaises(errors.YetiAuthError) as error: + self.api.search_indicators(name="test") + self.assertIn( + "401 Client Error: Unauthorized for url: ", + str(error.exception), + ) + + def test_new_indicator(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + indicator = self.api.new_indicator( + { + "name": "test", + "type": "regex", + "description": "test", + "pattern": "test[0-9]", + "diamond": "victim", + } + ) + self.assertEqual(indicator["name"], "test") + self.assertRegex(indicator["id"], r"[0-9]+") + + def test_auth_refresh(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + self.api.search_indicators(name="test") + + time.sleep(3) + + self.api.search_indicators(name="test") + + def test_search_indicators(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + result = self.api.search_indicators(name="test") + self.assertEqual(len(result), 1, result) From 487ed109c6a12e0d96b7e38ea6475934fbe94a14 Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 02:42:10 +0000 Subject: [PATCH 2/9] Tweak tests --- .github/workflows/e2e.yml | 4 ++++ .github/workflows/unittests.yml | 2 +- tests/e2e.py | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 9ada8c8..6db7717 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -5,6 +5,8 @@ on: [pull_request] jobs: unittest: runs-on: ubuntu-latest + env: + YETI_ENDPOINT: 'http://localhost:80' strategy: matrix: os: [ubuntu-latest] @@ -21,6 +23,8 @@ jobs: cache: poetry - name: Install a Yeti prod deployment run: git clone https://github.com/yeti-platform/yeti-docker && cd yeti-docker/prod && ./init.sh + run: sleep 10 + run: export YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}') - name: Install Python dependencies run: poetry install --no-root - name: e2e testing diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml index cf66077..f4cae35 100644 --- a/.github/workflows/unittests.yml +++ b/.github/workflows/unittests.yml @@ -23,4 +23,4 @@ jobs: run: poetry install --no-root - name: Test with unittest run: | - poetry run python -m unittest discover -s tests/ -p '*.py' + poetry run python -m unittest tests/api.py diff --git a/tests/e2e.py b/tests/e2e.py index 7d762f5..46d4152 100644 --- a/tests/e2e.py +++ b/tests/e2e.py @@ -8,10 +8,10 @@ from yeti import errors from yeti.api import YetiApi -os.environ["YETI_ENDPOINT"] = "http://dev-frontend-1:3000" -os.environ["YETI_API_KEY"] = ( - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdGluZyIsInN1YiI6InlldGkiLCJzY29wZXMiOlsiYWxsIl0sImNyZWF0ZWQiOiIyMDI1LTAzLTExVDIzOjQ1OjU5Ljg3OTQ1OVoiLCJleHAiOm51bGwsImxhc3RfdXNlZCI6bnVsbCwiZW5hYmxlZCI6dHJ1ZSwiZXhwaXJlZCI6ZmFsc2V9.yTidlJ5r8mURLpV9ER3APpO5MlPoG30Z0PqtMLbY1Vg" -) +# os.environ["YETI_ENDPOINT"] = "http://dev-frontend-1:3000" +# os.environ["YETI_API_KEY"] = ( +# "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdGluZyIsInN1YiI6InlldGkiLCJzY29wZXMiOlsiYWxsIl0sImNyZWF0ZWQiOiIyMDI1LTAzLTExVDIzOjQ1OjU5Ljg3OTQ1OVoiLCJleHAiOm51bGwsImxhc3RfdXNlZCI6bnVsbCwiZW5hYmxlZCI6dHJ1ZSwiZXhwaXJlZCI6ZmFsc2V9.yTidlJ5r8mURLpV9ER3APpO5MlPoG30Z0PqtMLbY1Vg" +# ) class YetiEndToEndTest(unittest.TestCase): From 6b06fc0d4968e61f539e8375ea0a94992f1c7a4a Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 02:49:48 +0000 Subject: [PATCH 3/9] Update --- .github/workflows/e2e.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 6db7717..f6f5e7a 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -22,8 +22,8 @@ jobs: python-version: ${{ matrix.python-version }} cache: poetry - name: Install a Yeti prod deployment - run: git clone https://github.com/yeti-platform/yeti-docker && cd yeti-docker/prod && ./init.sh - run: sleep 10 + run: git clone https://github.com/yeti-platform/yeti-docker && cd yeti-docker/prod && ./init.sh && sleep 10 + - name: Create test Yeti user run: export YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}') - name: Install Python dependencies run: poetry install --no-root From 3a838c95e2acaac5cece2973dfa82f4f836912da Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 03:05:17 +0000 Subject: [PATCH 4/9] Update --- .github/workflows/e2e.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index f6f5e7a..45e4f45 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -24,7 +24,7 @@ jobs: - name: Install a Yeti prod deployment run: git clone https://github.com/yeti-platform/yeti-docker && cd yeti-docker/prod && ./init.sh && sleep 10 - name: Create test Yeti user - run: export YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}') + run: cd yeti-docker/prod && export YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}') - name: Install Python dependencies run: poetry install --no-root - name: e2e testing From c5caa3efb611a1798f6a9f7501b9439e74773346 Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 03:11:03 +0000 Subject: [PATCH 5/9] Update --- .github/workflows/e2e.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 45e4f45..f84bc4d 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -27,6 +27,8 @@ jobs: run: cd yeti-docker/prod && export YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}') - name: Install Python dependencies run: poetry install --no-root + - name: Check env variables + run: env - name: e2e testing run: | - poetry run python -m unittest tests/e2e.py + YETI_API_KEY=$YETI_API_KEY poetry run python -m unittest tests/e2e.py From 6012b391c7c51fa8cdd89c4f12557e08b67ec44e Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 03:17:38 +0000 Subject: [PATCH 6/9] Set env variable appropriately --- .github/workflows/e2e.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index f84bc4d..0b87a18 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -24,7 +24,7 @@ jobs: - name: Install a Yeti prod deployment run: git clone https://github.com/yeti-platform/yeti-docker && cd yeti-docker/prod && ./init.sh && sleep 10 - name: Create test Yeti user - run: cd yeti-docker/prod && export YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}') + run: cd yeti-docker/prod && echo "YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}')" >> $GITHUB_ENV - name: Install Python dependencies run: poetry install --no-root - name: Check env variables From a37e16a4409ff525bc940e61b10226602867c69c Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 03:21:18 +0000 Subject: [PATCH 7/9] update e2e tests --- .github/workflows/e2e.yml | 25 ++++++++++++++++++++----- tests/e2e.py | 18 ++++++++++++------ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 0b87a18..888c274 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -13,22 +13,37 @@ jobs: python-version: ["3.10"] steps: - uses: actions/checkout@v4 - - run: - sudo apt-get update && sudo apt-get install -y python3-pip git && sudo - pip3 install poetry + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y python3-pip git + sudo pip3 install poetry + - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} cache: poetry + - name: Install a Yeti prod deployment - run: git clone https://github.com/yeti-platform/yeti-docker && cd yeti-docker/prod && ./init.sh && sleep 10 + run: | + git clone https://github.com/yeti-platform/yeti-docker + cd yeti-docker/prod + ./init.sh + sleep 10 + - name: Create test Yeti user - run: cd yeti-docker/prod && echo "YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}')" >> $GITHUB_ENV + run: | + cd yeti-docker/prod + echo "YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}')" >> $GITHUB_ENV + - name: Install Python dependencies run: poetry install --no-root + - name: Check env variables run: env + - name: e2e testing run: | YETI_API_KEY=$YETI_API_KEY poetry run python -m unittest tests/e2e.py diff --git a/tests/e2e.py b/tests/e2e.py index 46d4152..9dee457 100644 --- a/tests/e2e.py +++ b/tests/e2e.py @@ -8,11 +8,6 @@ from yeti import errors from yeti.api import YetiApi -# os.environ["YETI_ENDPOINT"] = "http://dev-frontend-1:3000" -# os.environ["YETI_API_KEY"] = ( -# "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdGluZyIsInN1YiI6InlldGkiLCJzY29wZXMiOlsiYWxsIl0sImNyZWF0ZWQiOiIyMDI1LTAzLTExVDIzOjQ1OjU5Ljg3OTQ1OVoiLCJleHAiOm51bGwsImxhc3RfdXNlZCI6bnVsbCwiZW5hYmxlZCI6dHJ1ZSwiZXhwaXJlZCI6ZmFsc2V9.yTidlJ5r8mURLpV9ER3APpO5MlPoG30Z0PqtMLbY1Vg" -# ) - class YetiEndToEndTest(unittest.TestCase): def setUp(self): @@ -54,5 +49,16 @@ def test_auth_refresh(self): def test_search_indicators(self): self.api.auth_api_key(os.getenv("YETI_API_KEY")) - result = self.api.search_indicators(name="test") + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + indicator = self.api.new_indicator( + { + "name": "testSearch", + "type": "regex", + "description": "test", + "pattern": "test[0-9]", + "diamond": "victim", + } + ) + result = self.api.search_indicators(name="testSear") self.assertEqual(len(result), 1, result) + self.assertEqual(result[0]["name"], "testSearch") From 0495aa2e577e4db7cb351f5d2b08303b2105d4de Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 03:24:21 +0000 Subject: [PATCH 8/9] Wait for indexes to catch up? --- tests/e2e.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e.py b/tests/e2e.py index 9dee457..c24a5c2 100644 --- a/tests/e2e.py +++ b/tests/e2e.py @@ -59,6 +59,7 @@ def test_search_indicators(self): "diamond": "victim", } ) + time.sleep(5) result = self.api.search_indicators(name="testSear") self.assertEqual(len(result), 1, result) self.assertEqual(result[0]["name"], "testSearch") From ed4f1420baa0e29dade0d8711863b781311bce4c Mon Sep 17 00:00:00 2001 From: tomchop Date: Wed, 12 Mar 2025 03:26:21 +0000 Subject: [PATCH 9/9] Remove debug --- .github/workflows/e2e.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 888c274..3af52f8 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -41,9 +41,6 @@ jobs: - name: Install Python dependencies run: poetry install --no-root - - name: Check env variables - run: env - - name: e2e testing run: | YETI_API_KEY=$YETI_API_KEY poetry run python -m unittest tests/e2e.py