github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/gubernator/main_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 18 """ 19 To run these tests: 20 $ pip install webtest nosegae 21 $ nosetests --with-gae --gae-lib-root ~/google_appengine/ 22 """ 23 24 import unittest 25 26 import webtest 27 28 import cloudstorage as gcs 29 30 import main 31 import gcs_async 32 import gcs_async_test 33 34 write = gcs_async_test.write 35 36 app = webtest.TestApp(main.app) 37 38 JUNIT_SUITE = """<testsuite tests="8" failures="0" time="1000.24"> 39 <testcase name="First" classname="Example e2e suite" time="0"> 40 <skipped/> 41 </testcase> 42 <testcase name="Second" classname="Example e2e suite" time="36.49"/> 43 <testcase name="Third" classname="Example e2e suite" time="96.49"> 44 <failure>/go/src/k8s.io/kubernetes/test.go:123 45 Error Goes Here</failure> 46 </testcase> 47 </testsuite>""" 48 49 50 def init_build(build_dir, started=True, finished=True, 51 finished_has_version=False): 52 """Create faked files for a build.""" 53 start_json = {'timestamp': 1406535800} 54 finish_json = {'passed': True, 'result': 'SUCCESS', 'timestamp': 1406536800} 55 (finish_json if finished_has_version else start_json)['version'] = 'v1+56' 56 if started: 57 write(build_dir + 'started.json', start_json) 58 if finished: 59 write(build_dir + 'finished.json', finish_json) 60 write(build_dir + 'artifacts/junit_01.xml', JUNIT_SUITE) 61 62 63 64 class TestBase(unittest.TestCase): 65 def init_stubs(self): 66 self.testbed.init_memcache_stub() 67 self.testbed.init_app_identity_stub() 68 self.testbed.init_urlfetch_stub() 69 self.testbed.init_blobstore_stub() 70 self.testbed.init_datastore_v3_stub() 71 self.testbed.init_app_identity_stub() 72 # redirect GCS calls to the local proxy 73 gcs_async.GCS_API_URL = gcs.common.local_api_url() 74 75 76 class AppTest(TestBase): 77 # pylint: disable=too-many-public-methods 78 BUILD_DIR = '/kubernetes-jenkins/logs/somejob/1234/' 79 80 def setUp(self): 81 self.init_stubs() 82 init_build(self.BUILD_DIR) 83 84 def test_index(self): 85 """Test that the index works.""" 86 response = app.get('/') 87 self.assertIn('kubernetes-e2e-gce', response) 88 89 def test_nodelog_missing_files(self): 90 """Test that a missing all files gives a 404.""" 91 build_dir = self.BUILD_DIR + 'nodelog?pod=abc' 92 response = app.get('/build' + build_dir, status=404) 93 self.assertIn('Unable to find', response) 94 95 def test_nodelog_kubelet(self): 96 """Test for a kubelet file with junit file. 97 - missing the default kube-apiserver""" 98 nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml' 99 init_build(self.BUILD_DIR) 100 write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE) 101 write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log', 102 'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"})\n') 103 response = app.get('/build' + nodelog_url) 104 self.assertIn("Wrap line", response) 105 106 def test_nodelog_apiserver(self): 107 """Test for default apiserver file 108 - no kubelet file to find objrefdict 109 - no file with junit file""" 110 nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml' 111 init_build(self.BUILD_DIR) 112 write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE) 113 write(self.BUILD_DIR + 'artifacts/tmp-node-image/kube-apiserver.log', 114 'apiserver pod abc\n') 115 response = app.get('/build' + nodelog_url) 116 self.assertIn("Wrap line", response) 117 118 def test_nodelog_no_junit(self): 119 """Test for when no junit in same folder 120 - multiple folders""" 121 nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml' 122 init_build(self.BUILD_DIR) 123 write(self.BUILD_DIR + 'artifacts/junit_01.xml', JUNIT_SUITE) 124 write(self.BUILD_DIR + 'artifacts/tmp-node-image/kube-apiserver.log', 125 'apiserver pod abc\n') 126 write(self.BUILD_DIR + 'artifacts/tmp-node-2/kube-apiserver.log', 127 'apiserver pod abc\n') 128 write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log', 129 'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"})\n') 130 response = app.get('/build' + nodelog_url) 131 self.assertIn("tmp-node-2", response) 132 133 def test_nodelog_no_junit_apiserver(self): 134 """Test for when no junit in same folder 135 - multiple folders 136 - no kube-apiserver.log""" 137 nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml' 138 init_build(self.BUILD_DIR) 139 write(self.BUILD_DIR + 'artifacts/junit_01.xml', JUNIT_SUITE) 140 write(self.BUILD_DIR + 'artifacts/tmp-node-image/docker.log', 141 'Containers\n') 142 write(self.BUILD_DIR + 'artifacts/tmp-node-2/kubelet.log', 143 'apiserver pod abc\n') 144 write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log', 145 'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"})\n') 146 response = app.get('/build' + nodelog_url) 147 self.assertIn("tmp-node-2", response) 148 149 def test_no_failed_pod(self): 150 """Test that filtering page still loads when no failed pod name is given""" 151 nodelog_url = self.BUILD_DIR + 'nodelog?junit=junit_01.xml' 152 init_build(self.BUILD_DIR) 153 write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE) 154 write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log', 155 'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"} failed)\n') 156 response = app.get('/build' + nodelog_url) 157 self.assertIn("Wrap line", response) 158 159 def test_parse_by_timestamp(self): 160 """Test parse_by_timestamp and get_woven_logs 161 - Weave separate logs together by timestamp 162 - Check that lines without timestamp are combined 163 - Test different timestamp formats""" 164 kubelet_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log' 165 kubeapi_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kube-apiserver.log' 166 query_string = 'nodelog?pod=abc&junit=junit_01.xml&weave=on&logfiles=%s&logfiles=%s' % ( 167 kubelet_filepath, kubeapi_filepath) 168 nodelog_url = self.BUILD_DIR + query_string 169 init_build(self.BUILD_DIR) 170 write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE) 171 write(kubelet_filepath, 172 'abc\n0101 01:01:01.001 Event(api.ObjectReference{Name:"abc", UID:"podabc"})\n') 173 write(kubeapi_filepath, 174 '0101 01:01:01.000 kubeapi\n0101 01:01:01.002 pod\n01-01T01:01:01.005Z last line') 175 expected = ('0101 01:01:01.000 kubeapi\n' 176 '<span class="highlight">abc0101 01:01:01.001 Event(api.ObjectReference{Name:' 177 '"<span class="keyword">abc</span>", UID:"podabc"})</span>\n' 178 '0101 01:01:01.002 pod\n' 179 '01-01T01:01:01.005Z last line') 180 response = app.get('/build' + nodelog_url) 181 print response 182 self.assertIn(expected, response) 183 184 def test_timestamp_no_apiserver(self): 185 """Test parse_by_timestamp and get_woven_logs without an apiserver file 186 - Weave separate logs together by timestamp 187 - Check that lines without timestamp are combined 188 - Test different timestamp formats 189 - no kube-apiserver.log""" 190 kubelet_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log' 191 proxy_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kube-proxy.log' 192 query_string = 'nodelog?pod=abc&junit=junit_01.xml&weave=on&logfiles=%s&logfiles=%s' % ( 193 kubelet_filepath, proxy_filepath) 194 nodelog_url = self.BUILD_DIR + query_string 195 init_build(self.BUILD_DIR) 196 write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE) 197 write(kubelet_filepath, 198 'abc\n0101 01:01:01.001 Event(api.ObjectReference{Name:"abc", UID:"podabc"})\n') 199 write(proxy_filepath, 200 '0101 01:01:01.000 proxy\n0101 01:01:01.002 pod\n01-01T01:01:01.005Z last line') 201 expected = ('0101 01:01:01.000 proxy\n' 202 '<span class="highlight">abc0101 01:01:01.001 Event(api.ObjectReference{Name:' 203 '"<span class="keyword">abc</span>", UID:"podabc"})</span>\n' 204 '0101 01:01:01.002 pod\n' 205 '01-01T01:01:01.005Z last line') 206 response = app.get('/build' + nodelog_url) 207 self.assertIn(expected, response)