Skip to content

Commit

Permalink
Implement container cleanup mechanism
Browse files Browse the repository at this point in the history
From now, every container started using gnomock will have a sidecar
container whose only purpose is to make sure it is cleaned up when the
tests complete.

To trigger the cleanup, send a request to /sync/:id. It will hang (make
sure timeout is long enough). When tests complete, the program exits,
and the request it released automatically, signaling the sidecar that
it's time. After that, regardless of the success of container
termination, cleaner process stops.

If a user chooses to use `gnomock.Stop()` anyway, the sidecar will be
terminated immediately. For that, sidecar id is now encoded into
container id as returned to the user (both directly and over HTTP)
  • Loading branch information
orlangure committed Jan 14, 2021
1 parent fcf15a3 commit e311160
Show file tree
Hide file tree
Showing 25 changed files with 1,172 additions and 220 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ jobs:
- name: Test Gnomock
run: go test -race -cover -coverprofile=gnomock-cover.txt -coverpkg=./... -v .
- name: Test gnomockd
run: go test -race -cover -coverprofile=gnomockd-cover.txt -v ./internal/gnomockd -run TestGnomockd
run: go test -race -cover -coverprofile=gnomockd-cover.txt -coverpkg=./... -v ./internal/gnomockd -run TestGnomockd
- name: Test internal
run: go test -race -cover -coverprofile=internal-cover.txt -v ./internal/errors ./internal/registry
run: |
go test -race -cover -coverprofile=internal-cover.txt -v -coverpkg=./... \
./internal/errors \
./internal/registry \
./internal/cleaner \
./internal/health
- name: Report coverage
run: |
cat gnomock-cover.txt gnomockd-cover.txt internal-cover.txt > coverage.txt
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ linters:
- gosec
- interfacer
- lll
- maligned
- misspell
- nakedret
- prealloc
Expand Down
102 changes: 14 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<img src="https://github.com/orlangure/gnomock/blob/master/gnomock.png">
<img src="https://github.com/orlangure/gnomock/raw/master/gnomock.png">
</div>

# <div align="center">Gnomock – tests without mocks</div>
Expand Down Expand Up @@ -51,14 +51,13 @@ services running in ephemeral Docker containers:
Gnomock can be used in two different ways:

- Imported directly as a package in any **Go** project
- Accessed over HTTP running as a daemon in any other project
- Accessed over HTTP running as a daemon in **any other language**

⚠️ Both ways **require** an active Docker daemon running locally in
the same environment.

### Using Gnomock in Go applications

Gnomock can be used in Go programs directly, without running a local server.
See the following example to get started:

