-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper_test.go
136 lines (120 loc) · 3.45 KB
/
wrapper_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package service
import (
"fmt"
"os"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const servNameTest = "go-spfc-test"
var (
someServiceThatNotExists *Handler
someServiceThatExists *Handler
sudo bool
timeout time.Duration
forceTimeout time.Duration
)
func init() {
timeout = 5 * time.Second
forceTimeout = 1 * time.Nanosecond
sudoEnv := os.Getenv("GO_SPFC_SUDO_TEST")
if len(sudoEnv) == 0 {
sudo = false
} else {
sudo, _ = strconv.ParseBool(sudoEnv)
}
fmt.Printf("As sudo? %t\n", sudo)
createService()
someServiceThatNotExists = &Handler{sudo, "someservicethatnotexists"}
someServiceThatExists = &Handler{sudo, servNameTest}
}
func TestNewHandler(t *testing.T) {
h := NewHandler("")
assert.NotNil(t, h)
}
func TestWhenStartAServiceThatDoesNotExist_ShouldGiveAnError(t *testing.T) {
ss, err := someServiceThatNotExists.Start()
assert.NotNil(t, err, "Should give an error")
assert.False(t, ss.Running)
assert.Equal(t, 0, ss.PID)
}
func TestWhenStartAServiceThatDoesExist_ShoudWorkFine(t *testing.T) {
st, err := someServiceThatExists.Start()
if err != nil {
panic(err.Error())
}
assert.True(t, st.Running)
assert.NotEqual(t, 0, st.PID)
}
func TestWhenGetStatusForAServiceThatDoesNotExist_ShouldGiveAnError(t *testing.T) {
st, err := someServiceThatNotExists.GetStatus()
assert.NotNil(t, err, "Should give an error")
assert.False(t, st.Running)
assert.Equal(t, 0, st.PID)
}
func TestWhenGetStatusForAServiceThatDoesExist_ShoudWorkFine(t *testing.T) {
st, err := someServiceThatExists.GetStatus()
if err != nil {
panic(err.Error())
}
assert.True(t, st.Running)
assert.NotEqual(t, 0, st.PID)
}
func TestWhenStopAServiceThatDoesNotExist_ShouldGiveAnError(t *testing.T) {
st, err := someServiceThatNotExists.Stop()
assert.NotNil(t, err, "Should give an error")
assert.False(t, st.Running)
assert.Equal(t, 0, st.PID)
}
func TestWhenStopAServiceThatDoesExist_ShoudWorkFine(t *testing.T) {
defer removeService()
st, _ := someServiceThatExists.GetStatus()
assert.True(t, st.Running)
assert.NotEqual(t, 0, st.PID)
st, err := someServiceThatExists.Stop()
if err != nil {
panic(err.Error())
}
waitStop()
assert.False(t, st.Running)
assert.Equal(t, 0, st.PID)
}
func TestWhenStartAndWaitAServiceThatDoesExist_ShoudWorkFine(t *testing.T) {
defer removeService()
createService()
st, err := someServiceThatExists.StartAndWait(timeout)
if err != nil {
panic(err.Error())
}
assert.True(t, st.Running)
assert.NotEqual(t, 0, st.PID)
}
func TestWhenStartAndWaitAServiceThatDoesExistButTimeout_ShouldGiveAnError(t *testing.T) {
createService()
_, err := someServiceThatExists.StartAndWait(forceTimeout)
assert.NotNil(t, err, "Should give an error")
assert.EqualError(t, err, "timeout after 1ns")
}
func TestWhenStopAndWaitAServiceThatDoesExist_ShoudWorkFine(t *testing.T) {
defer removeService()
st, err := someServiceThatExists.StopAndWait(timeout)
if err != nil {
panic(err.Error())
}
assert.False(t, st.Running)
assert.Equal(t, 0, st.PID)
}
func TestWhenStopAndWaitAServiceThatDoesExistButTimeout_ShouldGiveAnError(t *testing.T) {
defer removeService()
createService()
st, err := someServiceThatExists.StartAndWait(timeout)
assert.True(t, st.Running)
assert.NotEqual(t, 0, st.PID)
if err != nil {
panic(err.Error())
}
_, err = someServiceThatExists.StopAndWait(forceTimeout)
assert.NotNil(t, err, "Should give an error")
assert.EqualError(t, err, "timeout after 1ns")
}