Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit b5d26ac

Browse files
committed
testing gitlab-ci.yml e2e2-local job
1 parent bf6bf8f commit b5d26ac

File tree

4 files changed

+113
-80
lines changed

4 files changed

+113
-80
lines changed

.gitlab-ci.yml

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Build Documentation:
105105
- echo "Build backend complete"
106106

107107
# gitlab-runner exec docker "e2e cypress tests without docker-compose"
108-
.e2e cypress tests without docker-compose: &cypress
108+
e2e: &cypress
109109
stage: integration
110110
image: $CI_REGISTRY_IMAGE/backend:latest
111111
services:
@@ -138,80 +138,11 @@ Build Documentation:
138138
- backend/cypress/screenshots/
139139
expire_in: 7 days
140140

141-
# gitlab-runner exec docker "e2e cypress tests no compose local"
142-
e2e cypress tests no compose local:
141+
# use this test with gitlab-runner locally
142+
e2e-local:
143143
<<: *cypress
144144
image: localhost:5000/backend:latest
145145

146-
.e2e cypress tests with docker-compose:
147-
stage: integration
148-
image: docker:stable
149-
variables:
150-
DOCKER_HOST: tcp://docker:2375
151-
DOCKER_DRIVER: overlay2
152-
services:
153-
- docker:dind
154-
before_script:
155-
- apk add --update py-pip
156-
- pip install docker-compose~=1.23.0
157-
script:
158-
- ls
159-
- pwd
160-
- sh integration-tests.sh
161-
after_script:
162-
- echo "Integration tests passed."
163-
artifacts:
164-
paths:
165-
- cypress/videos/
166-
- tests/screenshots/
167-
expire_in: 7 days
168-
169-
# this doesn't work because services are not available to eachother,
170-
# services can only be accessed from the job's container
171-
# https://gitlab.com/gitlab-org/gitlab-selenium-server/issues/1#note_196708019
172-
.e2e: &e2e
173-
image: cypress/base:8
174-
stage: integration
175-
variables:
176-
# variables passed as env vars to *all services*
177-
SECRET_KEY: 'secret'
178-
DEBUG: ''
179-
DJANGO_SETTINGS_MODULE: 'backend.settings.gitlab-ci'
180-
CELERY_TASK_ALWAYS_EAGER: 'True'
181-
services:
182-
- name: postgres
183-
- name: $CI_REGISTRY_IMAGE/backend:latest
184-
alias: backend
185-
command: ["/start_ci.sh"]
186-
- name: redis
187-
- name: $CI_REGISTRY_IMAGE/frontend:latest
188-
alias: frontend
189-
before_script:
190-
- npm install --save-dev cypress
191-
- $(npm bin)/cypress verify
192-
script:
193-
- $(npm bin)/cypress run --config baseUrl=http://frontend
194-
after_script:
195-
- echo "Cypress tests complete"
196-
artifacts:
197-
paths:
198-
- cypress/videos/
199-
- cypress/screenshots/
200-
expire_in: 7 days
201-
202-
# for testing/debugging with gitlab-runner locally
203-
.e2e-local:
204-
<<: *e2e
205-
services:
206-
- name: postgres
207-
- name: localhost:5000/backend:latest
208-
alias: backend
209-
command: ["/start_ci.sh"]
210-
- name: redis
211-
- name: localhost:5000/frontend:latest
212-
alias: frontend
213-
artifacts: {}
214-
215146
Build Quasar PWA Assets:
216147
image: node:8
217148
stage: build

