Skip to content
This repository was archived by the owner on Nov 11, 2022. It is now read-only.

Commit 094b78c

Browse files
Set up Docker and edited Readme
1 parent bcc87ac commit 094b78c

File tree

6 files changed

+64
-63
lines changed

6 files changed

+64
-63
lines changed

.github/workflows/run-unittests.yml

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

Dockerfile

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
FROM python:3.10-alpine
1+
FROM python:3.9.5-slim
22

3+
# set env variables
34
ENV PYTHONDONTWRITEBYTECODE 1
45
ENV PYTHONUNBUFFERED 1
56

6-
RUN apk update \
7-
&& apk add postgresql-dev gcc python3-dev musl-dev
8-
9-
10-
RUN mkdir /code
11-
12-
WORKDIR /code
13-
14-
COPY . /code/
15-
RUN python -m pip install --upgrade pip
16-
7+
# install dependencies
8+
COPY requirements.txt .
179
RUN pip install -r requirements.txt
1810

19-
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
11+
# copy project
12+
COPY . .

Readme.md

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,61 @@
99
* _PyTest_
1010

1111

12-
## Some commands for comfortable run:
13-
14-
#### To run tests (pytest)
12+
## _Some commands for comfortable run:_
13+
### • To test:
14+
#### To run tests [pytest]:
1515
1. Activate venv
1616
2. `pip install -r requirements.txt`
1717
3. `coverage run -m pytest`
1818
4. `coverage report` or `coverage html` (for generating report in HTML)
19-
5. Remove `coverage-badge.svg` file and run `coverage-badge -o coverage.svg`
20-
 
19+
5. Remove `coverage-badge.svg` file and run `coverage-badge -o coverage.svg`
20+
21+
#### To run tests [unittest from task-source]:
22+
1. `pip install -r requirements.txt`
23+
2. Run server in Docker (see below how to run in docker) or in usual way ()
24+
3. `python unit_test.py`
2125

22-
#### To run application locally (you must have PostgreSQL installed and set up):
26+
### • To launch:
27+
#### To run application locally [no Docker] (you must have PostgreSQL installed and set up):
2328
1. Activate venv
2429
2. `pip install -r requirements.txt`
2530
3. Change `DOCKER` field in `config.json` to `false`
2631
4. `python manage.py makemigrations`
2732
5. `python manage.py migrate`
28-
6. `python manage.py runserver localhost:80` (or other port)
29-
30-
#### To set up remote machine:
31-
1. Connect to remote machine (via SSH)
32-
2. `sudo git clone <this_repo_url>`
33-
3. `cd rest-api-test-task`
34-
4. `sudo python3 -m venv venv`
35-
5. `source venv/bin/activate`
36-
6. `sudo pip3 install -r requirements.txt`
37-
7. [Auto running:](https://winitpro.ru/index.php/2019/10/11/avtozagruzka-servisov-i-skriptov-v-linux/)
38-
8. `sudo touch /etc/systemd/system/test-script.service`
39-
9. `sudo chmod 664 /etc/systemd/system/test-script.service`
40-
10. `sudo nano /etc/systemd/system/test-script.service`
41-
11. Write file
42-
12. Write entry-point SH executor
43-
13. `sudo systemctl enable test-script.service`
33+
6. `python manage.py runserver 0.0.0.0:80` (or other port, for example)
34+
35+
#### To run application locally [with Docker] (build & run)
36+
1. `docker-compose up -d --build`
37+
2. `docker-compose exec web python manage.py makemigrations --noinput`
38+
3. `docker-compose exec web python manage.py migrate --noinput`
39+
4. [optional] `docker-compose exec web python manage.py createsuperuser`
40+
5. `docker-compose up`
41+
42+
### • To deploy:
43+
#### To set up remote machine:
44+
1. Set `docker` field to `true`/`false` (depending on if you're going to run in Docker)
45+
2. Push changes to GitHub repository `git add <...>`, `git commit -m <...>`, `git push`
46+
3. Connect to remote machine (via SSH) `ssh login@ip`. Following actions are done from remote machine
47+
4. `git clone <this_repo_url>`
48+
5. `cd rest-api-test-task`
49+
6. For non-Docker run:
50+
1. `python3 -m venv venv`
51+
2. `source venv/bin/activate`
52+
3. `pip3 install -r requirements.txt`
53+
4. `python3 manage.py makemigrations`
54+
5. `python3 manage.py migrate`
55+
7. For Docker-run:
56+
1. `docker-compose up -d --build`
57+
2. `docker-compose exec web python manage.py makemigrations --noinput`
58+
3. `docker-compose exec web python manage.py migrate --noinput`
59+
4. [optional] `docker-compose exec web python manage.py createsuperuser`
60+
8. [Auto running:](https://winitpro.ru/index.php/2019/10/11/avtozagruzka-servisov-i-skriptov-v-linux/)
61+
9. `sudo touch /etc/systemd/system/test-script.service`
62+
10. `sudo chmod 664 /etc/systemd/system/test-script.service`
63+
11. `sudo nano /etc/systemd/system/test-script.service`
64+
12. Write file
65+
13. Write entry-point SH executor (for running with or without Docker)
66+
14. `sudo systemctl enable test-script.service`
4467
&nbsp;
4568

4669

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"DOCKER": false,
2+
"DOCKER": true,
33
"DEBUG": true
44
}

docker-compose.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
version: '3.7'
1+
version: '3.8'
22
services:
33
web:
4-
restart: always
54
build: .
6-
command: python manage.py runserver 0.0.0.0:80
7-
volumes:
8-
- ./:/usr/src/app/
5+
command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; python manage.py runserver 0.0.0.0:80'
96
ports:
10-
- "80:80"
11-
env_file:
12-
- ./.env.dev
7+
- 80:80
8+
environment:
9+
- DEBUG=1
1310
depends_on:
1411
- db
1512
db:
16-
image: postgres:13-alpine
13+
image: postgres:12-alpine
1714
volumes:
1815
- postgres_data:/var/lib/postgresql/data/
16+
expose:
17+
- 5432
1918
environment:
2019
- POSTGRES_USER=onlineserviceuser
2120
- POSTGRES_PASSWORD=onlineservicepassword

unit_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import urllib.parse
77
import urllib.request
88

9-
API_BASEURL = 'https://responses-1920.usr.yandex-academy.ru'
9+
API_BASEURL = 'http://localhost:80'
1010

1111

1212
ROOT_ID = "069cb8d7-bbdd-47d3-ad8f-82ef4c269df1"
@@ -239,8 +239,8 @@ def test_nodes():
239239

240240
def test_sales():
241241
status, response = request(f'/sales?date=2022-02-04T00:00:00.000Z')
242-
print(response)
243-
print(status)
242+
# print(response)
243+
# print(status)
244244
assert status == 200, f"Expected HTTP status code 200, got {status}"
245245
print("Test sales passed.")
246246

@@ -270,7 +270,7 @@ def test_delete():
270270
def test_all():
271271
test_import()
272272
test_nodes()
273-
#test_sales() # I have sales, but sometimes DEPLOYED app responses with 500
273+
test_sales() # I have sales, but sometimes DEPLOYED app responses with 500
274274
#test_stats()
275275
test_delete()
276276

0 commit comments

Comments
 (0)