-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
226 lines (180 loc) · 4.79 KB
/
fake.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package service
import (
"strings"
"github.com/juju/errors"
"github.com/juju/testing"
"github.com/juju/utils/set"
"github.com/juju/juju/service/common"
)
type serviceInfo interface {
Name() string
Conf() common.Conf
}
// FakeServiceData holds the results of Service method calls.
type FakeServiceData struct {
testing.Stub
// Installed is the list of all services that were installed.
Installed []serviceInfo
// Removed is the list of all services that were removed.
Removed []serviceInfo
// ManagedNames is the set of "currently" juju-managed services.
ManagedNames set.Strings
// InstalledNames is the set of "currently" installed services.
InstalledNames set.Strings
// RunningNames is the set of "currently" running services.
RunningNames set.Strings
// InstallCommands is the value to return for Service.InstallCommands.
InstallCommands []string
}
// NewFakeServiceData returns a new FakeServiceData.
func NewFakeServiceData() *FakeServiceData {
return &FakeServiceData{
ManagedNames: set.NewStrings(),
InstalledNames: set.NewStrings(),
RunningNames: set.NewStrings(),
}
}
// SetStatus updates the status of the named service.
func (fsd *FakeServiceData) SetStatus(name, status string) error {
if status == "" {
fsd.ManagedNames.Remove(name)
fsd.InstalledNames.Remove(name)
fsd.RunningNames.Remove(name)
return nil
}
managed := true
if strings.HasPrefix(status, "(") && strings.HasSuffix(status, ")") {
status = status[1 : len(status)-1]
managed = false
}
switch status {
case "installed":
fsd.InstalledNames.Add(name)
fsd.RunningNames.Remove(name)
case "running":
fsd.InstalledNames.Add(name)
fsd.RunningNames.Add(name)
default:
return errors.NotSupportedf("status %q", status)
}
if managed {
fsd.ManagedNames.Add(name)
}
return nil
}
var _ Service = (*FakeService)(nil)
// FakeService is a Service implementation for testing.
type FakeService struct {
*FakeServiceData
common.Service
}
// NewFakeService returns a new FakeService.
func NewFakeService(name string, conf common.Conf) *FakeService {
return &FakeService{
FakeServiceData: NewFakeServiceData(),
Service: common.Service{
Name: name,
Conf: conf,
},
}
}
// Name implements Service.
func (ss *FakeService) Name() string {
ss.AddCall("Name")
ss.NextErr()
return ss.Service.Name
}
// Conf implements Service.
func (ss *FakeService) Conf() common.Conf {
ss.AddCall("Conf")
ss.NextErr()
return ss.Service.Conf
}
// UpdateConfig implements Service.
func (ss *FakeService) UpdateConfig(conf common.Conf) {
ss.AddCall("Conf", conf)
ss.Service.UpdateConfig(conf)
ss.NextErr()
}
// Running implements Service.
func (ss *FakeService) Running() bool {
ss.AddCall("Running")
if ss.NextErr() != nil {
return false
}
return ss.running()
}
func (ss *FakeService) running() bool {
return ss.FakeServiceData.RunningNames.Contains(ss.Service.Name)
}
// Start implements Service.
func (ss *FakeService) Start() error {
ss.AddCall("Start")
// TODO(ericsnow) Check managed?
if ss.running() {
ss.FakeServiceData.RunningNames.Add(ss.Service.Name)
}
return ss.NextErr()
}
// Stop implements Service.
func (ss *FakeService) Stop() error {
ss.AddCall("Stop")
if !ss.running() {
ss.FakeServiceData.RunningNames.Remove(ss.Service.Name)
}
return ss.NextErr()
}
// StopAndRemove implements Service.
func (ss *FakeService) StopAndRemove() error {
if err := ss.Stop(); err != nil {
return err
}
return ss.Remove()
}
// Exists implements Service.
func (ss *FakeService) Exists() bool {
ss.AddCall("Exists")
if ss.NextErr() != nil {
return false
}
return ss.managed()
}
func (ss *FakeService) managed() bool {
return ss.FakeServiceData.ManagedNames.Contains(ss.Service.Name)
}
// Installed implements Service.
func (ss *FakeService) Installed() bool {
ss.AddCall("Installed")
if ss.NextErr() != nil {
return false
}
return ss.installed()
}
func (ss *FakeService) installed() bool {
return ss.FakeServiceData.InstalledNames.Contains(ss.Service.Name)
}
// Install implements Service.
func (ss *FakeService) Install() error {
ss.AddCall("Install")
if !ss.running() && !ss.installed() {
ss.FakeServiceData.Installed = append(ss.FakeServiceData.Installed, ss)
ss.FakeServiceData.InstalledNames.Add(ss.Service.Name)
}
return ss.NextErr()
}
// Remove implements Service.
func (ss *FakeService) Remove() error {
ss.AddCall("Remove")
if ss.installed() {
ss.FakeServiceData.Removed = append(ss.FakeServiceData.Removed, ss)
ss.FakeServiceData.InstalledNames.Remove(ss.Service.Name)
}
return ss.NextErr()
}
// InstallCommands implements Service.
func (ss *FakeService) InstallCommands() ([]string, error) {
ss.AddCall("InstallCommands")
return ss.FakeServiceData.InstallCommands, ss.NextErr()
}