TestRequireJob.py #1

  • //
  • guest/
  • perforce_software/
  • sdp/
  • dev/
  • Server/
  • Unix/
  • p4/
  • common/
  • bin/
  • triggers/
  • tests/
  • TestRequireJob.py
  • View
  • Commits
  • Open Download .zip Download (4 KB)
# -*- encoding: UTF8 -*-
# Test harness for RequireJob.py

from __future__ import print_function

import sys
import unittest
import os
import re

import p4testutils
import P4
import logging

parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parent_dir)
from RequireJob import RequireJob

os.environ["LOGS"] = "."
LOGGER_NAME = "TestRequireJob"
LOG_FILE = "log-RequireJob.log"

python3 = sys.version_info[0] >= 3

def ensureDirectory(directory):
    if not os.path.isdir(directory):
        os.makedirs(directory)

def localDirectory(root, *dirs):
    "Create and ensure it exists"
    dir_path = os.path.join(root, *dirs)
    ensureDirectory(dir_path)
    return dir_path

def create_file(file_name, contents):
    "Create file with specified contents"
    ensureDirectory(os.path.dirname(file_name))
    if python3:
        contents = bytes(contents.encode())
    with open(file_name, 'wb') as f:
        f.write(contents)

def append_to_file(file_name, contents):
    "Append contents to file"
    if python3:
        contents = bytes(contents.encode())
    with open(file_name, 'ab+') as f:
        f.write(contents)

class TestRequireJob(unittest.TestCase):
    def __init__(self, methodName='runTest'):
        super(TestRequireJob, self).__init__(methodName=methodName)
        self.logger = logging.getLogger(LOGGER_NAME)
        self.logger.setLevel(logging.DEBUG)
        logformat = '%(levelname)s [%(asctime)s] [%(filename)s : %(lineno)d] - %(message)s'
        logging.basicConfig(format=logformat, filename=LOG_FILE, level=logging.DEBUG)

    def assertRegex(self, *args, **kwargs):
        if python3:
            return super(TestRequireJob, self).assertRegex(*args, **kwargs)
        else:
            return super(TestRequireJob, self).assertRegexpMatches(*args, **kwargs)

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testRequireJob(self):
        """trigger fires and sends expected info to Swarm"""
        self.server = p4testutils.P4Server()
        trig_path = os.path.join(parent_dir, "RequireJob.py")
        config_path = os.path.join(self.server.root, "test_config.yaml")
        p4 = self.server.p4
        p4.logger = self.logger
        # This works if no spaces in server root pathname!
        port = p4.port.replace('"', '')
        self.logger.debug("port: |%s|" % port)
        triggers = p4.fetch_triggers()
        triggers['Triggers'] = ['require-job change-submit //... " python ' + trig_path +
                                " -p %quote%" + port + "%quote% -u " + p4.user +
                                " -c %s " % config_path +
                               '  %change% "']
        self.logger.debug(triggers)
        p4.save_triggers(triggers)
        # Reconnect to pick up changes
        p4.disconnect()
        p4.connect()

        inside = localDirectory(self.server.client_root, "inside")
        inside_file1 = os.path.join(inside, "inside_file1")
        create_file(inside_file1, 'Test content')

        # Submit without a config file still works fine
        p4.run('add', inside_file1)
        result = p4.run('submit', '-d', 'inside_file1 added')
        self.assertEquals("1", result[-1]['submittedChange'])

        with open(config_path, "w") as f:
            f.write("""
api: "api/v6"
user: swarmtest
ticket: A123453
review_description: "Please review me!\nDon't forget to check YYYY"
projects:
  - name:           ProjectA
    create_review:  y
    require_job:    y
    update_review:  n
    depot_paths:
      - //depot/inside/...
      - "-//depot/inside/A..."
""")

        # Now submit fails due to no job
        p4.run('edit', inside_file1)
        append_to_file(inside_file1, "more text")
        try:
            result = p4.run('submit', '-d', 'inside_file1 added')
            self.assertEquals("2", result[-1]['submittedChange'])
            self.assertTrue(False, "Expected exception not found")
        except P4.P4Exception as e:
            self.assertRegex(str(e), r"this codeline requires a job")

        job = p4.fetch_job()
        job["Description"] = "test job"
        p4.save_job(job)

        # This time submit succeeds
        p4.run_fix("-c", "2", "job000001")
        result = p4.run('submit', '-c', '2')
        self.assertEquals("2", result[-1]['submittedChange'])

if __name__ == '__main__':
    unittest.main()
# Change User Description Committed
#10 26652 Robert Cowham This is Tom's change:

Introduced new 'Unsupported' directory to clarify that some files
in the SDP are not officially supported. These files are samples for
illustration, to provide examples, or are deprecated but not yet
ready for removal from the package.

The Maintenance and many SDP triggers have been moved under here,
along with other SDP scripts and triggers.

Added comments to p4_vars indicating that it should not be edited
directly. Added reference to an optional site_global_vars file that,
if it exists, will be sourced to provide global user settings
without needing to edit p4_vars.

As an exception to the refactoring, the totalusers.py Maintenance
script will be moved to indicate that it is supported.

Removed settings to support long-sunset P4Web from supported structure.

Structure under new .../Unsupported folder is:
   Samples/bin             Sample scripts.
   Samples/triggers        Sample trigger scripts.
   Samples/triggers/tests  Sample trigger script tests.
   Samples/broker          Sample broker filter scripts.
   Deprecated/triggers     Deprecated triggers.

To Do in a subsequent change: Make corresponding doc changes.
#9 24250 Robert Cowham Fix ordered read of yaml files and allow exceptions for RequireJob
#8 23665 Robert Cowham Allow equivalent of Jenkins users to bypass trigger too.
#7 23574 Robert Cowham Fix deprecation warnings in Python3
#6 23419 Robert Cowham Refer to Workflow.yaml
Change name of field from create_review to post_submit_create_review
#5 23415 Robert Cowham Allow Swarm user to bypass trigger.
#4 23222 Robert Cowham Refactored to remove p4cmd call - p4 does its own logging now.
#3 23221 Robert Cowham Test RequireJob when used with shelved changes (e.g.
Swarm review)
#2 23151 Robert Cowham More refactoring - move common things into p4testutils
#1 23150 Robert Cowham Refactor to move tests into subdir
//guest/perforce_software/sdp/dev/Server/Unix/p4/common/bin/triggers/TestRequireJob.py
#2 23132 Robert Cowham Search for jobs and update existing review
Also add default reviewers
#1 23131 Robert Cowham New RequireJob pre-submit trigger.