README.md

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,126 @@ or use this single command:
5656
docker exec -it backend bash -c 'cd notebooks && ../manage.py shell_plus --notebook'
5757
```
5858

59-
# Current Issues
59+
# Running tests
6060

61-
Currently Cypress tests are passing locally, but some of the test are failing in GitLab CI.
61+
This project uses Pytest, Jest and Cypress for testing. This section describes the different ways to run tests locally.
6262

63-
To run Cypress locally you can either run Cypress directly on your computer against the containerized application (for active development), or you can run Cypress in a container against the containerized application (this should be the same environment in GitLab CI using docker-compose with dind).
63+
To run Pytest and Jest, you can use `docker-compose exec`, or you can shell into the container.
6464

65-
To setup Cypress locally, run:
65+
Using `docker-compose exec`:
6666

6767
```
68-
npm install cypress --save
68+
docker-compose exec backend pytest
69+
```
70+
71+
```
72+
docker exec -it backend bash
73+
root@b24c4206002e:/code# pytest
74+
```
75+
76+
To run Jest tests, you can run:
77+
78+
```
79+
docker-compose exec frontend npm run test
80+
```
81+
82+
## Integration tests
83+
84+
Cypress can be used in an interactive mode with a UI, or it can be used in a headless way (such as in GitLab CI).
85+
86+
## Running Cypress Interactively
87+
88+
To run Cypress tests interactively, install Cypress in the root of the project:
89+
90+
```
91+
npm install cypress
6992
```
7093

7194
Then open Cypress with:
7295

7396
```
74-
$(npm bin)/cypress open
97+
$(npm bin)/cypress open --config baseUrl=http://localhost
98+
```
99+
100+
Click on the button "Run all specs", and the tests will start running in a new browser window with a log of Cypress actions and test statements displayed on the left side.
101+
102+
## Run Cypress in headless mode
103+
104+
There are two ways to run Cypress in headless mode. You can run Cypress against the `docker-compose` file in `compose/test.yml`, or you can run the Cypress test using `gitlab-runner`.
105+
106+
### Using `compose/test.yml`
107+
108+
To run the test locally against the production image, run the following:
109+
110+
```
111+
docker-compose -f compose/test.yml up --build
112+
```
113+
114+
This will build a special environment that resembles the environment used in GitLab CI. It brings up three containers: redis, postgres and backend. The backend serves several purposes: it runs the Django webserver, it runs the ASGI server used by Django Channels, and it runs celery tasks synchronously, and it also serves the Vue application through Django templates and static files. See the Dockerfile located at `backend/scripts/prod/Dockerfile` for details on how this works.
115+
116+
Make sure that Cypress is properly installed with:
117+
118+
```
119+
$(npm bin)/cypress verify
120+
```
121+
122+
Then start the tests with:
123+
124+
```
125+
$(npm bin)/cypress run --config baseUrl=http://localhost:9000
75126
```
76127

128+
You could also run these tests in the interactive mode using `compose/test.yml` with the following command:
129+
130+
```
131+
$(npm bin)/cypress open --config baseUrl=http://localhost:9000
132+
```
133+
134+
Note that this is similar to the previous command, but we are using the `open` command instead of the `run` command.
135+
136+
### Using `gitlab-runner`
137+
138+
It can be useful to debug your `.gitlab-ci.yml` jobs before pushing changes to GitLab. This gives us a faster feedback loop, and it doesn't use any of the CI minutes on your free (2000 minutes/month) or paid GitLab plans.
139+
140+
There is a little bit of setup needed to run Cypress tests with `gitlab-runner`.
141+
142+
First, you need to install `gitlab-runner` on your machine.
143+
144+
Next, you need to start a local registry on you r computer. Run the following command ([taken from docker documentation](https://docs.docker.com/registry/deploying/)):
145+
146+
```
147+
docker run -d -p 5000:5000 --restart=always --name registry registry:2
148+
```
149+
150+
Next, you need to build the production image that you will use in the test. To do this, run the following command:
151+
152+
```
153+
docker-compose -f compose/test.yml build backend
154+
```
155+
156+
Then tag the image with the following command:
157+
158+
```
159+
docker tag compose_backend:latest localhost:5000/backend:latest
160+
```
161+
162+
Then push the tagged image to the local registry:
163+
164+
```
165+
docker push localhost:5000/backend:latest
166+
```
167+
168+
Now that we have pushed the production image to our local registry, we can run the `e2e-local` job with `gitlab-runner`.
169+
170+
To do this, we need to make sure that the `e2e-local` job defined in `.gitlab-ci.yml` is not commented. To do this, remove the `.` in front of the job name (change `.e2e-local` to `e2e-local`).
171+
172+
Then, commit your changes in git. Gitlab runner requires that you commit changes before running tests. Run the GitLab CI job with the following command:
173+
174+
```
175+
gitlab-runner exec docker e2e-local
176+
```
177+
178+
77179
Run cypress tests locally by running the following commands. First build the application stack and cypress container:
78180

79181
```
@@ -94,7 +196,6 @@ docker-compose -f docker-compose.ci.yml -f cypress.yml up
94196

95197
# ToDo
96198

97-
- Fix Cypress testing in GitLab CI
98199
- Add diagram of local development
99200
- Put django apps in apps folder
100201
- Redeploy django app to check settings files

backend/accounts/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_create_user(self):
2626
pass
2727
with self.assertRaises(TypeError):
2828
User.objects.create_user()
29-
with self.assertRaises(TypeError):
29+
with self.assertRaises(ValueError):
3030
User.objects.create_user(email='')
3131
with self.assertRaises(ValueError):
3232
User.objects.create_user(email='', password="foo")

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ services:
9191
networks:
9292
- main
9393
environment:
94+
- CI_PIPELINE_TRIGGERED=True
9495
- SECRET_KEY=${SECRET_KEY}
9596
- DEBUG=True
9697
- DJANGO_EMAIL_HOST=${DJANGO_EMAIL_HOST}

0 commit comments

Comments
 (0)