Skip to content

Commit

Permalink
Fix or skip tests on windows and macos
Browse files Browse the repository at this point in the history
Skip tests when the host system lacks tools to check. Fix tests that
work but outputs different things, for example CRLF on windows.
  • Loading branch information
orgrim committed May 11, 2021
1 parent acaa33a commit 965b068
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 38 deletions.
25 changes: 22 additions & 3 deletions hash_test.go
Expand Up @@ -32,6 +32,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
)

Expand Down Expand Up @@ -91,6 +92,11 @@ func TestChecksumFile(t *testing.T) {
t.Errorf("checksumFile returned: %v", err)
}

_, err := exec.LookPath(st.tool)
if err != nil {
t.Skip("check command not in the PATH:", st.tool)
}

c := exec.Command(st.tool, "-c", "test."+st.algo)
out, err := c.CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -125,7 +131,7 @@ func TestChecksumFile(t *testing.T) {
if err != nil {
t.Fatal("could not create test file")
}
fmt.Fprintf(f, "abdc%d\n", i)
fmt.Fprintf(f, "abdc%d", i)
f.Close()
}

Expand All @@ -136,13 +142,26 @@ func TestChecksumFile(t *testing.T) {
t.Errorf("checksumFile returned: %v", err)
}

_, err := exec.LookPath(st.tool)
if err != nil {
t.Skip("check command not in the PATH:", st.tool)
}

c := exec.Command(st.tool, "-c", fmt.Sprintf("test.d.%s", st.algo))
out, err := c.CombinedOutput()
if err != nil {
t.Errorf("check command failed: %s\n", out)
}
if string(out) != "test.d/test0: OK\ntest.d/test1: OK\ntest.d/test2: OK\n" {
t.Errorf("expected OK, got %q\n", out)

res := string(out)
if runtime.GOOS == "windows" {
if res != "test.d\\test0: OK\ntest.d\\test1: OK\ntest.d\\test2: OK\n" {
t.Errorf("expected OK, got %q\n", out)
}
} else {
if res != "test.d/test0: OK\ntest.d/test1: OK\ntest.d/test2: OK\n" {
t.Errorf("expected OK, got %q\n", out)
}
}
})
}
Expand Down
12 changes: 6 additions & 6 deletions hook_test.go
Expand Up @@ -31,6 +31,7 @@ import (
"os"
"os/exec"
"regexp"
"strings"
"testing"
)

