github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/gubernator/view_pr_test.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import datetime 18 import unittest 19 20 # TODO(fejta): use non-relative imports 21 # https://google.github.io/styleguide/pyguide.html?showone=Packages#Packages 22 import gcs_async_test 23 from github import models 24 import main_test 25 import view_pr 26 27 from webapp2_extras import securecookie 28 29 30 app = main_test.app 31 write = gcs_async_test.write 32 33 class PathTest(unittest.TestCase): 34 def test_org_repo(self): 35 def check(path, org, repo): 36 actual_org, actual_repo = view_pr.org_repo(path, 'kubernetes', 'kubernetes') 37 self.assertEquals(actual_org, org) 38 self.assertEquals(actual_repo, repo) 39 40 check('', 'kubernetes', 'kubernetes') 41 check('/test-infra', 'kubernetes', 'test-infra') 42 check('/kubernetes', 'kubernetes', 'kubernetes') 43 check('/kubernetes/test-infra', 'kubernetes', 'test-infra') 44 check('/kubernetes/kubernetes', 'kubernetes', 'kubernetes') 45 check('/google/cadvisor', 'google', 'cadvisor') 46 47 def test_pr_path(self): 48 def check(org, repo, pr, path): 49 actual_path = view_pr.pr_path(org, repo, pr, 'kubernetes', 'kubernetes', 'pull_prefix') 50 self.assertEquals(actual_path, '%s/%s' % ('pull_prefix', path)) 51 52 check('kubernetes', 'kubernetes', 1234, 1234) 53 check('kubernetes', 'kubernetes', 'batch', 'batch') 54 check('kubernetes', 'test-infra', 555, 'test-infra/555') 55 check('kubernetes', 'test-infra', 'batch', 'test-infra/batch') 56 check('google', 'cadvisor', '555', 'google_cadvisor/555') 57 check('google', 'cadvisor', 'batch', 'google_cadvisor/batch') 58 59 60 class PRTest(main_test.TestBase): 61 BUILDS = { 62 'build': [('12', {'version': 'bb', 'timestamp': 1467147654}, None), 63 ('11', {'version': 'bb', 'timestamp': 1467146654}, {'result': 'PASSED'}), 64 ('10', {'version': 'aa', 'timestamp': 1467136654}, {'result': 'FAILED'})], 65 'e2e': [('47', {'version': 'bb', 'timestamp': '1467147654'}, {'result': '[UNSET]'}), 66 ('46', {'version': 'aa', 'timestamp': '1467136700'}, {'result': '[UNSET]'})] 67 } 68 69 def setUp(self): 70 self.init_stubs() 71 72 def init_pr_directory(self): 73 gcs_async_test.install_handler(self.testbed.get_stub('urlfetch'), 74 {'123/': ['build', 'e2e'], 75 '123/build/': ['11', '10', '12'], # out of order 76 '123/e2e/': ['47', '46']}) 77 78 for job, builds in self.BUILDS.iteritems(): 79 for build, started, finished in builds: 80 path = '/kubernetes-jenkins/pr-logs/pull/123/%s/%s/' % (job, build) 81 if started: 82 write(path + 'started.json', started) 83 if finished: 84 write(path + 'finished.json', finished) 85 86 def test_pr_builds(self): 87 self.init_pr_directory() 88 org, repo = view_pr.org_repo('', 89 app.app.config['default_org'], 90 app.app.config['default_repo'], 91 ) 92 builds = view_pr.pr_builds(view_pr.pr_path(org, repo, '123', 93 app.app.config['default_repo'], 94 app.app.config['default_repo'], 95 app.app.config['external_services'][org]['gcs_pull_prefix'], 96 )) 97 self.assertEqual(builds, self.BUILDS) 98 99 def test_pr_handler(self): 100 self.init_pr_directory() 101 response = app.get('/pr/123') 102 self.assertIn('e2e/47', response) 103 self.assertIn('PASSED', response) 104 self.assertIn('colspan="3"', response) # header 105 self.assertIn('github.com/kubernetes/kubernetes/pull/123', response) 106 self.assertIn('28 20:44', response) 107 108 def test_pr_handler_missing(self): 109 gcs_async_test.install_handler(self.testbed.get_stub('urlfetch'), 110 {'124/': []}) 111 response = app.get('/pr/124') 112 self.assertIn('No Results', response) 113 114 def test_pr_build_log_redirect(self): 115 path = '123/some-job/55/build-log.txt' 116 response = app.get('/pr/' + path) 117 self.assertEqual(response.status_code, 302) 118 self.assertIn('https://storage.googleapis.com', response.location) 119 self.assertIn(path, response.location) 120 121 122 def make_pr(number, involved, payload, repo='kubernetes/kubernetes'): 123 payload.setdefault('attn', {}) 124 payload.setdefault('assignees', []) 125 payload.setdefault('author', involved[0]) 126 payload.setdefault('labels', {}) 127 digest = models.GHIssueDigest.make(repo, number, is_pr=True, is_open=True, 128 involved=involved, payload=payload, updated_at=datetime.datetime.now()) 129 digest.put() 130 131 132 class TestDashboard(main_test.TestBase): 133 def setUp(self): 134 app.reset() 135 self.init_stubs() 136 137 def test_empty(self): 138 resp = app.get('/pr/all') 139 self.assertIn('No Results', resp) 140 resp = app.get('/pr/nobody') 141 self.assertIn('No Results', resp) 142 143 def test_all(self): 144 make_pr(12, ['foo'], {'title': 'first'}, 'google/cadvisor') 145 make_pr(13, ['bar'], {'title': 'second'}, 'kubernetes/kubernetes') 146 resp = app.get('/pr/all') 147 self.assertIn('Open Kubernetes PRs', resp) 148 self.assertIn('first', resp) 149 self.assertIn('second', resp) 150 151 def test_json(self): 152 make_pr(12, ['a'], {'title': 'b'}, 'c/d') 153 resp = app.get('/pr/all?format=json') 154 self.assertEqual(resp.headers['Content-Type'], 'application/json') 155 self.assertEqual(len(resp.json), 1) 156 pr = resp.json[0] 157 self.assertEqual(pr['involved'], ['a']) 158 self.assertEqual(pr['number'], 12) 159 self.assertEqual(pr['repo'], 'c/d') 160 161 def test_one_entry(self): 162 make_pr(123, ['user'], {'attn': {'user': 'fix tests'}}) 163 resp = app.get('/pr/user') 164 self.assertIn('123', resp) 165 166 @staticmethod 167 def make_session(**kwargs): 168 # set the session cookie directly (easier than the full login flow) 169 serializer = securecookie.SecureCookieSerializer( 170 app.app.config['webapp2_extras.sessions']['secret_key']) 171 return serializer.serialize('session', kwargs) 172 173 def test_me(self): 174 make_pr(124, ['human'], {'title': 'huge pr!'}) 175 176 # no cookie: we get redirected 177 resp = app.get('/pr') 178 self.assertEqual(resp.status_code, 302) 179 self.assertEqual(resp.location, 'http://localhost/github_auth/pr') 180 181 # we have a cookie now: we should get results for 'human' 182 cookie = self.make_session(user='human') 183 resp = app.get('/pr', headers={'Cookie': 'session=%s' % cookie}) 184 self.assertEqual(resp.status_code, 200) 185 self.assertIn('huge pr!', resp) 186 187 def test_pr_links_user(self): 188 "Individual PR pages grab digest information" 189 make_pr(12345, ['human'], {'title': 'huge pr!'}) 190 resp = app.get('/pr/12345') 191 self.assertIn('href="/pr/human"', resp) 192 self.assertIn('huge pr!', resp) 193 194 def test_build_links_user(self): 195 "Build pages show PR information" 196 make_pr(12345, ['human'], {'title': 'huge pr!'}) 197 build_dir = '/kubernetes-jenkins/pr-logs/pull/12345/e2e/5/' 198 write(build_dir + 'started.json', '{}') 199 resp = app.get('/build' + build_dir) 200 self.assertIn('href="/pr/human"', resp) 201 self.assertIn('huge pr!', resp) 202 203 def test_acks(self): 204 make_pr(124, ['human'], {'title': 'huge pr', 'attn': {'human': 'help#123#456'}}, repo='k/k') 205 cookie = self.make_session(user='human') 206 headers = {'Cookie': 'session=%s' % cookie} 207 208 def expect_count(count): 209 resp = app.get('/pr', headers=headers) 210 self.assertEqual(resp.body.count('huge pr'), count) 211 212 # PR should appear twice 213 expect_count(2) 214 215 # Ack the PR... 216 ack_params = {'command': 'ack', 'repo': 'k/k', 'number': 124, 'latest': 456} 217 app.post_json('/pr', ack_params, headers=headers) 218 expect_count(1) 219 self.assertEqual(view_pr.get_acks('human', []), {'k/k 124': 456}) 220 221 # Clear the ack 222 app.post_json('/pr', {'command': 'ack-clear'}, headers=headers) 223 expect_count(2) 224 self.assertEqual(view_pr.get_acks('human', []), {}) 225 226 # Ack with an older latest 227 ack_params['latest'] = 123 228 app.post_json('/pr', ack_params, headers=headers) 229 expect_count(2) 230 231 232 if __name__ == '__main__': 233 unittest.main()