go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/tryjob/external_id_test.go (about) 1 // Copyright 2021 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tryjob 16 17 import ( 18 "testing" 19 20 "go.chromium.org/luci/cv/internal/cvtesting" 21 22 . "github.com/smartystreets/goconvey/convey" 23 . "go.chromium.org/luci/common/testing/assertions" 24 ) 25 26 func TestExternalID(t *testing.T) { 27 t.Parallel() 28 29 Convey("ExternalID works", t, func() { 30 31 Convey("BuildbucketID", func() { 32 eid, err := BuildbucketID("cr-buildbucket.appspot.com", 12) 33 So(err, ShouldBeNil) 34 So(eid, ShouldResemble, ExternalID("buildbucket/cr-buildbucket.appspot.com/12")) 35 36 host, build, err := eid.ParseBuildbucketID() 37 So(err, ShouldBeNil) 38 So(host, ShouldResemble, "cr-buildbucket.appspot.com") 39 So(build, ShouldEqual, 12) 40 41 So(eid.MustURL(), ShouldResemble, "https://cr-buildbucket.appspot.com/build/12") 42 }) 43 Convey("Bad ID", func() { 44 e := ExternalID("blah") 45 _, _, err := e.ParseBuildbucketID() 46 So(err, ShouldErrLike, "not a valid BuildbucketID") 47 48 _, err = e.URL() 49 So(err, ShouldErrLike, "invalid ExternalID") 50 }) 51 }) 52 53 Convey("Resolve works", t, func() { 54 ct := cvtesting.Test{} 55 ctx, cancel := ct.SetUp(t) 56 defer cancel() 57 host := "example.com" 58 59 Convey("None exist", func() { 60 ids := []ExternalID{ 61 MustBuildbucketID(host, 101), 62 MustBuildbucketID(host, 102), 63 MustBuildbucketID(host, 103), 64 } 65 // None of ids[:] are created. 66 67 tjIDs, err := Resolve(ctx, ids...) 68 So(err, ShouldBeNil) 69 So(tjIDs, ShouldHaveLength, len(ids)) 70 for _, tjID := range tjIDs { 71 So(tjID, ShouldEqual, 0) 72 } 73 tjs, err := ResolveToTryjobs(ctx, ids...) 74 So(err, ShouldBeNil) 75 So(tjs, ShouldHaveLength, len(ids)) 76 for _, tj := range tjs { 77 So(tj, ShouldBeNil) 78 } 79 }) 80 Convey("Some exist", func() { 81 ids := []ExternalID{ 82 MustBuildbucketID(host, 201), 83 MustBuildbucketID(host, 202), 84 MustBuildbucketID(host, 203), 85 } 86 // ids[0] is not created. 87 ids[1].MustCreateIfNotExists(ctx) 88 ids[2].MustCreateIfNotExists(ctx) 89 90 tjIDs, err := Resolve(ctx, ids...) 91 So(err, ShouldBeNil) 92 So(tjIDs, ShouldHaveLength, len(ids)) 93 for i, tjID := range tjIDs { 94 if i == 0 { 95 So(tjID, ShouldEqual, 0) 96 } else { 97 So(tjID, ShouldNotEqual, 0) 98 } 99 } 100 101 tjs, err := ResolveToTryjobs(ctx, ids...) 102 So(err, ShouldBeNil) 103 So(tjs, ShouldHaveLength, len(ids)) 104 for i, tj := range tjs { 105 if i == 0 { 106 So(tj, ShouldBeNil) 107 } else { 108 So(tj, ShouldNotBeNil) 109 So(tj.ExternalID, ShouldEqual, ids[i]) 110 } 111 } 112 }) 113 114 Convey("All exist", func() { 115 ids := []ExternalID{ 116 MustBuildbucketID(host, 301), 117 MustBuildbucketID(host, 302), 118 MustBuildbucketID(host, 303), 119 } 120 ids[0].MustCreateIfNotExists(ctx) 121 ids[1].MustCreateIfNotExists(ctx) 122 ids[2].MustCreateIfNotExists(ctx) 123 124 tjIDs, err := Resolve(ctx, ids...) 125 So(err, ShouldBeNil) 126 So(tjIDs, ShouldHaveLength, len(ids)) 127 for _, tjID := range tjIDs { 128 So(tjID, ShouldNotEqual, 0) 129 } 130 131 tjs, err := ResolveToTryjobs(ctx, ids...) 132 So(err, ShouldBeNil) 133 So(tjs, ShouldHaveLength, len(ids)) 134 for i, tj := range tjs { 135 So(tj, ShouldNotBeNil) 136 So(tj.ExternalID, ShouldEqual, ids[i]) 137 } 138 }) 139 }) 140 }