Skip to content

Commit

Permalink
feat: add etcd resource [PIPE-971] (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
BonapartePC authored Apr 23, 2024
1 parent 20e08de commit 860b454
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/throttled/throttled/v2 v2.12.0
github.com/tidwall/gjson v1.17.1
github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04
go.etcd.io/etcd/client/v3 v3.5.10
go.opentelemetry.io/otel v1.25.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.25.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.25.0
Expand All @@ -52,6 +53,7 @@ require (
golang.org/x/sync v0.7.0
golang.org/x/text v0.14.0
google.golang.org/api v0.172.0
google.golang.org/grpc v1.63.0
google.golang.org/protobuf v1.33.0
gopkg.in/alexcesaro/statsd.v2 v2.0.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
Expand All @@ -70,6 +72,8 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
Expand Down Expand Up @@ -137,6 +141,8 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.etcd.io/etcd/api/v3 v3.5.10 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
Expand All @@ -152,7 +158,6 @@ require (
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/grpc v1.63.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ github.com/containerd/containerd v1.6.8 h1:h4dOFDwzHmqFEP754PgfgTeVXFnLiRc6kiqC7
github.com/containerd/containerd v1.6.8/go.mod h1:By6p5KqPK0/7/CgO/A6t/Gz+CUYUu2zf1hUaaymVXB0=
github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg=
github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -107,6 +111,7 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down Expand Up @@ -371,6 +376,12 @@ github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 h1:qXafrlZL1WsJW5OokjraLLRURHiw0OzKHD/RNdspp4w=
github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04/go.mod h1:FiwNQxz6hGoNFBC4nIx+CxZhI3nne5RmIOlT/MXcSD4=
go.etcd.io/etcd/api/v3 v3.5.10 h1:szRajuUUbLyppkhs9K6BRtjY37l66XQQmw7oZRANE4k=
go.etcd.io/etcd/api/v3 v3.5.10/go.mod h1:TidfmT4Uycad3NM/o25fG3J07odo4GBB9hoxaodFCtI=
go.etcd.io/etcd/client/pkg/v3 v3.5.10 h1:kfYIdQftBnbAq8pUWFXfpuuxFSKzlmM5cSn76JByiT0=
go.etcd.io/etcd/client/pkg/v3 v3.5.10/go.mod h1:DYivfIviIuQ8+/lCq4vcxuseg2P2XbHygkKwFo9fc8U=
go.etcd.io/etcd/client/v3 v3.5.10 h1:W9TXNZ+oB3MCd/8UjxHTWK5J9Nquw9fQBLJd5ne5/Ao=
go.etcd.io/etcd/client/v3 v3.5.10/go.mod h1:RVeBnDz2PUEZqTpgqwAtUd8nAPf5kjyFyND7P1VkOKc=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg=
Expand Down
13 changes: 13 additions & 0 deletions testhelper/docker/resource/etcd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package etcd

type Opt func(*Config)

func WithTag(tag string) Opt {
return func(c *Config) {
c.Tag = tag
}
}

type Config struct {
Tag string
}
67 changes: 67 additions & 0 deletions testhelper/docker/resource/etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package etcd

import (
"fmt"
"strconv"
"time"

"github.com/ory/dockertest/v3"
etcd "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"

"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource"
)

type Resource struct {
Client *etcd.Client
Hosts []string
Port int
}

func Setup(pool *dockertest.Pool, cln resource.Cleaner) (*Resource, error) {
etcdImage := "bitnami/etcd"
container, err := pool.Run(etcdImage, "3.5", []string{
"ALLOW_NONE_AUTHENTICATION=yes",
})
if err != nil {
return nil, fmt.Errorf("could not create container: %v", err)
}
cln.Cleanup(func() {
if err := pool.Purge(container); err != nil {
cln.Log(fmt.Errorf("could not purge ETCD resource: %v", err))
}
})

var (
etcdClient *etcd.Client
etcdHosts []string
etcdPort int

etcdPortStr = container.GetPort("2379/tcp")
)
etcdPort, err = strconv.Atoi(etcdPortStr)
if err != nil {
return nil, fmt.Errorf("could not convert port %q to int: %v", etcdPortStr, err)
}

etcdHosts = []string{"http://localhost:" + etcdPortStr}
err = pool.Retry(func() (err error) {
etcdClient, err = etcd.New(etcd.Config{
Endpoints: etcdHosts,
DialOptions: []grpc.DialOption{
grpc.WithBlock(), // block until the underlying connection is up
},
DialTimeout: 10 * time.Second,
})
return err
})
if err != nil {
return nil, fmt.Errorf("could not connect to dockerized ETCD: %v", err)
}

return &Resource{
Client: etcdClient,
Hosts: etcdHosts,
Port: etcdPort,
}, nil
}
35 changes: 35 additions & 0 deletions testhelper/docker/resource/etcd/etcd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package etcd

import (
"context"
"testing"

"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
)

const (
dummyKey = "dummyKey"
dummyValue = "dummyValue"
)

func TestETCD(t *testing.T) {
pool, err := dockertest.NewPool("")
require.NoError(t, err)

etcdRes, err := Setup(pool, t)
require.NoError(t, err)

getResp, err := etcdRes.Client.Get(context.Background(), dummyKey) // no value should be present during start
require.NoError(t, err)

require.Zero(t, getResp.Kvs)

_, err = etcdRes.Client.Put(context.Background(), dummyKey, dummyValue) // put value in to dummyKey
require.NoError(t, err)

getResp, err = etcdRes.Client.Get(context.Background(), dummyKey) // check value in dummyKey
require.NoError(t, err)
require.Len(t, getResp.Kvs, 1)
require.Equal(t, string(getResp.Kvs[0].Value), dummyValue)
}

0 comments on commit 860b454

Please sign in to comment.