Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
unmock: unmock/unmock@0.0.14
Use unmock
elements in your existing workflows and jobs.
Opt-in to use of uncertified orbs on your organization’s Security settings page.
Upload your Unmock test reporter files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: 2.1
orbs:
unmock: unmock/unmock@volatile
jobs:
build:
docker:
- image: circleci/node:10.15.1
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: yarn test
- unmock/upload:
file: __unmock__/report.html
Example of using Unmock server for mocking HTTPS traffic, taken from Unmock Golang example https://github.com/unmock/golang-example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
version: 2.1
orbs:
unmock: unmock/unmock@volatile
jobs:
build:
working_directory: ~/repo
docker:
- image: circleci/golang:1.12
steps:
- checkout
- run: go mod download
- unmock/start
- unmock/set:
code: 200
- run:
command: go test
environment:
GITHUB_TOKEN: fake-token
NO_PROXY: gopkg.in
- unmock/stop
workflows:
version: 2
test-and-build:
jobs:
- build
Starts Unmock server and proxy and sets the proxy environment variables. NOTE: This command is in pre-alpha and will require (1) installing Node.js and (2) running server from source.
Starts Unmock server and proxy in Docker container and sets the proxy environment variables. NOTE: This requires running the job in `machine` executor. Running this in `docker` will NOT work.
Sets unmock to return specific status code
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
code | Status code you want Unmock to return | No | 200 | integer |
Stores Unmock test report as a CircleCI artifact and sends a link in your GitHub PR if you have Unmock GitHub app installed (https://github.com/apps/unmock). Note that the uploaded report will be publicly available on the internet if your CircleCI project is public. Any and all feedback is appreciated!
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
file | Path to test report | No | __unmock__/unmock-report.html | string |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: >
Use unmock to mock API integrations in your CircleCI builds.
Repo Home: https://github.com/unmock/unmock-orb
Website: https://www.unmock.io/
orbs:
jq: circleci/jq@1.9.0
parameters: ¶meters
file:
type: string
description: "Path to test report"
default: "__unmock__/unmock-report.html"
set_environment_variables: &set_environment_variables
name: "Download server certificate and set environment variables"
command: |
wget https://raw.githubusercontent.com/unmock/unmock-js/dev/packages/unmock-server/certs/ca.pem
sleep 5
PROXY_URL=http://localhost:8008
echo "export HTTP_PROXY=${PROXY_URL}" >> $BASH_ENV
echo "export HTTPS_PROXY=${PROXY_URL}" >> $BASH_ENV
echo "export http_proxy=${PROXY_URL}" >> $BASH_ENV
echo "export https_proxy=${PROXY_URL}" >> $BASH_ENV
echo "export SSL_CERT_FILE=ca.pem" >> $BASH_ENV
check_report: &check_report
name: Check report exists
command: |
REPORT_FILE=<< parameters.file >>
if [ -z ${REPORT_FILE} ];
then
echo "Empty or missing environment variable: REPORT_FILE";
exit 1;
fi;
if [ -f ${REPORT_FILE} ]; # File exists
then
echo "Found file, proceeding with: ${REPORT_FILE}"
else
echo "Report not found: ${REPORT_FILE}, exiting."
exit 1;
fi
commands:
start:
description: "Starts Unmock server and proxy and sets the proxy environment variables. NOTE: This command is in pre-alpha and will require (1) installing Node.js and (2) running server from source."
steps:
- run:
name: "Install Node.js"
command: |
sudo apt-get update && sudo apt-get install -y nodejs npm
- run:
name: "Checkout Unmock server"
command: |
set -o errexit
set -o pipefail
git clone --single-branch --branch dev https://github.com/unmock/unmock-js.git
cd unmock-js && npm i && npm run compile
- run:
name: "Start Unmock proxy and server"
command: |
DEBUG=unmock* node unmock-js/packages/unmock-server/index.js
background: true
- run: *set_environment_variables
start-docker:
description: "Starts Unmock server and proxy in Docker container and sets the proxy environment variables. NOTE: This requires running the job in `machine` executor. Running this in `docker` will NOT work."
steps:
# - setup_remote_docker
- run:
name: "Ensure Docker is available"
command: |
docker pull unmock/unmock-server:latest
- run:
name: "Start Unmock proxy and server"
command: |
docker run -d --rm -p 8000:8000 -p 8008:8008 -p 8443:8443 -v $(pwd)/__unmock__:/app/__unmock__ --name unmock-server unmock/unmock-server
background: true
- run: *set_environment_variables
stop-docker:
description: "Stops Unmock Docker container started with `start-docker` command."
steps:
- run:
name: "Stop container"
command: "docker stop unmock-server"
call:
description: "Make a test call to Unmock server"
steps:
- run:
name: "Test curl"
command: |
curl -i http://petstore.swagger.io/v2/pet/23
set:
description: "Sets unmock to return specific status code"
parameters:
code:
type: integer
description: "Status code you want Unmock to return"
default: 200
steps:
- run:
name: "Set Unmock status code"
command: curl -X POST http://localhost:8000/api?code=<<parameters.code>>
stop:
description: "Stop the server"
steps:
- run:
name: "Kill Node.js processes"
command: |
echo $(pgrep node) # TODO Kill
upload:
description: "Stores Unmock test report as a CircleCI artifact and sends a link in your GitHub PR if you have Unmock GitHub app installed (https://github.com/apps/unmock). Note that the uploaded report will be publicly available on the internet if your CircleCI project is public. Any and all feedback is appreciated!"
parameters: *parameters
steps:
- run: *check_report
- store_artifacts:
path: << parameters.file >>
- jq/install
- run:
name: "Determine artifact URL"
command: |
set -o errexit
set -o pipefail
BUILD_ARTIFACTS_URL=https://circleci.com/api/v1.1/project/github/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BUILD_NUM}/artifacts
echo "Fetching artifact URL from: ${BUILD_ARTIFACTS_URL}"
REPORT_ARTIFACT_URL=$(curl --fail ${BUILD_ARTIFACTS_URL} | jq .[0].url) || echo "" # TODO Handle multiple artifacts
if [ -z ${REPORT_ARTIFACT_URL} ]; # Failed calling the API
then
# Fall back to the build page of the form
# https://circleci.com/gh/unmock/unmock-orb-example/32#artifacts
REPORT_ARTIFACT_URL=https://circleci.com/gh/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BUILD_NUM}#artifacts
fi
echo "Your report lives here: ${REPORT_ARTIFACT_URL}. Enjoy!"
echo "export REPORT_ARTIFACT_URL=${REPORT_ARTIFACT_URL}" >> $BASH_ENV
- run:
name: Post notification to the pull request if Unmock GitHub app is installed
command: |
set -o errexit
set -o pipefail
if [ -z ${REPORT_ARTIFACT_URL} ];
then
echo "REPORT_ARTIFACT_URL empty or undefined, exiting";
exit 0;
fi
# Try sending a notification if pull-request, allow failure.
if [ ! -z ${CIRCLE_PULL_REQUEST} ];
then
REPLY_CODE=$(curl -i --header "Content-Type: application/json" --request POST --data '{"pr":"'"${CIRCLE_PULL_REQUEST}"'","sha1":"'"${CIRCLE_SHA1}"'","url":"'"${REPORT_ARTIFACT_URL}"'"}' https://unmock-ci.azurewebsites.net/notifications/github |
head -n 1 | # Keep first line of response
cut -d$' ' -f2) # Split the line and take the second value (status code)
if [ ! ${REPLY_CODE} == 200 ];
then
echo "Failed sending notification with code ${REPLY_CODE}.";
else
echo "Sent notification."
fi;
else
echo "Not a pull-request, skipping sending notification."
fi
examples:
upload:
description: "Upload your Unmock test reporter files."
usage:
version: 2.1
orbs:
unmock: unmock/unmock@volatile
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:10.15.1
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
# run tests!
- run: yarn test
- unmock/upload:
file: "__unmock__/report.html"
start:
description: "Example of using Unmock server for mocking HTTPS traffic, taken from Unmock Golang example https://github.com/unmock/golang-example."
usage:
version: 2.1
orbs:
unmock: unmock/unmock@volatile
jobs:
build:
working_directory: ~/repo
docker:
- image: circleci/golang:1.12
steps:
- checkout
- run: go mod download
# Start Unmock proxy server in the background and set
# HTTP(S)_PROXY environment variables
- unmock/start
# Set server to return code 200
- unmock/set:
code: 200
# Run tests
- run:
command: go test
environment:
GITHUB_TOKEN: fake-token
# Do not use proxy for gopkg.in
NO_PROXY: gopkg.in
- unmock/stop
workflows:
version: 2
test-and-build:
jobs:
- build