github.com/abayer/test-infra@v0.0.5/kettle/make_json_test.py (about)

     1  #!/usr/bin/env python2
     2  
     3  # Copyright 2017 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 cStringIO as StringIO
    18  import json
    19  import time
    20  import unittest
    21  
    22  import make_json
    23  import model
    24  
    25  
    26  class ValidateBuckets(unittest.TestCase):
    27      def test_buckets(self):
    28          prefixes = set()
    29          for name, options in sorted(make_json.BUCKETS.iteritems()):
    30              if name == 'gs://kubernetes-jenkins/logs/':
    31                  continue  # only bucket without a prefix
    32              prefix = options.get('prefix', '')
    33              self.assertNotEqual(prefix, '', 'bucket %s must have a prefix' % name)
    34              self.assertNotIn(prefix, prefixes, "bucket %s prefix %r isn't unique" % (name, prefix))
    35              self.assertEqual(prefix[-1], ':', "bucket %s prefix should be %s:" % (name, prefix))
    36  
    37  
    38  class MakeJsonTest(unittest.TestCase):
    39      def setUp(self):
    40          self.db = model.Database(':memory:')
    41  
    42      def test_path_to_job_and_number(self):
    43          def expect(path, job, number):
    44              self.assertEqual(make_json.path_to_job_and_number(path), (job, number))
    45  
    46          expect('gs://kubernetes-jenkins/logs/some-build/123', 'some-build', 123)
    47          expect('gs://kubernetes-jenkins/logs/some-build/123asdf', 'some-build', None)
    48          expect('gs://kubernetes-jenkins/pr-logs/123/e2e-node/456', 'pr:e2e-node', 456)
    49  
    50          with self.assertRaises(ValueError):
    51              expect('gs://unknown-bucket/foo/123', None, None)
    52  
    53      def test_row_for_build(self):
    54          def expect(path, start, finish, results, **kwargs):
    55              expected = {
    56                  'path': path,
    57                  'test': [],
    58                  'tests_failed': 0,
    59                  'tests_run': 0,
    60              }
    61              if finish:
    62                  expected['passed'] = kwargs.get('result') == 'SUCCESS'
    63              expected.update(kwargs)
    64              row = make_json.row_for_build(path, start, finish, results)
    65              self.assertEqual(row, expected)
    66  
    67          path = 'gs://kubernetes-jenkins/logs/J/123'
    68          expect(path, None, None, [], job='J', number=123)
    69          expect(path, None, None, [], job='J', number=123)
    70          expect(path,
    71                 {'timestamp': 10, 'node': 'agent-34'},
    72                 {'timestamp': 15, 'result': 'SUCCESS', 'version': 'v1.2.3'},
    73                 [],
    74                 job='J', number=123,
    75                 started=10, finished=15, elapsed=5,
    76                 version='v1.2.3', result='SUCCESS', executor='agent-34',
    77                )
    78          expect(path,
    79                 {'timestamp': 10},
    80                 {'timestamp': 15, 'passed': True},
    81                 [],
    82                 job='J', number=123,
    83                 started=10, finished=15, elapsed=5,
    84                 result='SUCCESS',
    85                )
    86          expect(path, None,
    87                 {'timestamp': 15, 'result': 'FAILURE',
    88                  'metadata': {'repo': 'ignored', 'pull': 'asdf'}}, [],
    89                 result='FAILURE', job='J', number=123, finished=15,
    90                 metadata=[{'key': 'pull', 'value': 'asdf'}, {'key': 'repo', 'value': 'ignored'}])
    91          expect(path, None, None, ['''
    92                     <testsuite>
    93                      <properties><property name="test" value="don't crash!"></property></properties>
    94                      <testcase name="t1" time="1.0"><failure>stacktrace</failure></testcase>
    95                      <testcase name="t2" time="2.0"></testcase>
    96                      <testcase name="t2#1" time="2.0"></testcase>
    97                     </testsuite>'''],
    98                 job='J', number=123,
    99                 tests_run=2, tests_failed=1,
   100                 test=[{'name': 't1', 'time': 1.0, 'failed': True, 'failure_text': 'stacktrace'},
   101                       {'name': 't2', 'time': 2.0}])
   102  
   103      def test_main(self):
   104          now = time.time()
   105          last_month = now - (60 * 60 * 24 * 30)
   106          junits = ['<testsuite><testcase name="t1" time="3.0"></testcase></testsuite>']
   107  
   108          def add_build(path, start, finish, result, junits):
   109              path = 'gs://kubernetes-jenkins/logs/%s' % path
   110              self.db.insert_build(
   111                  path, {'timestamp': start}, {'timestamp': finish, 'result': result})
   112              # fake build rowid doesn't matter here
   113              self.db.insert_build_junits(
   114                  hash(path),
   115                  {'%s/artifacts/junit_%d.xml' % (path, n): junit for n, junit in enumerate(junits)})
   116              self.db.commit()
   117  
   118          def expect(args, needles, negneedles, expected_ret=None):
   119              buf = StringIO.StringIO()
   120              opts = make_json.parse_args(args)
   121              ret = make_json.main(self.db, opts, buf)
   122              result = buf.getvalue()
   123  
   124              if expected_ret is not None:
   125                  self.assertEqual(ret, expected_ret)
   126  
   127              # validate that output is newline-delimited JSON
   128              for line in result.split('\n'):
   129                  if line.strip():
   130                      json.loads(line)
   131  
   132              # test for expected patterns / expected missing patterns
   133              for needle in needles:
   134                  self.assertIn(needle, result)
   135              for needle in negneedles:
   136                  # Only match negative needles in the middle of a word, to avoid
   137                  # failures on timestamps that happen to contain a short number.
   138                  self.assertNotRegexpMatches(result, r'\b%s\b' % needle)
   139  
   140          add_build('some-job/123', last_month, last_month + 10, 'SUCCESS', junits)
   141          add_build('some-job/456', now - 10, now, 'FAILURE', junits)
   142  
   143          expect([], ['123', '456', 'SUCCESS', 'FAILURE'], [])  # everything
   144          expect([], [], ['123', '456', 'SUCCESS', 'FAILURE'])  # nothing
   145  
   146          expect(['--days=1'], ['456'], [])  # recent
   147          expect(['--days', '1'], [], ['456'])  # nothing (already emitted)
   148  
   149          add_build('some-job/457', now + 1, now + 11, 'SUCCESS', junits)
   150          expect(['--days=1'], ['457'], ['456'])  # latest (day)
   151          expect([], ['457'], ['456'])         # latest (all)
   152  
   153          expect(['--days=1', '--reset-emitted'], ['456', '457'], [])  # both (reset)
   154          expect([], [], ['123', '456', '457'])                     # reset only works for given day
   155  
   156          # verify that direct paths work
   157          expect(['gs://kubernetes-jenkins/logs/some-job/123'], ['123'], [])
   158          expect(['gs://kubernetes-jenkins/logs/some-job/123'], ['123'], [])
   159  
   160          # verify that assert_oldest works
   161          expect(['--days=30'], ['123', '456'], [])
   162          expect(['--days=30', '--assert-oldest=60'], [], [], 0)
   163          expect(['--days=30', '--assert-oldest=25'], [], [], 1)
   164  
   165  
   166  
   167  if __name__ == '__main__':
   168      unittest.main()