-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This preset doesn't support setting a custom username and password since I couldn't make it work with bitnami/cassandra docker image, which appears to always use `cassandra`/`cassandra` credentials.
- Loading branch information
Showing
14 changed files
with
366 additions
and
0 deletions.
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
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
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,52 @@ | ||
package gnomockd_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/orlangure/gnomock" | ||
"github.com/orlangure/gnomock/internal/gnomockd" | ||
_ "github.com/orlangure/gnomock/preset/cassandra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCassandra(t *testing.T) { | ||
t.Parallel() | ||
|
||
h := gnomockd.Handler() | ||
bs, err := ioutil.ReadFile("./testdata/cassandra.json") | ||
require.NoError(t, err) | ||
|
||
buf := bytes.NewBuffer(bs) | ||
w, r := httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/start/cassandra", buf) | ||
h.ServeHTTP(w, r) | ||
|
||
res := w.Result() | ||
|
||
defer func() { require.NoError(t, res.Body.Close()) }() | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
require.NoError(t, err) | ||
|
||
require.Equalf(t, http.StatusOK, res.StatusCode, string(body)) | ||
|
||
var c *gnomock.Container | ||
|
||
err = json.Unmarshal(body, &c) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, c.DefaultAddress()) | ||
|
||
bs, err = json.Marshal(c) | ||
require.NoError(t, err) | ||
|
||
buf = bytes.NewBuffer(bs) | ||
w, r = httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/stop", buf) | ||
h.ServeHTTP(w, r) | ||
|
||
res = w.Result() | ||
require.Equal(t, http.StatusOK, res.StatusCode) | ||
} |
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 @@ | ||
{"options":{},"preset":{"version":"latest"}} |
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,50 @@ | ||
# Gnomock Cassandra | ||
|
||
Gnomock Cassandra is a [Gnomock](https://github.com/orlangure/gnomock) preset for | ||
running tests against a real Cassandra container, without mocks. | ||
|
||
```go | ||
package cassandra_test | ||
|
||
func TestPreset(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, version := range []string{"4.0", "3"} { | ||
t.Run(version, testPreset(version)) | ||
} | ||
} | ||
|
||
func testPreset(version string) func(t *testing.T) { | ||
return func(t *testing.T) { | ||
p := cassandra.Preset( | ||
cassandra.WithVersion(version), | ||
) | ||
container, err := gnomock.Start(p) | ||
|
||
defer func() { require.NoError(t, gnomock.Stop(container)) }() | ||
|
||
require.NoError(t, err) | ||
|
||
addr := container.DefaultAddress() | ||
require.NotEmpty(t, addr) | ||
|
||
cluster := gocql.NewCluster(addr) | ||
cluster.Authenticator = gocql.PasswordAuthenticator{ | ||
Username: cassandra.DefaultUser, | ||
Password: cassandra.DefaultPassword, | ||
} | ||
|
||
session, err := cluster.CreateSession() | ||
require.NoError(t, err) | ||
|
||
defer session.Close() | ||
|
||
err = session.Query("create keyspace gnomock with replication = {'class':'SimpleStrategy', 'replication_factor' : 1};").Exec() | ||
require.NoError(t, err) | ||
|
||
err = session.Query("CREATE TABLE gnomock.test (id UUID, PRIMARY KEY (id));").Exec() | ||
require.NoError(t, err) | ||
} | ||
} | ||
``` | ||
|
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,12 @@ | ||
package cassandra | ||
|
||
// Option is an optional configuration of this Gnomock preset. Use available | ||
// Options to configure the container. | ||
type Option func(*P) | ||
|
||
// WithVersion sets image version. | ||
func WithVersion(version string) Option { | ||
return func(o *P) { | ||
o.Version = version | ||
} | ||
} |
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,94 @@ | ||
// Package cassandra includes Cassandra implementation of Gnomock Preset | ||
// interface. This Preset can be passed to gnomock.Start() function to create a | ||
// configured Cassandra container to use in tests. | ||
// | ||
// Cassandra containers always use cassandra/cassandra username/password pair, | ||
// it is currently not possible to use different values. | ||
package cassandra | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/gocql/gocql" | ||
"github.com/orlangure/gnomock" | ||
"github.com/orlangure/gnomock/internal/registry" | ||
) | ||
|
||
// By default, Cassandra containers will use these values. | ||
const ( | ||
DefaultUser = "cassandra" | ||
DefaultPassword = "cassandra" | ||
|
||
defaultVersion = "3" | ||
defaultPort = 9042 | ||
) | ||
|
||
func init() { | ||
registry.Register("cassandra", func() gnomock.Preset { return &P{} }) | ||
} | ||
|
||
// Preset creates a new Gmomock Cassandra preset. This preset includes a | ||
// Cassandra specific healthcheck function and default Cassandra image and | ||
// port. | ||
// | ||
// Containers created using this preset should be accessed using | ||
// cassandra/cassandra username/password pair. | ||
func Preset(opts ...Option) gnomock.Preset { | ||
p := &P{} | ||
|
||
for _, opt := range opts { | ||
opt(p) | ||
} | ||
|
||
return p | ||
} | ||
|
||
// P is a Gnomock Preset implementation for Cassandra. | ||
type P struct { | ||
Version string `json:"version"` | ||
} | ||
|
||
// Image returns an image that should be pulled to create this container. | ||
func (p *P) Image() string { | ||
return fmt.Sprintf("docker.io/bitnami/cassandra:%s", p.Version) | ||
} | ||
|
||
// Ports returns ports that should be used to access this container. | ||
func (p *P) Ports() gnomock.NamedPorts { | ||
return gnomock.DefaultTCP(defaultPort) | ||
} | ||
|
||
// Options returns a list of options to configure this container. | ||
func (p *P) Options() []gnomock.Option { | ||
p.setDefaults() | ||
|
||
opts := []gnomock.Option{ | ||
gnomock.WithHealthCheck(p.healthcheck), | ||
} | ||
|
||
return opts | ||
} | ||
|
||
func (p *P) setDefaults() { | ||
if p.Version == "" { | ||
p.Version = defaultVersion | ||
} | ||
} | ||
|
||
func (p *P) healthcheck(ctx context.Context, c *gnomock.Container) error { | ||
cluster := gocql.NewCluster(c.DefaultAddress()) | ||
cluster.Authenticator = gocql.PasswordAuthenticator{ | ||
Username: DefaultUser, | ||
Password: DefaultPassword, | ||
} | ||
|
||
session, err := cluster.CreateSession() | ||
if err != nil { | ||
return fmt.Errorf("failed to create a new session: %w", err) | ||
} | ||
|
||
session.Close() | ||
|
||
return 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,54 @@ | ||
package cassandra_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gocql/gocql" | ||
"github.com/orlangure/gnomock" | ||
"github.com/orlangure/gnomock/preset/cassandra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPreset(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, version := range []string{"4.0", "3"} { | ||
t.Run(version, testPreset(version)) | ||
} | ||
} | ||
|
||
func testPreset(version string) func(t *testing.T) { | ||
return func(t *testing.T) { | ||
p := cassandra.Preset( | ||
cassandra.WithVersion(version), | ||
) | ||
container, err := gnomock.Start(p) | ||
|
||
defer func() { require.NoError(t, gnomock.Stop(container)) }() | ||
|
||
require.NoError(t, err) | ||
|
||
addr := container.DefaultAddress() | ||
require.NotEmpty(t, addr) | ||
|
||
cluster := gocql.NewCluster(addr) | ||
cluster.Authenticator = gocql.PasswordAuthenticator{ | ||
Username: cassandra.DefaultUser, | ||
Password: cassandra.DefaultPassword, | ||
} | ||
|
||
session, err := cluster.CreateSession() | ||
require.NoError(t, err) | ||
|
||
defer session.Close() | ||
|
||
err = session.Query(` | ||
create keyspace gnomock | ||
with replication = {'class':'SimpleStrategy', 'replication_factor' : 1}; | ||
`).Exec() | ||
require.NoError(t, err) | ||
|
||
err = session.Query("CREATE TABLE gnomock.test (id UUID, PRIMARY KEY (id));").Exec() | ||
require.NoError(t, err) | ||
} | ||
} |
Oops, something went wrong.