github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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 = {'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          # redirect GCS calls to the local proxy
    72          gcs_async.GCS_API_URL = gcs.common.local_api_url()
    73  
    74  
    75  class AppTest(TestBase):
    76      # pylint: disable=too-many-public-methods
    77      BUILD_DIR = '/kubernetes-jenkins/logs/somejob/1234/'
    78  
    79      def setUp(self):
    80          self.init_stubs()
    81          init_build(self.BUILD_DIR)
    82  
    83      def test_index(self):
    84          """Test that the index works."""
    85          response = app.get('/')
    86          self.assertIn('kubernetes-e2e-gce', response)
    87  
    88      def test_nodelog_missing_files(self):
    89          """Test that a missing all files gives a 404."""
    90          build_dir = self.BUILD_DIR + 'nodelog?pod=abc'
    91          response = app.get('/build' + build_dir, status=404)
    92          self.assertIn('Unable to find', response)
    93  
    94      def test_nodelog_kubelet(self):
    95          """Test for a kubelet file with junit file.
    96           - missing the default kube-apiserver"""
    97          nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml'
    98          init_build(self.BUILD_DIR)
    99          write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE)
   100          write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log',
   101              'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"})\n')
   102          response = app.get('/build' + nodelog_url)
   103          self.assertIn("Wrap line", response)
   104  
   105      def test_nodelog_apiserver(self):
   106          """Test for default apiserver file
   107           - no kubelet file to find objrefdict
   108           - no file with junit file"""
   109          nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml'
   110          init_build(self.BUILD_DIR)
   111          write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE)
   112          write(self.BUILD_DIR + 'artifacts/tmp-node-image/kube-apiserver.log',
   113              'apiserver pod abc\n')
   114          response = app.get('/build' + nodelog_url)
   115          self.assertIn("Wrap line", response)
   116  
   117      def test_nodelog_no_junit(self):
   118          """Test for when no junit in same folder
   119           - multiple folders"""
   120          nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml'
   121          init_build(self.BUILD_DIR)
   122          write(self.BUILD_DIR + 'artifacts/junit_01.xml', JUNIT_SUITE)
   123          write(self.BUILD_DIR + 'artifacts/tmp-node-image/kube-apiserver.log',
   124              'apiserver pod abc\n')
   125          write(self.BUILD_DIR + 'artifacts/tmp-node-2/kube-apiserver.log',
   126              'apiserver pod abc\n')
   127          write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log',
   128              'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"})\n')
   129          response = app.get('/build' + nodelog_url)
   130          self.assertIn("tmp-node-2", response)
   131  
   132      def test_nodelog_no_junit_apiserver(self):
   133          """Test for when no junit in same folder
   134           - multiple folders
   135           - no kube-apiserver.log"""
   136          nodelog_url = self.BUILD_DIR + 'nodelog?pod=abc&junit=junit_01.xml'
   137          init_build(self.BUILD_DIR)
   138          write(self.BUILD_DIR + 'artifacts/junit_01.xml', JUNIT_SUITE)
   139          write(self.BUILD_DIR + 'artifacts/tmp-node-image/docker.log',
   140              'Containers\n')
   141          write(self.BUILD_DIR + 'artifacts/tmp-node-2/kubelet.log',
   142              'apiserver pod abc\n')
   143          write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log',
   144              'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"})\n')
   145          response = app.get('/build' + nodelog_url)
   146          self.assertIn("tmp-node-2", response)
   147  
   148      def test_no_failed_pod(self):
   149          """Test that filtering page still loads when no failed pod name is given"""
   150          nodelog_url = self.BUILD_DIR + 'nodelog?junit=junit_01.xml'
   151          init_build(self.BUILD_DIR)
   152          write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE)
   153          write(self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log',
   154              'abc\nEvent(api.ObjectReference{Name:"abc", UID:"podabc"} failed)\n')
   155          response = app.get('/build' + nodelog_url)
   156          self.assertIn("Wrap line", response)
   157  
   158      def test_parse_by_timestamp(self):
   159          """Test parse_by_timestamp and get_woven_logs
   160           - Weave separate logs together by timestamp
   161           - Check that lines without timestamp are combined
   162           - Test different timestamp formats"""
   163          kubelet_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log'
   164          kubeapi_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kube-apiserver.log'
   165          query_string = 'nodelog?pod=abc&junit=junit_01.xml&weave=on&logfiles=%s&logfiles=%s' % (
   166              kubelet_filepath, kubeapi_filepath)
   167          nodelog_url = self.BUILD_DIR + query_string
   168          init_build(self.BUILD_DIR)
   169          write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE)
   170          write(kubelet_filepath,
   171              'abc\n0101 01:01:01.001 Event(api.ObjectReference{Name:"abc", UID:"podabc"})\n')
   172          write(kubeapi_filepath,
   173              '0101 01:01:01.000 kubeapi\n0101 01:01:01.002 pod\n01-01T01:01:01.005Z last line')
   174          expected = ('0101 01:01:01.000 kubeapi\n'
   175                      '<span class="highlight">abc0101 01:01:01.001 Event(api.ObjectReference{Name:'
   176                      '&#34;<span class="keyword">abc</span>&#34;, UID:&#34;podabc&#34;})</span>\n'
   177                      '0101 01:01:01.002 pod\n'
   178                      '01-01T01:01:01.005Z last line')
   179          response = app.get('/build' + nodelog_url)
   180          print response
   181          self.assertIn(expected, response)
   182  
   183      def test_timestamp_no_apiserver(self):
   184          """Test parse_by_timestamp and get_woven_logs without an apiserver file
   185           - Weave separate logs together by timestamp
   186           - Check that lines without timestamp are combined
   187           - Test different timestamp formats
   188           - no kube-apiserver.log"""
   189          kubelet_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kubelet.log'
   190          proxy_filepath = self.BUILD_DIR + 'artifacts/tmp-node-image/kube-proxy.log'
   191          query_string = 'nodelog?pod=abc&junit=junit_01.xml&weave=on&logfiles=%s&logfiles=%s' % (
   192              kubelet_filepath, proxy_filepath)
   193          nodelog_url = self.BUILD_DIR + query_string
   194          init_build(self.BUILD_DIR)
   195          write(self.BUILD_DIR + 'artifacts/tmp-node-image/junit_01.xml', JUNIT_SUITE)
   196          write(kubelet_filepath,
   197              'abc\n0101 01:01:01.001 Event(api.ObjectReference{Name:"abc", UID:"podabc"})\n')
   198          write(proxy_filepath,
   199              '0101 01:01:01.000 proxy\n0101 01:01:01.002 pod\n01-01T01:01:01.005Z last line')
   200          expected = ('0101 01:01:01.000 proxy\n'
   201                      '<span class="highlight">abc0101 01:01:01.001 Event(api.ObjectReference{Name:'
   202                      '&#34;<span class="keyword">abc</span>&#34;, UID:&#34;podabc&#34;})</span>\n'
   203                      '0101 01:01:01.002 pod\n'
   204                      '01-01T01:01:01.005Z last line')
   205          response = app.get('/build' + nodelog_url)
   206          self.assertIn(expected, response)