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
Prev Previous commit
Next Next commit
lint files
  • Loading branch information
hubertdeng123 committed Mar 14, 2024
commit 1eadfea82ede72f18ac377be4a62019263fe2981
7 changes: 5 additions & 2 deletions _integration-test/custom-ca-roots/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ openssl req -new -nodes -newkey rsa:2048 -keyout $TEST_NGINX_CONF_PATH/self.test

# openssl req -in nginx/self.test.req -text -noout

# Store extension details in a temporary file since python subprocess can't use process substitution
printf "subjectAltName=DNS:self.test" >/tmp/extension_details

openssl x509 -req -in $TEST_NGINX_CONF_PATH/self.test.req -CA $TEST_NGINX_CONF_PATH/ca.crt -CAkey $TEST_NGINX_CONF_PATH/ca.key \
-extfile <(printf "subjectAltName=DNS:self.test") \
-extfile /tmp/extension_details \
-CAcreateserial -out $TEST_NGINX_CONF_PATH/self.test.crt -days 1 -sha256

# openssl x509 -in nginx/self.test.crt -text -noout
Expand All @@ -42,4 +45,4 @@ openssl req -x509 -newkey rsa:2048 -nodes -days 1 -keyout $TEST_NGINX_CONF_PATH/

cp _integration-test/custom-ca-roots/test.py sentry/test-custom-ca-roots.py

$dc up -d fixture-custom-ca-roots
docker compose --ansi never up -d fixture-custom-ca-roots
108 changes: 80 additions & 28 deletions _integration-test/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,37 @@

SENTRY_CONFIG_PY = "sentry/sentry.conf.py"
SENTRY_TEST_HOST = os.getenv("SENTRY_TEST_HOST", "https://localhost:9000")
TEST_USER = '[email protected]'
TEST_PASS = 'test123TEST'
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()
response = None
for i in range(5):
response = requests.get(SENTRY_TEST_HOST)
if response.status_code == 200:
try:
response = requests.get(SENTRY_TEST_HOST)
except requests.ConnectionError:
pass
if response is not None and 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}")
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:
def poll_for_response(self, request: str) -> requests.Response:
for i in range(60):
response = self.session.get(request,
headers={'Referer': SENTRY_TEST_HOST}
)
response = self.session.get(request, headers={"Referer": SENTRY_TEST_HOST})
if response.status_code == 200:
break
time.sleep(1)
Expand All @@ -42,64 +48,110 @@ def wait_for_event(self, request: str) -> requests.Response:

@lru_cache
def get_sentry_dsn(self) -> str:
response = self.session.get(f"{SENTRY_TEST_HOST}/api/0/projects/sentry/internal/keys/")
for i in range(60):
response = self.session.get(
f"{SENTRY_TEST_HOST}/api/0/projects/sentry/internal/keys/",
headers={"Referer": SENTRY_TEST_HOST},
)
if response.status_code == 200:
sentry_dsn = json.loads(response.text)[0]["dsn"]["public"]
if len(sentry_dsn) > 0:
break
time.sleep(1)
sentry_dsn = json.loads(response.text)[0]["dsn"]["public"]
assert len(sentry_dsn) > 0
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/"}
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}
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,
},
)
assert response.status_code == 200

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 = self.poll_for_response(
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'
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
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")
run_shell_command(". _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(". _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):
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")
profiles_response = self.poll_for_response(
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")
spans_response = self.poll_for_response(
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
4 changes: 2 additions & 2 deletions integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $dc up -d

if [[ "$test_option" == "--initial-install" ]]; then
echo "Testing initial install"
python _integration-test/run.py
pytest _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
Expand All @@ -34,7 +34,7 @@ EOT
echo "Testing in-place upgrade and customizations"
export MINIMIZE_DOWNTIME=1
./install.sh
python _integration-test/run.py
pytest _integration-test/run.py
source _integration-test/ensure-customizations-work.sh
source _integration-test/ensure-backup-restore-works.sh
fi
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
sentry-sdk>=1.39.2
requests>=2.31.0
requests>=2.31.0
pytest>=8.0.0
Loading