Skip to content

Commit 383a2c4

Browse files
committed
feat: add unit test
1 parent e53ec41 commit 383a2c4

27 files changed

+842
-194
lines changed

.github/workflows/pylama.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request:
55

66
jobs:
7-
build:
7+
pylama:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:

.github/workflows/semver_build_publish.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ jobs:
6363
run: |
6464
CHANNEL=${{ needs.semver.outputs.new_release_channel }}
6565
docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:${CHANNEL:-'latest'}
66+
67+
- name: Docker Hub Description
68+
uses: peter-evans/dockerhub-description@v3
69+
with:
70+
username: ${{ secrets.DOCKERHUB_USERNAME }}
71+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
72+
repository: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}

.github/workflows/tests.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: tests-unittest
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
services:
10+
postgres:
11+
image: postgres
12+
env:
13+
POSTGRES_PASSWORD: postgres
14+
ports:
15+
- 5432:5432
16+
options: >-
17+
--health-cmd pg_isready
18+
--health-interval 10s
19+
--health-timeout 5s
20+
--health-retries 5
21+
strategy:
22+
matrix:
23+
python-version: ["3.10"]
24+
poetry-version: ["1.2"]
25+
steps:
26+
- uses: actions/checkout@v2
27+
- name: Set up Python ${{ matrix.python-version }}
28+
uses: actions/setup-python@v2
29+
with:
30+
python-version: ${{ matrix.python-version }}
31+
- name: Run image
32+
uses: abatilo/[email protected]
33+
with:
34+
poetry-version: ${{ matrix.poetry-version }}
35+
- name: Install dependencies
36+
run: poetry install
37+
- name: Run tests with coverage
38+
run: |
39+
poetry run coverage run
40+
poetry run coverage xml
41+
env:
42+
POSTGRES_HOST: localhost
43+
POSTGRES_USER: postgres
44+
POSTGRES_PASSWORD: postgres
45+
TESTING: "true"
46+
SECRET_KEY: "very_secret_key"
47+
OIDC_OP_JWKS_ENDPOINT: "http://nowhere.com"
48+
- name: Upload Coverage to Codecov
49+
uses: codecov/codecov-action@v3
50+
with:
51+
verbose: true
52+
files: coverage.xml
53+
flags: unittests
54+
name: codecov-umbrella
55+
fail_ci_if_error: true

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# Sentry Dynamic Sampling Controller
22

3+
![Tests Status](https://github.com/SpikeeLabs/sentry-dynamic-sampling-controller/actions/workflows/.github/workflows/test.yml/badge.svg)
4+
[![codecov](https://codecov.io/gh/SpikeeLabs/sentry-dynamic-sampling-controller/branch/main/graph/badge.svg?token=NK5V6YMWW0)](https://codecov.io/gh/SpikeeLabs/sentry-dynamic-sampling-controller)
5+
6+
---
7+
38
This project aims to provide dynamic sampling without relying on Sentry Dynamic Sampling.
49

510

6-
It work by installing the library [sentry-dynamic-sampling-lib](https://github.com/SpikeeLabs/sentry-dynamic-sampling-lib) on each project that use sentry. This lib hooks into the sentry callback to change the sampling rate. to get the rate the lib calls this service.
11+
It work by installing the library [sentry-dynamic-sampling-lib](https://github.com/SpikeeLabs/sentry-dynamic-sampling-lib) on each project that use sentry. This lib hooks into the sentry callback to change the sampling rate. To get the rate the lib calls this service.
712

813

914

1015

11-
## Install
16+
## Development
1217
```bash
1318
# install deps
1419
poetry install
@@ -23,6 +28,8 @@ poetry run pre-commit install --install-hooks --hook-type commit-msg
2328
```bash
2429
poetry shell
2530

31+
python manage.py migrate
32+
2633
# add user
2734
python manage.py createsuperuser
2835

config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
worker_class = "gevent"
55
workers = int(os.getenv("WORKERS", "4"))
6-
bind = [
7-
f'{os.environ.get("HTTP_ADDR", "0.0.0.0")}:{os.environ.get("HTTP_PORT", "8000")}'
8-
]
6+
bind = [f'{os.environ.get("HTTP_ADDR", "0.0.0.0")}:{os.environ.get("HTTP_PORT", "8000")}']
97
daemon = os.environ.get("DEAMON_RUNNING", False)
108
timeout = os.environ.get("TIMEOUT", 60)
119
loglevel = os.environ.get("LOG_LEVEL", "info")

controller/sentry/admin.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
from controller.sentry.models import App
1919
from controller.sentry.utils import invalidate_cache
2020

21-
admin.site
22-
2321

2422
@admin.register(App)
2523
class AppAdmin(
@@ -102,7 +100,7 @@ def get_change_actions(self, request, object_id, form_url):
102100
@add_form_to_action(BumpForm)
103101
@confirm_action()
104102
@admin.action(description="Bump Sample Rate")
105-
def bump_sample_rate(self, request, queryset, form: BumpForm = None):
103+
def bump_sample_rate(self, request, queryset, form: BumpForm = None): # pylint: disable=unused-argument
106104
new_date = timezone.now() + form.cleaned_data["duration"]
107105
queryset.update(
108106
active_sample_rate=form.cleaned_data["new_sample_rate"],
@@ -122,7 +120,7 @@ def has_bump_sample_rate_permission(self, request):
122120
@takes_instance_or_queryset
123121
@confirm_action(display_queryset=False)
124122
@admin.action(description="Panic")
125-
def panic(self, request, queryset):
123+
def panic(self, request, queryset): # pylint: disable=unused-argument
126124
cache.set(settings.PANIC_KEY, True, timeout=None)
127125

128126
panic.allowed_permissions = ("panic",)
@@ -138,7 +136,7 @@ def has_panic_permission(self, request):
138136
@takes_instance_or_queryset
139137
@confirm_action(display_queryset=False)
140138
@admin.action(description="UnPanic")
141-
def unpanic(self, request, queryset):
139+
def unpanic(self, request, queryset): # pylint: disable=unused-argument
142140
cache.delete(settings.PANIC_KEY)
143141

144142
unpanic.allowed_permissions = ("unpanic",)

controller/sentry/auth.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
from functools import lru_cache
2+
13
from django.conf import settings
24
from django.contrib.auth.models import Group
35
from mozilla_django_oidc.auth import OIDCAuthenticationBackend
46

5-
DEVELOPER_GROUP = Group.objects.get(name=settings.DEVELOPER_GROUP)
7+
8+
@lru_cache
9+
def get_group(group):
10+
return Group.objects.get(name=group)
611

712

813
class ControllerOIDCAuthenticationBackend(OIDCAuthenticationBackend):
@@ -30,4 +35,4 @@ def _set_username(self, user, claims):
3035

3136
def _set_perms(self, user):
3237
user.is_staff = True
33-
user.groups.add(DEVELOPER_GROUP)
38+
user.groups.add(get_group(settings.DEVELOPER_GROUP))

controller/sentry/fixtures/groups.json

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

controller/sentry/forms.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ def clean_new_sample_rate(self):
1919

2020
def clean_duration(self):
2121
data = self.cleaned_data["duration"]
22-
if (
23-
data.total_seconds() < 0
24-
or data.total_seconds() > settings.MAX_BUMP_TIME_SEC
25-
):
26-
raise ValidationError("duration must be between 0 and 600")
22+
if data.total_seconds() < 0 or data.total_seconds() > settings.MAX_BUMP_TIME_SEC:
23+
raise ValidationError(f"duration must be between 0 and {settings.MAX_BUMP_TIME_SEC}")
2724
return data

controller/sentry/metrics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from controller.sentry.metrics.celery import celery_merger
22
from controller.sentry.metrics.wsgi import wsgi_merger
33

4-
__all__ = [wsgi_merger, celery_merger]
4+
__all__ = ["wsgi_merger", "celery_merger"]

0 commit comments

Comments
 (0)