Skip to content
quarry Artifact [5e971ecd7e]
Login

Artifact [5e971ecd7e]

Artifact 5e971ecd7eb87cfadf0da295facbb1ee2c45e2fa4809aa09cc7db9d5d0457252:


//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")
	}
}