splunk

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: 15 Imported by: 0

README

Gnomock Splunk

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

package splunk_test

import (
	"fmt"
	"sort"
	"time"

	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/splunk"
)

func ExamplePreset() {
	events := []splunk.Event{
		{
			Event:      "action=foo",
			Index:      "events",
			Source:     "app",
			SourceType: "http",
			Time:       time.Now().UnixNano(),
		},
		{
			Event:      "action=bar",
			Index:      "events",
			Source:     "app",
			SourceType: "http",
			Time:       time.Now().UnixNano(),
		},
	}

	p := splunk.Preset(
		splunk.WithVersion("latest"),
		splunk.WithLicense(true),
		splunk.WithPassword("12345678"),
		splunk.WithValues(events),
	)

	// created container now includes two events in "events" index
	container, err := gnomock.Start(p)
	fmt.Println("error:", err)
	fmt.Println(len(container.Ports), "exposed ports:")

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

	// Port numbers as well as container address are non-deterministic, so they
	// are skipped in this example. The usage would be:
	//
	//		container.Address("web")
	// 		container.Address("api")

	portNames := make([]string, 0)

	for portName := range container.Ports {
		portNames = append(portNames, portName)
	}

	sort.Strings(portNames)
	fmt.Println(portNames)

	// Output:
	// error: <nil>
	// 3 exposed ports:
	// [api collector web]
}

Documentation

Overview

Package splunk includes Splunk Enterprise implementation of Gnomock Preset interface. This Preset can be passed to gnomock.StartPreset function to create a configured Splunk container to use in tests.

Splunk image is relatively heavy (larger than 1.5GB), and its startup time is longer than usual. Using this container may make the tests much longer

Index

Constants

View Source
const (
	// CollectorPort is the name of a port exposed by Splunk Collector
	CollectorPort string = "collector"

	// APIPort is the name of a port exposed by Splunk API
	APIPort string = "api"

	// WebPort is the name of a port exposed by Splunk web UI
	WebPort string = "web"
)

Variables

This section is empty.

Functions

func Ingest

func Ingest(ctx context.Context, c *gnomock.Container, password string, events ...Event) error

Ingest adds the provided events to splunk container. Use the same password you provided in WithPassword. Send as many events as you like, this function only returns when all the events were indexed, or when the context is timed out

func Preset

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

Preset creates a new Gnomock Splunk preset. This preset includes a Splunk specific healthcheck function, default Splunk image and ports, and allows to optionally ingest initial logs

Types

type Event

type Event struct {
	// Event is the actual log entry. Can be any format
	Event string `json:"event"`

	// Index is the name of index to ingest the log into. If the index does not
	// exist, it will be created
	Index string `json:"index"`

	// Source will be used as "source" value of this event in Splunk
	Source string `json:"source"`

	// SourceType will be used as "sourcetype" value of this event in Splunk
	SourceType string `json:"sourcetype"`

	// Time represents event timestamp in seconds, milliseconds or nanoseconds
	// (and maybe even in microseconds, whatever splunk recognizes)
	Time int64 `json:"time"`
}

Event is a type used during Splunk initialization. Pass events to WithValues to ingest them into the container before the control over it is passed to the caller

type Option

type Option func(*P)

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

func WithLicense

func WithLicense(accept bool) Option

WithLicense lets the user choose to accept Splunk enterprise license (see more at https://hub.docker.com/_/splunk-enterprise). Failure to accept the license will prevent Splunk container from starting

func WithPassword

func WithPassword(pass string) Option

WithPassword sets admin password in Splunk container. Use this password to connect to the container when it is ready. Note that Splunk has password requirements. Failure to meet those will prevent the container from starting (see defaults at https://docs.splunk.com/Documentation/Splunk/latest/Security/Configurepasswordsinspecfile)

func WithValues

func WithValues(vs []Event) Option

WithValues initializes Splunk with the provided values as log entries

func WithValuesFile

func WithValuesFile(file string) Option

WithValuesFile sets file name to use as a source of initial events to ingest. These events are ingested first, followed by any other events sent using WithValues

func WithVersion

func WithVersion(version string) Option

WithVersion sets splunk version (see https://hub.docker.com/r/splunk/splunk/tags) for available versions

type P

type P struct {
	Values        []Event `json:"values"`
	ValuesFile    string  `json:"values_file"`
	AcceptLicense bool    `json:"accept_license"`
	AdminPassword string  `json:"admin_password"`
	Version       string  `json:"version"`
}

P is a Gnomock Preset implementation of Splunk

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