-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfleet_test.go
More file actions
184 lines (148 loc) · 3.63 KB
/
fleet_test.go
File metadata and controls
184 lines (148 loc) · 3.63 KB
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
package fleet
import (
"bytes"
"fmt"
"sync"
"testing"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/schema"
)
type stubFleetClient struct {
testUnits []*schema.Unit
testUnitStates []*schema.UnitState
testMachineStates []machine.MachineState
unitStatesMutex *sync.Mutex
unitsMutex *sync.Mutex
}
func (c *stubFleetClient) Machines() ([]machine.MachineState, error) {
return c.testMachineStates, nil
}
func (c *stubFleetClient) Unit(name string) (*schema.Unit, error) {
c.unitsMutex.Lock()
defer c.unitsMutex.Unlock()
for _, unit := range c.testUnits {
if unit.Name == name {
return unit, nil
}
}
return nil, fmt.Errorf("Unit %s not found", name)
}
func (c *stubFleetClient) Units() ([]*schema.Unit, error) {
c.unitsMutex.Lock()
defer c.unitsMutex.Unlock()
return c.testUnits, nil
}
func (c *stubFleetClient) UnitStates() ([]*schema.UnitState, error) {
c.unitStatesMutex.Lock()
defer c.unitStatesMutex.Unlock()
return c.testUnitStates, nil
}
type failingFleetClient struct {
stubFleetClient
}
func (c *failingFleetClient) SetUnitTargetState(name, target string) error {
if err := c.stubFleetClient.SetUnitTargetState(name, target); err != nil {
return err
}
last := len(c.testUnitStates) - 1
c.testUnitStates[last] = &schema.UnitState{
Name: name,
SystemdSubState: "failed",
SystemdActiveState: "failed",
}
return nil
}
func (c *stubFleetClient) SetUnitTargetState(name, target string) error {
var activeState string
var subState string
switch target {
case "loaded":
activeState = "inactive"
subState = "dead"
case "launched":
activeState = "active"
subState = "running"
}
unit, err := c.Unit(name)
if err != nil {
return err
}
c.unitsMutex.Lock()
unit.DesiredState = target
c.unitsMutex.Unlock()
c.unitStatesMutex.Lock()
defer c.unitStatesMutex.Unlock()
for _, unitState := range c.testUnitStates {
if name == unitState.Name {
unitState.SystemdSubState = subState
unitState.SystemdActiveState = activeState
return nil
}
}
c.testUnitStates = append(c.testUnitStates, &schema.UnitState{Name: name, SystemdSubState: subState, SystemdActiveState: activeState})
return nil
}
func (c *stubFleetClient) CreateUnit(unit *schema.Unit) error {
c.unitsMutex.Lock()
c.testUnits = append(c.testUnits, unit)
c.unitsMutex.Unlock()
return nil
}
func (c *stubFleetClient) DestroyUnit(name string) error {
c.unitsMutex.Lock()
for i := len(c.testUnits) - 1; i >= 0; i-- {
if c.testUnits[i].Name == name {
c.testUnits = append(c.testUnits[:i], c.testUnits[i+1:]...)
}
}
c.unitsMutex.Unlock()
return nil
}
func newOutErr() *outErr {
return &outErr{
&syncBuffer{},
&syncBuffer{},
}
}
// Wrap output and error streams for ease of testing.
type outErr struct {
out, ew buffer
}
// buffer represents a buffer for collecting written test output.
//
// This is used only in testing, so add more bytes.Buffer methods as needed.
type buffer interface {
Bytes() []byte
String() string
Write([]byte) (int, error)
}
// syncBuffer simply synchronizes writes on a bytes.Buffer.
type syncBuffer struct {
bytes.Buffer
mx sync.RWMutex
}
func (s *syncBuffer) Write(b []byte) (int, error) {
s.mx.Lock()
defer s.mx.Unlock()
return s.Buffer.Write(b)
}
func (s *syncBuffer) String() string {
s.mx.RLock()
defer s.mx.RUnlock()
return s.Buffer.String()
}
func (s *syncBuffer) Bytes() []byte {
s.mx.RLock()
defer s.mx.RUnlock()
return s.Buffer.Bytes()
}
func TestNewClient(t *testing.T) {
t.Parallel()
// set required flags
Flags.Endpoint = "http://127.0.0.1:4001"
// instantiate client
_, err := NewClient()
if err != nil {
t.Fatal(err)
}
}