github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/hook/hook_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package hook 18 19 import ( 20 "encoding/json" 21 "net/http/httptest" 22 "testing" 23 "time" 24 25 "k8s.io/test-infra/prow/config" 26 "k8s.io/test-infra/prow/github" 27 "k8s.io/test-infra/prow/phony" 28 "k8s.io/test-infra/prow/plugins" 29 ) 30 31 var ice = github.IssueCommentEvent{ 32 Action: "reopened", 33 Repo: github.Repo{ 34 Owner: github.User{ 35 Login: "foo", 36 }, 37 Name: "bar", 38 FullName: "foo/bar", 39 }, 40 } 41 42 // TestHook sets up a hook.Server and then sends a fake webhook at it. It then 43 // ensures that a fake plugin is called. 44 func TestHook(t *testing.T) { 45 called := make(chan bool, 1) 46 secret := []byte("123abc") 47 payload, err := json.Marshal(&ice) 48 if err != nil { 49 t.Fatalf("Marshalling ICE: %v", err) 50 } 51 plugins.RegisterIssueHandler( 52 "baz", 53 func(pc plugins.PluginClient, ie github.IssueEvent) error { 54 called <- true 55 return nil 56 }, 57 nil, 58 ) 59 pa := &plugins.PluginAgent{} 60 pa.Set(&plugins.Configuration{Plugins: map[string][]string{"foo/bar": {"baz"}}}) 61 ca := &config.Agent{} 62 metrics := NewMetrics() 63 64 getSecret := func() []byte { 65 return []byte("123abc") 66 } 67 68 s := httptest.NewServer(&Server{ 69 Plugins: pa, 70 ConfigAgent: ca, 71 Metrics: metrics, 72 TokenGenerator: getSecret, 73 }) 74 defer s.Close() 75 if err := phony.SendHook(s.URL, "issues", payload, secret); err != nil { 76 t.Fatalf("Error sending hook: %v", err) 77 } 78 select { 79 case <-called: // All good. 80 case <-time.After(time.Second): 81 t.Error("Plugin not called after one second.") 82 } 83 }