You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
description:"Launching a MongoDB service container"
4
4
group:yaml-examples
5
5
sub_group:examples
6
6
redirect_from:
@@ -9,79 +9,92 @@ redirect_from:
9
9
toc:true
10
10
---
11
11
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/).
13
13
14
-
This project uses`Node.js, Mongo` to build an application which will eventually become a distributable Docker image.
caption="MongoDB integration tests with Codefresh"
20
+
max-width="90%"
21
+
%}
15
22
16
-
##Looking around
23
+
The Mocha tests are looking for a MongoDB connection at`mongo:27017`.
17
24
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
19
26
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`
21
36
{% highlight yaml %}
22
-
version: '1.0'
37
+
{% raw %}
38
+
version: "1.0"
39
+
stages:
40
+
- prepare
41
+
- build
42
+
- test
23
43
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:
36
70
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
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.
81
91
82
92
##What to read next
83
93
84
-
-[Integration Tests with Redis]({{site.baseurl}}/docs/yaml-examples/examples/integration-tests-with-redis/)