Skip to content

Commit

Permalink
Add tilt button to run pytest, fix local dev config for pytest, minor…
Browse files Browse the repository at this point in the history
… improvements to til (#3927)

# What this PR does

Fixes for grafana/oncall-private#2423

## Which issue(s) this PR fixes

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required)

---------

Co-authored-by: Joey Orlando <[email protected]>
Co-authored-by: Dominik <[email protected]>
  • Loading branch information
3 people authored Jul 9, 2024
1 parent 6c575e0 commit 6511e43
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
36 changes: 35 additions & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if not is_ci:

local_resource(
"e2e-tests",
labels=["E2eTests"],
labels=["allTests"],
cmd=e2e_tests_cmd,
trigger_mode=TRIGGER_MODE_MANUAL,
auto_init=is_ci,
Expand Down Expand Up @@ -127,6 +127,34 @@ cmd_button(
icon_name="dangerous",
)

# Inspired by https://github.com/grafana/slo/blob/main/Tiltfile#L72
pod_engine_pytest_script = '''
set -eu
# get engine k8s pod name from tilt resource name
POD_NAME="$(tilt get kubernetesdiscovery "engine" -ojsonpath='{.status.pods[0].name}')"
kubectl exec "$POD_NAME" -- pytest . $STOP_ON_FIRST_FAILURE $TESTS_FILTER
'''
local_resource(
"pytest-tests",
labels=["allTests"],
cmd=['sh', '-c', pod_engine_pytest_script],
trigger_mode=TRIGGER_MODE_MANUAL,
auto_init=False,
resource_deps=["engine"]
)

cmd_button(
name="pytest Tests - headless run",
argv=['sh', '-c', pod_engine_pytest_script],
text="Run pytest",
resource="pytest-tests",
icon_name="replay",
inputs=[
text_input("TESTS_FILTER", "pytest optional arguments (e.g. \"apps/webhooks/tests/test_webhook.py::test_build_url_private_raises\")", "", "Test file names to run"),
bool_input("STOP_ON_FIRST_FAILURE", "Stop on first failure", True, "-x", ""),
]
)

helm_oncall_values = ["./dev/helm-local.yml", "./dev/helm-local.dev.yml"]
if is_ci:
helm_oncall_values = helm_oncall_values + ["./.github/helm-ci.yml"]
Expand Down Expand Up @@ -174,7 +202,10 @@ k8s_resource(
resource_deps=["mariadb", "redis-master"],
labels=["OnCallBackend"],
)
k8s_resource(workload="engine-migrate", labels=["OnCallBackend"])

k8s_resource(workload="redis-master", labels=["OnCallDeps"])
k8s_resource(workload="prometheus-server", labels=["OnCallDeps"])
k8s_resource(
workload="mariadb",
port_forwards='3307:3306', # <host_port>:<container_port>
Expand All @@ -184,6 +215,9 @@ k8s_resource(

# name all tilt resources after the k8s object namespace + name
def resource_name(id):
# Remove variable date from job name
if id.name.startswith(HELM_PREFIX + "-engine-migrate"):
return "engine-migrate"
return id.name.replace(HELM_PREFIX + "-", "")

workload_to_resource_function(resource_name)
16 changes: 16 additions & 0 deletions dev/helm-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ env:
value: "False"
- name: FEATURE_PROMETHEUS_EXPORTER_ENABLED
value: "True"
- name: DJANGO_SETTINGS_MODULE
value: "settings.dev"
- name: FEATURE_TELEGRAM_INTEGRATION_ENABLED
value: "True"
- name: FEATURE_SLACK_INTEGRATION_ENABLED
value: "True"
- name: SLACK_SLASH_COMMAND_NAME
value: "/oncall"
# enabled to be able to test docker.host.internal in the webhook e2e tests
- name: DANGEROUS_WEBHOOKS_ENABLED
value: "True"
Expand Down Expand Up @@ -131,6 +139,14 @@ service:
nodePort: 30001
prometheus:
enabled: true
alertmanager:
enabled: false
kube-state-metrics:
enabled: false
prometheus-node-exporter:
enabled: false
prometheus-pushgateway:
enabled: false
server:
global:
scrape_interval: 10s
Expand Down
10 changes: 10 additions & 0 deletions engine/apps/alerts/tests/test_alert_receiver_channel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
from unittest import mock
from unittest.mock import patch

import pytest
from django.conf import settings
from django.db import IntegrityError
from django.urls import reverse
from django.utils import timezone
Expand All @@ -10,6 +12,7 @@
from common.api_helpers.utils import create_engine_url
from common.exceptions import UnableToSendDemoAlert
from engine.management.commands import alertmanager_v2_migrate
from settings.base import DatabaseTypes


@pytest.mark.django_db
Expand Down Expand Up @@ -272,6 +275,13 @@ def test_create_missing_direct_paging_integrations(
def test_create_duplicate_direct_paging_integrations(make_organization, make_team, make_alert_receive_channel):
"""Check that it's not possible to have more than one active direct paging integration per team."""

# MariaDB is not supported for this test
# See comment: https://github.com/grafana/oncall/commit/381a9ecf54bf0dd076f233b207c13d72ed792181#diff-9d96504027309f2bd1e95352bac1433b09b60eb4fafb611b52a6c15ed16cbc48R219-R223
is_local_dev_env = os.environ.get("DJANGO_SETTINGS_MODULE") == "settings.dev"
is_db_type_mysql = settings.DATABASE_TYPE == DatabaseTypes.MYSQL
if is_local_dev_env and is_db_type_mysql:
pytest.skip("This test is not supported by Mariadb (used by settings.dev)")

organization = make_organization()
team = make_team(organization)
make_alert_receive_channel(organization, team=team, integration=AlertReceiveChannel.INTEGRATION_DIRECT_PAGING)
Expand Down
4 changes: 3 additions & 1 deletion engine/apps/integrations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ def create_alert(
},
countdown=countdown,
)
logger.warning(f"Retrying the task gracefully in {countdown} seconds due to ConcurrentUpdateError")
logger.warning(
f"Retrying the task gracefully in {countdown} seconds due to ConcurrentUpdateError for alert_receive_channel={alert_receive_channel_pk}"
)


@shared_dedicated_queue_retry_task()
Expand Down
6 changes: 6 additions & 0 deletions engine/apps/webhooks/tests/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ def test_build_url_invalid_url(make_organization, make_custom_webhook):

@pytest.mark.django_db
def test_build_url_private_raises(make_organization, make_custom_webhook):
if settings.DANGEROUS_WEBHOOKS_ENABLED:
pytest.skip("Dangerous webhooks are enabled")

organization = make_organization()
webhook = make_custom_webhook(organization=organization, url="{{foo}}")

Expand Down Expand Up @@ -241,6 +244,9 @@ def test_make_request(make_organization, make_custom_webhook):
@httpretty.activate(verbose=True, allow_net_connect=False)
@pytest.mark.django_db
def test_make_request_bad_redirect(make_organization, make_custom_webhook):
if settings.DANGEROUS_WEBHOOKS_ENABLED:
pytest.skip("Dangerous webhooks are enabled")

organization = make_organization()
webhook = make_custom_webhook(organization=organization, http_method="POST")

Expand Down
7 changes: 7 additions & 0 deletions engine/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
TELEGRAM_TOKEN = "0000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXX"
TWILIO_AUTH_TOKEN = "twilio_auth_token"

# charset/collation related tests don't work without this
TEST_SETTINGS = {
"CHARSET": "utf8mb4",
"COLLATION": "utf8mb4_unicode_ci",
}
DATABASES["default"]["TEST"] = TEST_SETTINGS

INTERNAL_IPS = [
"127.0.0.1",
]
Expand Down
2 changes: 0 additions & 2 deletions engine/settings/prod_without_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ def on_uwsgi_worker_exit():
# Only works under uwsgi web server environment
pass


SLACK_SIGNING_SECRET = os.environ.get("SLACK_SIGNING_SECRET")
SLACK_SIGNING_SECRET_LIVE = os.environ.get("SLACK_SIGNING_SECRET_LIVE", "")


STATICFILES_DIRS = [
"/etc/app/static",
]
Expand Down

0 comments on commit 6511e43

Please sign in to comment.