influxdb

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 6 Imported by: 0

README

Gnomock InfluxDB

Gnomock InfluxDB is a Gnomock preset for running tests against a real InfluxDB container, without mocks.

package influxdb_test

import (
	"context"
	"fmt"
	"testing"

	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
	"github.com/influxdata/influxdb-client-go/v2/domain"
	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/influxdb"
	"github.com/stretchr/testify/require"
)

func TestInfluxDB(t *testing.T) {
	username := "gnomock"
	password := "gnomock-password"
	org := "gnomock-org"
	bucket := "gnomock-bucket"
	token := "gnomock-influxdb-token"

	p := influxdb.Preset(
		influxdb.WithVersion("alpine"),
		influxdb.WithAuthToken(token),
		influxdb.WithBucket(bucket),
		influxdb.WithOrg(org),
		influxdb.WithUser(username, password),
	)
	container, err := gnomock.Start(p)
	require.NoError(t, err)

	defer func() { require.NoError(t, gnomock.Stop(container)) }()

	addr := fmt.Sprintf("http://%s", container.DefaultAddress())
	client := influxdb2.NewClient(addr, token)
	ctx := context.Background()

	h, err := client.Health(ctx)
	require.NoError(t, err)
	require.Equal(t, domain.HealthCheckStatusPass, h.Status)

	buckets, err := client.BucketsAPI().GetBuckets(ctx)
	require.NoError(t, err)
	require.NotEmpty(t, buckets)

	bucketNames := make([]string, 0, len(*buckets))
	for _, bucket := range *buckets {
		bucketNames = append(bucketNames, bucket.Name)
	}

	require.Contains(t, bucketNames, bucket)

	users, err := client.UsersAPI().GetUsers(ctx)
	require.NoError(t, err)

	userNames := make([]string, 0, len(*users))
	for _, user := range *users {
		userNames = append(userNames, user.Name)
	}

	require.Contains(t, userNames, username)

	orgs, err := client.OrganizationsAPI().GetOrganizations(ctx)
	require.NoError(t, err)
	require.NotEmpty(t, orgs)

	orgNames := make([]string, 0, len(*orgs))
	for _, org := range *orgs {
		orgNames = append(orgNames, org.Name)
	}

	require.Contains(t, orgNames, org)
}

Documentation

Overview

Package influxdb includes InfluxDB implementation of Gnomock Preset interface. This Preset can be passed to gnomock.Start() function to create a configured InfluxDB container to use in tests.

Currently this preset only supports InfluxDB 2.x. If you are interested in earlier versions, please consider opening an issue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Preset

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

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

Types

type Option

type Option func(*P)

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

func WithAuthToken

func WithAuthToken(token string) Option

WithAuthToken sets authentication token to be used to connect to this container.

func WithBucket

func WithBucket(bucket string) Option

WithBucket sets the initial bucket name for this container.

func WithOrg

func WithOrg(org string) Option

WithOrg sets organization name for this container.

func WithUser

func WithUser(username, password string) Option

WithUser sets super-user name and password for this container.

func WithVersion

func WithVersion(version string) Option

WithVersion sets image version.

type P

type P struct {
	Version   string `json:"version"`
	Username  string `json:"username"`
	Password  string `json:"password"`
	Org       string `json:"org"`
	Bucket    string `json:"bucket"`
	AuthToken string `json:"auth_token"`
}

P is a Gnomock Preset implementation for InfluxDB.

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