Skip to content

Commit 9b3b9bc

Browse files
Write Customization tests in python (#2918)
* port everything integration test related to python
1 parent cb7cc84 commit 9b3b9bc

File tree

8 files changed

+130
-83
lines changed

8 files changed

+130
-83
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ jobs:
5555
integration-test:
5656
if: github.repository_owner == 'getsentry'
5757
runs-on: ubuntu-20.04
58-
name: "integration test"
58+
name: integration test ${{ matrix.compose_version }} - customizations ${{ matrix.customizations }}
5959
strategy:
6060
fail-fast: false
6161
matrix:
62-
test_type: ["initial-install", "customizations"]
62+
customizations: ["disabled", "enabled"]
6363
compose_version: ["v2.0.1", "v2.7.0"]
6464
include:
6565
- compose_version: "v2.0.1"
@@ -68,18 +68,19 @@ jobs:
6868
compose_path: "/usr/local/lib/docker/cli-plugins"
6969
env:
7070
COMPOSE_PROJECT_NAME: self-hosted-${{ strategy.job-index }}
71-
SENTRY_DSN: https://[email protected]/6627632
72-
REPORT_SELF_HOSTED_ISSUES: 1
71+
REPORT_SELF_HOSTED_ISSUES: 0
72+
SELF_HOSTED_TESTING_DSN: ${{ vars.SELF_HOSTED_TESTING_DSN }}
7373
steps:
7474
- name: Checkout
7575
uses: actions/checkout@v4
7676

7777
- name: Setup dev environment
7878
run: |
7979
pip install -r requirements-dev.txt
80+
echo "PY_COLORS=1" >> "$GITHUB_ENV"
8081
### pytest-sentry configuration ###
8182
if [ "$GITHUB_REPOSITORY" = "getsentry/self-hosted" ]; then
82-
echo "PYTEST_SENTRY_DSN=${{ env.SELF_HOSTED_TESTING_DSN }}" >> $GITHUB_ENV
83+
echo "PYTEST_SENTRY_DSN=$SELF_HOSTED_TESTING_DSN" >> $GITHUB_ENV
8384
echo "PYTEST_SENTRY_TRACES_SAMPLE_RATE=0" >> $GITHUB_ENV
8485
8586
# This records failures on master to sentry in order to detect flakey tests, as it's
@@ -102,13 +103,29 @@ jobs:
102103
sudo chmod +x "${{ matrix.compose_path }}/docker-compose"
103104
104105
- name: Install self-hosted
105-
run: ./install.sh
106+
uses: nick-fields/retry@v3
107+
with:
108+
timeout_minutes: 10
109+
max_attempts: 3
110+
command: ./install.sh
106111

107112
- name: Integration Test
108-
run: ./integration-test.sh --${{ matrix.test_type }}
113+
run: pytest --cov --junitxml=junit.xml --reruns 3 _integration-test/ --customizations=${{ matrix.customizations }}
109114

110115
- name: Inspect failure
111116
if: failure()
112117
run: |
113118
docker compose ps
114119
docker compose logs
120+
121+
- name: Upload coverage to Codecov
122+
uses: codecov/codecov-action@v4
123+
with:
124+
token: ${{ secrets.CODECOV_TOKEN }}
125+
slug: getsentry/self-hosted
126+
127+
- name: Upload test results to Codecov
128+
if: ${{ !cancelled() }}
129+
uses: codecov/test-results-action@v1
130+
with:
131+
token: ${{ secrets.CODECOV_TOKEN }}

_integration-test/conftest.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,49 @@
1010
TEST_PASS = "test123TEST"
1111
TIMEOUT_SECONDS = 60
1212

13+
def pytest_addoption(parser):
14+
parser.addoption("--customizations", default="disabled")
1315

1416
@pytest.fixture(scope="session", autouse=True)
15-
def configure_self_hosted_environment():
17+
def configure_self_hosted_environment(request):
1618
subprocess.run(["docker", "compose", "--ansi", "never", "up", "-d"], check=True)
1719
for i in range(TIMEOUT_SECONDS):
1820
try:
1921
response = httpx.get(SENTRY_TEST_HOST, follow_redirects=True)
20-
except httpx.ConnectionError:
22+
except httpx.NetworkError:
2123
time.sleep(1)
2224
else:
2325
if response.status_code == 200:
2426
break
2527
else:
2628
raise AssertionError("timeout waiting for self-hosted to come up")
2729

30+
if request.config.getoption("--customizations") == "enabled":
31+
os.environ['TEST_CUSTOMIZATIONS'] = "enabled"
32+
script_content = '''\
33+
#!/bin/bash
34+
touch /created-by-enhance-image
35+
apt-get update
36+
apt-get install -y gcc libsasl2-dev python-dev libldap2-dev libssl-dev
37+
'''
38+
39+
with open('sentry/enhance-image.sh', 'w') as script_file:
40+
script_file.write(script_content)
41+
# Set executable permissions for the shell script
42+
os.chmod('sentry/enhance-image.sh', 0o755)
43+
44+
# Write content to the requirements.txt file
45+
with open('sentry/requirements.txt', 'w') as req_file:
46+
req_file.write('python-ldap\n')
47+
os.environ['MINIMIZE_DOWNTIME'] = "1"
48+
subprocess.run(["./install.sh"], check=True)
2849
# Create test user
2950
subprocess.run(
3051
[
3152
"docker",
3253
"compose",
3354
"exec",
55+
"-T",
3456
"web",
3557
"sentry",
3658
"createuser",
@@ -45,3 +67,8 @@ def configure_self_hosted_environment():
4567
check=True,
4668
text=True,
4769
)
70+
71+
@pytest.fixture()
72+
def setup_backup_restore_env_variables():
73+
os.environ['SENTRY_DOCKER_IO_DIR'] = os.path.join(os.getcwd(), 'sentry')
74+
os.environ['SKIP_USER_CREATION'] = "1"

_integration-test/ensure-customizations-not-present.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

_integration-test/ensure-customizations-work.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

_integration-test/backup.py renamed to _integration-test/test_backup.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import subprocess
22
import os
3-
import pytest
43

5-
@pytest.fixture()
6-
def setup_env():
7-
os.environ['SENTRY_DOCKER_IO_DIR'] = os.path.join(os.getcwd(), 'sentry')
8-
os.environ['SKIP_USER_CREATION'] = "1"
94

10-
def test_backup(setup_env):
5+
def test_backup(setup_backup_restore_env_variables):
116
# Docker was giving me permissioning issues when trying to create this file and write to it even after giving read + write access
127
# to group and owner. Instead, try creating the empty file and then give everyone write access to the backup file
138
file_path = os.path.join(os.getcwd(), 'sentry', 'backup.json')
@@ -18,7 +13,7 @@ def test_backup(setup_env):
1813
subprocess.run([sentry_admin_sh, "export", "global", "/sentry-admin/backup.json", "--no-prompt"], check=True)
1914
assert os.path.getsize(file_path) > 0
2015

21-
def test_import(setup_env):
16+
def test_import(setup_backup_restore_env_variables):
2217
# Bring postgres down and recreate the docker volume
2318
subprocess.run(["docker", "compose", "--ansi", "never", "stop", "postgres"], check=True)
2419
subprocess.run(["docker", "compose", "--ansi", "never", "rm", "-f", "-v", "postgres"], check=True)

_integration-test/run.py renamed to _integration-test/test_run.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,18 @@ def test_custom_cas():
141141
try:
142142
subprocess.run(["./_integration-test/custom-ca-roots/setup.sh"], check=True)
143143
subprocess.run(
144-
["docker", "compose", "--ansi", "never", "run", "--no-deps", "web", "python3", "/etc/sentry/test-custom-ca-roots.py"], check=True
144+
[
145+
"docker",
146+
"compose",
147+
"--ansi",
148+
"never",
149+
"run",
150+
"--no-deps",
151+
"web",
152+
"python3",
153+
"/etc/sentry/test-custom-ca-roots.py",
154+
],
155+
check=True,
145156
)
146157
finally:
147158
subprocess.run(["./_integration-test/custom-ca-roots/teardown.sh"], check=True)
@@ -171,3 +182,64 @@ def placeholder_fn():
171182
client,
172183
lambda x: len(json.loads(x)["data"]) > 0,
173184
)
185+
186+
187+
def test_customizations():
188+
commands = [
189+
[
190+
"docker",
191+
"compose",
192+
"--ansi",
193+
"never",
194+
"run",
195+
"--no-deps",
196+
"web",
197+
"bash",
198+
"-c",
199+
"if [ ! -e /created-by-enhance-image ]; then exit 1; fi",
200+
],
201+
[
202+
"docker",
203+
"compose",
204+
"--ansi",
205+
"never",
206+
"run",
207+
"--no-deps",
208+
"--entrypoint=/etc/sentry/entrypoint.sh",
209+
"sentry-cleanup",
210+
"bash",
211+
"-c",
212+
"if [ ! -e /created-by-enhance-image ]; then exit 1; fi",
213+
],
214+
[
215+
"docker",
216+
"compose",
217+
"--ansi",
218+
"never",
219+
"run",
220+
"--no-deps",
221+
"web",
222+
"python",
223+
"-c",
224+
"import ldap",
225+
],
226+
[
227+
"docker",
228+
"compose",
229+
"--ansi",
230+
"never",
231+
"run",
232+
"--no-deps",
233+
"--entrypoint=/etc/sentry/entrypoint.sh",
234+
"sentry-cleanup",
235+
"python",
236+
"-c",
237+
"import ldap",
238+
]
239+
]
240+
for command in commands:
241+
result = subprocess.run(command, check=False)
242+
if os.getenv("TEST_CUSTOMIZATIONS", "disabled") == "enabled":
243+
assert result.returncode == 0
244+
else:
245+
assert result.returncode != 0

integration-test.sh

Lines changed: 0 additions & 40 deletions
This file was deleted.

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
codecov-cli>=0.4.8
12
sentry-sdk>=1.39.2
23
pytest>=8.0.0
4+
pytest-cov>=4.1.0
35
pytest-rerunfailures>=11.0
46
pytest-sentry>=0.1.11
57
httpx>=0.25.2

0 commit comments

Comments
 (0)