#! /usr/bin/env python ''' /* * Copyright (c) 2016, 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. p4ismappedTest.py UnitTest for p4ismapped. 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. */ ''' import unittest import P4Server import logging import p4ismapped 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 = {'P4USER': 'p4tester', 'P4CLIENT': 'c_p4tester', 'P4D':'p4d.exe', #windows path #'P4D':'/usr/local/bin/p4d', #linux path #'TESTDIR': 'test', #windows path #'TESTDIR': '/Users/cmclouth/P4Server/src/p4test', #linux path } class Test_P4IsMapped(unittest.TestCase): p4server = object foundTestResults = [] @classmethod def setUpClass(cls): logger.debug('Enter') #p4user, p4client, testdir, p4d, testcase logger.debug(testConfiguration) Test_P4IsMapped.p4server = P4Server.P4Server(testConfiguration['P4USER'], testConfiguration['P4CLIENT'], testConfiguration.get('TESTDIR', 'test'), testConfiguration['P4D'], Test_P4IsMapped) if not Test_P4IsMapped.p4server.p4api.connected(): Test_P4IsMapped.p4server.p4api.connect() result = Test_P4IsMapped.p4server.p4api.run('info') Test_P4IsMapped.assertTrue(result is not None and len(result) > 0, 'info returned no results') Test_P4IsMapped.InitTestData() logger.debug('Exit') @classmethod def tearDownClass(cls): logger.debug('Enter') Test_P4IsMapped.p4server.p4api.disconnect() Test_P4IsMapped.p4server.dispose() logger.debug('Exit') @classmethod def InitTestData(cls): logger.debug('Enter') # create a client to view all things clientName = testConfiguration.get('P4CLIENT') client = Test_P4IsMapped.p4server.p4api.fetch_client(clientName) client['Root'] = Test_P4IsMapped.p4server.clientroot client['View'][0] = '//depot/... //' + clientName + '/...' del(client['View'][1:]) Test_P4IsMapped.p4server.p4api.save_client(client) clientName = 'test001' client = Test_P4IsMapped.p4server.p4api.fetch_client(clientName) client['Root'] = Test_P4IsMapped.p4server.clientroot client['View'][0] = '//depot/' + clientName + '/... //' + clientName + '/...' del(client['View'][1:]) Test_P4IsMapped.p4server.p4api.save_client(client) clientName = 'test002' client = Test_P4IsMapped.p4server.p4api.fetch_client(clientName) client['Root'] = Test_P4IsMapped.p4server.clientroot client['View'][0] = '//depot/' + clientName + '/... //' + clientName + '/...' del(client['View'][1:]) Test_P4IsMapped.p4server.p4api.save_client(client) clientName = 'test003' client = Test_P4IsMapped.p4server.p4api.fetch_client(clientName) client['Root'] = Test_P4IsMapped.p4server.clientroot client['View'][0] = '//depot/' + clientName + '/... //' + clientName + '/...' del(client['View'][1:]) Test_P4IsMapped.p4server.p4api.save_client(client) def test000(self): self.foundTestResults = [] expected = set(['test001', 'test002', 'test003', testConfiguration.get('P4CLIENT')]) clients = self.p4server.p4api.run_clients() self.assertEqual(len(clients), len(expected), 'number of clients mismatch') for clientName in clients: self.assertTrue(clientName['client'] in expected, clientName['client'] +' missing') args = p4ismapped.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'clients', '//depot/test001/a', '//depot/test002/b', '//depot/test003/c']) clients = p4ismapped.getP4Clients(self.p4server.p4api, args) self.assertEqual(len(clients), len(expected), 'number of clients mismatch') for clientName in clients: self.assertTrue(clientName['client'] in expected, clientName['client'] +' missing') p4ismapped.isMappedClients(args, self) self.assertEqual(len(self.foundTestResults), len(expected), 'number of clients mismatch') for item in self.foundTestResults: clientName = item[0] self.assertTrue(clientName['Client'] in expected, clientName['Client'] +' missing') def test001(self): self.foundTestResults = [] expected = set(['test001', testConfiguration.get('P4CLIENT')]) args = p4ismapped.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'clients', '//depot/test001/d']) p4ismapped.isMappedClients(args, self) self.assertEqual(len(self.foundTestResults), len(expected), 'number of clients mismatch') for item in self.foundTestResults: clientName = item[0] self.assertTrue(clientName['Client'] in expected, clientName['Client'] +' missing') def test002(self): self.foundTestResults = [] expected = set(['test002', testConfiguration.get('P4CLIENT')]) args = p4ismapped.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'clients', '//depot/test002/e']) p4ismapped.isMappedClients(args, self) self.assertEqual(len(self.foundTestResults), len(expected), 'number of clients mismatch') for item in self.foundTestResults: clientName = item[0] self.assertTrue(clientName['Client'] in expected, clientName['Client'] +' missing') def test003(self): self.foundTestResults = [] expected = set(['test003', testConfiguration.get('P4CLIENT')]) args = p4ismapped.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'clients', '//depot/test003/f']) p4ismapped.isMappedClients(args, self) self.assertEqual(len(self.foundTestResults), len(expected), 'number of clients mismatch') for item in self.foundTestResults: clientName = item[0] self.assertTrue(clientName['Client'] in expected, clientName['Client'] +' missing') def test004(self): self.foundTestResults = [] expected = set([testConfiguration.get('P4CLIENT')]) args = p4ismapped.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'clients', '-m', '//depot/test001/g', '//depot/test002/h', '//depot/test003/i']) p4ismapped.isMappedClients(args, self) self.assertEqual(len(self.foundTestResults), len(expected), 'number of clients mismatch') for item in self.foundTestResults: clientName = item[0] self.assertTrue(clientName['Client'] in expected, clientName['Client'] +' missing') if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main()