Categorygithub.com/CSCfi/vault-testing-stepwise
modulepackage
0.7.1
Repository: https://github.com/cscfi/vault-testing-stepwise.git
Documentation: pkg.go.dev

# README

Stepwise

Vault testing Stepwise was cloned from https://github.com/hashicorp/vault-testing-stepwise.

It provides a mini framework for writing acceptance tests for custom Vault plugins.

Usage

The framework is simple, set the plugin name, which is used for building and mounting the plugin. Use that to create a new environment, and add test steps

package pluginname

import (
	"os"
	"testing"

	stepwise "github.com/CSCfi/vault-testing-stepwise"
	"github.com/CSCfi/vault-testing-stepwise/environments/docker"
)

func TestPlugin(t *testing.T) {
	err := os.Setenv("VAULT_ACC", "1")
	if err != nil {
		t.Error("Failed to set environment variable VAULT_ACC")
	}
	
	// The mount options are also used for locating and building the plugin
	mountOptions := stepwise.MountOptions{
		MountPathPrefix: "plugin-mount-prefix",
		RegistryName:    "plugin-name-registry",
		PluginType:      stepwise.PluginTypeHere,
		PluginName:      "plugin-name",
	}
	keysCase := stepwise.Case{
		Environment:  docker.NewEnvironment("DockerPluginEnvironment", &mountOptions, "hashicorp/vault:latest"),
		SkipTeardown: false,
		Steps: []stepwise.Step{
			stepwise.Step{
				Name:      "testThatPathWrites",
				Operation: stepwise.WriteOperation,
				Data:      map[string]interface{}{"field": "value"},
				// Instead of passing static data, it's possible to use GetData with a function that returns the data    
				Path:      "/path/here",
			},
			stepwise.Step{
                Name:      "testThatPathReturnsX",
                Operation: stepwise.ReadOperation,
                Path:      "/path/here",
				// There's support for ReadOperation with data by passing ReadData 
                Assert: func (resp *api.Secret, err error) error {
                
                    // resp.Data contains the `data` part of the response from Vault
                    
                    return nil
                },
            },
		},
	}
	
    // Running the case compiles the plugin with Docker, and runs Vault with the plugin enabled.
    // Each step in a case is run sequentially.
    // At the end of the case, the Docker container and network are removed, unless `SkipTeardown` is set to `true`
    stepwise.Run(t, keysCase)
}

Debugging

Set SkipTeardown to true, and go into the container with docker exec -it container-id sh. Find the container ID with docker ps.

When leaving the container running, it's possible to get the root token, and make API calls directly to Vault. For that, don't execute stepwise.Run instead, create an environment, run it's Setup() function, and print the env.RootToken() to the command line.

Tests

Can be executed with

go test -v ./...

Licensing

vault-testing-stepwise is licensed under MPL-2.0.

SPDX-License-Identifier: MPL-2.0

# Packages

No description provided by the author

# Functions

CompilePlugin is a helper method to compile a source plugin.
No description provided by the author
Run performs an acceptance test on a backend with the given test case.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
These are originally defined in sdk/helper/consts/plugin_types.go.
These are originally defined in sdk/helper/consts/plugin_types.go.
These are originally defined in sdk/helper/consts/plugin_types.go.
These are originally defined in sdk/helper/consts/plugin_types.go.
No description provided by the author
TestEnvVar must be set to a non-empty value for acceptance tests to run.
No description provided by the author
No description provided by the author

# Structs

Case represents a scenario we want to test which involves a series of steps to be followed sequentially, evaluating the results after each step.
CertificateGetter satisfies ReloadFunc and its GetCertificate method satisfies the tls.GetCertificate function signature.
MountOptions are a collection of options each step driver should support.
Step represents a single step of a test Case.

# Interfaces

Environment is the interface Environments need to implement to be used in Case to execute each Step.
TestT is the interface used to handle the test lifecycle of a test.

# Type aliases

AssertionFunc is the callback used for Assert in Steps.
Operation defines operations each step could perform.
PluginType defines the types of plugins supported This type re-create constants as a convenience so users don't need to import/use the consts package.
ReloadFunc are functions that are called when a reload is requested.