Skip to content

Commit 7472fb7

Browse files
XinRanZhAWSADOT Patch workflow
andauthored
Implement Django Contract Test (#103)
*Description of changes:* Implement Django Contract Test Django Application is created by "python manage.py startapp api", modified views.py and ../django_server/urls.py to listen on specific endpoint. Unused file have been removed. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Co-authored-by: ADOT Patch workflow <[email protected]>
1 parent def483b commit 7472fb7

File tree

16 files changed

+423
-27
lines changed

16 files changed

+423
-27
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Meant to be run from aws-otel-python-instrumentation/contract-tests.
2+
# Assumes existence of dist/aws_opentelemetry_distro-<pkg_version>-py3-none-any.whl.
3+
# Assumes filename of aws_opentelemetry_distro-<pkg_version>-py3-none-any.whl is passed in as "DISTRO" arg.
4+
FROM python:3.10
5+
WORKDIR /django
6+
COPY ./dist/$DISTRO /django
7+
COPY ./contract-tests/images/applications/django /django
8+
9+
ENV PIP_ROOT_USER_ACTION=ignore
10+
ARG DISTRO
11+
RUN pip install --upgrade pip && pip install -r requirements.txt && pip install ${DISTRO} --force-reinstall
12+
RUN opentelemetry-bootstrap -a install
13+
14+
# Without `-u`, logs will be buffered and `wait_for_logs` will never return.
15+
CMD ["opentelemetry-instrument", "python", "-u", "./manage.py", "runserver", "0.0.0.0:8080","--noreload"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from django.apps import AppConfig
5+
6+
7+
class ApiConfig(AppConfig):
8+
default_auto_field = "django.db.models.BigAutoField"
9+
name = "api"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from django.http import HttpResponse
4+
from django.views.decorators.csrf import csrf_exempt
5+
from django.views.decorators.http import require_http_methods
6+
7+
8+
def success(request):
9+
return HttpResponse("Success", status=200)
10+
11+
12+
def fault(request):
13+
return HttpResponse("Server Error", status=500)
14+
15+
16+
def error(request):
17+
return HttpResponse("Bad Request", status=400)
18+
19+
20+
def user_order(request, user_id, order_id):
21+
request.GET.get("filter", None)
22+
return HttpResponse("Routed", status=200)
23+
24+
25+
@csrf_exempt
26+
@require_http_methods(["POST"])
27+
def post_success(request):
28+
return HttpResponse("Create Success", status=201)
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
ASGI config for django_server project.
5+
6+
It exposes the ASGI callable as a module-level variable named ``application``.
7+
8+
For more information on this file, see
9+
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
10+
"""
11+
12+
import os
13+
14+
from django.core.asgi import get_asgi_application
15+
16+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_server.settings")
17+
18+
application = get_asgi_application()
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
Django settings for django_server project.
5+
6+
Generated by 'django-admin startproject' using Django 4.2.11.
7+
8+
For more information on this file, see
9+
https://docs.djangoproject.com/en/4.2/topics/settings/
10+
11+
For the full list of settings and their values, see
12+
https://docs.djangoproject.com/en/4.2/ref/settings/
13+
"""
14+
15+
from pathlib import Path
16+
17+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
18+
BASE_DIR = Path(__file__).resolve().parent.parent
19+
20+
21+
# Quick-start development settings - unsuitable for production
22+
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
23+
24+
# SECURITY WARNING: keep the secret key used in production secret!
25+
SECRET_KEY = "django-insecure-6yvsuj8cd4qlr7uhp@0^j#dbly=5qmh^-m=1n#fnz(1@out_cn"
26+
27+
# SECURITY WARNING: don't run with debug turned on in production!
28+
DEBUG = True
29+
30+
ALLOWED_HOSTS = ["*"]
31+
32+
33+
# Application definition
34+
35+
INSTALLED_APPS = [
36+
"django.contrib.admin",
37+
"django.contrib.auth",
38+
"django.contrib.contenttypes",
39+
"django.contrib.sessions",
40+
"django.contrib.messages",
41+
"django.contrib.staticfiles",
42+
]
43+
44+
MIDDLEWARE = [
45+
"django.middleware.security.SecurityMiddleware",
46+
"django.contrib.sessions.middleware.SessionMiddleware",
47+
"django.middleware.common.CommonMiddleware",
48+
"django.middleware.csrf.CsrfViewMiddleware",
49+
"django.contrib.auth.middleware.AuthenticationMiddleware",
50+
"django.contrib.messages.middleware.MessageMiddleware",
51+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
52+
]
53+
54+
ROOT_URLCONF = "django_server.urls"
55+
56+
TEMPLATES = [
57+
{
58+
"BACKEND": "django.template.backends.django.DjangoTemplates",
59+
"DIRS": [],
60+
"APP_DIRS": True,
61+
"OPTIONS": {
62+
"context_processors": [
63+
"django.template.context_processors.debug",
64+
"django.template.context_processors.request",
65+
"django.contrib.auth.context_processors.auth",
66+
"django.contrib.messages.context_processors.messages",
67+
],
68+
},
69+
},
70+
]
71+
72+
WSGI_APPLICATION = "django_server.wsgi.application"
73+
74+
75+
# Database
76+
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
77+
78+
DATABASES = {
79+
"default": {
80+
"ENGINE": "django.db.backends.sqlite3",
81+
"NAME": BASE_DIR / "db.sqlite3",
82+
}
83+
}
84+
85+
86+
# Password validation
87+
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
88+
89+
AUTH_PASSWORD_VALIDATORS = [
90+
{
91+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
92+
},
93+
{
94+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
95+
},
96+
{
97+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
98+
},
99+
{
100+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
101+
},
102+
]
103+
104+
105+
# Internationalization
106+
# https://docs.djangoproject.com/en/4.2/topics/i18n/
107+
108+
LANGUAGE_CODE = "en-us"
109+
110+
TIME_ZONE = "UTC"
111+
112+
USE_I18N = True
113+
114+
USE_TZ = True
115+
116+
117+
# Static files (CSS, JavaScript, Images)
118+
# https://docs.djangoproject.com/en/4.2/howto/static-files/
119+
120+
STATIC_URL = "static/"
121+
122+
# Default primary key field type
123+
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
124+
125+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
URL configuration for django_server project.
5+
6+
The `urlpatterns` list routes URLs to views. For more information please see:
7+
https://docs.djangoproject.com/en/4.2/topics/http/urls/
8+
Examples:
9+
Function views
10+
1. Add an import: from my_app import views
11+
2. Add a URL to urlpatterns: path('', views.home, name='home')
12+
Class-based views
13+
1. Add an import: from other_app.views import Home
14+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
15+
Including another URLconf
16+
1. Import the include() function: from django.urls import include, path
17+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
18+
"""
19+
20+
from api.views import error, fault, post_success, success, user_order
21+
from django.contrib import admin
22+
from django.urls import path
23+
24+
urlpatterns = [
25+
path("admin/", admin.site.urls),
26+
path("success", success),
27+
path("fault", fault),
28+
path("error", error),
29+
path("users/<str:user_id>/orders/<str:order_id>", user_order),
30+
path("post_success", post_success),
31+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
WSGI config for django_server project.
5+
6+
It exposes the WSGI callable as a module-level variable named ``application``.
7+
8+
For more information on this file, see
9+
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
10+
"""
11+
12+
import os
13+
14+
from django.core.wsgi import get_wsgi_application
15+
16+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_server.settings")
17+
18+
application = get_wsgi_application()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
# pylint: skip-file
5+
"""Django's command-line utility for administrative tasks."""
6+
import os
7+
import sys
8+
9+
10+
def main():
11+
"""Run administrative tasks."""
12+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_server.settings")
13+
try:
14+
from django.core.management import execute_from_command_line
15+
except ImportError as exc:
16+
raise ImportError(
17+
"Couldn't import Django. Are you sure it's installed and "
18+
"available on your PYTHONPATH environment variable? Did you "
19+
"forget to activate a virtual environment?"
20+
) from exc
21+
execute_from_command_line(sys.argv)
22+
23+
24+
if __name__ == "__main__":
25+
main()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[project]
2+
name = "django-server"
3+
description = "Simple server that relies on django library"
4+
version = "1.0.0"
5+
license = "Apache-2.0"
6+
requires-python = ">=3.8"

0 commit comments

Comments
 (0)