105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
package storage
|
|
|
|
import "testing"
|
|
|
|
func TestBuildSnapraidArgs(t *testing.T) {
|
|
tests := []struct {
|
|
kind string
|
|
cfg SnapraidConfig
|
|
want []string
|
|
}{
|
|
{
|
|
"snapraid_diff",
|
|
SnapraidConfig{Conf: "/etc/snapraid.conf"},
|
|
[]string{"snapraid", "-c", "/etc/snapraid.conf", "diff"},
|
|
},
|
|
{
|
|
"snapraid_sync",
|
|
SnapraidConfig{Conf: "/etc/snapraid.conf"},
|
|
[]string{"snapraid", "-c", "/etc/snapraid.conf", "sync"},
|
|
},
|
|
{
|
|
"snapraid_check",
|
|
SnapraidConfig{Conf: "/etc/snapraid.conf"},
|
|
[]string{"snapraid", "-c", "/etc/snapraid.conf", "check"},
|
|
},
|
|
{
|
|
"snapraid_scrub",
|
|
SnapraidConfig{Conf: "/etc/snapraid.conf", ScrubPlan: 8},
|
|
[]string{"snapraid", "-c", "/etc/snapraid.conf", "scrub", "-p", "8"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.kind, func(t *testing.T) {
|
|
got, err := BuildSnapraidArgs(tt.kind, tt.cfg)
|
|
if err != nil {
|
|
t.Fatalf("BuildSnapraidArgs: %v", err)
|
|
}
|
|
if len(got) != len(tt.want) {
|
|
t.Errorf("BuildSnapraidArgs = %v, want %v", got, tt.want)
|
|
return
|
|
}
|
|
for i := range got {
|
|
if got[i] != tt.want[i] {
|
|
t.Errorf("BuildSnapraidArgs[%d] = %q, want %q", i, got[i], tt.want[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildSnapraidArgsNoConf(t *testing.T) {
|
|
_, err := BuildSnapraidArgs("snapraid_sync", SnapraidConfig{Conf: ""})
|
|
if err == nil {
|
|
t.Error("expected error for empty conf")
|
|
}
|
|
}
|
|
|
|
func TestValidateScrubPlan(t *testing.T) {
|
|
tests := []struct {
|
|
plan int
|
|
wantErr bool
|
|
}{
|
|
{0, true},
|
|
{-1, true},
|
|
{100, true},
|
|
{1, false},
|
|
{50, false},
|
|
{99, false},
|
|
}
|
|
for _, tt := range tests {
|
|
err := ValidateScrubPlan(tt.plan)
|
|
if tt.wantErr && err == nil {
|
|
t.Errorf("ValidateScrubPlan(%d) = nil, want error", tt.plan)
|
|
}
|
|
if !tt.wantErr && err != nil {
|
|
t.Errorf("ValidateScrubPlan(%d) = %v, want nil", tt.plan, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseSnapraidDataDirs(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want []string
|
|
}{
|
|
{"", nil},
|
|
{"/disk1", []string{"/disk1"}},
|
|
{"/disk1,/disk2", []string{"/disk1", "/disk2"}},
|
|
{"/disk1, /disk2 , /disk3", []string{"/disk1", "/disk2", "/disk3"}},
|
|
{" /disk1 , /disk2 ", []string{"/disk1", "/disk2"}},
|
|
}
|
|
for _, tt := range tests {
|
|
got := ParseSnapraidDataDirs(tt.input)
|
|
if len(got) != len(tt.want) {
|
|
t.Errorf("ParseSnapraidDataDirs(%q) = %v, want %v", tt.input, got, tt.want)
|
|
continue
|
|
}
|
|
for i := range got {
|
|
if got[i] != tt.want[i] {
|
|
t.Errorf("ParseSnapraidDataDirs(%q)[%d] = %q, want %q", tt.input, i, got[i], tt.want[i])
|
|
}
|
|
}
|
|
}
|
|
}
|