-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfleet_test.go
More file actions
140 lines (115 loc) · 2.77 KB
/
fleet_test.go
File metadata and controls
140 lines (115 loc) · 2.77 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
package fleet
import (
"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
}
func (c *stubFleetClient) SetUnitTargetState(name, target string) error {
var activeState string
var subState string
if target == "loaded" {
activeState = "inactive"
subState = "dead"
} else if target == "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 logState(outchan chan string, errchan chan error, errOutput *string, mutex *sync.Mutex) {
for {
select {
case _, ok := <-outchan:
if !ok {
outchan = nil
}
case err, ok := <-errchan:
if !ok {
errchan = nil
}
if err != nil {
mutex.Lock()
*errOutput += err.Error()
mutex.Unlock()
}
}
if outchan == nil && errchan == nil {
break
}
}
}
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)
}
}