| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package model | 5 package model |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "testing" | 8 "testing" |
| 9 | 9 |
| 10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
| 11 | 11 |
| 12 . "github.com/smartystreets/goconvey/convey" | 12 . "github.com/smartystreets/goconvey/convey" |
| 13 | 13 |
| 14 "github.com/luci/gae/impl/memory" | 14 "github.com/luci/gae/impl/memory" |
| 15 "github.com/luci/gae/service/datastore" | 15 "github.com/luci/gae/service/datastore" |
| 16 "github.com/luci/luci-go/common/clock/testclock" | 16 "github.com/luci/luci-go/common/clock/testclock" |
| 17 google_pb "github.com/luci/luci-go/common/proto/google" | 17 google_pb "github.com/luci/luci-go/common/proto/google" |
| 18 . "github.com/luci/luci-go/common/testing/assertions" | 18 . "github.com/luci/luci-go/common/testing/assertions" |
| 19 | 19 |
| 20 » "github.com/luci/luci-go/common/api/dm/service/v1" | 20 » dm "github.com/luci/luci-go/common/api/dm/service/v1" |
| 21 ) | 21 ) |
| 22 | 22 |
| 23 func TestQuest(t *testing.T) { | 23 func TestQuest(t *testing.T) { |
| 24 t.Parallel() | 24 t.Parallel() |
| 25 | 25 |
| 26 desc := func(cfg, jsonData string) *dm.Quest_Desc { | |
| 27 return &dm.Quest_Desc{ | |
| 28 DistributorConfigName: cfg, | |
| 29 JsonPayload: jsonData, | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 Convey("Quest", t, func() { | 26 Convey("Quest", t, func() { |
| 34 c := memory.Use(context.Background()) | 27 c := memory.Use(context.Background()) |
| 35 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | 28 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) |
| 36 | 29 |
| 37 Convey("QuestDescriptor", func() { | 30 Convey("QuestDescriptor", func() { |
| 38 Convey("good", func() { | 31 Convey("good", func() { |
| 39 Convey("normal (normalized)", func() { | 32 Convey("normal (normalized)", func() { |
| 40 » » » » » qd := desc("swarming", `{ "key" : ["v
alue"]}`) | 33 » » » » » qd := dm.NewQuestDesc("swarming", `{ "k
ey" : ["value"]}`, nil) |
| 41 » » » » » q, err := NewQuest(c, qd) | 34 » » » » » So(qd.Normalize(), ShouldBeNil) |
| 42 » » » » » So(err, ShouldBeNil) | 35 » » » » » So(NewQuest(c, qd), ShouldResemble, &Que
st{ |
| 43 » » » » » So(q, ShouldResemble, &Quest{ | 36 » » » » » » "i7dd71-dXSiEY2gVRDEOnZcR_ZswA68
Znk1I-BJdfHU", |
| 44 » » » » » » "eMpqiyje5ItTX8IistN7IlAMVxyCsJc
ez4DAHKvhm7Y", | 37 » » » » » » *qd, |
| 45 » » » » » » *desc("swarming", `{"key":["valu
e"]}`), | |
| 46 nil, | 38 nil, |
| 47 testclock.TestTimeUTC, | 39 testclock.TestTimeUTC, |
| 48 }) | 40 }) |
| 49 }) | 41 }) |
| 50 | 42 |
| 51 Convey("extra data", func() { | 43 Convey("extra data", func() { |
| 52 » » » » » qd := desc("swarming", `{"key":["value"]
} foof`) | 44 » » » » » qd := dm.NewQuestDesc("swarming", `{"key
":["value"]} foof`, nil) |
| 53 » » » » » _, err := NewQuest(c, qd) | 45 » » » » » So(qd.Normalize(), ShouldErrLike, "extra
junk") |
| 54 » » » » » So(err, ShouldErrLike, "extra junk") | |
| 55 }) | 46 }) |
| 56 | 47 |
| 57 Convey("data ordering", func() { | 48 Convey("data ordering", func() { |
| 58 » » » » » qd := desc("swarming", `{"key":["value"]
, "abc": true}`) | 49 » » » » » qd := dm.NewQuestDesc("swarming", `{"key
":["value"], "abc": true}`, nil) |
| 59 » » » » » q, err := NewQuest(c, qd) | 50 » » » » » So(qd.Normalize(), ShouldBeNil) |
| 60 » » » » » So(err, ShouldBeNil) | 51 » » » » » So(NewQuest(c, qd), ShouldResemble, &Que
st{ |
| 61 » » » » » So(q, ShouldResemble, &Quest{ | 52 » » » » » » "iEnVmX4BbYB0y7Df7pIXTIcNWsPgsXN
z3KOjg1xX-sM", |
| 62 » » » » » » "KO5hRgXIFouei7Xg5Oai0K5hJeuuiO7
0jRaDyqQO0MM", | 53 » » » » » » *qd, |
| 63 » » » » » » *desc("swarming", `{"abc":true,"
key":["value"]}`), | |
| 64 nil, | 54 nil, |
| 65 testclock.TestTimeUTC, | 55 testclock.TestTimeUTC, |
| 66 }) | 56 }) |
| 67 }) | 57 }) |
| 68 | 58 |
| 69 }) | 59 }) |
| 70 | 60 |
| 71 Convey("bad", func() { | 61 Convey("bad", func() { |
| 72 Convey("payload too large", func() { | 62 Convey("payload too large", func() { |
| 73 payload := make([]byte, 512*1000) | 63 payload := make([]byte, 512*1000) |
| 74 » » » » » qd := desc("swarming", string(payload)) | 64 » » » » » qd := dm.NewQuestDesc("swarming", string
(payload), nil) |
| 75 » » » » » _, err := NewQuest(c, qd) | 65 » » » » » So(qd.Normalize(), ShouldErrLike, "too l
arge: 512000 > 262144") |
| 76 » » » » » So(err, ShouldErrLike, "too large: 51200
0 > 262144") | |
| 77 }) | 66 }) |
| 78 | 67 |
| 79 Convey("json with null byte", func() { | 68 Convey("json with null byte", func() { |
| 80 » » » » » qd := desc("swarming", "{\"key\": \"\x00
\"}") | 69 » » » » » qd := dm.NewQuestDesc("swarming", "{\"ke
y\": \"\x00\"}", nil) |
| 81 » » » » » _, err := NewQuest(c, qd) | 70 » » » » » So(qd.Normalize(), ShouldErrLike, "inval
id character") |
| 82 » » » » » So(err, ShouldErrLike, "invalid characte
r") | |
| 83 }) | 71 }) |
| 84 | 72 |
| 85 Convey("not a dictionary", func() { | 73 Convey("not a dictionary", func() { |
| 86 » » » » » qd := desc("swarming", "[]") | 74 » » » » » qd := dm.NewQuestDesc("swarming", "[]",
nil) |
| 87 » » » » » _, err := NewQuest(c, qd) | 75 » » » » » So(qd.Normalize(), ShouldErrLike, "canno
t unmarshal array") |
| 88 » » » » » So(err, ShouldErrLike, "cannot unmarshal
array") | |
| 89 }) | 76 }) |
| 90 }) | 77 }) |
| 91 }) | 78 }) |
| 92 | 79 |
| 93 Convey("ToProto", func() { | 80 Convey("ToProto", func() { |
| 94 » » » q, err := NewQuest(c, desc("swarming", `{"key": ["value"
]}`)) | 81 » » » qd := dm.NewQuestDesc("swarming", `{"key": ["value"]}`,
nil) |
| 95 » » » So(err, ShouldBeNil) | 82 » » » So(qd.Normalize(), ShouldBeNil) |
| 83 » » » q := NewQuest(c, qd) |
| 96 p := q.ToProto() | 84 p := q.ToProto() |
| 97 So(p, ShouldResemble, &dm.Quest{ | 85 So(p, ShouldResemble, &dm.Quest{ |
| 98 » » » » Id: dm.NewQuestID("eMpqiyje5ItTX8IistN7IlAMVxyCs
Jcez4DAHKvhm7Y"), | 86 » » » » Id: dm.NewQuestID("i7dd71-dXSiEY2gVRDEOnZcR_ZswA
68Znk1I-BJdfHU"), |
| 99 Data: &dm.Quest_Data{ | 87 Data: &dm.Quest_Data{ |
| 100 Created: google_pb.NewTimestamp(testcloc
k.TestTimeUTC), | 88 Created: google_pb.NewTimestamp(testcloc
k.TestTimeUTC), |
| 101 Desc: &q.Desc, | 89 Desc: &q.Desc, |
| 102 BuiltBy: []*dm.Quest_TemplateSpec{}, | 90 BuiltBy: []*dm.Quest_TemplateSpec{}, |
| 103 }, | 91 }, |
| 104 }) | 92 }) |
| 105 So(p.Data.Desc.JsonPayload, ShouldResemble, `{"key":["va
lue"]}`) | 93 So(p.Data.Desc.JsonPayload, ShouldResemble, `{"key":["va
lue"]}`) |
| 106 }) | 94 }) |
| 107 | 95 |
| 108 Convey("QueryAttemptsForQuest", func() { | 96 Convey("QueryAttemptsForQuest", func() { |
| 109 » » » q, err := NewQuest(c, desc("swarming", `{"key": ["value"
]}`)) | 97 » » » qd := dm.NewQuestDesc("swarming", `{"key": ["value"]}`,
nil) |
| 110 » » » So(err, ShouldBeNil) | 98 » » » So(qd.Normalize(), ShouldBeNil) |
| 99 » » » q := NewQuest(c, qd) |
| 111 ds := datastore.Get(c) | 100 ds := datastore.Get(c) |
| 112 So(ds.Put(q), ShouldBeNil) | 101 So(ds.Put(q), ShouldBeNil) |
| 113 ds.Testable().CatchupIndexes() | 102 ds.Testable().CatchupIndexes() |
| 114 | 103 |
| 115 as := []*Attempt(nil) | 104 as := []*Attempt(nil) |
| 116 So(ds.GetAll(QueryAttemptsForQuest(c, q.ID), &as), Shoul
dBeNil) | 105 So(ds.GetAll(QueryAttemptsForQuest(c, q.ID), &as), Shoul
dBeNil) |
| 117 So(as, ShouldBeNil) | 106 So(as, ShouldBeNil) |
| 118 | 107 |
| 119 a := &Attempt{ID: *dm.NewAttemptID(q.ID, 1)} | 108 a := &Attempt{ID: *dm.NewAttemptID(q.ID, 1)} |
| 120 So(ds.Put(a), ShouldBeNil) | 109 So(ds.Put(a), ShouldBeNil) |
| 121 a.ID.Id = 2 | 110 a.ID.Id = 2 |
| 122 So(ds.Put(a), ShouldBeNil) | 111 So(ds.Put(a), ShouldBeNil) |
| 123 a.ID.Quest = "eMpqiyje5ItTX8IistN7IlAMVxyCsJcez4DAHKvhm7
X" // one less | 112 a.ID.Quest = "eMpqiyje5ItTX8IistN7IlAMVxyCsJcez4DAHKvhm7
X" // one less |
| 124 a.ID.Id = 1 | 113 a.ID.Id = 1 |
| 125 So(ds.Put(a), ShouldBeNil) | 114 So(ds.Put(a), ShouldBeNil) |
| 126 a.ID.Quest = "eMpqiyje5ItTX8IistN7IlAMVxyCsJcez4DAHKvhm7
Z" // one more | 115 a.ID.Quest = "eMpqiyje5ItTX8IistN7IlAMVxyCsJcez4DAHKvhm7
Z" // one more |
| 127 So(ds.Put(a), ShouldBeNil) | 116 So(ds.Put(a), ShouldBeNil) |
| 128 | 117 |
| 129 as = nil | 118 as = nil |
| 130 So(ds.GetAll(QueryAttemptsForQuest(c, q.ID), &as), Shoul
dBeNil) | 119 So(ds.GetAll(QueryAttemptsForQuest(c, q.ID), &as), Shoul
dBeNil) |
| 131 So(as, ShouldBeNil) | 120 So(as, ShouldBeNil) |
| 132 | 121 |
| 133 ds.Testable().CatchupIndexes() | 122 ds.Testable().CatchupIndexes() |
| 134 as = nil | 123 as = nil |
| 135 So(ds.GetAll(QueryAttemptsForQuest(c, q.ID), &as), Shoul
dBeNil) | 124 So(ds.GetAll(QueryAttemptsForQuest(c, q.ID), &as), Shoul
dBeNil) |
| 136 So(as, ShouldResemble, []*Attempt{ | 125 So(as, ShouldResemble, []*Attempt{ |
| 137 » » » » {ID: *dm.NewAttemptID("eMpqiyje5ItTX8IistN7IlAMV
xyCsJcez4DAHKvhm7Y", 2)}, | 126 » » » » {ID: *dm.NewAttemptID("i7dd71-dXSiEY2gVRDEOnZcR_
ZswA68Znk1I-BJdfHU", 2)}, |
| 138 » » » » {ID: *dm.NewAttemptID("eMpqiyje5ItTX8IistN7IlAMV
xyCsJcez4DAHKvhm7Y", 1)}, | 127 » » » » {ID: *dm.NewAttemptID("i7dd71-dXSiEY2gVRDEOnZcR_
ZswA68Znk1I-BJdfHU", 1)}, |
| 139 }) | 128 }) |
| 140 | 129 |
| 141 }) | 130 }) |
| 142 }) | 131 }) |
| 143 } | 132 } |
| OLD | NEW |