test_validity_check.py 2.84 KB
Newer Older
1
#!/usr/bin/env python3
2 3 4 5 6 7 8

import os
import logging
import shutil
import tempfile
import sys

9 10 11 12 13 14 15 16
import git

import token_file_processing

# Taken from
# https://stackoverflow.com/questions/19425736/how-to-redirect-stdout-and-stderr-to-logger-in-python


17
class LoggerWriter:
18 19 20 21
   def __init__(self, level):
      # self.level is really like using log.debug(message)
      # at least in my case
      self.level = level
22

23 24 25 26 27
   def write(self, message):
      # if statement reduces the amount of newlines that are
      # printed to the logger
      if message != '\n':
         self.level(message)
28

29 30 31 32 33 34
   def flush(self):
      # create a flush method so things can be flushed when
      # the system wants to. Not sure if simply 'printing'
      # sys.stderr is the correct way to do it, but it seemed
      # to work properly for me.
      self.level(sys.stderr)
35 36 37 38 39 40 41


def main():
   # Get stderr reporting to logging
   log = logging.getLogger()
   sys.stdout = LoggerWriter(log.debug)
   sys.stderr = LoggerWriter(log.warning)
42

43 44 45 46 47 48 49
   try:
      # Create temporary staging area
      temp_staging_area = tempfile.mkdtemp(prefix='temp_staging_location')
      # Get logging reporting to file that can be interogated
      log_filename = os.path.join(temp_staging_area, 'error_file.txt')
      logging.basicConfig(filename=log_filename)
      # Clone buildstream repo to temporary staging area
50 51 52
      repo = git.Repo.clone_from(
         'https://gitlab.com/BuildStream/buildstream.git',
         temp_staging_area)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      # Get buildstream head SHA
      test_sha = str(repo.head.object.hexsha)
      # Cloned buildstream repo path
      buildstream_path = os.path.join(temp_staging_area, 'buildstream')

      # Check that reference token file parses and is valid
      token_data = token_file_processing.process_token_file('bst_benchmarks/test_run_token.yml')
      valid = token_file_processing.verify_token_data(token_data, '', '', buildstream_path)

      assert valid

      # Check that a token file can be generated, that this can then be parsed and verified
      token_file_processing.generate_token_file('test.yml', test_sha, 'master', '')
      token_data = token_file_processing.process_token_file('test.yml')
      valid = token_file_processing.verify_token_data(token_data, '', '', buildstream_path)

      assert valid

      # Check that an invalid token file can be generated and that this fails verification
      # and failure is reported through stderr logging to file
      token_file_processing.generate_token_file('test.yml', test_sha, 'faux-master', '')
      token_data = token_file_processing.process_token_file('test.yml')
      valid = token_file_processing.verify_token_data(token_data, '', '', buildstream_path)

      assert not valid
      assert 'fatal: Needed a single revision' in open(log_filename)
   finally:
      shutil.rmtree(temp_staging_area)

82

83 84
if __name__ == "__main__":
   main()