Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/secret/secrettest"

Package secrettest is the conformance kit for secret.Vault implementations, and an in-memory Vault with fault hooks for tests of code that keeps credentials. zelk’s keychain vaults run the kit, and so should any vault a host writes (docs/design/keychain.md).

func Run(t *testing.T, f Factory)

Run checks that the vaults f makes behave as secret.Vault requires.

type Factory func(t *testing.T) secret.Vault

Factory returns a vault for one case, empty of the items the case will make. A vault backed by a real keychain should make its ids unique and remove its items when t ends.

type Vault struct {
// Name is the backend's name; "fake" when empty.
Name string
Before func(op string, r secret.Ref) error
After func(op string, r secret.Ref) error
// contains filtered or unexported fields
}

Vault is a vault kept in memory, for tests. Before and After, when set, run around each call’s effect with the call’s name (“create”, “get”, “delete”) and ref: an error from Before stops the call before its effect, one from After is returned after it, as a call whose result was lost would be (ErrUncertain, say).

func (v *Vault) Backend() string

Backend returns the vault’s name.

func (v *Vault) Create(_ context.Context, r secret.Ref, _ string, data []byte) error

Create stores a new item.

func (v *Vault) Delete(_ context.Context, r secret.Ref) error

Delete removes an item.

func (v *Vault) Get(_ context.Context, r secret.Ref) ([]byte, error)

Get returns the item’s data.

func (v *Vault) Items() map[string][]byte

Items returns a copy of what the vault holds, by id.

func (v *Vault) NewRef(context.Context) (secret.Ref, error)

NewRef returns a ref with a random id.

func (v *Vault) Put(id string, data []byte)

Put stores data under id directly, as another program might.