azurite

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 8 Imported by: 0

README

Gnomock Azurite

Gnomock Blobstorage is a Gnomock preset for running tests against Azure Blobstorage locally, powered by Azurite project. It allows to setup a number of supported Azure services locally, run tests against them, and tear them down easily.

See Azurite documentation for more details.

Testing against local Azurite
package azurite_test

import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/orlangure/gnomock"
)

func ExamplePresetBlobStorage() {
	p := azurite.Preset(
		azurite.WithVersion(azurite.DefaultVersion),
	)
	c, _ := gnomock.Start(p)

	defer func() { _ = gnomock.Stop(c) }()

	connString := fmt.Sprintf(azurite.ConnectionStringFormat, azurite.AccountName, azurite.AccountKey, c.Address(azurite.BlobServicePort), azurite.AccountName)
	ctx := context.Background()

	azblobClient, _ := azblob.NewClientFromConnectionString(connString, nil)

	containerName := "foo"
	_, _ = azblobClient.CreateContainer(ctx, containerName, nil)

	pager := azblobClient.NewListBlobsFlatPager(containerName, nil)
	pages := 0
	for pager.More() {
		resp, _ := pager.NextPage(context.TODO())
		fmt.Println("keys before:", len(resp.Segment.BlobItems))
		pages = pages + 1
	}
	fmt.Println("pages before:", pages)

	blobName := "bar"
	_, _ = azblobClient.UploadBuffer(ctx, containerName, blobName, []byte{15, 16, 17}, nil)

	pager = azblobClient.NewListBlobsFlatPager(containerName, nil)

	pages = 0
	for pager.More() {
		resp, _ := pager.NextPage(context.TODO())

		fmt.Println("keys after:", len(resp.Segment.BlobItems))
		for _, v := range resp.Segment.BlobItems {
			fmt.Println("filename:", *v.Name)
		}
		pages = pages + 1
	}
	fmt.Println("pages after:", 1)

	//Output:
	//keys before: 0
	//pages before: 1
	//keys after: 1
	//filename: bar
	//pages after: 1
}

Documentation

Overview

Package azurite provides a Gnomock Preset for azurite project It allows to easily setup local Blobstorage for testing

Index

Examples

Constants

View Source
const (
	BlobServicePort  = "blob"
	QueueServicePort = "queue"
	TableServicePort = "table"
)
View Source
const (
	ConnectionStringFormat = "DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s;BlobEndpoint=http://%s/%s;"
	AccountName            = "devstoreaccount1"
	AccountKey             = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
	DefaultVersion         = "3.22.0"
)

Variables

This section is empty.

Functions

func Preset

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

Preset creates a new azurite preset to use with gnomock.Start. See package docs for a list of exposed ports.

Example
package main

import (
	"context"
	"fmt"

	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
	"github.com/orlangure/gnomock/preset/azurite"

	"github.com/orlangure/gnomock"
)

func main() {
	p := azurite.Preset(
		azurite.WithVersion(azurite.DefaultVersion),
	)

	c, startErr := gnomock.Start(p)
	if startErr != nil {
		fmt.Println("Starting azurite gnomock failed ", startErr)
		return
	}

	defer func() { _ = gnomock.Stop(c) }()

	connString := fmt.Sprintf(
		azurite.ConnectionStringFormat,
		azurite.AccountName,
		azurite.AccountKey,
		c.Address(azurite.BlobServicePort),
		azurite.AccountName)
	ctx := context.Background()

	azblobClient, connectError := azblob.NewClientFromConnectionString(connString, nil)
	if connectError != nil {
		fmt.Println("Connecting to azurite failed ", connectError)
		return
	}

	containerName := "foo"

	_, createContainerError := azblobClient.CreateContainer(ctx, containerName, nil)
	if createContainerError != nil {
		fmt.Println("Creating azure container failed ", createContainerError)
		return
	}

	pages := 0

	pager := azblobClient.NewListBlobsFlatPager(containerName, nil)
	for pager.More() {
		resp, _ := pager.NextPage(context.Background())
		fmt.Println("keys before:", len(resp.Segment.BlobItems))
		pages++
	}
	fmt.Println("pages before:", pages)

	blobName := "bar"
	_, _ = azblobClient.UploadBuffer(ctx, containerName, blobName, []byte{15, 16, 17}, nil)

	pages = 0

	pager = azblobClient.NewListBlobsFlatPager(containerName, nil)
	for pager.More() {
		resp, _ := pager.NextPage(context.Background())

		fmt.Println("keys after:", len(resp.Segment.BlobItems))

		for _, v := range resp.Segment.BlobItems {
			fmt.Println("filename:", *v.Name)
		}
		pages++
	}
	fmt.Println("pages after:", 1)

}
Output:

keys before: 0
pages before: 1
keys after: 1
filename: bar
pages after: 1

Types

type Option

type Option func(*P)

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

func WithBlobstorageFiles

func WithBlobstorageFiles(path string) Option

WithBlobstorageFiles sets up Blobstorage service running in azurite with the contents of `path` directory. The first level children of `path` must be directories, their names will be used to create containers. Below them, all the files in any other directories, these files will be uploaded as-is.

For example, if you put your test files in testdata/my-container/dir/, Gnomock will create "my-container" for you, and pull "dir" with all its contents into this container.

func WithVersion

func WithVersion(version string) Option

WithVersion sets image version.

type P

type P struct {
	BlobstorePath string `json:"blobstore_path"`
	Version       string `json:"version"`
}

P is a Gnomock Preset blobstorage implementation.

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