-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathunit_test.go
More file actions
197 lines (154 loc) · 4.21 KB
/
unit_test.go
File metadata and controls
197 lines (154 loc) · 4.21 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
185
186
187
188
189
190
191
192
193
194
195
196
197
package fleet
import (
"io/ioutil"
"path"
"reflect"
"strings"
"sync"
"testing"
"github.com/coreos/fleet/schema"
"github.com/deis/deis/deisctl/units"
)
func TestUnits(t *testing.T) {
t.Parallel()
testUnits := []*schema.Unit{
&schema.Unit{
Name: "deis-router@1.service",
},
&schema.Unit{
Name: "deis-router@2.service",
},
&schema.Unit{
Name: "deis-router@3.service",
},
}
c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{}}}
expected := []string{"deis-router@1.service", "deis-router@2.service", "deis-router@3.service"}
// Test that the correct units are resolved when providing a target string
// that EXCEPT for lacking the "deis-" prefix matches the beginning of one
// or more units' name...
targets, err := c.Units("router")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(targets, expected) {
t.Fatalf("Expected %v, Got %v", expected, targets)
}
// Test that the correct units are resolved when providing a target string
// INCLUDING the "deis-" prefix that matches the beginning of one or more
// units' name...
targets, err = c.Units("deis-router")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(targets, expected) {
t.Fatalf("Expected %v, Got %v", expected, targets)
}
// Test that no units are resolved and an error is returned when providing
// a target string that does not match the BEGINNING of a service unit name,
// either with or without the "deis-" prefix. We deliberately test here using
// a string that IS a substring (albeit in the wrong position) of service
// units in the stub...
targets, err = c.Units("outer")
if err == nil || !strings.HasPrefix(err.Error(), "could not find unit:") {
t.Fatalf("Expected an error beginning with \"could not find unit:\", but did not get one")
}
}
func TestNextUnit(t *testing.T) {
t.Parallel()
testUnits := []*schema.Unit{
&schema.Unit{
Name: "deis-router@1.service",
},
&schema.Unit{
Name: "deis-router@3.service",
},
}
c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{}}}
num, err := c.nextUnit("router")
if err != nil {
t.Fatal(err)
}
expected := 2
if num != expected {
t.Fatalf("Expected %d, Got %d", expected, num)
}
}
func TestLastUnit(t *testing.T) {
t.Parallel()
testUnits := []*schema.Unit{
&schema.Unit{
Name: "deis-router@1.service",
},
&schema.Unit{
Name: "deis-router@3.service",
},
}
c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{}}}
num, err := c.lastUnit("router")
if err != nil {
t.Fatal(err)
}
expected := 3
if num != expected {
t.Fatalf("Expected %d, Got %d", expected, num)
}
}
func TestFormatUnitName(t *testing.T) {
t.Parallel()
unitName, err := formatUnitName("router", 1)
if err != nil {
t.Fatal(err)
}
if unitName != "deis-router@1.service" {
t.Fatalf("invalid unit name: %v", unitName)
}
}
func TestNewUnit(t *testing.T) {
t.Parallel()
name, err := ioutil.TempDir("", "deisctl-fleetctl")
unitFile := `[Unit]
Description=deis-controller`
unit := "deis-controller"
ioutil.WriteFile(path.Join(name, unit+".service"), []byte(unitFile), 777)
uf, err := NewUnit(unit[5:], []string{name}, false)
if err != nil {
t.Fatal(err)
}
result := uf.Contents["Unit"]["Description"][0]
expected := unitFile[19:]
if result != expected {
t.Errorf("Expected: %s, Got %s", expected)
}
}
func TestReadTemplate(t *testing.T) {
t.Parallel()
name, err := ioutil.TempDir("", "deisctl-fleetctl")
expected := []byte("test")
if err != nil {
t.Error(err)
}
for _, unit := range units.Names {
ioutil.WriteFile(path.Join(name, unit+".service"), expected, 777)
output, err := readTemplate(unit[5:], []string{name})
if err != nil {
t.Error(err)
}
if string(output) != string(expected) {
t.Errorf("Unit %s: Expected %s, Got %s", unit, expected, output)
}
}
}
func TestReadTemplateError(t *testing.T) {
t.Parallel()
name, err := ioutil.TempDir("", "deisctl-fleetctl")
if err != nil {
t.Error(err)
}
_, err = readTemplate("foo", []string{name})
expected := "Could not find unit template for foo"
errorf := err.Error()
if errorf != expected {
t.Errorf("Expected %s, Got %s", expected, errorf)
}
}