mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-11-05 05:04:13 +01:00
Add test for locking behaviour (#211)
This commit is contained in:
parent
465bc037c2
commit
2b9dc9f17c
@ -3,6 +3,7 @@ package lock
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
@ -62,6 +63,33 @@ func TestLock(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// locking a locked instance exits the instance
|
||||
// this trick to capture os.Exit(1) is discussed here:
|
||||
// https://talks.golang.org/2014/testing.slide#23
|
||||
t.Run("lock twice", func(t *testing.T) {
|
||||
if os.Getenv("CRASH") == "1" {
|
||||
err := Lock()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
// should fail
|
||||
Lock()
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], "-test.run=TestLock/lock_twice")
|
||||
cmd.Env = append(os.Environ(), "CRASH=1")
|
||||
err := cmd.Run()
|
||||
|
||||
err, ok := err.(*exec.ExitError)
|
||||
if !ok {
|
||||
t.Error("unexpected error")
|
||||
}
|
||||
expected := "exit status 1"
|
||||
if err.Error() != expected {
|
||||
t.Errorf("got %q, want %q", err.Error(), expected)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("set cron", func(t *testing.T) {
|
||||
expected := int64(5)
|
||||
SetCron("foo", expected)
|
||||
|
Loading…
Reference in New Issue
Block a user