service: fix templates. Add full test.

This commit is contained in:
Daniel Theophanes
2015-03-07 09:42:36 -08:00
parent 1e3a000092
commit 9056e1f7ee
2 changed files with 90 additions and 9 deletions
+5 -9
View File
@@ -188,15 +188,11 @@ func (s *linuxService) Install() error {
}
var to = &struct {
Display string
Description string
Path string
Arguments []string
*Config
Path string
}{
s.DisplayName,
s.Description,
s.Config,
path,
s.Config.Arguments,
}
err = flavor.Template().Execute(f, to)
@@ -319,7 +315,7 @@ const systemVScript = `#!/bin/sh
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: {{.Display}}
# Short-Description: {{.DisplayName}}
# Description: {{.Description}}
### END INIT INFO
@@ -405,7 +401,7 @@ exit 0`
// the program before the Stop handler can run.
const upstartScript = `# {{.Description}}
description "{{.Display}}"
{{if .DisplayName}}description "{{.DisplayName}}"{{end}}
kill signal INT
start on filesystem or runlevel [2345]
+85
View File
@@ -0,0 +1,85 @@
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.package service
package service_test
import (
"log"
"os"
"testing"
"github.com/kardianos/service"
)
const runAsServiceArg = "RunThisAsService"
var sc = &service.Config{
Name: "go_service_test",
Arguments: []string{runAsServiceArg},
}
func TestMain(m *testing.M) {
if len(os.Args) > 1 && os.Args[1] == runAsServiceArg {
runService()
return
}
os.Exit(m.Run())
}
func TestInstallRunRestartStopRemove(t *testing.T) {
p := &program{}
s, err := service.New(p, sc)
if err != nil {
t.Fatal(err)
}
_ = s.Uninstall()
err = s.Install()
if err != nil {
t.Fatal("install", err)
}
defer s.Uninstall()
err = s.Start()
if err != nil {
t.Fatal("start", err)
}
err = s.Restart()
if err != nil {
t.Fatal("restart", err)
}
err = s.Stop()
if err != nil {
t.Fatal("stop", err)
}
err = s.Uninstall()
if err != nil {
t.Fatal("stop", err)
}
}
func runService() {
p := &program{}
s, err := service.New(p, sc)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
log.Fatal(err)
}
}
type program struct{}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {
// Do work here
}
func (p *program) Stop(s service.Service) error {
return nil
}