forked from kardianos/service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
112 lines (98 loc) · 1.76 KB
/
service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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
import (
"log"
"os"
"runtime"
"testing"
)
const runAsServiceArg = "RunThisAsService"
var sc = &Config{
Name: "go_service_test",
Arguments: []string{runAsServiceArg},
Option: KeyValue{
"UserService": userService(),
},
}
func userService() bool {
if runtime.GOOS == "darwin" {
return true
} else {
return false
}
}
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 := New(p, sc)
if err != nil {
t.Fatalf("New err: %s", err)
}
err = s.Status()
if err != ErrServiceIsNotInstalled {
t.Fatal("status", err)
}
err = s.Install()
if err != nil {
t.Fatal("install", err)
}
defer s.Uninstall()
err = s.Status()
if err != ErrServiceIsNotRunning {
t.Fatal("status", err)
}
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.Status()
if err != ErrServiceIsNotRunning {
t.Fatal("status", err)
}
err = s.Uninstall()
if err != nil {
t.Fatal("uninstall", err)
}
err = s.Status()
if err != ErrServiceIsNotInstalled {
t.Fatal("status", err)
}
}
func runService() {
p := &program{}
s, err := 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) error {
go p.run()
return nil
}
func (p *program) run() {
// Do work here
}
func (p *program) Stop(s Service) error {
return nil
}