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
Copy file name to clipboardExpand all lines: README.md
+97-13Lines changed: 97 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@
13
13
</p>
14
14
15
15
# OpenMock
16
+
16
17
OpenMock is a Go service that can mock services in integration tests, staging environment, or anywhere.
17
18
The goal is to simplify the process of writing mocks in various channels.
18
19
Currently it supports the following channels:
@@ -23,22 +24,27 @@ Currently it supports the following channels:
23
24
- AMQP (e.g. RabbitMQ)
24
25
25
26
# Usage
27
+
26
28
Use it with docker.
29
+
27
30
```bash
28
31
$ docker run -it -p 9999:9999 -v $(pwd)/demo_templates:/data/templates checkr/openmock
29
32
```
30
33
31
34
More complete openmock instance (e.g. redis) with docker-compose.
35
+
32
36
```bash
33
37
$ docker-compose up
34
38
```
35
39
36
40
Test it.
41
+
37
42
```bash
38
43
$ curl localhost:9999/ping
39
44
```
40
45
41
46
Dependencies.
47
+
42
48
- HTTP (native supported, thanks to https://echo.labstack.com/)
43
49
- One can configure HTTP port, set env `OPENMOCK_HTTP_PORT=80`
44
50
- GRPC (supported through through HTTP/2 interface)
@@ -57,9 +63,11 @@ Dependencies.
57
63
- Used in Makefile during swagger admin API server generation
58
64
59
65
# OpenMock Templates
66
+
60
67
Templates are YAML files that describe the behavior of OpenMock.
61
68
62
69
## Templates Directory
70
+
63
71
You can put any number of `.yaml` or `.yml` files in a directory, and then point
64
72
environment variable `OPENMOCK_TEMPLATES_DIR` to it. OpenMock
65
73
will recursively (including subdirectories) load all the YAML files. For example:
@@ -78,8 +86,10 @@ will recursively (including subdirectories) load all the YAML files. For example
78
86
```
79
87
80
88
## Schema
89
+
81
90
OpenMock is configured a list of behaviors for it to follow. Each behavior is
82
91
identified by a key, and a kind:
92
+
83
93
```yaml
84
94
- key: respond-to-resource
85
95
kind: Behavior
@@ -116,9 +126,11 @@ we proceed with the actions.
116
126
routing_key: key_in
117
127
queue: key_in
118
128
```
119
-
129
+
120
130
### Actions
131
+
121
132
Actions are a series of functions to run. Availabe actions are:
133
+
122
134
- publish_amqp
123
135
- publish_kafka
124
136
- redis
@@ -152,6 +164,7 @@ Actions are a series of functions to run. Availabe actions are:
152
164
```
153
165
154
166
The actions by default run in the order defined in the mock file; you can adjust this by adding an int 'order' value from lowest to highest number. The default value for 'order' is 0.
167
+
155
168
```yaml
156
169
- key: every-op
157
170
kind: Behavior
@@ -174,6 +187,7 @@ The actions by default run in the order defined in the mock file; you can adjust
174
187
```
175
188
176
189
### Templates
190
+
177
191
Templates can be useful to assemble your payloads from parts
178
192
179
193
```yaml
@@ -209,10 +223,12 @@ Templates can be useful to assemble your payloads from parts
209
223
```
210
224
211
225
### Abstract Behaviors
226
+
212
227
Abstract Behaviors can be used to parameterize some data.
213
228
214
229
When an abstract behavior and a behavior extending it both have actions defined, all of them are run when the behavior matches. Actions will run from lowest to highest value of the 'order' field; if this is the same for two actions the action defined earlier in the abstract behavior runs first, followed by actions in the concrete behavior.
215
230
Be aware that values with all digits will be interpreted into `int` type (YAML syntax), and it will fail the condition check given that some helper functions are returning `string` types. Pipe to `toString` before the comparison or alternatively put quotes around the values. See example in `abstract_behaviors.yml`.
231
+
216
232
```yaml
217
233
- key: fruit-of-the-day
218
234
kind: AbstractBehavior
@@ -249,56 +265,61 @@ Be aware that values with all digits will be interpreted into `int` type (YAML s
249
265
- sleep:
250
266
duration: 1s
251
267
order: -1000
252
-
253
268
```
254
269
255
270
### Dynamic templating
271
+
256
272
OpenMock leverages [https://golang.org/pkg/text/template/](https://golang.org/pkg/text/template/) to write dynamic templates. Specifically, it supports a lot of _Context_ and _Helper Functions_.
257
273
258
274
- Usage of `{{ expr }}`. One can put `{{ expr }}` inside three types of places:
- `action.http.body_from_file`, `action.http.body_from_binary_file` `action.grpc.payload_from_file`, `action.kafka.payload_from_file`, `action.amqp.payload_from_file` (`{{ expr }}` will be in the file)
278
+
- `action.http.body_from_file`, `action.http.body_from_binary_file`, `action.http.binary_file_name` ,`action.grpc.payload_from_file`, `action.kafka.payload_from_file`, `action.amqp.payload_from_file` (`{{ expr }}` will be in the file)
Openmock also by default provides an API on port 9998 to control the running instance. See [api documentation](docs/api_docs/bundle.yaml). You can serve the api documentation by getting [go-swagger](https://github.com/go-swagger/go-swagger) and running:
Openmock has a command-line interface to help with certain tasks interacting with openmock instances. This is
317
341
invoked with the `omctl` command. This uses the [cobra](https://github.com/spf13/cobra) library to provide a discoverable CLI; run `omctl` for a list of commands / flags.
318
342
319
343
### CLI: Directory
344
+
320
345
#### Push
346
+
321
347
Pushes a local openmock model from the file system to a remote instance.
348
+
322
349
```
323
350
# Adds templates from the ./demo_templates directory to the instance running on localhost.
To enable advanced mocks, for example, your own encoding/decoding of the kafka messages,
573
650
one can develop by directly importing the `github.com/checkr/openmock` package, making a copy of the swagger-generated server main, and passing in a custom OpenMock.
574
651
575
652
For example:
576
653
(see [example](https://github.com/sesquipedalian-dev/openmock-custom-example/blob/master/main.go))
0 commit comments