OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package cloud |
| 6 |
| 7 import ( |
| 8 "crypto/rand" |
| 9 "encoding/hex" |
| 10 "fmt" |
| 11 "os" |
| 12 "testing" |
| 13 "time" |
| 14 |
| 15 ds "github.com/luci/gae/service/datastore" |
| 16 "github.com/luci/gae/service/info" |
| 17 |
| 18 "github.com/luci/luci-go/common/errors" |
| 19 "golang.org/x/net/context" |
| 20 "google.golang.org/cloud/datastore" |
| 21 |
| 22 . "github.com/smartystreets/goconvey/convey" |
| 23 ) |
| 24 |
| 25 func mkProperties(index bool, vals ...interface{}) []ds.Property { |
| 26 indexSetting := ds.ShouldIndex |
| 27 if !index { |
| 28 indexSetting = ds.NoIndex |
| 29 } |
| 30 |
| 31 result := make([]ds.Property, len(vals)) |
| 32 for i, v := range vals { |
| 33 result[i].SetValue(v, indexSetting) |
| 34 } |
| 35 return result |
| 36 } |
| 37 |
| 38 func mkp(vals ...interface{}) []ds.Property { return mkProperties(true, vals..
.) } |
| 39 func mkpNI(vals ...interface{}) []ds.Property { return mkProperties(false, vals.
..) } |
| 40 |
| 41 // TestDatastore tests the cloud datastore implementation. |
| 42 // |
| 43 // This test uses the gcloud datastore emulator. Like the Go datastore package, |
| 44 // the emulator must use the gRPC interface. At the time of writing, the |
| 45 // emulator included with the "gcloud" tool is an older emulator that does NOT |
| 46 // support gRPC. |
| 47 // |
| 48 // Download the emulator linked here: |
| 49 // https://code.google.com/p/google-cloud-sdk/issues/detail?id=719#c3 |
| 50 // |
| 51 // Run it in "--testing" mode, which removes random consistency failures and |
| 52 // runs in-memory: |
| 53 // $ ./gcd.sh start --testing |
| 54 // |
| 55 // Export the DATASTORE_EMULATOR_HOST environment variable. By default: |
| 56 // $ export DATASTORE_EMULATOR_HOST=localhost:8080 |
| 57 // |
| 58 // If the emulator environment is not detected, this test will be skipped. |
| 59 func TestDatastore(t *testing.T) { |
| 60 t.Parallel() |
| 61 |
| 62 // See if an emulator is running. If no emulator is running, we will ski
p this |
| 63 // test suite. |
| 64 emulatorHost := os.Getenv("DATASTORE_EMULATOR_HOST") |
| 65 if emulatorHost == "" { |
| 66 t.Logf("No emulator detected. Skipping test suite.") |
| 67 return |
| 68 } |
| 69 |
| 70 Convey(fmt.Sprintf(`A cloud installation using datastore emulator %q`, e
mulatorHost), t, func() { |
| 71 c := context.Background() |
| 72 client, err := datastore.NewClient(c, "") |
| 73 So(err, ShouldBeNil) |
| 74 defer client.Close() |
| 75 |
| 76 testTime := ds.RoundTime(time.Date(2016, 1, 1, 0, 0, 0, 0, time.
UTC)) |
| 77 |
| 78 c = Use(c, client) |
| 79 |
| 80 Convey(`Supports namespaces`, func() { |
| 81 namespaces := []string{"foo", "bar", "baz"} |
| 82 |
| 83 // Clear all used entities from all namespaces. |
| 84 for _, ns := range namespaces { |
| 85 nsCtx := info.Get(c).MustNamespace(ns) |
| 86 di := ds.Get(nsCtx) |
| 87 |
| 88 keys := make([]*ds.Key, len(namespaces)) |
| 89 for i := range keys { |
| 90 keys[i] = di.MakeKey("Test", i+1) |
| 91 } |
| 92 So(errors.Filter(di.DeleteMulti(keys), ds.ErrNoS
uchEntity), ShouldBeNil) |
| 93 } |
| 94 |
| 95 // Put one entity per namespace. |
| 96 for i, ns := range namespaces { |
| 97 nsCtx := info.Get(c).MustNamespace(ns) |
| 98 |
| 99 pmap := ds.PropertyMap{"$kind": mkp("Test"), "$i
d": mkp(i + 1), "Value": mkp(i)} |
| 100 So(ds.Get(nsCtx).Put(pmap), ShouldBeNil) |
| 101 } |
| 102 |
| 103 // Make sure that entity only exists in that namespace. |
| 104 for _, ns := range namespaces { |
| 105 nsCtx := info.Get(c).MustNamespace(ns) |
| 106 |
| 107 for i := range namespaces { |
| 108 pmap := ds.PropertyMap{"$kind": mkp("Tes
t"), "$id": mkp(i + 1)} |
| 109 err := ds.Get(nsCtx).Get(pmap) |
| 110 |
| 111 if namespaces[i] == ns { |
| 112 So(err, ShouldBeNil) |
| 113 } else { |
| 114 So(err, ShouldEqual, ds.ErrNoSuc
hEntity) |
| 115 } |
| 116 } |
| 117 } |
| 118 }) |
| 119 |
| 120 Convey(`In a clean random testing namespace`, func() { |
| 121 // Enter a namespace for this round of tests. |
| 122 randNamespace := make([]byte, 32) |
| 123 if _, err := rand.Read(randNamespace); err != nil { |
| 124 panic(err) |
| 125 } |
| 126 c = info.Get(c).MustNamespace(fmt.Sprintf("testing-%s",
hex.EncodeToString(randNamespace))) |
| 127 di := ds.Get(c) |
| 128 |
| 129 // Execute a kindless query to clear the namespace. |
| 130 q := ds.NewQuery("").KeysOnly(true) |
| 131 var allKeys []*ds.Key |
| 132 So(di.GetAll(q, &allKeys), ShouldBeNil) |
| 133 So(di.DeleteMulti(allKeys), ShouldBeNil) |
| 134 |
| 135 Convey(`Can allocate an ID range`, func() { |
| 136 // The datastore emulator does not allocate a co
ntiguous ID space. |
| 137 // Consequently, we will test with allocation si
zes of 1 so the contiguous |
| 138 // assumption is never violated. |
| 139 // |
| 140 // TODO: Re-work allocate IDs API to allow for n
on-contiugous IDs. |
| 141 idx, err := di.AllocateIDs(di.MakeKey("Foo", "12
", "Bar", 0), 1) |
| 142 So(err, ShouldBeNil) |
| 143 So(idx, ShouldBeGreaterThanOrEqualTo, 0) |
| 144 |
| 145 next, err := di.AllocateIDs(di.MakeKey("Foo", "1
2", "Bar", 0), 1) |
| 146 So(err, ShouldBeNil) |
| 147 So(next, ShouldNotEqual, idx) |
| 148 }) |
| 149 |
| 150 Convey(`Can get, put, and delete entities`, func() { |
| 151 // Put: "foo", "bar", "baz". |
| 152 put := []ds.PropertyMap{ |
| 153 {"$kind": mkp("test"), "$id": mkp("foo")
, "Value": mkp(1337)}, |
| 154 {"$kind": mkp("test"), "$id": mkp("bar")
, "Value": mkp(42)}, |
| 155 {"$kind": mkp("test"), "$id": mkp("baz")
, "Value": mkp(0xd065)}, |
| 156 } |
| 157 So(di.PutMulti(put), ShouldBeNil) |
| 158 delete(put[0], "$key") |
| 159 delete(put[1], "$key") |
| 160 delete(put[2], "$key") |
| 161 |
| 162 // Delete: "bar". |
| 163 So(di.Delete(di.MakeKey("test", "bar")), ShouldB
eNil) |
| 164 |
| 165 // Get: "foo", "bar", "baz" |
| 166 get := []ds.PropertyMap{ |
| 167 {"$kind": mkp("test"), "$id": mkp("foo")
}, |
| 168 {"$kind": mkp("test"), "$id": mkp("bar")
}, |
| 169 {"$kind": mkp("test"), "$id": mkp("baz")
}, |
| 170 } |
| 171 |
| 172 err := di.GetMulti(get) |
| 173 So(err, ShouldHaveSameTypeAs, errors.MultiError(
nil)) |
| 174 |
| 175 merr := err.(errors.MultiError) |
| 176 So(len(merr), ShouldEqual, 3) |
| 177 So(merr[0], ShouldBeNil) |
| 178 So(merr[1], ShouldEqual, ds.ErrNoSuchEntity) |
| 179 So(merr[2], ShouldBeNil) |
| 180 |
| 181 // put[1] will not be retrieved (delete) |
| 182 put[1] = get[1] |
| 183 So(get, ShouldResemble, put) |
| 184 }) |
| 185 |
| 186 Convey(`Can put and get all supported entity fields.`, f
unc() { |
| 187 put := ds.PropertyMap{ |
| 188 "$id": mkpNI("foo"), |
| 189 "$kind": mkpNI("FooType"), |
| 190 "Number": mkp(1337), |
| 191 "String": mkpNI("hello"), |
| 192 "Bytes": mkp([]byte("world")), |
| 193 "Time": mkp(testTime), |
| 194 "Float": mkpNI(3.14), |
| 195 "Key": mkp(di.MakeKey("Parent", "Pare
ntID", "Child", 1337)), |
| 196 |
| 197 "ComplexSlice": mkp(1337, "string", []by
te("bytes"), testTime, float32(3.14), |
| 198 float64(2.71), true, di.MakeKey(
"SomeKey", "SomeID")), |
| 199 } |
| 200 So(di.Put(put), ShouldBeNil) |
| 201 delete(put, "$key") |
| 202 |
| 203 get := ds.PropertyMap{ |
| 204 "$id": mkpNI("foo"), |
| 205 "$kind": mkpNI("FooType"), |
| 206 } |
| 207 So(di.Get(get), ShouldBeNil) |
| 208 So(get, ShouldResemble, put) |
| 209 }) |
| 210 |
| 211 Convey(`With several entities installed`, func() { |
| 212 So(di.PutMulti([]ds.PropertyMap{ |
| 213 {"$kind": mkp("Test"), "$id": mkp("foo")
, "FooBar": mkp(true)}, |
| 214 {"$kind": mkp("Test"), "$id": mkp("bar")
, "FooBar": mkp(true)}, |
| 215 {"$kind": mkp("Test"), "$id": mkp("baz")
}, |
| 216 {"$kind": mkp("Test"), "$id": mkp("qux")
}, |
| 217 }), ShouldBeNil) |
| 218 |
| 219 q := ds.NewQuery("Test") |
| 220 |
| 221 Convey(`Can query for entities with FooBar == tr
ue.`, func() { |
| 222 var results []ds.PropertyMap |
| 223 q = q.Eq("FooBar", true) |
| 224 So(di.GetAll(q, &results), ShouldBeNil) |
| 225 |
| 226 So(results, ShouldResemble, []ds.Propert
yMap{ |
| 227 {"$key": mkpNI(di.MakeKey("Test"
, "bar")), "FooBar": mkp(true)}, |
| 228 {"$key": mkpNI(di.MakeKey("Test"
, "foo")), "FooBar": mkp(true)}, |
| 229 }) |
| 230 }) |
| 231 |
| 232 Convey(`Can query for entities whose __key__ > "
baz".`, func() { |
| 233 var results []ds.PropertyMap |
| 234 q = q.Gt("__key__", di.MakeKey("Test", "
baz")) |
| 235 So(di.GetAll(q, &results), ShouldBeNil) |
| 236 |
| 237 So(results, ShouldResemble, []ds.Propert
yMap{ |
| 238 {"$key": mkpNI(di.MakeKey("Test"
, "foo")), "FooBar": mkp(true)}, |
| 239 {"$key": mkpNI(di.MakeKey("Test"
, "qux"))}, |
| 240 }) |
| 241 }) |
| 242 |
| 243 Convey(`Can transactionally get and put.`, func(
) { |
| 244 err := di.RunInTransaction(func(c contex
t.Context) error { |
| 245 di := ds.Get(c) |
| 246 |
| 247 pmap := ds.PropertyMap{"$kind":
mkp("Test"), "$id": mkp("qux")} |
| 248 if err := di.Get(pmap); err != n
il { |
| 249 return err |
| 250 } |
| 251 |
| 252 pmap["ExtraField"] = mkp("Presen
t!") |
| 253 return di.Put(pmap) |
| 254 }, nil) |
| 255 So(err, ShouldBeNil) |
| 256 |
| 257 pmap := ds.PropertyMap{"$kind": mkp("Tes
t"), "$id": mkp("qux")} |
| 258 err = di.RunInTransaction(func(c context
.Context) error { |
| 259 return ds.Get(c).Get(pmap) |
| 260 }, nil) |
| 261 So(err, ShouldBeNil) |
| 262 So(pmap, ShouldResemble, ds.PropertyMap{
"$kind": mkp("Test"), "$id": mkp("qux"), "ExtraField": mkp("Present!")}) |
| 263 }) |
| 264 |
| 265 Convey(`Can fail in a transaction with no effect
.`, func() { |
| 266 testError := errors.New("test error") |
| 267 |
| 268 err := di.RunInTransaction(func(c contex
t.Context) error { |
| 269 di := ds.Get(c) |
| 270 |
| 271 pmap := ds.PropertyMap{"$kind":
mkp("Test"), "$id": mkp("quux")} |
| 272 if err := di.Put(pmap); err != n
il { |
| 273 return err |
| 274 } |
| 275 return testError |
| 276 }, nil) |
| 277 So(err, ShouldEqual, testError) |
| 278 |
| 279 pmap := ds.PropertyMap{"$kind": mkp("Tes
t"), "$id": mkp("quux")} |
| 280 err = di.RunInTransaction(func(c context
.Context) error { |
| 281 return ds.Get(c).Get(pmap) |
| 282 }, nil) |
| 283 So(err, ShouldEqual, ds.ErrNoSuchEntity) |
| 284 }) |
| 285 }) |
| 286 }) |
| 287 }) |
| 288 } |
OLD | NEW |