This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
podstore_test.go
218 lines (182 loc) · 6.4 KB
/
podstore_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package podstore
import (
"testing"
"time"
podstore_protos "github.com/square/p2/pkg/grpc/podstore/protos"
"github.com/square/p2/pkg/grpc/testutil"
"github.com/square/p2/pkg/launch"
"github.com/square/p2/pkg/store/consul"
"github.com/square/p2/pkg/store/consul/consulutil"
"github.com/square/p2/pkg/store/consul/podstore"
"github.com/square/p2/pkg/store/consul/statusstore/podstatus"
"github.com/square/p2/pkg/store/consul/statusstore/statusstoretest"
"github.com/square/p2/pkg/types"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
func TestSchedulePod(t *testing.T) {
fakePodStore, server := setupServerWithFakePodStore()
req := &podstore_protos.SchedulePodRequest{
Manifest: validManifestString(),
NodeName: "test_node",
}
resp, err := server.SchedulePod(context.Background(), req)
if err != nil {
t.Fatalf("Unexpected error from SchedulePod: %s", err)
}
// check that the key we got back actually returns an entry from the store
pod, err := fakePodStore.ReadPod(types.PodUniqueKey(resp.PodUniqueKey))
if err != nil {
t.Fatalf("Unexpected error reading pod out of store after scheduling: %s", err)
}
if pod.Manifest.ID() != "test_app" {
t.Errorf("Scheduled pod manifest had wrong ID, expected %q but got %q", "test_app", pod.Manifest.ID())
}
if pod.Node != "test_node" {
t.Errorf("Scheduled node didn't match expectation, expected %q but got %q", "test_node", pod.Node)
}
}
func TestSchedulePodFailsNoNodeName(t *testing.T) {
_, server := setupServerWithFakePodStore()
req := &podstore_protos.SchedulePodRequest{
Manifest: validManifestString(),
NodeName: "", // MISSING
}
_, err := server.SchedulePod(context.Background(), req)
if err == nil {
t.Fatal("Expected an error when the request is missing node name, but didn't get one")
}
if grpc.Code(err) != codes.InvalidArgument {
t.Errorf("Expected error to be %s but was %s", codes.InvalidArgument.String(), grpc.ErrorDesc(err))
}
}
func TestSchedulePodFailsNoManifest(t *testing.T) {
_, server := setupServerWithFakePodStore()
req := &podstore_protos.SchedulePodRequest{
Manifest: "", // MISSING
NodeName: "test_node",
}
_, err := server.SchedulePod(context.Background(), req)
if err == nil {
t.Fatal("Expected an error when the request is missing manifest, but didn't get one")
}
if grpc.Code(err) != codes.InvalidArgument {
t.Errorf("Expected error to be %s but was %s", codes.InvalidArgument.String(), grpc.ErrorDesc(err))
}
}
func validManifestString() string {
return "id: test_app"
}
// implements podstore_protos.PodStore_WatchPodStatusServer
type WatchPodStatusStream struct {
*testutil.FakeServerStream
ResponseCh chan *podstore_protos.PodStatusResponse
}
// Records responses sent on the stream
func (w *WatchPodStatusStream) Send(resp *podstore_protos.PodStatusResponse) error {
w.ResponseCh <- resp
return nil
}
func TestWatchPodStatus(t *testing.T) {
respCh := make(chan *podstore_protos.PodStatusResponse)
stream := &WatchPodStatusStream{
FakeServerStream: testutil.NewFakeServerStream(context.Background()),
ResponseCh: respCh,
}
podStatusStore, server := setupServerWithFakePodStatusStore()
podUniqueKey := types.NewPodUUID()
req := &podstore_protos.WatchPodStatusRequest{
StatusNamespace: consul.PreparerPodStatusNamespace.String(),
PodUniqueKey: podUniqueKey.String(),
WaitForExists: true,
}
watchErrCh := make(chan error)
defer close(watchErrCh)
go func() {
err := server.WatchPodStatus(req, stream)
if err != nil {
watchErrCh <- err
}
}()
expectedTime := time.Now()
expectedManifest := `id: "test_app"`
expectedPodState := podstatus.PodLaunched
expectedLaunchableID := launch.LaunchableID("nginx")
expectedEntryPoint := "launch"
expectedExitCode := 3
expectedExitStatus := 4
setStatusErrCh := make(chan error)
defer close(setStatusErrCh)
go func() {
err := podStatusStore.Set(podUniqueKey, podstatus.PodStatus{
Manifest: expectedManifest,
PodStatus: expectedPodState,
ProcessStatuses: []podstatus.ProcessStatus{
{
LaunchableID: expectedLaunchableID,
EntryPoint: expectedEntryPoint,
LastExit: &podstatus.ExitStatus{
ExitTime: expectedTime,
ExitCode: expectedExitCode,
ExitStatus: expectedExitStatus,
},
},
},
})
if err != nil {
setStatusErrCh <- err
}
}()
select {
case <-time.After(5 * time.Second):
t.Fatal("Didn't receive value after 5 seconds")
case err := <-setStatusErrCh:
t.Fatalf("Error setting status to trigger watch: %s", err)
case err := <-watchErrCh:
t.Fatalf("Unexpected error watching for status: %s", err)
case resp := <-respCh:
if resp.Manifest != expectedManifest {
t.Errorf("Manifest didn't match expected, wanted %q got %q", expectedManifest, resp.Manifest)
}
if resp.PodState != expectedPodState.String() {
t.Errorf("PodState didn't match expcted, wanted %q got %q", expectedPodState, resp.PodState)
}
if len(resp.ProcessStatuses) != 1 {
t.Fatalf("Expected 1 process status in pod status but got %d", len(resp.ProcessStatuses))
}
processStatus := resp.ProcessStatuses[0]
if processStatus.LaunchableId != expectedLaunchableID.String() {
t.Errorf("Expected process status for launchable %q but found %q", expectedLaunchableID, processStatus.LaunchableId)
}
if processStatus.EntryPoint != expectedEntryPoint {
t.Errorf("Expected process status for entry point %q but found %q", expectedEntryPoint, processStatus.EntryPoint)
}
if processStatus.LastExit == nil {
t.Fatal("Expected exit information for process")
}
lastExit := processStatus.LastExit
if lastExit.ExitTime != expectedTime.Unix() {
t.Error("Exit time for process in status didn't match expected")
}
if lastExit.ExitCode != int64(expectedExitCode) {
t.Errorf("Expected exit code %d but got %d", expectedExitCode, lastExit.ExitCode)
}
if lastExit.ExitStatus != int64(expectedExitStatus) {
t.Errorf("Expected exit status %d but got %d", expectedExitStatus, lastExit.ExitStatus)
}
}
}
func setupServerWithFakePodStore() (podstore.Store, store) {
fakePodStore := podstore.NewConsul(consulutil.NewFakeClient().KV_)
server := store{
scheduler: fakePodStore,
}
return fakePodStore, server
}
func setupServerWithFakePodStatusStore() (podstatus.Store, store) {
fakePodStatusStore := podstatus.NewConsul(statusstoretest.NewFake(), consul.PreparerPodStatusNamespace)
return fakePodStatusStore, store{
podStatusStore: fakePodStatusStore,
}
}