Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0b01e88

Browse files
Integration tests with MongoDB using service containers
1 parent8888df8 commit0b01e88

File tree

2 files changed

+78
-65
lines changed

2 files changed

+78
-65
lines changed
Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title:"Integration Tests with Mongo"
3-
description:""
3+
description:"Launching a MongoDB service container"
44
group:yaml-examples
55
sub_group:examples
66
redirect_from:
@@ -9,79 +9,92 @@ redirect_from:
99
toc:true
1010
---
1111

12-
Using thisrepository, we'll help you get up to speed with basic functionality such as: compiling, testing and building Docker images.
12+
In thisexample we will see a NodeJS project that is using MongoDB for data storage. For the integration test phase we will launch an instance of MongoDB in order to run a set of[Mocha tests](https://mochajs.org/).
1313

14-
This project uses`Node.js, Mongo` to build an application which will eventually become a distributable Docker image.
14+
{% include image.html
15+
lightbox="true"
16+
file="/images/examples/integration-tests/mongodb-integration-tests.png"
17+
url="/images/examples/integration-tests/mongodb-integration-tests.png"
18+
alt="MongoDB integration tests with Codefresh"
19+
caption="MongoDB integration tests with Codefresh"
20+
max-width="90%"
21+
%}
1522

16-
##Looking around
23+
The Mocha tests are looking for a MongoDB connection at`mongo:27017`.
1724

18-
In the root of this repository, you'll find a file named`codefresh.yml`, this is our build descriptor and it describes the different steps that comprise our process. Let's quickly review the contents of this file:
25+
##The example NodeJS project
1926

20-
`codefresh.yml`
27+
You can see the example project at[https://github.com/codefreshdemo/example_nodejs_mongo](https://github.com/codefreshdemo/example_nodejs_mongo). The repository contains the NodeJS source code and the Mocha tests.
28+
29+
You can play with it locally by using Docker compose to launch both the applicaton and the MongoDB datastore.
30+
31+
##Create a pipeline with MongoDB integration tests
32+
33+
Here is the whole pipeline:
34+
35+
`codefresh.yml`
2136
{% highlight yaml %}
22-
version: '1.0'
37+
{% raw %}
38+
version: "1.0"
39+
stages:
40+
- prepare
41+
- build
42+
- test
2343
steps:
24-
build_step:
25-
type: build
26-
image_name: codefreshio/example_nodejs_mongo
27-
dockerfile: Dockerfile
28-
tag: {% raw %}${{CF_BRANCH}}{% endraw %}
29-
30-
unit_test:
31-
type: composition
32-
working_directory: {% raw %}${{build_step}}{% endraw %}
33-
composition:
34-
version: '2'
35-
services:
44+
main_clone:
45+
type: "git-clone"
46+
description: "Cloning main repository..."
47+
repo: "codefreshdemo/example_nodejs_mongo"
48+
revision: "master"
49+
git: github
50+
stage: prepare
51+
build_app_image:
52+
title: "Building Docker Image"
53+
type: "build"
54+
image_name: "node-mongo-app"
55+
tag: "master"
56+
dockerfile: "Dockerfile"
57+
stage: build
58+
run_integration_tests:
59+
title: "Running integration tests"
60+
stage: test
61+
image: '${{build_app_image}}'
62+
environment:
63+
- MONGO_PORT=27017
64+
commands:
65+
# MongoDB is certainly up at this point
66+
- cd /src
67+
- npm test
68+
services:
69+
composition:
3670
mongo:
37-
image: mongo
38-
composition_candidates:
39-
test:
40-
image: {% raw %}${{build_step}}{% endraw %}
41-
links:
42-
- mongo
43-
command: bash -c "/src/test-script.sh"
44-
environment:
45-
- MONGO_PORT=27017
46-
{% endhighlight %}
47-
48-
In this test script, we wait for`mongo` is ready, then we can run the tests
49-
50-
`script.sh`
51-
{% highlight sh %}
52-
#!/usr/bin/env bash
53-
wait_for_db() {
54-
nslookup mongo
55-
if ! nc -z mongo 27017; then
56-
echo "Waiting for db..."
57-
sleep 2
58-
wait_for_db
59-
fi
60-
}
61-
62-
wait_for_db
63-
64-
export MONGO_PORT=27017
65-
66-
cd /src
67-
npm install
68-
npm install -g mocha
69-
npm test
70-
{% endhighlight %}
71-
72-
<divclass="bd-callout bd-callout-info"markdown="1">
73-
#####Example
74-
75-
Just head over to the example[__repository__](https://github.com/codefreshdemo/example_nodejs_mongo){:target="_blank"} in Github.
76-
</div>
77-
78-
##Expected result
79-
80-
{% include image.html lightbox="true" file="/images/5033cde-codefresh_unit_test_mongo.png" url="/images/5033cde-codefresh_unit_test_mongo.png" alt="Codefresh unit test Mongo" max-width="65%" %}
71+
image: mongo:latest
72+
ports:
73+
- 27017
74+
readiness:
75+
timeoutSeconds: 30
76+
periodSeconds: 15
77+
image: '${{build_app_image}}'
78+
commands:
79+
- "nslookup mongo"
80+
- "nc -z mongo 27017"
81+
{% endraw %}
82+
{% endhighlight %}
83+
84+
This pipeline does the following:
85+
86+
1. Clones the source code with a[Git clone step]({{site.baseurl}}/docs/codefresh-yaml/steps/git-clone/)
87+
1.[Builds a Docker image]({{site.baseurl}}/docs/codefresh-yaml/steps/build/) with the application source code as well as the Mocha tests
88+
1. Runs mocha tests while launching a[service container]({{site.baseurl}}/docs/codefresh-yaml/service-containers/) for an active MongoDB instance
89+
90+
Notice that we also use the`readiness` property in the testing phase so that we can verify MongoDB is ready and listening, before running the tests.
8191

8292
##What to read next
8393

84-
-[Integration Tests with Redis]({{site.baseurl}}/docs/yaml-examples/examples/integration-tests-with-redis/)
94+
-[Service Containers]({{site.baseurl}}/docs/codefresh-yaml/service-containers/)
8595
-[Integration Tests with Postgres]({{site.baseurl}}/docs/yaml-examples/examples/integration-tests-with-postgres/)
8696
-[Integration Tests with MySQL]({{site.baseurl}}/docs/yaml-examples/examples/integration-tests-with-mysql/)
87-
-[Integration Tests with Mongo]({{site.baseurl}}/docs/yaml-examples/examples/integration-tests-with-mongo/)
97+
-[Integration Tests with Redis]({{site.baseurl}}/docs/yaml-examples/examples/integration-tests-with-redis/)
98+
99+
100+
74.5 KB
Loading

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp