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

Port backup tests to python #2907

Merged
merged 19 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
integration tests in python?
  • Loading branch information
hubertdeng123 committed Mar 13, 2024
commit 4bc3a715df76aeeed0218592d9732c0d596769e7
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Install dev dependencies
run: pip install -r requirements-dev.txt

- name: Get Compose
run: |
# Always remove `docker compose` support as that's the newer version
Expand Down
105 changes: 105 additions & 0 deletions _integration-test/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import subprocess
import os
from functools import lru_cache
import requests
import unittest
import sentry_sdk
import time
import json

SENTRY_CONFIG_PY = "sentry/sentry.conf.py"
SENTRY_TEST_HOST = os.getenv("SENTRY_TEST_HOST", "http:https://localhost:9000")
TEST_USER = '[email protected]'
TEST_PASS = 'test123TEST'

def run_shell_command(cmd, **kwargs) -> subprocess.CompletedProcess:
return subprocess.run(cmd, shell=True, check=True, **kwargs)

class IntegrationTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.session = requests.Session()
for i in range(5):
response = requests.get(SENTRY_TEST_HOST)
if response.status_code == 200:
break
time.sleep(1)
assert response.status_code == 200

# Create test user
run_shell_command(f"echo y | docker compose exec web sentry createuser --force-update --superuser --email {TEST_USER} --password {TEST_PASS}")

def wait_for_event(self, request: str) -> requests.Response:
for i in range(60):
response = self.session.get(request,
headers={'Referer': SENTRY_TEST_HOST}
)
if response.status_code == 200:
break
time.sleep(1)
assert response.status_code == 200
return response

@lru_cache
def get_sentry_dsn(self) -> str:
response = self.session.get(f"{SENTRY_TEST_HOST}/api/0/projects/sentry/internal/keys/")
sentry_dsn = json.loads(response.text)[0]["dsn"]["public"]
return sentry_dsn

def test_initial_redirect(self):
initial_auth_redirect = self.session.get(SENTRY_TEST_HOST)
assert initial_auth_redirect.url == f"{SENTRY_TEST_HOST}/auth/login/sentry/"

def test_login(self):
login_csrf_token = self.session.get(SENTRY_TEST_HOST).text.split('"csrfmiddlewaretoken" value="')[1].split('"')[0]
login_response = self.session.post(f"{SENTRY_TEST_HOST}/auth/login/sentry/",
data={'op': 'login', 'username': TEST_USER, 'password': TEST_PASS, 'csrfmiddlewaretoken': login_csrf_token},
headers={'Referer': f"{SENTRY_TEST_HOST}/auth/login/sentry/"}
)
assert '"isAuthenticated":true' in login_response.text
assert '"username":"[email protected]"' in login_response.text
assert '"isSuperuser":true' in login_response.text
assert login_response.cookies["sc"] is not None
# Set up initial/required settings (InstallWizard request)
self.session.headers.update({'X-CSRFToken': login_response.cookies['sc']})
response = self.session.put(f"{SENTRY_TEST_HOST}/api/0/internal/options/?query=is:required",
headers={'Referer':SENTRY_TEST_HOST},
data={"mail.use-tls":False,"mail.username":"","mail.port":25,"system.admin-email":"[email protected]","mail.password":"","system.url-prefix":SENTRY_TEST_HOST,"auth.allow-registration":False,"beacon.anonymous":True}
)

def test_receive_event(self):
event_id = None
with sentry_sdk.init(dsn=self.get_sentry_dsn()):
event_id = sentry_sdk.capture_exception(Exception("a failure"))
assert event_id is not None
response = self.wait_for_event(f"{SENTRY_TEST_HOST}/api/0/projects/sentry/internal/events/{event_id}/")
response_json = json.loads(response.text)
assert response_json['eventID'] == event_id
assert response_json['metadata']['value'] == 'a failure'

def test_cleanup_crons_running(self):
cleanup_crons = run_shell_command("docker compose --ansi never ps -a | tee debug.log | grep -E -e '\\-cleanup\\s+running\\s+' -e '\\-cleanup[_-].+\\s+Up\\s+'", capture_output=True).stdout
assert len(cleanup_crons) > 0

def test_custom_cas(self):
run_shell_command("source _integration-test/custom-ca-roots/setup.sh")
run_shell_command("docker compose --ansi never run --no-deps web python3 /etc/sentry/test-custom-ca-roots.py")
run_shell_command("source _integration-test/custom-ca-roots/teardown.sh")

def test_receive_transaction_events(self):
with sentry_sdk.init(dsn=self.get_sentry_dsn(), profiles_sample_rate=1.0, traces_sample_rate=1.0):
def dummy_func():
sum = 0
for i in range(5):
sum += i
time.sleep(0.25)
with sentry_sdk.start_transaction(op="task", name="Test Transactions"):
dummy_func()
profiles_response = self.wait_for_event(f"{SENTRY_TEST_HOST}/api/0/organizations/sentry/events/?dataset=profiles&field=profile.id&project=1&statsPeriod=1h")
assert profiles_response.status_code == 200
profiles_response_json = json.loads(profiles_response.text)
assert len(profiles_response_json) > 0
spans_response = self.wait_for_event(f"{SENTRY_TEST_HOST}/api/0/organizations/sentry/events/?dataset=spansIndexed&field=id&project=1&statsPeriod=1h")
assert spans_response.status_code == 200
spans_response_json = json.loads(spans_response.text)
assert len(spans_response_json) > 0
6 changes: 3 additions & 3 deletions integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ rm -f sentry/requirements.txt

test_option="$1"
export MINIMIZE_DOWNTIME=0
$dc up -d

if [[ "$test_option" == "--initial-install" ]]; then
echo "Testing initial install"
source _integration-test/run.sh
python _integration-test/run.py
source _integration-test/ensure-customizations-not-present.sh
source _integration-test/ensure-backup-restore-works.sh
elif [[ "$test_option" == "--customizations" ]]; then
echo "Testing customizations"
$dc up -d
echo "Making customizations"
cat <<EOT >sentry/enhance-image.sh
#!/bin/bash
Expand All @@ -34,7 +34,7 @@ EOT
echo "Testing in-place upgrade and customizations"
export MINIMIZE_DOWNTIME=1
./install.sh
source _integration-test/run.sh
python _integration-test/run.py
source _integration-test/ensure-customizations-work.sh
source _integration-test/ensure-backup-restore-works.sh
fi
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sentry-sdk>=1.39.2
requests>=2.31.0
Loading