| 1 |
package mongodocstore |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"go.mongodb.org/mongo-driver/mongo" |
| 10 |
"go.mongodb.org/mongo-driver/mongo/options" |
| 11 |
"gocloud.dev/docstore/drivertest" |
| 12 |
"gocloud.dev/internal/testing/setup" |
| 13 |
) |
| 14 |
|
| 15 |
const connectionStringTemplate = "mongodb://%s:%s@%s/?connect=direct&connectTimeoutMS=3000" |
| 16 |
|
| 17 |
// To run the conformance tests against Amazon DocumentDB: |
| 18 |
// |
| 19 |
// 1. Run `terraform apply` in awsdocdb directory to provision a docdb cluster and an EC2 instance. |
| 20 |
// 2. Run the command provided by the terraform output string to setup port-forwarding. |
| 21 |
// 3. Set the following environment variables and run this test with `-record` flag. |
| 22 |
|
| 23 |
var ( |
| 24 |
username = os.Getenv("AWSDOCDB_USERNAME") |
| 25 |
password = os.Getenv("AWSDOCDB_PASSWORD") |
| 26 |
endpoint = os.Getenv("AWSDOCDB_ENDPOINT") // optional, default to localhost:27019 |
| 27 |
) |
| 28 |
|
| 29 |
func TestConformanceDocDB(t *testing.T) { |
| 30 |
if !*setup.Record { |
| 31 |
t.Skip("replaying is not yet supported for Amazon DocumentDB") |
| 32 |
} |
| 33 |
if username == "" || password == "" { |
| 34 |
t.Fatal("environment not setup to run DocDB test") |
| 35 |
} |
| 36 |
|
| 37 |
client := newDocDBTestClient(t) |
| 38 |
defer client.Disconnect(context.Background()) |
| 39 |
|
| 40 |
newHarness := func(context.Context, *testing.T) (drivertest.Harness, error) { |
| 41 |
return &harness{db: client.Database(dbName)}, nil |
| 42 |
} |
| 43 |
drivertest.RunConformanceTests(t, newHarness, codecTester{}, []drivertest.AsTest{verifyAs{}}) |
| 44 |
} |
| 45 |
|
| 46 |
func newDocDBTestClient(t *testing.T) *mongo.Client { |
| 47 |
ctx := context.Background() |
| 48 |
if endpoint == "" { |
| 49 |
endpoint = "localhost:27019" |
| 50 |
} |
| 51 |
connectionURI := fmt.Sprintf(connectionStringTemplate, username, password, endpoint) |
| 52 |
|
| 53 |
o := options.Client().ApplyURI(connectionURI) |
| 54 |
if err := o.Validate(); err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
client, err := mongo.NewClient(o) |
| 58 |
if err != nil { |
| 59 |
t.Fatalf("Failed to create client: %v", err) |
| 60 |
} |
| 61 |
|
| 62 |
err = client.Connect(ctx) |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("Failed to connect to cluster: %v", err) |
| 65 |
} |
| 66 |
|
| 67 |
// Force a connection to verify our connection string |
| 68 |
err = client.Ping(ctx, nil) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("Failed to ping cluster: %v", err) |
| 71 |
} |
| 72 |
return client |
| 73 |
} |