Expand All @@ -41,13 +42,12 @@ func TestHookCommand(t *testing.T) {
}{
{"echo 'a'", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: test: a\n$`},
{"echo a'", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ERROR: unable to parse hook command: No closing quotation\n$`},
{"echo -e 'a\nb'", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: test: a\n\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: test: b\n$`},
{"echo 'a\nb'", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: test: a\n\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: test: b\n$`},
{"", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ERROR: unable to run an empty command\n$`},
{"/nothingBLA a", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ERROR: fork/exec /nothingBLA: no such file or directory\n$`},
{"/nothingBLA a", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ERROR: .*/nothingBLA.*\n$`},
{"sh -c 'echo test; exit 1'", `^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ERROR: test: test\n\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ERROR: exit status 1\n$`},
}

// l := NewLevelLog()
for i, subt := range tests {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
buf := new(bytes.Buffer)
Expand All @@ -57,7 +57,7 @@ func TestHookCommand(t *testing.T) {
l.Errorln(err)
}

lines := buf.String()
lines := strings.ReplaceAll(buf.String(), "\r", "")
matched, err := regexp.MatchString(subt.re, lines)
if err != nil {
t.Fatal("pattern did not compile:", err)
Expand All @@ -78,7 +78,7 @@ func TestPreBackupHook(t *testing.T) {
}{
{"echo 'a'", `\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: running pre-backup command: echo 'a'\n\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: pre-backup: a\n$`, false},
{"", "", false},
{"/nothingBLA a", `\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: running pre-backup command: /nothingBLA a\n\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} FATAL: hook command failed: fork/exec /nothingBLA: no such file or directory\n$`, true},
{"/nothingBLA a", `\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} INFO: running pre-backup command: /nothingBLA a\n\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} FATAL: .*/nothingBLA.*\n$`, true},
}
for i, subt := range tests {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
Expand All @@ -95,7 +95,7 @@ func TestPreBackupHook(t *testing.T) {
}
}

lines := buf.String()
lines := strings.ReplaceAll(buf.String(), "\r", "")
matched, err := regexp.MatchString(subt.re, lines)
if err != nil {
t.Fatal("pattern did not compile:", err)
Expand Down
11 changes: 7 additions & 4 deletions lock_test.go
Expand Up @@ -30,6 +30,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
)

Expand All @@ -46,12 +47,14 @@ func TestLockPath(t *testing.T) {
t.Fatal("could not create temp subdir:", err)
}

_, _, err = lockPath(filepath.Join(dir, "subfail", "subfail", "lockfail"))
var e *os.PathError
if !errors.As(err, &e) {
t.Errorf("expected a *os.PathError, got %q\n", err)
// On windows the directory is created even with a mode of the tempdir that should make it fail
if runtime.GOOS != "windows" {
_, _, err = lockPath(filepath.Join(dir, "subfail", "subfail", "lockfail"))
if !errors.As(err, &e) {
t.Errorf("expected a *os.PathError, got %q\n", err)
}
}

// path is subdir of tempdir to make os.create fail
_, _, err = lockPath(filepath.Join(dir, "subfail"))
if !errors.As(err, &e) {
Expand Down
65 changes: 40 additions & 25 deletions purge_test.go
Expand Up @@ -30,6 +30,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
Expand All @@ -43,49 +44,63 @@ func TestPurgeDumps(t *testing.T) {
}
defer os.RemoveAll(dir)

// empty path
// empty path - on windows chmod does not work as expected
wd := filepath.Join(dir, "real", "bad")
if err := os.MkdirAll(wd, 0755); err != nil {
t.Fatal("could not create test dir")
}
os.Chmod(filepath.Dir(wd), 0444)
err = purgeDumps(wd, "", 0, time.Time{})
if err == nil {
t.Errorf("empty path gave error <nil>\n")

if runtime.GOOS != "windows" {
os.Chmod(filepath.Dir(wd), 0444)
err = purgeDumps(wd, "", 0, time.Time{})
if err == nil {
t.Errorf("empty path gave error <nil>\n")
}
os.Chmod(filepath.Dir(wd), 0755)
}
os.Chmod(filepath.Dir(wd), 0755)

// empty dbname
tf := formatDumpPath(wd, time.RFC3339, "dump", "", time.Now().Add(-time.Hour))
when := time.Now().Add(-time.Hour)
tf := formatDumpPath(wd, "2006-01-02_15-04-05", "dump", "", when)
f, err := os.Create(tf)
if err != nil {
t.Errorf("could not create temp file %s: %s", tf, err)
}

f.Close()
os.Chtimes(tf, when, when)

err = purgeDumps(wd, "", 0, time.Now())
if err != nil {
t.Errorf("empty dbname gave error %s", err)
t.Errorf("empty dbname (file: %s) gave error %s", tf, err)
}
if _, err := os.Stat(tf); err == nil {
t.Errorf("file still exists")
}

// file without write perms
tf = formatDumpPath(wd, time.RFC3339, "dump", "db", time.Now().Add(-time.Hour))
ioutil.WriteFile(tf, []byte("truc\n"), 0644)
os.Chmod(filepath.Dir(tf), 0555)
if runtime.GOOS != "windows" {
tf = formatDumpPath(wd, time.RFC3339, "dump", "db", time.Now().Add(-time.Hour))
ioutil.WriteFile(tf, []byte("truc\n"), 0644)
os.Chmod(filepath.Dir(tf), 0555)

err = purgeDumps(wd, "db", 0, time.Now())
if err == nil {
t.Errorf("bad perms on file did not gave an error")
}
os.Chmod(filepath.Dir(tf), 0755)
err = purgeDumps(wd, "db", 0, time.Now())
if err == nil {
t.Errorf("bad perms on file did not gave an error")
}
os.Chmod(filepath.Dir(tf), 0755)

// dir without write perms
tf = formatDumpPath(wd, time.RFC3339, "d", "db", time.Now().Add(-time.Hour))
os.MkdirAll(tf, 0755)
os.Chmod(filepath.Dir(tf), 0555)
// dir without write perms
tf = formatDumpPath(wd, time.RFC3339, "d", "db", time.Now().Add(-time.Hour))
os.MkdirAll(tf, 0755)
os.Chmod(filepath.Dir(tf), 0555)

err = purgeDumps(wd, "db", 0, time.Now())
if err == nil {
t.Errorf("bad perms on dir did not gave an error")
err = purgeDumps(wd, "db", 0, time.Now())
if err == nil {
t.Errorf("bad perms on dir did not gave an error")
}
os.Chmod(filepath.Dir(tf), 0755)
}
os.Chmod(filepath.Dir(tf), 0755)

// time and keep limits
var tests = []struct {
Expand All @@ -111,7 +126,7 @@ func TestPurgeDumps(t *testing.T) {
}
for i := 1; i <= 3; i++ {
when := time.Now().Add(-time.Hour * time.Duration(i))
tf = formatDumpPath(wd, time.RFC3339, "dump", "db", when)
tf = formatDumpPath(wd, "2006-01-02_15-04-05", "dump", "db", when)
ioutil.WriteFile(tf, []byte("truc\n"), 0644)
os.Chtimes(tf, when, when)
}
Expand Down

0 comments on commit 965b068

Please sign in to comment.