Пример #1
0
func TestList(t *testing.T) {
	lRunner, err := NewZfs(runcmd.NewLocalRunner())
	if err != nil {
		t.Error(err)
	}

	// List fs: non-recursive
	list, err := lRunner.List(fs[0], FS, false)
	if err != nil {
		t.Error(err)
	}
	if len(list) > 1 {
		fmt.Println(list)
		t.Error(errors.New("error list fs non-recursive: more than one fs: "))
	}
	fmt.Println(list[0])

	// List fs: recursive
	list, err = lRunner.List(fs[0]+"*", FS, true)
	if err != nil {
		t.Error(err)
	}
	if len(list) == 1 {
		fmt.Println(list)
		t.Error(errors.New("error list fs recursive: only one fs: "))
	}
	for _, fs := range list {
		fmt.Println(fs)
	}

	// List snap: non-recursive
	list, err = lRunner.List(fs[1]+"@"+snap[1], SNAP, false)
	if err != nil {
		t.Error(err)
	}
	if len(list) > 1 {
		fmt.Println(list)
		t.Error(errors.New("error list snap non-recursive: more than one fs: "))
	}

	if list == nil {
		t.Fatal(errors.New("error list snap should not be nil"))
	}

	fmt.Println(list[0])

	// List snap: recursive
	list, err = lRunner.List(fs[0]+"*", SNAP, true)
	if err != nil {
		t.Error(err)
	}
	if len(list) == 1 {
		fmt.Println(list)
		t.Error(errors.New("error list fs recursive: only one fs: "))
	}
	for _, fs := range list {
		fmt.Println(fs)
	}
}
Пример #2
0
func NewBackuper(c *Config, snapSuff string) (*Backuper, error) {
	srcZfs, err := zfs.NewZfs(runcmd.NewLocalRunner())
	if err != nil {
		return nil, err
	}
	dstZfs, err := zfs.NewZfs(runcmd.NewRemoteKeyAuthRunner(c.User, c.Host, c.Key))
	if err != nil {
		return nil, err
	}
	return &Backuper{srcZfs, dstZfs, c, snapSuff}, nil
}
Пример #3
0
// should be here because snapshot will be removed and other test cases will be
// failed
func TestDestroySnap(t *testing.T) {
	lRunner, err := NewZfs(runcmd.NewLocalRunner())
	if err != nil {
		t.Error(err)
	}

	// Delete valid snapshot:
	if err := lRunner.Destroy(fs[0] + "@" + snap[0]); err != nil {
		t.Error(err)
	}

	if err := lRunner.Destroy(fs[1] + "@" + snap[1]); err != nil {
		t.Error(err)
	}

	// Delete invalid snapshot, error is normal:
	if err := lRunner.Destroy(fs[0] + "blah" + "@" + snap[0]); err != nil {
		fmt.Println(err.Error())
	}
}
Пример #4
0
func TestProperty(t *testing.T) {
	lRunner, err := NewZfs(runcmd.NewLocalRunner())
	if err != nil {
		t.Error(err)
	}

	// Get valid property:
	val, err := lRunner.Property(fs[0], "readonly")
	if err != nil {
		t.Error(err)
	}
	fmt.Println(val)

	// Get invalid property:
	val, err = lRunner.Property(fs[0], "readonly-blah")
	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println(val)
		t.Error("must no property")
	}
}
Пример #5
0
func TestExistSnap(t *testing.T) {
	lRunner, err := NewZfs(runcmd.NewLocalRunner())
	if err != nil {
		t.Error(err)
	}

	// Check exists valid snapshot:
	exists, err := lRunner.ExistSnap(fs[0], snap[0])
	if err != nil {
		t.Error(err)
	}
	if exists {
		fmt.Println(fs[0] + "@" + snap[0] + " exists")
	} else {
		fmt.Println(fs[0] + "@" + snap[0] + " does not exists")
	}

	// Checks exists invalid snapshot:
	if err := lRunner.Destroy(fs[0] + "blah" + "@" + snap[0]); err != nil {
		fmt.Println(err.Error())
	}
}
Пример #6
0
func TestCreateSnap(t *testing.T) {
	lRunner, err := NewZfs(runcmd.NewLocalRunner())
	if err != nil {
		t.Error(err)
	}

	// Create valid snapshot:
	if err := lRunner.CreateSnap(fs[0], snap[0]); err != nil {
		fmt.Println("test")
		t.Error(err)
	}

	if err := lRunner.CreateSnap(fs[1], snap[1]); err != nil {
		fmt.Println("test")
		t.Error(err)
	}

	// Create invalid snapshot, error is normal:
	if err := lRunner.CreateSnap(fs[0]+"blah", snap[0]); err != nil {
		fmt.Println(err.Error())
	}
}
Пример #7
0
func loadConfigFromArgs(
	property,
	remote,
	expire,
	host,
	user,
	key string,
	maxio int,
) (*Config, error) {
	config := new(Config)
	config.Backup = make([]Backup, 0)
	config.Host = host
	config.User = user
	config.Key = key
	config.MaxIoThreads = maxio

	srcZfs, err := zfs.NewZfs(runcmd.NewLocalRunner())
	if err != nil {
		return nil, err
	}
	list, err := srcZfs.List("", zfs.FS, true)
	if err != nil {
		return nil, err
	}
	for _, fs := range list {
		out, err := srcZfs.Property(fs, property)
		if err != nil {
			return nil, err
		}
		if out == "true" {
			config.Backup = append(
				config.Backup,
				Backup{false, expire, fs, remote, ""},
			)
		}
	}
	return config, nil
}
Пример #8
0
	"strings"

	"github.com/theairkit/runcmd"
)

var (
	FS      = "filesystem"
	SNAP    = "snapshot"
	DATANOE = "dataset does not exist"
)

type Zfs struct {
	runcmd.Runner
}

var std, _ = NewZfs(runcmd.NewLocalRunner())

func NewZfs(r runcmd.Runner, err error) (*Zfs, error) {
	if err != nil {
		return nil, err
	}
	return &Zfs{r}, nil
}

func CreateSnap(fs, snap string) error {
	return std.CreateSnap(fs, snap)
}

func (this *Zfs) CreateSnap(fs, snap string) error {
	c, err := this.Command("zfs snapshot " + fs + "@" + snap)
	if err != nil {