rabbitmq

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2021 License: MIT Imports: 12 Imported by: 0

README

Gnomock RabbitMQ

Gnomock RabbitMQ is a Gnomock preset for running tests against a real RabbitMQ message queue, without mocks.

package rabbitmq_test

import (
	"fmt"
	"testing"

	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/rabbitmq"
	"github.com/streadway/amqp"
	"github.com/stretchr/testify/require"
)

func TestPreset(t *testing.T) {
	t.Parallel()

	// gnomock setup
	p := rabbitmq.Preset(
		rabbitmq.WithUser("gnomock", "strong-password"),
	)

	container, err := gnomock.Start(p)
	require.NoError(t, err)
	defer func() { require.NoError(t, gnomock.Stop(container)) }()

	// actual test code
	uri := fmt.Sprintf(
		"amqp://%s:%s@%s",
		"gnomock", "strong-password",
		container.DefaultAddress(),
	)
	conn, err := amqp.Dial(uri)
	require.NoError(t, err)
	defer func() { require.NoError(t, conn.Close()) }()

	ch, err := conn.Channel()
	require.NoError(t, err)
	defer func() { require.NoError(t, ch.Close()) }()

	q, err := ch.QueueDeclare(
		"gnomock",
		false, // Durable
		false, // Delete when unused
		false, // Exclusive
		false, // No-wait
		nil,   // Arguments
	)
	require.NoError(t, err)

	msgBody := []byte("hello from Gnomock!")
	err = ch.Publish(
		"",     // exchange
		q.Name, // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        msgBody,
		},
	)
	require.NoError(t, err)

	msgs, err := ch.Consume(
		q.Name, // queue
		"",     // consumer
		true,   // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	require.NoError(t, err)

	m := <-msgs
	require.Equal(t, msgBody, m.Body)
}

Documentation

Overview

Package rabbitmq provides a Gnomock Preset for RabbitMQ.

Index

Constants

View Source
const ManagementPort = "management"

ManagementPort is a name of the port exposed by RabbitMQ management plugin. This port is only available when an appropriate version of RabbitMQ docker image is used. See `Preset` docs for more info.

Variables

This section is empty.

Functions

func Preset

func Preset(opts ...Option) gnomock.Preset

Preset creates a new Gmomock RabbitMQ preset. This preset includes a RabbitMQ specific healthcheck function and default RabbitMQ image and port.

By default, this preset does not use RabbitMQ Management plugin. To enable it, use one of the management tags with `WithVersion` option. Management port will be accessible using `container.Port(rabbitmq.ManagementPort)`. See https://hub.docker.com/_/rabbitmq/?tab=tags for a list of available tags.

When used without specifying username/password, default ones are used: guest/guest. Default version for this preset is 3.8.9.

Types

type Message

type Message struct {
	Queue       string `json:"queue"`
	ContentType string `json:"content_type"`
	StringBody  string `json:"string_body"`
	Body        []byte `json:"body"`
}

Message is a single message sent to RabbitMQ.

type Option

type Option func(*P)

Option is an optional configuration of this Gnomock preset. Use available Options to configure the container.

func WithMessages

func WithMessages(messages ...Message) Option

WithMessages makes sure that these messages can be consumed during the test once the container is ready.

func WithMessagesFile

func WithMessagesFile(file string) Option

WithMessagesFile allows to load messages to be sent into RabbitMQ from one or multiple files.

func WithUser

func WithUser(user, password string) Option

WithUser creates a new superuser with the provided credentials in the container.

func WithVersion

func WithVersion(version string) Option

WithVersion sets image version. See https://hub.docker.com/_/rabbitmq/?tab=tags for a list of available tags. Use ones with "management" to enable RabbitMQ management plugin, and get the mapped port using `container.Port(rabbitmq.ManagementPort)`.

type P

type P struct {
	User          string    `json:"user"`
	Password      string    `json:"password"`
	Version       string    `json:"version"`
	Messages      []Message `json:"messages"`
	MessagesFiles []string  `json:"messages_files"`
}

P is a Gnomock Preset implementation of RabbitMQ.

func (*P) Image

func (p *P) Image() string

Image returns an image that should be pulled to create this container.

func (*P) Options

func (p *P) Options() []gnomock.Option

Options returns a list of options to configure this container.

func (*P) Ports

func (p *P) Ports() gnomock.NamedPorts

Ports returns ports that should be used to access this container.

Jump to

Keyboard shortcuts

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