mongotest

package module
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2020 License: MIT Imports: 15 Imported by: 0

README

mongotest

mongotest was written with the goal of testing code that relies on mongo simpler. The only requirement to use mongotest is to have docker 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.

Documentation

Overview

Package mongotest provides helpers for running regressions using mongo. You can find helpers for: - ?running a database using docker - importing data from files - cleaning up a database

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFailedToConnectToDockerDaemon denotes that we couldn't connect to the docker daemon
	// running locally on the machine.
	ErrFailedToConnectToDockerDaemon = errors.New("could not connect to docker daemon")
	// ErrNoAvailablePorts denotes that no ports were available for binding the docker mongo instance to
	ErrNoAvailablePorts = errors.New("no ports are available to bind the docker mongo instance to")
	// ErrMongoContainerAlreadyRunning
	ErrMongoContainerAlreadyRunning = errors.New("the mongo container is already running - an attempt was made to call it a second time")
)

Functions

func GetAvailablePort

func GetAvailablePort() (port int, err error)

GetAvailablePort returns an available port on the system.

Types

type MongoTestError

type MongoTestError struct {
	// contains filtered or unexported fields
}

func NewMongoTestError

func NewMongoTestError(err error) *MongoTestError

func (*MongoTestError) Unwrap

func (mte *MongoTestError) Unwrap() error

type TestConnection

type TestConnection struct {
	*easymongo.Connection
	// contains filtered or unexported fields
}

TestConnection contains helpers for creating your own tests with mongo.

func NewTestConnection

func NewTestConnection(spinupDockerContainer bool) (*TestConnection, error)

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. Note that the first time this is called on a new system, the mongo docker container will be pulled. Any subsequent calls on the system should succeed without calls to pull. If spinupDockerContainer is False, then no docker shenanigans occur, instead an attempt is made to connect to a locally running mongo instance (e.g. mongodb://127.0.0.1:27017).

func (*TestConnection) KillMongoContainer

func (tc *TestConnection) KillMongoContainer() (err error)

KillMongoContainer tears down the specified container TODO: Is there some nifty hook I could use that allows me to always call this as the scope of a test exits?

func (*TestConnection) MongoContainerID

func (tc *TestConnection) MongoContainerID() string

MongoContainerID returns the ID of the running docker container If no container is running, an empty string will be returned.

func (*TestConnection) StartMongoContainer

func (tc *TestConnection) StartMongoContainer(portNumber int) (containerID string, err error)

StartMongoContainer starts a mongo docker container A note that the docker daemon on the system is expected to be running TODO: Is there a way to spawn the docker daemon myself?

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL