Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 1 | package testing |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 4 | "io" |
| 5 | "runtime" |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 6 | "sync" |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 7 | "testing" |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 8 | |
Tilak Sharma | 8657132 | 2014-06-11 14:15:07 -0700 | [diff] [blame] | 9 | "veyron/services/store/raw" |
| 10 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 11 | "veyron2/context" |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 12 | "veyron2/ipc" |
Andres Erbsen | cdeacfe | 2014-06-11 14:55:16 -0700 | [diff] [blame] | 13 | "veyron2/naming" |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 14 | "veyron2/rt" |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 15 | "veyron2/security" |
| 16 | "veyron2/services/watch" |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 17 | "veyron2/services/watch/types" |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 18 | "veyron2/storage" |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 21 | // FakeServerContext implements ipc.ServerContext. |
| 22 | type FakeServerContext struct { |
| 23 | context.T |
| 24 | cancel context.CancelFunc |
| 25 | id security.PublicID |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 28 | func NewFakeServerContext(id security.PublicID) *FakeServerContext { |
| 29 | ctx, cancel := rt.R().NewContext().WithCancel() |
| 30 | |
| 31 | return &FakeServerContext{ |
| 32 | T: ctx, |
| 33 | cancel: cancel, |
| 34 | id: id, |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 38 | func (*FakeServerContext) Server() ipc.Server { return nil } |
| 39 | func (*FakeServerContext) Method() string { return "" } |
| 40 | func (*FakeServerContext) Name() string { return "" } |
| 41 | func (*FakeServerContext) Suffix() string { return "" } |
| 42 | func (*FakeServerContext) Label() (l security.Label) { return } |
| 43 | func (*FakeServerContext) CaveatDischarges() security.CaveatDischargeMap { return nil } |
| 44 | func (ctx *FakeServerContext) LocalID() security.PublicID { return ctx.id } |
| 45 | func (ctx *FakeServerContext) RemoteID() security.PublicID { return ctx.id } |
| 46 | func (*FakeServerContext) Blessing() security.PublicID { return nil } |
| 47 | func (*FakeServerContext) LocalEndpoint() naming.Endpoint { return nil } |
| 48 | func (*FakeServerContext) RemoteEndpoint() naming.Endpoint { return nil } |
| 49 | func (ctx *FakeServerContext) Cancel() { ctx.cancel() } |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 50 | |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 51 | // Utilities for PutMutations. |
| 52 | |
| 53 | // storeServicePutMutationsStream implements raw.StoreServicePutMutationsStream |
| 54 | type storeServicePutMutationsStream struct { |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 55 | mus <-chan raw.Mutation |
| 56 | value raw.Mutation |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 59 | func (s *storeServicePutMutationsStream) RecvStream() interface { |
| 60 | Advance() bool |
| 61 | Value() raw.Mutation |
| 62 | Err() error |
| 63 | } { |
| 64 | return s |
| 65 | } |
| 66 | |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 67 | func (s *storeServicePutMutationsStream) Advance() bool { |
| 68 | var ok bool |
| 69 | s.value, ok = <-s.mus |
| 70 | return ok |
| 71 | } |
| 72 | |
| 73 | func (s *storeServicePutMutationsStream) Value() raw.Mutation { |
| 74 | return s.value |
| 75 | } |
| 76 | |
| 77 | func (s *storeServicePutMutationsStream) Err() error { |
| 78 | return nil |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // storePutMutationsStream implements raw.StorePutMutationsStream |
| 82 | type storePutMutationsStream struct { |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 83 | closed bool |
| 84 | mus chan<- raw.Mutation |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func (s *storePutMutationsStream) Send(mu raw.Mutation) error { |
| 88 | s.mus <- mu |
| 89 | return nil |
| 90 | } |
| 91 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 92 | func (s *storePutMutationsStream) Close() error { |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 93 | if !s.closed { |
| 94 | s.closed = true |
| 95 | close(s.mus) |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 100 | type storePutMutationsCall struct { |
| 101 | ctx ipc.ServerContext |
| 102 | stream storePutMutationsStream |
| 103 | err <-chan error |
| 104 | } |
| 105 | |
| 106 | func (s *storePutMutationsCall) SendStream() interface { |
| 107 | Send(mu raw.Mutation) error |
| 108 | Close() error |
| 109 | } { |
| 110 | return &s.stream |
| 111 | } |
| 112 | |
| 113 | func (s *storePutMutationsCall) Finish() error { |
| 114 | s.stream.Close() |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 115 | return <-s.err |
| 116 | } |
| 117 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 118 | func (s *storePutMutationsCall) Cancel() { |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 119 | s.ctx.(*FakeServerContext).Cancel() |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 120 | s.stream.Close() |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 123 | func PutMutations(id security.PublicID, putMutationsFn func(ipc.ServerContext, raw.StoreServicePutMutationsStream) error) raw.StorePutMutationsCall { |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 124 | ctx := NewFakeServerContext(id) |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 125 | mus := make(chan raw.Mutation) |
| 126 | err := make(chan error) |
| 127 | go func() { |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 128 | err <- putMutationsFn(ctx, &storeServicePutMutationsStream{mus: mus}) |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 129 | close(err) |
| 130 | }() |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 131 | return &storePutMutationsCall{ |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 132 | ctx: ctx, |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 133 | err: err, |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 134 | stream: storePutMutationsStream{ |
| 135 | mus: mus, |
| 136 | }, |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | func PutMutationsBatch(t *testing.T, id security.PublicID, putMutationsFn func(ipc.ServerContext, raw.StoreServicePutMutationsStream) error, mus []raw.Mutation) { |
| 141 | storePutMutationsStream := PutMutations(id, putMutationsFn) |
| 142 | for _, mu := range mus { |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 143 | storePutMutationsStream.SendStream().Send(mu) |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 144 | } |
| 145 | if err := storePutMutationsStream.Finish(); err != nil { |
| 146 | _, file, line, _ := runtime.Caller(1) |
| 147 | t.Errorf("%s(%d): can't put mutations %s: %s", file, line, mus, err) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Utilities for Watch. |
| 152 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 153 | // watcherServiceWatchStreamSender implements watch.WatcherServiceWatchStreamSender |
| 154 | type watcherServiceWatchStreamSender struct { |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 155 | mu *sync.Mutex |
| 156 | ctx ipc.ServerContext |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 157 | output chan<- types.ChangeBatch |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 160 | func (s *watcherServiceWatchStreamSender) Send(cb types.ChangeBatch) error { |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 161 | s.mu.Lock() |
| 162 | defer s.mu.Unlock() |
| 163 | select { |
| 164 | case s.output <- cb: |
| 165 | return nil |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 166 | case <-s.ctx.Done(): |
Tilak Sharma | 13329d9 | 2014-07-14 12:25:28 -0700 | [diff] [blame] | 167 | return io.EOF |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 171 | // watcherServiceWatchStream implements watch.WatcherServiceWatchStream |
| 172 | type watcherServiceWatchStream struct { |
| 173 | watcherServiceWatchStreamSender |
| 174 | } |
| 175 | |
| 176 | func (s *watcherServiceWatchStream) SendStream() interface { |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 177 | Send(cb types.ChangeBatch) error |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 178 | } { |
| 179 | return s |
| 180 | } |
| 181 | func (*watcherServiceWatchStream) Cancel() {} |
| 182 | |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 183 | // watcherWatchStream implements watch.WatcherWatchStream. |
| 184 | type watcherWatchStream struct { |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 185 | ctx *FakeServerContext |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 186 | value types.ChangeBatch |
| 187 | input <-chan types.ChangeBatch |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 188 | err <-chan error |
| 189 | } |
| 190 | |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 191 | func (s *watcherWatchStream) Advance() bool { |
| 192 | var ok bool |
| 193 | s.value, ok = <-s.input |
| 194 | return ok |
| 195 | } |
| 196 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 197 | func (s *watcherWatchStream) Value() types.ChangeBatch { |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 198 | return s.value |
| 199 | } |
| 200 | |
| 201 | func (*watcherWatchStream) Err() error { |
| 202 | return nil |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | func (s *watcherWatchStream) Finish() error { |
| 206 | <-s.input |
| 207 | return <-s.err |
| 208 | } |
| 209 | |
| 210 | func (s *watcherWatchStream) Cancel() { |
| 211 | s.ctx.Cancel() |
| 212 | } |
| 213 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 214 | func (s *watcherWatchStream) RecvStream() interface { |
| 215 | Advance() bool |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 216 | Value() types.ChangeBatch |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 217 | Err() error |
| 218 | } { |
| 219 | return s |
| 220 | } |
| 221 | |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 222 | func watchImpl(id security.PublicID, watchFn func(ipc.ServerContext, *watcherServiceWatchStream) error) *watcherWatchStream { |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 223 | mu := &sync.Mutex{} |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 224 | ctx := NewFakeServerContext(id) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 225 | c := make(chan types.ChangeBatch, 1) |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 226 | errc := make(chan error, 1) |
| 227 | go func() { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 228 | stream := &watcherServiceWatchStream{ |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 229 | watcherServiceWatchStreamSender{ |
| 230 | mu: mu, |
| 231 | ctx: ctx, |
| 232 | output: c, |
| 233 | }, |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 234 | } |
| 235 | err := watchFn(ctx, stream) |
Tilak Sharma | 4820234 | 2014-05-13 23:49:49 -0700 | [diff] [blame] | 236 | mu.Lock() |
| 237 | defer mu.Unlock() |
| 238 | ctx.Cancel() |
| 239 | close(c) |
| 240 | errc <- err |
| 241 | close(errc) |
| 242 | }() |
| 243 | return &watcherWatchStream{ |
| 244 | ctx: ctx, |
| 245 | input: c, |
| 246 | err: errc, |
| 247 | } |
| 248 | } |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 249 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 250 | func WatchRaw(id security.PublicID, watchFn func(ipc.ServerContext, raw.Request, raw.StoreServiceWatchStream) error, req raw.Request) raw.StoreWatchCall { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 251 | return watchImpl(id, func(ctx ipc.ServerContext, stream *watcherServiceWatchStream) error { |
| 252 | return watchFn(ctx, req, stream) |
| 253 | }) |
| 254 | } |
| 255 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 256 | func WatchGlob(id security.PublicID, watchFn func(ipc.ServerContext, types.GlobRequest, watch.GlobWatcherServiceWatchGlobStream) error, req types.GlobRequest) watch.GlobWatcherWatchGlobCall { |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 257 | return watchImpl(id, func(ctx ipc.ServerContext, iterator *watcherServiceWatchStream) error { |
| 258 | return watchFn(ctx, req, iterator) |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 259 | }) |
| 260 | } |
| 261 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 262 | func WatchGlobOnPath(id security.PublicID, watchFn func(ipc.ServerContext, storage.PathName, types.GlobRequest, watch.GlobWatcherServiceWatchGlobStream) error, path storage.PathName, req types.GlobRequest) watch.GlobWatcherWatchGlobCall { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 263 | return watchImpl(id, func(ctx ipc.ServerContext, stream *watcherServiceWatchStream) error { |
| 264 | return watchFn(ctx, path, req, stream) |
| 265 | }) |
| 266 | } |
| 267 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 268 | func ExpectInitialStateSkipped(t *testing.T, change types.Change) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 269 | if change.Name != "" { |
| 270 | t.Fatalf("Expect Name to be \"\" but was: %v", change.Name) |
| 271 | } |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 272 | if change.State != types.InitialStateSkipped { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 273 | t.Fatalf("Expect State to be InitialStateSkipped but was: %v", change.State) |
| 274 | } |
| 275 | if len(change.ResumeMarker) != 0 { |
| 276 | t.Fatalf("Expect no ResumeMarker but was: %v", change.ResumeMarker) |
| 277 | } |
| 278 | } |
| 279 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 280 | func ExpectEntryExists(t *testing.T, changes []types.Change, name string, id storage.ID, value string) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 281 | change := findEntry(t, changes, name) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 282 | if change.State != types.Exists { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 283 | t.Fatalf("Expected name to exist: %v", name) |
| 284 | } |
| 285 | cv, ok := change.Value.(*storage.Entry) |
| 286 | if !ok { |
| 287 | t.Fatal("Expected an Entry") |
| 288 | } |
| 289 | if cv.Stat.ID != id { |
| 290 | t.Fatalf("Expected ID to be %v, but was: %v", id, cv.Stat.ID) |
| 291 | } |
| 292 | if cv.Value != value { |
| 293 | t.Fatalf("Expected Value to be %v, but was: %v", value, cv.Value) |
| 294 | } |
| 295 | } |
| 296 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 297 | func ExpectEntryDoesNotExist(t *testing.T, changes []types.Change, name string) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 298 | change := findEntry(t, changes, name) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 299 | if change.State != types.DoesNotExist { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 300 | t.Fatalf("Expected name to not exist: %v", name) |
| 301 | } |
| 302 | if change.Value != nil { |
| 303 | t.Fatal("Expected entry to be nil") |
| 304 | } |
| 305 | } |
| 306 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 307 | func ExpectServiceEntryExists(t *testing.T, changes []types.Change, name string, id storage.ID, value string) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 308 | change := findEntry(t, changes, name) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 309 | if change.State != types.Exists { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 310 | t.Fatalf("Expected name to exist: %v", name) |
| 311 | } |
Ken Ashcraft | b8ddc35 | 2014-08-01 15:11:57 -0700 | [diff] [blame] | 312 | cv, ok := change.Value.(*storage.Entry) |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 313 | if !ok { |
| 314 | t.Fatal("Expected a service Entry") |
| 315 | } |
| 316 | if cv.Stat.ID != id { |
| 317 | t.Fatalf("Expected ID to be %v, but was: %v", id, cv.Stat.ID) |
| 318 | } |
| 319 | if cv.Value != value { |
| 320 | t.Fatalf("Expected Value to be %v, but was: %v", value, cv.Value) |
| 321 | } |
| 322 | } |
| 323 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 324 | func ExpectServiceEntryDoesNotExist(t *testing.T, changes []types.Change, name string) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 325 | change := findEntry(t, changes, name) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 326 | if change.State != types.DoesNotExist { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 327 | t.Fatalf("Expected name to not exist: %v", name) |
| 328 | } |
| 329 | if change.Value != nil { |
| 330 | t.Fatal("Expected entry to be nil") |
| 331 | } |
| 332 | } |
| 333 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 334 | func findEntry(t *testing.T, changes []types.Change, name string) types.Change { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 335 | for _, change := range changes { |
| 336 | if change.Name == name { |
| 337 | return change |
| 338 | } |
| 339 | } |
| 340 | t.Fatalf("Expected a change for name: %v", name) |
| 341 | panic("Should not reach here") |
| 342 | } |
| 343 | |
| 344 | var ( |
Ken Ashcraft | c79f0c0 | 2014-08-26 13:11:43 -0700 | [diff] [blame^] | 345 | EmptyDir = []raw.DEntry{} |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 346 | ) |
| 347 | |
Ken Ashcraft | c79f0c0 | 2014-08-26 13:11:43 -0700 | [diff] [blame^] | 348 | func DirOf(name string, id storage.ID) []raw.DEntry { |
| 349 | return []raw.DEntry{raw.DEntry{ |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 350 | Name: name, |
| 351 | ID: id, |
| 352 | }} |
| 353 | } |
| 354 | |
Ken Ashcraft | c79f0c0 | 2014-08-26 13:11:43 -0700 | [diff] [blame^] | 355 | func ExpectMutationExists(t *testing.T, changes []types.Change, id storage.ID, pre, post raw.Version, isRoot bool, value string, dir []raw.DEntry) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 356 | change := findMutation(t, changes, id) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 357 | if change.State != types.Exists { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 358 | t.Fatalf("Expected id to exist: %v", id) |
| 359 | } |
| 360 | cv := change.Value.(*raw.Mutation) |
| 361 | if cv.PriorVersion != pre { |
| 362 | t.Fatalf("Expected PriorVersion to be %v, but was: %v", pre, cv.PriorVersion) |
| 363 | } |
| 364 | if cv.Version != post { |
| 365 | t.Fatalf("Expected Version to be %v, but was: %v", post, cv.Version) |
| 366 | } |
| 367 | if cv.IsRoot != isRoot { |
| 368 | t.Fatalf("Expected IsRoot to be: %v, but was: %v", isRoot, cv.IsRoot) |
| 369 | } |
| 370 | if cv.Value != value { |
| 371 | t.Fatalf("Expected Value to be: %v, but was: %v", value, cv.Value) |
| 372 | } |
| 373 | expectDirEquals(t, cv.Dir, dir) |
| 374 | } |
| 375 | |
Ken Ashcraft | 530d3c5 | 2014-08-26 10:21:47 -0700 | [diff] [blame] | 376 | func ExpectMutationDoesNotExist(t *testing.T, changes []types.Change, id storage.ID, pre raw.Version, isRoot bool) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 377 | change := findMutation(t, changes, id) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 378 | if change.State != types.DoesNotExist { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 379 | t.Fatalf("Expected id to not exist: %v", id) |
| 380 | } |
| 381 | cv := change.Value.(*raw.Mutation) |
| 382 | if cv.PriorVersion != pre { |
| 383 | t.Fatalf("Expected PriorVersion to be %v, but was: %v", pre, cv.PriorVersion) |
| 384 | } |
Ken Ashcraft | 530d3c5 | 2014-08-26 10:21:47 -0700 | [diff] [blame] | 385 | if cv.Version != raw.NoVersion { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 386 | t.Fatalf("Expected Version to be NoVersion, but was: %v", cv.Version) |
| 387 | } |
| 388 | if cv.IsRoot != isRoot { |
| 389 | t.Fatalf("Expected IsRoot to be: %v, but was: %v", isRoot, cv.IsRoot) |
| 390 | } |
| 391 | if cv.Value != nil { |
| 392 | t.Fatal("Expected Value to be nil") |
| 393 | } |
| 394 | if cv.Dir != nil { |
| 395 | t.Fatal("Expected Dir to be nil") |
| 396 | } |
| 397 | } |
| 398 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 399 | func ExpectMutationExistsNoVersionCheck(t *testing.T, changes []types.Change, id storage.ID, value string) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 400 | change := findMutation(t, changes, id) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 401 | if change.State != types.Exists { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 402 | t.Fatalf("Expected id to exist: %v", id) |
| 403 | } |
| 404 | cv := change.Value.(*raw.Mutation) |
| 405 | if cv.Value != value { |
| 406 | t.Fatalf("Expected Value to be: %v, but was: %v", value, cv.Value) |
| 407 | } |
| 408 | } |
| 409 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 410 | func ExpectMutationDoesNotExistNoVersionCheck(t *testing.T, changes []types.Change, id storage.ID) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 411 | change := findMutation(t, changes, id) |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 412 | if change.State != types.DoesNotExist { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 413 | t.Fatalf("Expected id to not exist: %v", id) |
| 414 | } |
| 415 | } |
| 416 | |
Robin Thellend | e971490 | 2014-08-21 15:01:10 -0700 | [diff] [blame] | 417 | func findMutation(t *testing.T, changes []types.Change, id storage.ID) types.Change { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 418 | for _, change := range changes { |
| 419 | cv, ok := change.Value.(*raw.Mutation) |
| 420 | if !ok { |
| 421 | t.Fatal("Expected a Mutation") |
| 422 | } |
| 423 | if cv.ID == id { |
| 424 | return change |
| 425 | } |
| 426 | } |
| 427 | t.Fatalf("Expected a change for id: %v", id) |
| 428 | panic("Should not reach here") |
| 429 | } |
| 430 | |
Ken Ashcraft | c79f0c0 | 2014-08-26 13:11:43 -0700 | [diff] [blame^] | 431 | func expectDirEquals(t *testing.T, actual, expected []raw.DEntry) { |
Tilak Sharma | dfba8ac | 2014-06-18 13:49:32 -0700 | [diff] [blame] | 432 | if len(actual) != len(expected) { |
| 433 | t.Fatalf("Expected Dir to have %v refs, but had %v", len(expected), len(actual)) |
| 434 | } |
| 435 | for i, e := range expected { |
| 436 | a := actual[i] |
| 437 | if a != e { |
| 438 | t.Fatalf("Expected Dir entry %v to be %v, but was %v", i, e, a) |
| 439 | } |
| 440 | } |
| 441 | } |