github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/yuks/yuks_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 yuks 18 19 import ( 20 "flag" 21 "fmt" 22 "net/http" 23 "net/http/httptest" 24 "strings" 25 "testing" 26 27 "github.com/sirupsen/logrus" 28 29 "k8s.io/test-infra/prow/github" 30 "k8s.io/test-infra/prow/github/fakegithub" 31 ) 32 33 type fakeJoke string 34 35 var human = flag.Bool("human", false, "Enable to run additional manual tests") 36 37 func (j fakeJoke) readJoke() (string, error) { 38 return string(j), nil 39 } 40 41 func TestRealJoke(t *testing.T) { 42 if !*human { 43 t.Skip("Real jokes disabled for automation. Manual users can add --human") 44 } 45 if joke, err := jokeURL.readJoke(); err != nil { 46 t.Errorf("Could not read joke from %s: %v", jokeURL, err) 47 } else { 48 fmt.Println(joke) 49 } 50 } 51 52 // Medium integration test (depends on ability to open a TCP port) 53 func TestJokesMedium(t *testing.T) { 54 j := "What do you get when you cross a joke with a rhetorical question?" 55 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 56 fmt.Fprintf(w, `{"joke": "%s"}`, j) 57 })) 58 defer ts.Close() 59 fc := &fakegithub.FakeClient{ 60 IssueComments: make(map[int][]github.IssueComment), 61 } 62 63 comment := "/joke" 64 65 e := &github.GenericCommentEvent{ 66 Action: github.GenericCommentActionCreated, 67 Body: comment, 68 Number: 5, 69 IssueState: "open", 70 } 71 if err := handle(fc, logrus.WithField("plugin", pluginName), e, realJoke(ts.URL)); err != nil { 72 t.Errorf("didn't expect error: %v", err) 73 return 74 } 75 if len(fc.IssueComments[5]) != 1 { 76 t.Error("should have commented.") 77 return 78 } 79 if c := fc.IssueComments[5][0]; !strings.Contains(c.Body, j) { 80 t.Errorf("missing joke: %s from comment: %v", j, c) 81 } 82 } 83 84 // Small, unit tests 85 func TestJokes(t *testing.T) { 86 var testcases = []struct { 87 name string 88 action github.GenericCommentEventAction 89 body string 90 state string 91 joke fakeJoke 92 pr bool 93 shouldComment bool 94 shouldError bool 95 }{ 96 { 97 name: "ignore edited comment", 98 state: "open", 99 action: github.GenericCommentActionEdited, 100 body: "/joke", 101 joke: "this? that.", 102 shouldComment: false, 103 shouldError: false, 104 }, 105 { 106 name: "leave joke on pr", 107 state: "open", 108 action: github.GenericCommentActionCreated, 109 body: "/joke", 110 joke: "this? that.", 111 pr: true, 112 shouldComment: true, 113 shouldError: false, 114 }, 115 { 116 name: "leave joke on issue", 117 state: "open", 118 action: github.GenericCommentActionCreated, 119 body: "/joke", 120 joke: "this? that.", 121 shouldComment: true, 122 shouldError: false, 123 }, 124 { 125 name: "leave joke on issue, trailing space", 126 state: "open", 127 action: github.GenericCommentActionCreated, 128 body: "/joke \r", 129 joke: "this? that.", 130 shouldComment: true, 131 shouldError: false, 132 }, 133 { 134 name: "reject bad joke chars", 135 state: "open", 136 action: github.GenericCommentActionCreated, 137 body: "/joke", 138 joke: "[hello](url)", 139 shouldComment: false, 140 shouldError: true, 141 }, 142 } 143 for _, tc := range testcases { 144 fc := &fakegithub.FakeClient{ 145 IssueComments: make(map[int][]github.IssueComment), 146 } 147 e := &github.GenericCommentEvent{ 148 Action: tc.action, 149 Body: tc.body, 150 Number: 5, 151 IssueState: tc.state, 152 IsPR: tc.pr, 153 } 154 err := handle(fc, logrus.WithField("plugin", pluginName), e, tc.joke) 155 if !tc.shouldError && err != nil { 156 t.Errorf("For case %s, didn't expect error: %v", tc.name, err) 157 continue 158 } else if tc.shouldError && err == nil { 159 t.Errorf("For case %s, expected an error to occur", tc.name) 160 continue 161 } 162 if tc.shouldComment && len(fc.IssueComments[5]) != 1 { 163 t.Errorf("For case %s, should have commented.", tc.name) 164 } else if !tc.shouldComment && len(fc.IssueComments[5]) != 0 { 165 t.Errorf("For case %s, should not have commented.", tc.name) 166 } 167 } 168 }