Categorygithub.com/tophergopher/mongotest
modulepackage
0.2.9
Repository: https://github.com/tophergopher/mongotest.git
Documentation: pkg.go.dev

# README

mongotest

mongotest was written with the goal of creating simple, segregated testing mongo environments. The only requirement to use mongotest is to have the docker daemon running (should you wish to use the docker container method).

Example:

func TestFoo(t *testing.T) {
  useDockerContainer := true
  // conn is a mongotest.TestConnection object which embeds an easymongo.Connection object
  conn, err := mongotest.NewTestConnection(useDockerContainer)
  is.NoError(err)
  t.Cleanup(func() {
    conn.KillMongoContainer()
  })
  // Insert a document
  type enemy struct {
    ID   primitive.ObjectID  `bson:"_id"`
    Name string              `bson:"name"`
  }
  id, err := conn.Insert().One(&enemy{
    ID:   primitive.NewObjectID(),
    Name: "The Joker",
  })
}

The above code will spin-up a docker mongo container on a randomly assigned port, insert a document into the collection and when the test exits, the mongo container will be destroyed. In order to ensure that the docker container gracefully exits, it is recommended to run the .KillMongoContainer() command in a t.Cleanup() function.

If you choose to use a defer (rather than t.Cleanup()), note that it is (presently) not possible to automatically cleanup the created container should the test panic.

* I was wondering if a compiler flag might be the way to go to always ensure clean-up, but I truly welcome input on how this might be accomplished cleanly.

Cleaning up rogue containers

Containers are created with a label of mongotest=regression. If you run docker ps and note a lot of unreaped mongo containers, try running:

    docker rm --force $(docker ps -a -q --filter=label=mongotest=regression)

This will thwack any containers that were created via mongotest.

# Functions

EasyMongoWithContainer spawns a docker container on an available port, connects to the mongo database, runs the provided function, then kills the mongo container as it exits.
GenerateCARoot generates a new CA root PEM file and private key Huge thanks to Mattemagikern for publishing this code in a random gist https://gist.github.com/Mattemagikern/328cdd650be33bc33105e26db88e487d.
GetAvailablePort returns an available port on the system.
MongoClientWithContainer spawns a docker container on an available port, connects to the mongo database, runs the provided function, then kills the mongo container as it exits.
No description provided by the author
NewReplicaSetContainer spawns a new docker container and configures it as a 1 member replicaset.
NewTestConnection is the standard method for initializing a TestConnection - it has a side-effect of spawning a new docker container if spinupDockerContainer is set to true.
No description provided by the author

# Variables

ErrFailedToConnectToDockerDaemon denotes that we couldn't connect to the docker daemon running locally on the machine.
ErrMongoContainerAlreadyRunning.
ErrNoAvailablePorts denotes that no ports were available for binding the docker mongo instance to.
No description provided by the author

# Structs

DatabaseExporter is a wrapper to enable easily exporting a live database TODO: Move this into easymongo.
No description provided by the author
TestConnection contains helpers for creating your own tests with mongo.