setectest

package
v0.0.0-...-0f9da31 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package setectest implements a wrapper around setec types for testing.

Usage

// Construct a database and add some secrets.
db := setecttest.NewDB(t, nil)  // nil for default options
db.MustPut(db.Superuser, "name", "value")

// Construct a test server.
ss := setectest.NewServer(t, db, &setectest.ServerOptions{
  WhoIs: setectest.AllAccess,
})

// Hook up the Server to the httptest package.
hs := httptest.NewServer(ss.Mux)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllAccess

func AllAccess(ctx context.Context, addr string) (*apitype.WhoIsResponse, error)

AllAccess is a WhoIs function implementation that returns a successful response containing a capability for full access to all secrets.

Types

type DB

type DB struct {
	Path      string    // the path of the database file
	Key       tink.AEAD // the key-encryption key (dummy)
	Actual    *db.DB    // the underlying database
	Superuser db.Caller // a pre-defined super-user for all secrets & operations
	// contains filtered or unexported fields
}

DB is a wrapper around a setec database to simplify creating database instances for unit tests with the testing package.

func NewDB

func NewDB(t *testing.T, opts *DBOptions) *DB

NewDB constructs a new empty DB that persists for the duration of the test and subtests governed by t. When t ends, the database is cleaned up. If opts == nil, default options are used (see DBOptions).

func (*DB) MustActivate

func (db *DB) MustActivate(caller db.Caller, name string, version api.SecretVersion)

MustActivate sets the active version of the named secret or fails.

func (*DB) MustGet

func (db *DB) MustGet(caller db.Caller, name string) *api.SecretValue

MustGet returns the active version of the named secret or fails.

func (*DB) MustGetVersion

func (db *DB) MustGetVersion(caller db.Caller, name string, version api.SecretVersion) *api.SecretValue

MustGetVersion returns the specified version of the named secret or fails.

func (*DB) MustList

func (db *DB) MustList(caller db.Caller) []*api.SecretInfo

MustList lists the contents of the database or fails.

func (*DB) MustPut

func (db *DB) MustPut(caller db.Caller, name, value string) api.SecretVersion

MustPut adds the specified secret to the database or fails.

type DBOptions

type DBOptions struct {
	// AuditLog is where audit logs are written; if nil, audit logs are
	// discarded without error.
	AuditLog *audit.Writer
}

DBOptions are options for constructing a test database. A nil *Options is ready for use and provides defaults as described.

type Server

type Server struct {
	Actual *server.Server // the underlying server
	Mux    *http.ServeMux // the serving mux populated by the server
}

Server is a wrapper to support running a standalone setec Server for unit tests with the testing package.

func NewServer

func NewServer(t *testing.T, db *DB, opts *ServerOptions) *Server

NewServer constructs a new Server that reads data from db and persists for the duration of the test and subtests governed by t. When t ends, the server and its database are cleaned up. If opts == nil, default options are used (see ServerOptions).

type ServerOptions

type ServerOptions struct {
	// WhoIs is a function implementing the corresponding method of a local
	// client. If nil, setectest.AllAccess is used by default.
	WhoIs func(context.Context, string) (*apitype.WhoIsResponse, error)

	// AuditLog is where audit logs are written; if nil, audit logs are
	// discarded without error.
	AuditLog *audit.Writer
}

ServerOptions are options for constructing a test server. A nil *ServerOptions is ready for use and provides defaults as described.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL