This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 302
/
http_test.go
173 lines (161 loc) · 3.83 KB
/
http_test.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
package client
import (
"reflect"
"testing"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/schema"
"github.com/coreos/fleet/unit"
)
func TestMapUnitEntityToJob(t *testing.T) {
loaded := job.JobStateLoaded
tests := []struct {
input schema.Unit
expect *job.Job
}{
{
schema.Unit{
Name: "XXX",
CurrentState: "loaded",
Systemd: &schema.SystemdState{
LoadState: "loaded",
ActiveState: "active",
SubState: "running",
},
FileContents: "W1NlcnZpY2VdCkV4ZWNTdGFydD0vdXNyL2Jpbi9zbGVlcCAzMDAwCg==",
FileHash: "248b997d6becee1b835b7ec7d9c8e68d7dd24623",
},
&job.Job{
Name: "XXX",
State: &loaded,
Unit: unit.Unit{
Raw: "[Service]\nExecStart=/usr/bin/sleep 3000\n",
Contents: map[string]map[string][]string{
"Service": map[string][]string{
"ExecStart": []string{"/usr/bin/sleep 3000"},
},
},
},
UnitState: &unit.UnitState{
LoadState: "loaded",
ActiveState: "active",
SubState: "running",
},
},
},
// Lack of LoadState should result in a nil UnitState
{
schema.Unit{
Name: "XXX",
CurrentState: "loaded",
FileContents: "W1NlcnZpY2VdCkV4ZWNTdGFydD0vdXNyL2Jpbi9zbGVlcCAzMDAwCg==",
FileHash: "248b997d6becee1b835b7ec7d9c8e68d7dd24623",
},
&job.Job{
Name: "XXX",
State: &loaded,
Unit: unit.Unit{
Raw: "[Service]\nExecStart=/usr/bin/sleep 3000\n",
Contents: map[string]map[string][]string{
"Service": map[string][]string{
"ExecStart": []string{"/usr/bin/sleep 3000"},
},
},
},
},
},
}
for i, tt := range tests {
output, err := mapUnitToJob(&tt.input, nil)
if err != nil {
t.Errorf("case %d: err=%v", i, err)
continue
}
if !reflect.DeepEqual(tt.expect, output) {
t.Errorf("case %d: expect=%v, got=%v", i, tt.expect, *output)
}
}
}
func TestMapUnitEntityToJobFailure(t *testing.T) {
units := []schema.Unit{
// Poorly-formatted FileContents should result in an error
schema.Unit{
Name: "XXX",
CurrentState: "loaded",
Systemd: &schema.SystemdState{
LoadState: "loaded",
ActiveState: "active",
SubState: "running",
MachineID: "YYY",
},
FileContents: "XXX",
FileHash: "248b997d6becee1b835b7ec7d9c8e68d7dd24623",
},
}
for i, u := range units {
output, err := mapUnitToJob(&u, nil)
if err == nil {
t.Errorf("case %d: expected non-nil error", i)
}
if output != nil {
t.Errorf("case %d: expected nil Job, got %v", i, output)
}
}
}
func TestMapUnitEntityToJobMachineFields(t *testing.T) {
tests := []struct {
input schema.Unit
expect *job.Job
}{
{
schema.Unit{
Systemd: &schema.SystemdState{
LoadState: "loaded",
ActiveState: "active",
SubState: "running",
MachineID: "YYY",
},
},
&job.Job{
UnitState: &unit.UnitState{
LoadState: "loaded",
ActiveState: "active",
SubState: "running",
MachineID: "YYY",
},
},
},
// Missing MachineState in map does not result in loss of Machine ID
{
schema.Unit{
Systemd: &schema.SystemdState{
LoadState: "loaded",
ActiveState: "active",
SubState: "running",
MachineID: "FFF",
},
},
&job.Job{
UnitState: &unit.UnitState{
LoadState: "loaded",
ActiveState: "active",
SubState: "running",
MachineID: "FFF",
},
},
},
}
mm := map[string]*machine.MachineState{
"YYY": &machine.MachineState{ID: "YYY", PublicIP: "ZZZ"},
}
for i, tt := range tests {
output, err := mapUnitToJob(&tt.input, mm)
if err != nil {
t.Errorf("case %d: err=%v", i, err)
continue
}
if !reflect.DeepEqual(tt.expect.UnitState, output.UnitState) {
t.Errorf("case %d: expect=%v, got=%v", i, tt.expect.UnitState, output.UnitState)
}
}
}