forked from lostz/percona-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
96 lines (82 loc) · 2.57 KB
/
service.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
/*
Copyright (c) 2014-2015, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package mock
import (
"fmt"
"github.com/percona/cloud-protocol/proto/v1"
"github.com/percona/percona-agent/pct"
)
type MockServiceManager struct {
name string
traceChan chan string
readyChan chan bool
StartErr error
StopErr error
IsRunningVal bool
status *pct.Status
Cmds []*proto.Cmd
}
func NewMockServiceManager(name string, readyChan chan bool, traceChan chan string) *MockServiceManager {
m := &MockServiceManager{
name: name,
readyChan: readyChan,
traceChan: traceChan,
status: pct.NewStatus([]string{name}),
Cmds: []*proto.Cmd{},
}
return m
}
func (m *MockServiceManager) Start() error {
m.traceChan <- fmt.Sprintf("Start %s", m.name)
// Return when caller is ready. This allows us to simulate slow starts.
m.status.Update(m.name, "Starting")
<-m.readyChan
m.IsRunningVal = true
m.status.Update(m.name, "Ready")
return m.StartErr
}
func (m *MockServiceManager) Stop() error {
m.traceChan <- "Stop " + m.name
// Return when caller is ready. This allows us to simulate slow stops.
m.status.Update(m.name, "Stopping")
<-m.readyChan
m.IsRunningVal = false
m.status.Update(m.name, "Stopped")
return m.StopErr
}
func (m *MockServiceManager) Status() map[string]string {
m.traceChan <- "Status " + m.name
return m.status.All()
}
func (m *MockServiceManager) GetConfig() ([]proto.AgentConfig, []error) {
configs := []proto.AgentConfig{
{
InternalService: m.name,
Config: `{"Foo":"bar"}`,
Running: m.IsRunningVal,
},
}
return configs, nil
}
func (m *MockServiceManager) IsRunning() bool {
m.traceChan <- "IsRunning " + m.name
return m.IsRunningVal
}
func (m *MockServiceManager) Handle(cmd *proto.Cmd) *proto.Reply {
m.Cmds = append(m.Cmds, cmd)
return cmd.Reply(nil)
}
func (m *MockServiceManager) Reset() {
m.status.Update(m.name, "")
}