zfs

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: May 23, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoOutput = errors.New("no output")

ErrNoOutput signals that zfs did not write any output to stdout.

Functions

func RunTests

func RunTests(t *testing.T, tests []TestCase, parallel bool)

RunTests runs all tests. If parallel is true t.Parallel is called for each test.

Types

type Adapter

type Adapter CmdFunc

Adapter wraps the CmdFunc for a zfs executable.

func New

func New(zfsCmdPath string) (Adapter, error)

New creates a new Adapter for the zfs executable located at zfsCmdPath.

func (Adapter) CreateSnapshot

func (z Adapter) CreateSnapshot(name string) error

CreateSnapshot creates a snapshot with the name.

As described in the zfs(8) man page name must be of the format filesystem@snapname or volume@snapname. filesystem must be an existing zfs filesystem, volume an existing zfs volume. snapname will be the name of the snapshot.

func (Adapter) Destroy

func (z Adapter) Destroy(name string) error

Destroy removes the zfs object with name.

Destroy merely calls zfs destroy. Provided all conditions for destroying an object are met, the object will be destroyed.

func (Adapter) List

func (z Adapter) List(typ ListType) ([]string, error)

List returns a slice containing the names of all available zfs objects of the passed type.

List does not make any guarantees about the ordering of the names in the returned slice. In fact, callers must expect to be shuffled.

The following caveats for the various supported types apply:

FileSystem

List returns the names of all file systems in path notation. In the
case of nested file systems, children are prefixed with the name of their
parent file system. The topmost file system is usually the zfs pool
itself.
It is up to the caller to re-construct this hierarchical structure if
required.

List returns an error if calling the zfs CmdFunc fails or the output could not be parsed.

func (Adapter) Receive

func (z Adapter) Receive(name string, r io.Reader) error

Receive receives a named zfs object from r.

func (Adapter) Send

func (z Adapter) Send(name, ref string, w io.Writer) error

Send writes the snapshot name to w.

This also writes all snapshots that have been created before name. If ref is not empty only snapshots between ref and name are written to w.

type CmdFunc

type CmdFunc func(args ...string) *exec.Cmd

CmdFunc is a function that creates an *exec.Cmd.

It allows to abstract from an installed program for testing purposes.

When the Run method of the returned Cmd is called the program is executed with all args appended to its call.

args must not contain the program to execute.

func NewCmdFunc

func NewCmdFunc(name string, args ...string) CmdFunc

NewCmdFunc creates a new CmdFunc for the named program.

The returned CmdFunc takes the args passed to NewCmdFunc as well as the args passed to the CmdFunc itself into account.

Internally the returned CmdFunc uses exec.Command. Everything about exec.Command applies here as well.

Example
package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"

	"github.com/fhofherr/zsm/internal/zfs"
)

func main() {
	var out bytes.Buffer

	cmdFunc := zfs.NewCmdFunc("tr", "a-z")
	cmd := cmdFunc("A-Z")
	cmd.Stdin = strings.NewReader("some input")
	cmd.Stdout = &out

	// Execution equivalent to
	//
	//     echo "some input" | tr "a-z" "A-Z"
	if err := cmd.Run(); err != nil {
		log.Fatalf("run cmd: %v", err)
	}
	fmt.Printf("output: %q", out.String())
}
Output:

func SwallowFurtherArgs

func SwallowFurtherArgs(f CmdFunc, swallowed *[]string) CmdFunc

SwallowFurtherArgs returns a CmdFunc that does not pass any args it it received on to the program, but instead writes them to swallowed.

func WithEnv

func WithEnv(f CmdFunc, env map[string]string) CmdFunc

WithEnv modifies the command returned by the CmdFunc f to execute with the passed environment.

env is a slice of strings of the form KEY=VALUE.

type Error

type Error struct {
	SubCommand string
	ExitCode   int
	Stderr     string
}

Error represents errors that occurred while executing zfs.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is returns true if target is an *Error and is equal to e.

type ListType

type ListType string

ListType defines the type of items the caller of List is interested in.

const (
	// FileSystem causes List to list ZFS file systems only.
	FileSystem ListType = "filesystem"

	// Snapshot causes List to list ZFS snapshots only.
	Snapshot ListType = "snapshot"
)

type TestCase

type TestCase struct {
	Name string

	// Call calls the respective adapter function; should return any error
	// returned by the zfs adapter. May use the passed *testing.T to do further
	// assertions on the result of calling zsf.
	Call func(*testing.T, Adapter) error

	ZFSArgs     []string                  // expected arguments
	ZFSExitCode int                       // expected exit code of zfs
	Stdin       func(t *testing.T) []byte // returns stdin expected to be sent to zfs
	Stdout      func(t *testing.T) []byte // returns stdout expected to be sent by zfs
	Stderr      func(t *testing.T) []byte // returns stderr expected to be sent by zfs
}

TestCase represents a test case for a call to the zfs adapter.

Jump to

Keyboard shortcuts

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