Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithAcceptEULA ¶
func WithAcceptEULA() testcontainers.CustomizeRequestOption
WithAcceptEULA sets the ACCEPT_EULA environment variable to "Y" for the eventhubs container.
func WithConfig ¶
func WithConfig(r io.Reader) testcontainers.CustomizeRequestOption
WithConfig sets the eventhubs config file for the eventhubs container, copying the content of the reader to the container file at "/ServiceBus_Emulator/ConfigFiles/Config.json".
Types ¶
type Container ¶
type Container struct {
testcontainers.Container
// contains filtered or unexported fields
}
Container represents the Azure ServiceBus container type used in the module
func Run ¶
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
Run creates an instance of the Azure ServiceBus container type
Example ¶
ctx := context.Background()
serviceBusContainer, err := servicebus.Run(ctx, "mcr.microsoft.com/azure-messaging/servicebus-emulator:1.1.2", servicebus.WithAcceptEULA())
defer func() {
if err := testcontainers.TerminateContainer(serviceBusContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
state, err := serviceBusContainer.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(state.Running)
Output: true
Example (AuthenticateCreateClient) ¶
ExampleRun_authenticateCreateClient is inspired by the example from the Azure Service Bus Go SDK: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-go-how-to-use-queues?tabs=bash
// ===== 0. Create a Service Bus config including one queue =====
// cfg {
cfg := `{
"UserConfig": {
"Namespaces": [
{
"Name": "sbemulatorns",
"Queues": [
{
"Name": "queue.1",
"Properties": {
"DeadLetteringOnMessageExpiration": false,
"DefaultMessageTimeToLive": "PT1H",
"DuplicateDetectionHistoryTimeWindow": "PT20S",
"ForwardDeadLetteredMessagesTo": "",
"ForwardTo": "",
"LockDuration": "PT1M",
"MaxDeliveryCount": 10,
"RequiresDuplicateDetection": false,
"RequiresSession": false
}
}
]
}
],
"Logging": {
"Type": "File"
}
}
}`
// }
// ===== 1. Run the Service Bus container =====
// runServiceBusContainer {
ctx := context.Background()
serviceBusContainer, err := servicebus.Run(
ctx,
"mcr.microsoft.com/azure-messaging/servicebus-emulator:1.1.2",
servicebus.WithAcceptEULA(),
servicebus.WithConfig(strings.NewReader(cfg)),
)
defer func() {
if err := testcontainers.TerminateContainer(serviceBusContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// }
// ===== 2. Create a Service Bus client using a connection string to the namespace =====
// createClient {
connectionString, err := serviceBusContainer.ConnectionString(ctx)
if err != nil {
log.Printf("failed to get connection string: %s", err)
return
}
client, err := azservicebus.NewClientFromConnectionString(connectionString, nil)
if err != nil {
log.Printf("failed to create client: %s", err)
return
}
// }
// ===== 3. Send messages to a queue =====
// sendMessages {
message := "Hello, Testcontainers!"
sender, err := client.NewSender("queue.1", nil)
if err != nil {
log.Printf("failed to create sender: %s", err)
return
}
defer sender.Close(context.TODO())
sbMessage := &azservicebus.Message{
Body: []byte(message),
}
maxRetries := 3
// Retry sending the message 3 times, because the queue is created from the configuration
// and Testcontainers cannot add a wait strategy for the queue to be created.
for retries := 0; retries < maxRetries; retries++ {
err = sender.SendMessage(context.TODO(), sbMessage, nil)
if err == nil {
break
}
if retries == maxRetries-1 {
fmt.Printf("failed to send message after %d attempts: %s", maxRetries, err)
return
}
}
// }
// ===== 4. Receive messages from the queue =====
// receiveMessages {
receiver, err := client.NewReceiverForQueue("queue.1", nil)
if err != nil {
fmt.Printf("failed to create receiver: %s", err)
return
}
defer receiver.Close(context.TODO())
// Receive 1 message from the queue
messagesCount := 1
messages, err := receiver.ReceiveMessages(context.TODO(), messagesCount, nil)
if err != nil {
fmt.Printf("failed to receive messages: %s", err)
return
}
fmt.Printf("received %d messages\n", len(messages))
for _, message := range messages {
body := message.Body
fmt.Printf("%s\n", string(body))
err = receiver.CompleteMessage(context.TODO(), message, nil)
if err != nil {
fmt.Printf("failed to complete message: %s", err)
return
}
}
// }
Output: received 1 messages Hello, Testcontainers!
func (*Container) ConnectionString ¶
ConnectionString returns the connection string for the servicebus container, using the following format: Endpoint=sb://<hostname>:<port>;SharedAccessKeyName=<key-name>;SharedAccessKey=<key>;UseDevelopmentEmulator=true;
func (*Container) MSSQLContainer ¶
func (c *Container) MSSQLContainer() *mssql.MSSQLServerContainer
MSSQLContainer returns the mssql container that is used by the servicebus container
func (*Container) Terminate ¶
func (c *Container) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error
Terminate terminates the servicebus container, the mssql container, and the network to communicate between them.
type Option ¶
type Option func(*options) error
Option is an option for the ServiceBus container.
func WithMSSQL ¶
func WithMSSQL(img string, opts ...testcontainers.ContainerCustomizer) Option
WithMSSQL sets the image and options for the MSSQL container. By default, the image is "mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04".
func (Option) Customize ¶
func (o Option) Customize(*testcontainers.GenericContainerRequest) error
Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.