Skip to content

Commit

Permalink
Fix errors around deamonization, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcfarlane committed Nov 21, 2017
1 parent 26285f7 commit 31e1e0c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
31 changes: 20 additions & 11 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ import (
log "github.com/sirupsen/logrus"
)

// Daemonize (please use something like upstart, daemontools, systemd)
func daemonize() {
args := os.Args[1:]
i := 0
for ; i < len(args); i++ {
if strings.HasPrefix(args[i], "-daemon") {
args = append(args[:i], args[i+1:]...)
break
func daemonizeCmd(args []string) (string, []string) {
name, args := args[0], args[1:]
filtered := []string{}
for _, arg := range args {
if strings.HasPrefix(arg, "-browser") {
continue
}
if strings.HasPrefix(arg, "-daemon") {
if arg == "-daemon=false" {
panic("Refusing to daemonize as proc asked for foreground")
}
continue
}
filtered = append(filtered, arg)
}
args = append(args, "-browser=false")
args = append(args, "-daemon=false")
cmd := exec.Command(os.Args[0], args...)
return name, append(filtered, "-browser=false", "-daemon=false")
}

// Daemonize (please use something like upstart, daemontools, systemd)
func daemonize() {
name, args := daemonizeCmd(os.Args)
cmd := exec.Command(name, args...)
cmd.Start()
log.Infof("Started pid=%v", cmd.Process.Pid)
os.Exit(0)
Expand Down
46 changes: 46 additions & 0 deletions daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,49 @@ func TestRunning(t *testing.T) {
*bind = h
assert.True(t, running())
}

func TestDaemonizeCmd(t *testing.T) {
p := "/usr/local/bin/notable"
args := []string{p}
name, out := daemonizeCmd(args)
assert.Equal(t, p, name)
assert.Equal(t, []string{"-browser=false", "-daemon=false"}, out)
}

func TestDaemonizeCmdWithBrowser(t *testing.T) {
p := "/usr/local/bin/notable"
args := []string{p, "-browser=true"}
name, out := daemonizeCmd(args)
assert.Equal(t, p, name)
assert.Equal(t, []string{"-browser=false", "-daemon=false"}, out)
}

func TestDaemonizeCmdWithPort(t *testing.T) {
p := "/usr/local/bin/notable"
args := []string{p, "-port=8000"}
name, out := daemonizeCmd(args)
assert.Equal(t, p, name)
assert.Equal(t, []string{"-port=8000", "-browser=false", "-daemon=false"}, out)
}

func TestDaemonizeCmdWantingToDaemonize(t *testing.T) {
p := "/usr/local/bin/notable"
args := []string{p, "-daemon=true"}
name, out := daemonizeCmd(args)
assert.Equal(t, p, name)
assert.Equal(t, []string{"-browser=false", "-daemon=false"}, out)

args = []string{p, "-daemon=true", "-browser=true"}
name, out = daemonizeCmd(args)
assert.Equal(t, p, name)
assert.Equal(t, []string{"-browser=false", "-daemon=false"}, out)
}

func TestDaemonizeCannotRecurse(t *testing.T) {
p := "/usr/local/bin/notable"
// If the args to the running process asked to run in the
// foreground, nothing should even attempt to daemonize it. If
// something accidentally does... panic.
args := []string{p, "-daemon=false"}
assert.Panics(t, func() { daemonizeCmd(args) })
}
3 changes: 1 addition & 2 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ func TestPidHandler(t *testing.T) {
assert.Nil(t, err)
body, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
pid, err := strconv.Atoi(string(body))
_, err = strconv.Atoi(string(body))
assert.Nil(t, err, fmt.Sprintf("Not a valid pid: %s", string(body)))
assert.True(t, pid > 1024)
}

func TestVersionHandler(t *testing.T) {
Expand Down

0 comments on commit 31e1e0c

Please sign in to comment.