-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add etcd resource [PIPE-971] (#440)
- Loading branch information
1 parent
20e08de
commit 860b454
Showing
5 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |