service: fix templates. Add full test.
This commit is contained in:
+5
-9
@@ -188,15 +188,11 @@ func (s *linuxService) Install() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var to = &struct {
|
var to = &struct {
|
||||||
Display string
|
*Config
|
||||||
Description string
|
Path string
|
||||||
Path string
|
|
||||||
Arguments []string
|
|
||||||
}{
|
}{
|
||||||
s.DisplayName,
|
s.Config,
|
||||||
s.Description,
|
|
||||||
path,
|
path,
|
||||||
s.Config.Arguments,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = flavor.Template().Execute(f, to)
|
err = flavor.Template().Execute(f, to)
|
||||||
@@ -319,7 +315,7 @@ const systemVScript = `#!/bin/sh
|
|||||||
# Required-Stop:
|
# Required-Stop:
|
||||||
# Default-Start: 2 3 4 5
|
# Default-Start: 2 3 4 5
|
||||||
# Default-Stop: 0 1 6
|
# Default-Stop: 0 1 6
|
||||||
# Short-Description: {{.Display}}
|
# Short-Description: {{.DisplayName}}
|
||||||
# Description: {{.Description}}
|
# Description: {{.Description}}
|
||||||
### END INIT INFO
|
### END INIT INFO
|
||||||
|
|
||||||
@@ -405,7 +401,7 @@ exit 0`
|
|||||||
// the program before the Stop handler can run.
|
// the program before the Stop handler can run.
|
||||||
const upstartScript = `# {{.Description}}
|
const upstartScript = `# {{.Description}}
|
||||||
|
|
||||||
description "{{.Display}}"
|
{{if .DisplayName}}description "{{.DisplayName}}"{{end}}
|
||||||
|
|
||||||
kill signal INT
|
kill signal INT
|
||||||
start on filesystem or runlevel [2345]
|
start on filesystem or runlevel [2345]
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user