//go:build unix
package main
import (
"os"
"path/filepath"
"syscall"
"testing"
)
// deploy.sh holds an exclusive flock on its lock file for the whole
// deploy; the check reads that, so a killed deploy leaves nothing behind.
func TestDeployMarkerIsTheDeployLock(t *testing.T) {
lock := filepath.Join(t.TempDir(), "deploy.lock")
if deployRunning(lock) {
t.Errorf("FAIL: a missing lock file reads as a deploy")
}
f, err := os.OpenFile(lock, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
t.Fatal(err)
}
defer f.Close()
if deployRunning(lock) {
t.Errorf("FAIL: an unlocked lock file reads as a deploy")
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
t.Fatal(err)
}
if !deployRunning(lock) {
t.Errorf("FAIL: a held lock does not read as a deploy")
}
syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
if deployRunning(lock) {
t.Errorf("FAIL: a released lock still reads as a deploy")
}
if !t.Failed() {
t.Log("ok")
}
}