mongofake

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mongofake is a fake of a MongoDB server for the tests of the MongoDB flag source: it speaks the wire protocol (OP_MSG, and the OP_QUERY of the first handshake) on a loopback port, and answers hello, ping, find (sorted by _id, in batches), getMore, killCursors, listCollections and endSessions from collections it keeps in memory. A client that uses authMechanism=PLAIN can log in; the other mechanisms are refused. What it models is from the documentation of the protocol and the behaviour of the Go driver; it is no MongoDB: the docker tests of the flag source run the real server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fault

type Fault int

Fault is what the server does with the next data commands (find, getMore, listCollections).

const (
	// FaultNone answers.
	FaultNone Fault = iota
	// FaultUnauthorized answers with the error "Unauthorized" (code 13): the user has no role that reads.
	FaultUnauthorized
	// FaultShutdown answers with the error "InterruptedAtShutdown" (code 11600): a failure that may pass.
	FaultShutdown
	// FaultDrop closes the connection without an answer.
	FaultDrop
)

The faults.

type Server

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

Server is a MongoDB fake on a loopback port. Its listener is open from New on.

func New

func New(tb testing.TB) *Server

New starts the server, and closes it when the test ends.

func NewTLS

func NewTLS(tb testing.TB, cfg *tls.Config) *Server

NewTLS starts a server that speaks TLS with the configuration (its certificate and its versions). The URI of the server asks for TLS: tls=true.

func (*Server) Close

func (s *Server) Close()

Close stops the server and closes its connections. It is safe to call twice.

func (*Server) Commands

func (s *Server) Commands() []string

Commands are the data commands the server received so far, as "name database.collection" (listCollections as "listCollections database").

func (*Server) Drop

func (s *Server) Drop(database, collection string)

Drop removes the collection.

func (*Server) Hold

func (s *Server) Hold(hook func(context.Context) error)

Hold makes the data commands wait for hook before they are answered; a hook that returns an error closes the connection. The context of the hook ends when the connection or the server closes. Nil removes the hold.

func (*Server) Host

func (s *Server) Host() string

Host is the address of the server, host and port.

func (*Server) Inject

func (s *Server) Inject(fault Fault)

Inject makes the data commands fail the way the fault says until it is called again with FaultNone.

func (*Server) InjectOn

func (s *Server) InjectOn(command string, fault Fault)

InjectOn is Server.Inject for one command (find, getMore or listCollections); the fault of the command wins over the one of Inject.

func (*Server) OpenCursors

func (s *Server) OpenCursors() int

OpenCursors is the number of cursors that the server still keeps: a cursor is kept for the documents that no batch has carried yet, and ends when its last batch is read or when the client kills it.

func (*Server) Require

func (s *Server) Require(user, password string)

Require makes the server ask for a login: the user and the password of authMechanism=PLAIN. The URI of such a client is "mongodb://user:password@host/?authMechanism=PLAIN".

func (*Server) Set

func (s *Server) Set(database, collection string, docs ...bson.D)

Set replaces the documents of the collection of the database; no document makes an empty collection that exists.

func (*Server) URI

func (s *Server) URI() string

URI is the connection string of the server, without credentials (with tls=true for a server of NewTLS).

Jump to

Keyboard shortcuts

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