```
Expand Down Expand Up @@ -100,91 +99,21 @@ Preset documentation, refer to [Presets](#official-presets) section.
### Using Gnomock in other languages

If you use Go, please refer to [Using Gnomock in Go
applications](#using-gnomock-in-go-applications) section. Otherwise, you'll
need to setup a helper container, and communicate with it over HTTP.
applications](#using-gnomock-in-go-applications) section. Otherwise, refer to
[documentation](docs/server.md).

To start a `gnomock` server, run the following on any Unix-based system:

```bash
docker run --rm \
-p 23042:23042 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:`pwd` \
orlangure/gnomock
```

`-p 23042:23042` exposes a port on the host to communicate with `gnomock`. You
can use any port you like, just make sure to configure the client properly.

`-v /var/run/docker.sock:/var/run/docker.sock` allows `gnomock` to communicate
with the docker engine running on host. Without it `gnomock` can't access
docker.

If you use any file-related `gnomock` options, like `WithQueriesFile`, you have
to make the path you use available inside the container:

```
# this makes the current folder appear inside the container under the same
# path and name:
-v `pwd`:`pwd`
```

Any program in any language can communicate with `gnomock` server using OpenAPI
3.0 [specification](https://app.swaggerhub.com/apis/orlangure/gnomock/).

Below is an example of setting up a **MySQL** container using a `POST` request:

```
$ cat mysql-preset.json
{
"preset": {
"db": "mydb",
"user": "gnomock",
"password": "p@s$w0rD",
"queries": [
"create table foo(bar int)",
"insert into foo(bar) values(1)"
],
"queries_file": "/home/gnomock/project/testdata/mysql/queries.sql"
},
"options": {}
}
$ curl --data @mysql-preset.json http://127.0.0.1:23042/start/mysql
{
"id": "f5d08dc84421",
"host": "string",
"ports": {
"default": {
"protocol": "tcp",
"port": 35973
}
}
}
```

There are auto-generated wrappers for the available API:

| Client | Sample code |
|--------|-------------|
| [Python SDK](https://github.com/orlangure/gnomock-python-sdk) | [Code](https://github.com/orlangure/gnomock/blob/master/sdktest/python/test/test_sdk.py) |
| JavaScript SDK | |
| Ruby SDK | |
| PHP SDK | |
| Java SDK | |
| [Other](https://openapi-generator.tech/docs/generators) languages | |
## Official presets

**For more details and a full specification, see
[documentation](https://app.swaggerhub.com/apis/orlangure/gnomock/).**
The power of Gnomock is in the Presets. Existing Presets with their
supported<sup>\*</sup> versions are listed below.

## Official presets
<small>*\* **Supported** versions are tested as part of CI pipeline. Other
versions might work as well.*</small>

The power of Gnomock is in the Presets. Presets, both existing and planned, are
listed below:

| Preset | Go package | HTTP API | Go API | Supported versions |
|--------|------------|----------|--------|---------------------|
Localstack | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/localstack) | [Docs](https://app.swaggerhub.com/apis/orlangure/gnomock/1.4.6#/presets/startLocalstack) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/localstack?tab=doc) | `0.12.2`
Localstack (AWS) | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/localstack) | [Docs](https://app.swaggerhub.com/apis/orlangure/gnomock/1.4.6#/presets/startLocalstack) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/localstack?tab=doc) | `0.12.2`
Splunk | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/splunk) | [Docs](https://app.swaggerhub.com/apis/orlangure/gnomock/1.4.6#/presets/startSplunk) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/splunk?tab=doc) | `8.0.2`
Redis | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/redis) | [Docs](https://app.swaggerhub.com/apis/orlangure/gnomock/1.4.6#/presets/startRedis) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/redis?tab=doc) | `5.0.10`, `6.0.9`
Memcached | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/memcached) | [Docs](https://app.swaggerhub.com/apis/orlangure/gnomock/1.4.6#/presets/startMemcached) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/memcached?tab=doc) | `1.6.9`
Expand All @@ -199,13 +128,10 @@ Elasticsearch | [Go package](https://github.com/orlangure/gnomock/tree/master/pr
Kubernetes | [Go package](https://github.com/orlangure/gnomock/tree/master/preset/k3s) | [Docs](https://app.swaggerhub.com/apis/orlangure/gnomock/1.4.6#/presets/startKubernetes) | [Reference](https://pkg.go.dev/github.com/orlangure/gnomock/preset/k3s?tab=doc) | `v1.19.3`
<!-- new presets go here -->

Please note that "Supported versions" means that only these versions are
tested. There is a chance that other versions work as well, unless they are
very new with breaking changes, or very old and no longer supported by anybody.

It is possible to use Gnomock directly from Go code without any presets. HTTP
API only allows to setup containers using presets that exist in this
repository.
It is possible to use Gnomock [directly from
Go](https://pkg.go.dev/github.com/orlangure/gnomock#StartCustom) code without
any presets. HTTP API only allows to setup containers using presets that exist
in this repository.

## Similar projects

Expand Down
14 changes: 14 additions & 0 deletions cmd/cleaner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:latest AS builder

WORKDIR /cleaner/
ADD go.mod .
ADD go.sum .
RUN go mod download
RUN go mod verify
ADD . .
RUN CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o /gnomock-cleaner .

FROM scratch

COPY --from=builder /gnomock-cleaner /gnomock-cleaner
ENTRYPOINT ["/gnomock-cleaner"]
5 changes: 5 additions & 0 deletions cmd/cleaner/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/orlangure/gnomock/cmd/cleaner

go 1.15

require github.com/orlangure/gnomock v0.10.1
Loading

0 comments on commit e311160

Please sign in to comment.