#!/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()