#!/usr/local/bin/python # Copyright (c) 2017, Charles McLouth # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # # Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # # Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL STEWART LORD BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. '''P4ServerTest.py UnitTest for P4Server and base classes that may be used. To run, modify the testConfiguration map. P4USER - P4 user that will be running the tests P4CLIENT - P4 client that will be running the tests P4D - fully qualified path to the p4d (Perforce Server) binary TESTDIR - fully qualified path to a directory that will be used for file storage needs of the tests. This will be cleaned up on exit. For example usage of P4Server see test method testP4Server which illustrates: instantiating the object issuing p4 commands dispose ''' import unittest from P4Server import P4Server, P4ServerError, p4dbin, P4Testcase,\ P4TestcaseShared from os import environ as osenviron import logging logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) debugHandler = logging.StreamHandler() debugHandler.setFormatter(logging.Formatter("%(levelname)s:%(filename)s:" "%(lineno)d:%(funcName)s:" "%(message)s")) logger.addHandler(debugHandler) testConfiguration = {} class TestP4Server(unittest.TestCase): def testBadData(self): p4user = None p4client= None testdir = None p4d = None with self.assertRaises(P4ServerError) as cm: P4Server(p4user, p4client, testdir, p4d) expected = "Invalid p4user '" + str(p4user) + "'." actual = str(cm.exception) self.assertEqual(expected, actual, "wrong exception caught.") p4user = testConfiguration.get("P4USER", "p4tester") with self.assertRaises(P4ServerError) as cm: P4Server(p4user, p4client, testdir, p4d) expected = "Invalid p4client '" + str(p4client) + "'." actual = str(cm.exception) self.assertEqual(expected, actual, "wrong exception caught.") p4client = testConfiguration.get("P4CLIENT", "c_p4tester") with self.assertRaises(P4ServerError) as cm: P4Server(p4user, p4client, testdir, p4d) expected = "Invalid p4d '" + str(p4d) + "'." actual = str(cm.exception) self.assertEqual(expected, actual, "wrong exception caught.") p4d = testConfiguration.get("P4D", p4dbin) with self.assertRaises(P4ServerError) as cm: P4Server(p4user, p4client, testdir, p4d) expected = "Invalid testdir '" + str(testdir) + "'." actual = str(cm.exception) self.assertEqual(expected, actual, "wrong exception caught.") testdir = "badpath" with self.assertRaises(P4ServerError) as cm: P4Server(p4user, p4client, testdir, p4d) expected = "Invalid testdir '" + str(testdir) + "' not found." actual = str(cm.exception) self.assertEqual(expected, actual, "wrong exception caught.") testdir = testConfiguration.get("TESTDIR", "test") p4server = P4Server(p4user, p4client, testdir, p4d) p4server.dispose() def testP4Server(self): p4user = testConfiguration.get("P4USER", "p4tester") p4client= testConfiguration.get("P4CLIENT", "c_p4tester") testdir = testConfiguration.get("TESTDIR", "test") p4d = testConfiguration.get("P4D", p4dbin) p4server = P4Server(p4user, p4client, testdir, p4d) if not p4server.p4api.connected(): p4server.p4api.connect() result = p4server.p4api.run("info") self.assertTrue(result is not None and len(result) > 0, "info returned no results") p4server.p4api.disconnect() p4server.dispose() p4server = P4Server(p4user, p4client, testdir, p4d) if not p4server.p4api.connected(): p4server.p4api.connect() result = p4server.p4api.run("info") self.assertTrue(result is not None and len(result) > 0, "info returned no results") depot = p4server.p4api.fetch_depot("depot") p4server.p4api.save_depot(depot) p4server.p4api.disconnect() p4server.dispose() class TestP4ServerTest(P4Testcase): def initTestData(self, p4api): logger.debug(self.id()) def testFoo(self): logger.debug(str(self.p4server.p4api)) self.p4server.p4api.run_info() def testFoo2(self): logger.debug(str(self.p4server.p4api)) self.p4server.p4api.run_info() class TestP4ServerSharedTest(P4TestcaseShared): def initTestData(self, p4api): logger.debug(self.id()) def testFoo(self): for k,v in osenviron.items(): if k.startswith("P4"): logger.info("{}={!s}".format(k, v)) def testFoo2(self): for k,v in osenviron.items(): if k.startswith("P4"): logger.info("{}={!s}".format(k, v)) if __name__ == "__main__": #import sys;sys.argv = ["", "Test.testName"] unittest.main()
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#7 | 23655 | Charlie McLouth | change 23521 had unintended consequences | ||
#6 | 23521 | Charlie McLouth |
save and restore environment settings Refactored testcase classes Note: this will break existing classes deriving from |
||
#5 | 23075 | Charlie McLouth | Refactored test base classes to be in P4Server | ||
#4 | 23072 | Charlie McLouth | reconfigured testConfiguration | ||
#3 | 23069 | Charlie McLouth | Added P4TestServerBase and P4TestServerBase2 which can be subclassed for other test applications | ||
#2 | 21136 | Charlie McLouth | convert relative path of testdir to absolute path | ||
#1 | 21135 | Charlie McLouth | Branching to project directory | ||
//guest/cmclouth/python-utils/P4ServerTest.py | |||||
#1 | 11780 | Charlie McLouth | Adding mergetracker - a Python tool that tracks where changes have been merged nor not merged. |