Skip to content

Commit

Permalink
initial commit (#722)
Browse files Browse the repository at this point in the history
Co-authored-by: wujiabang <wujiabang@actionsky.com>
  • Loading branch information
bo-er and bo-er authored Jan 4, 2023
1 parent 67184ce commit 77a7e99
Show file tree
Hide file tree
Showing 10 changed files with 620 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ updates:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/consul
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/datastore
schedule:
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/consul-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Consul example pipeline

on: [push, pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.sha }}
cancel-in-progress: true

jobs:
test-consul:
strategy:
matrix:
go-version: [1.18.x, 1.x]
runs-on: "ubuntu-latest"
steps:

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: modVerify
working-directory: ./examples/consul
run: go mod verify

- name: modTidy
working-directory: ./examples/consul
run: make tools-tidy

- name: gotestsum
working-directory: ./examples/consul
run: make test-unit

- name: Run checker
run: |
./scripts/check_environment.sh
- name: Test Summary
uses: test-summary/action@4ee9ece4bca777a38f05c8fc578ac2007fe266f7
with:
paths: "**/TEST-consul*.xml"
if: always()
9 changes: 9 additions & 0 deletions docs/examples/consul.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Consul

<!--codeinclude-->
[Creating a Consul container](../../examples/consul/consul.go)
<!--/codeinclude-->

<!--codeinclude-->
[Test for a Consul container](../../examples/consul/consul_test.go)
<!--/codeinclude-->
5 changes: 5 additions & 0 deletions examples/consul/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-consul
44 changes: 44 additions & 0 deletions examples/consul/consul.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package consul

import (
"context"
"fmt"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

// consulContainer represents the consul container type used in the module
type consulContainer struct {
testcontainers.Container
endpoint string
}

// setupConsul creates an instance of the consul container type
func setupConsul(ctx context.Context) (*consulContainer, error) {
req := testcontainers.ContainerRequest{
Image: "consul:latest",
ExposedPorts: []string{"8500/tcp", "8600/udp"},
Name: "badger",
Cmd: []string{"agent", "-server", "-ui", "-node=server-1", "-bootstrap-expect=1", "-client=0.0.0.0"},
WaitingFor: wait.ForListeningPort("8500/tcp"),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
mappedPort, err := container.MappedPort(ctx, "8500")
if err != nil {
return nil, err
}

host, err := container.Host(ctx)
if err != nil {
return nil, err
}

return &consulContainer{Container: container, endpoint: fmt.Sprintf("%s:%s", host, mappedPort.Port())}, nil
}
48 changes: 48 additions & 0 deletions examples/consul/consul_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package consul

import (
"bytes"
"context"
"testing"

"github.com/hashicorp/consul/api"
)

func TestConsul(t *testing.T) {
ctx := context.Background()

container, err := setupConsul(ctx)
if err != nil {
t.Fatal(err)
}

// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

// perform assertions
cfg := api.DefaultConfig()
cfg.Address = container.endpoint
client, err := api.NewClient(cfg)
if nil != err {
t.Fatal(err)
}
bs := []byte("apple")
_, err = client.KV().Put(&api.KVPair{
Key: "fruit",
Value: bs,
}, nil)
if nil != err {
t.Fatal(err)
}
pair, _, err := client.KV().Get("fruit", nil)
if err != nil {
t.Fatal(err)
}
if pair.Key != "fruit" || !bytes.Equal(pair.Value, []byte("apple")) {
t.Errorf("get KV: %v %s,expect them to be: 'fruit' and 'apple'\n", pair.Key, pair.Value)
}
}
64 changes: 64 additions & 0 deletions examples/consul/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module github.com/testcontainers/testcontainers-go/examples/consul

go 1.18

require (
github.com/hashicorp/consul/api v1.18.0
github.com/testcontainers/testcontainers-go v0.17.0
gotest.tools/gotestsum v1.8.2
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/containerd/containerd v1.6.14 // indirect
github.com/dnephin/pflag v1.0.7 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.20+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-hclog v0.12.0 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.0.0-20221128092401-c43b287e0e0f // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gotest.tools/v3 v3.4.0 // indirect
)

replace (
github.com/docker/docker => github.com/docker/docker v20.10.3-0.20221013203545-33ab36d6b304+incompatible // 22.06 branch
github.com/testcontainers/testcontainers-go => ../..
)
Loading

0 comments on commit 77a7e99

Please sign in to comment.