Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
testcontainers.Container
}
Container represents the CosmosDB 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 CosmosDB container type
Example ¶
ctx := context.Background()
cosmosdbContainer, err := cosmosdb.Run(ctx, "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview")
defer func() {
if err := testcontainers.TerminateContainer(cosmosdbContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// }
state, err := cosmosdbContainer.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(state.Running)
Output: true
Example (Connect) ¶
ctx := context.Background()
cosmosdbContainer, err := cosmosdb.Run(ctx, "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview")
defer func() {
if err := testcontainers.TerminateContainer(cosmosdbContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
connString, err := cosmosdbContainer.ConnectionString(ctx)
if err != nil {
log.Printf("failed to get connection string: %s", err)
return
}
p, err := cosmosdb.NewContainerPolicy(ctx, cosmosdbContainer)
if err != nil {
log.Printf("failed to create policy: %s", err)
return
}
client, err := azcosmos.NewClientFromConnectionString(connString, p.ClientOptions())
if err != nil {
log.Printf("failed to create cosmosdb client: %s", err)
return
}
createDatabaseResp, err := client.CreateDatabase(ctx, azcosmos.DatabaseProperties{ID: "myDatabase"}, nil)
if err != nil {
log.Printf("failed to create database: %s", err)
return
}
// }
fmt.Println(createDatabaseResp.RawResponse.StatusCode == http.StatusCreated)
Output: true
func (*Container) ConnectionString ¶
ConnectionString returns a connection string that can be used to connect to the CosmosDB emulator. The connection string includes the account endpoint (host:port) and the default test account key. It returns an error if the port endpoint cannot be determined.
Format: "AccountEndpoint=<host>:<port>;AccountKey=<accountKey>"
type ContainerPolicy ¶
type ContainerPolicy struct {
// contains filtered or unexported fields
}
ContainerPolicy ensures that requests always target the CosmosDB emulator container endpoint. It overrides the CosmosDB client's globalEndpointManager, which would otherwise dynamically update http.Request.Host based on global endpoint discovery, pinning all requests to the container.
func NewContainerPolicy ¶
func NewContainerPolicy(ctx context.Context, c *Container) (*ContainerPolicy, error)
func (*ContainerPolicy) ClientOptions ¶
func (p *ContainerPolicy) ClientOptions() *azcosmos.ClientOptions
ClientOptions returns Azure CosmosDB client options that contain ContainerPolicy.