go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/task/urlfetch/urlfetch_test.go (about) 1 // Copyright 2015 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 urlfetch 16 17 import ( 18 "context" 19 "net/http" 20 "net/http/httptest" 21 "testing" 22 "time" 23 24 "github.com/golang/protobuf/proto" 25 26 "go.chromium.org/luci/common/clock" 27 "go.chromium.org/luci/common/clock/testclock" 28 "go.chromium.org/luci/config/validation" 29 "go.chromium.org/luci/gae/service/urlfetch" 30 31 "go.chromium.org/luci/scheduler/appengine/messages" 32 "go.chromium.org/luci/scheduler/appengine/task" 33 "go.chromium.org/luci/scheduler/appengine/task/utils/tasktest" 34 35 . "github.com/smartystreets/goconvey/convey" 36 . "go.chromium.org/luci/common/testing/assertions" 37 ) 38 39 var _ task.Manager = (*TaskManager)(nil) 40 41 func TestValidateProtoMessage(t *testing.T) { 42 t.Parallel() 43 44 tm := TaskManager{} 45 Convey("ValidateProtoMessage works", t, func() { 46 c := &validation.Context{Context: context.Background()} 47 validate := func(msg proto.Message) error { 48 tm.ValidateProtoMessage(c, msg, "some-project:some-realm") 49 return c.Finalize() 50 } 51 Convey("ValidateProtoMessage passes good msg", func() { 52 So(validate(&messages.UrlFetchTask{ 53 Url: "https://blah.com", 54 }), ShouldBeNil) 55 }) 56 57 Convey("ValidateProtoMessage wrong type", func() { 58 So(validate(&messages.NoopTask{}), ShouldErrLike, "wrong type") 59 }) 60 61 Convey("ValidateProtoMessage empty", func() { 62 So(validate(tm.ProtoMessageType()), ShouldErrLike, "expecting a non-empty UrlFetchTask") 63 }) 64 65 Convey("ValidateProtoMessage bad method", func() { 66 So(validate(&messages.UrlFetchTask{ 67 Method: "BLAH", 68 }), ShouldErrLike, "unsupported HTTP method") 69 }) 70 71 Convey("ValidateProtoMessage no URL", func() { 72 So(validate(&messages.UrlFetchTask{}), ShouldErrLike, "field 'url' is required") 73 }) 74 75 Convey("ValidateProtoMessage bad URL", func() { 76 So(validate(&messages.UrlFetchTask{ 77 Url: "%%%%", 78 }), ShouldErrLike, "invalid URL") 79 }) 80 81 Convey("ValidateProtoMessage non-absolute URL", func() { 82 So(validate(&messages.UrlFetchTask{ 83 Url: "/abc", 84 }), ShouldErrLike, "not an absolute url") 85 }) 86 87 Convey("ValidateProtoMessage bad timeout", func() { 88 So(validate(&messages.UrlFetchTask{ 89 Url: "https://blah.com", 90 TimeoutSec: -1, 91 }), ShouldErrLike, "minimum allowed 'timeout_sec' is 1 sec") 92 }) 93 94 Convey("ValidateProtoMessage large timeout", func() { 95 So(validate(&messages.UrlFetchTask{ 96 Url: "https://blah.com", 97 TimeoutSec: 10000, 98 }), ShouldErrLike, "maximum allowed 'timeout_sec' is 480 sec") 99 }) 100 }) 101 } 102 103 func TestLaunchTask(t *testing.T) { 104 t.Parallel() 105 106 tm := TaskManager{} 107 108 Convey("LaunchTask works", t, func(c C) { 109 ts, ctx := newTestContext(time.Unix(0, 1)) 110 defer ts.Close() 111 ctl := &tasktest.TestController{ 112 TaskMessage: &messages.UrlFetchTask{ 113 Url: ts.URL, 114 }, 115 SaveCallback: func() error { return nil }, 116 } 117 So(tm.LaunchTask(ctx, ctl), ShouldBeNil) 118 So(ctl.Log[0], ShouldEqual, "GET "+ts.URL) 119 So(ctl.Log[1], ShouldStartWith, "Finished with overall status SUCCEEDED in 0") 120 }) 121 } 122 123 func newTestContext(now time.Time) (*httptest.Server, context.Context) { 124 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 125 w.Write([]byte("Hello, Client!")) 126 })) 127 c := context.Background() 128 c = clock.Set(c, testclock.New(now)) 129 c = urlfetch.Set(c, http.DefaultTransport) 130 return ts, c 131 }