#! /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. p4submitTest.py UnitTest for p4submit. 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 p4submit import os.path import P4 logger = logging.getLogger(p4submit.__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': 'p4supertester', '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_P4Submit(unittest.TestCase): p4server = object # foundTestResults = [] @classmethod def setUpClass(cls): logger.debug('Enter') #p4user, p4client, testdir, p4d, testcase logger.debug(testConfiguration) Test_P4Submit.p4server = P4Server.P4Server(testConfiguration['P4USER'], testConfiguration['P4CLIENT'], testConfiguration.get('TESTDIR', 'test'), testConfiguration['P4D'], Test_P4Submit) if not Test_P4Submit.p4server.p4api.connected(): Test_P4Submit.p4server.p4api.connect() result = Test_P4Submit.p4server.p4api.run('info') Test_P4Submit.assertTrue(result is not None and len(result) > 0, 'info returned no results') Test_P4Submit.InitTestData() logger.debug('Exit') @classmethod def tearDownClass(cls): logger.debug('Enter') Test_P4Submit.p4server.p4api.run_logout() Test_P4Submit.p4server.p4api.disconnect() Test_P4Submit.p4server.dispose() logger.debug('Exit') @classmethod def InitTestData(cls): logger.debug('Enter') # create users Test_P4Submit.p4server.p4api.save_user(Test_P4Submit.p4server.p4api.fetch_user()) Test_P4Submit.p4server.p4api.input = Test_P4Submit.p4server.p4api.user Test_P4Submit.p4server.p4api.run('password') # we already have a depot called depot. # create a streams depot depot = Test_P4Submit.p4server.p4api.fetch_depot('streamtest') depot['Type'] = 'stream' Test_P4Submit.p4server.p4api.save_depot(depot) # create stream (main) stream = Test_P4Submit.p4server.p4api.fetch_stream('-t', 'mainline', '//streamtest/main') Test_P4Submit.p4server.p4api.save_stream(stream) # create template client spec clientName = 'template_depot' client = Test_P4Submit.p4server.p4api.fetch_client(clientName) client['Root'] = Test_P4Submit.p4server.clientroot client['View'][0] = '//depot/... //' + clientName + '/...' del(client['View'][1:]) Test_P4Submit.p4server.p4api.save_client(client) #save the protect table Test_P4Submit.p4server.p4api.save_protect(Test_P4Submit.p4server.p4api.fetch_protect()) Test_P4Submit.p4server.p4api.user = 'p4tester2' Test_P4Submit.p4server.p4api.save_user(Test_P4Submit.p4server.p4api.fetch_user()) Test_P4Submit.p4server.p4api.input = Test_P4Submit.p4server.p4api.user Test_P4Submit.p4server.p4api.run('password') Test_P4Submit.p4server.p4api.user = 'p4tester' Test_P4Submit.p4server.p4api.save_user(Test_P4Submit.p4server.p4api.fetch_user()) Test_P4Submit.p4server.p4api.input = Test_P4Submit.p4server.p4api.user Test_P4Submit.p4server.p4api.run('password') Test_P4Submit.p4server.p4api.user = testConfiguration['P4USER'] Test_P4Submit.p4server.p4api.password = Test_P4Submit.p4server.p4api.user logger.debug(Test_P4Submit.p4server.p4api.run_configure('set', 'security=3')) Test_P4Submit.p4server.p4api.run_login() Test_P4Submit.p4server.p4api.user = 'p4tester' Test_P4Submit.p4server.p4api.password = Test_P4Submit.p4server.p4api.user Test_P4Submit.p4server.p4api.run_login() # create a client to view all things clientName = testConfiguration.get('P4CLIENT') client = Test_P4Submit.p4server.p4api.fetch_client('-t', 'template_depot', clientName) client['Root'] = Test_P4Submit.p4server.clientroot Test_P4Submit.p4server.p4api.save_client(client) # create a shelf #1 change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'a.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #2 change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'b.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #3 change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'c.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #4 change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'd.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #5 change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test #async' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'e.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #6 change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test #async' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'f.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #7 Test_P4Submit.p4server.p4api.run_client('-sf', '-S', '//streamtest/main', Test_P4Submit.p4server.p4api.client) change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test #async' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'g.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #8 Test_P4Submit.p4server.p4api.run_client('-sf', '-S', '//streamtest/main', Test_P4Submit.p4server.p4api.client) change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test #async' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'h.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') # create a shelf #9 Test_P4Submit.p4server.p4api.run_client('-sf', '-S', '//streamtest/main', Test_P4Submit.p4server.p4api.client) change = Test_P4Submit.p4server.p4api.fetch_change() change['Description'] = 'shelf to test' changeNo = Test_P4Submit.p4server.p4api.save_change(change)[0].split()[1] testFile = os.path.join(Test_P4Submit.p4server.clientroot, 'i.txt') f = open(testFile, mode='a') f.write('a') f.close() Test_P4Submit.p4server.p4api.run_add('-c', changeNo, testFile) Test_P4Submit.p4server.p4api.run_opened() Test_P4Submit.p4server.p4api.run_shelve('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_revert('-c', changeNo, '//...') Test_P4Submit.p4server.p4api.run_logout() Test_P4Submit.p4server.p4api.user = testConfiguration['P4USER'] Test_P4Submit.p4server.p4api.password = Test_P4Submit.p4server.p4api.user logger.debug(Test_P4Submit.p4server.p4api.run_login()) logger.debug(Test_P4Submit.p4server.p4api.fetch_protect()) logger.debug(Test_P4Submit.p4server.p4api.run_users()) logger.debug(Test_P4Submit.p4server.p4api.run_changes()) logger.debug(Test_P4Submit.p4server.p4api.run_opened()) logger.debug(Test_P4Submit.p4server.p4api.run_logout()) def setupClient(self, streams=False): self.p4server.p4api.password = self.p4server.p4api.user self.p4server.p4api.run_login() if streams: self.p4server.p4api.run_client('-sf', '-S', '//streamtest/main', testConfiguration.get('P4CLIENT')) else: self.p4server.p4api.run_client('-sf', '-t', 'template_depot', testConfiguration.get('P4CLIENT')) self.p4server.p4api.run_logout() def assertClientSpecs(self): self.p4server.p4api.password = self.p4server.p4api.user self.p4server.p4api.run_login() result = self.p4server.p4api.run_clients('-e', '%s*' % (testConfiguration.get('P4CLIENT'))) self.assertEqual(1, len(result)) self.p4server.p4api.run_logout() def test001(self): self.setupClient(False) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, '-l', 'p4submitTest.log', 'submit', '1']) p4Test = p4submit.getP4Connection(args) p4Test.password = p4Test.user p4Test.run_login() result = p4submit.submitShelf(args) self.assertEqual('submitted', result[0]['status']) self.assertEqual('p4tester', result[0]['user']) #self.assertEqual(testConfiguration.get('P4CLIENT'), result[0]['client']) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() def test002(self): self.setupClient(False) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', 'p4tester2', '-p', self.p4server.p4api.port, 'submit', '2']) p4Test = p4submit.getP4Connection(args) p4Test.password = p4Test.user p4Test.run_login() with self.assertRaises(P4.P4Exception) as cm: p4submit.submitShelf(args) logger.debug(cm.exception) self.assertTrue('You don\'t have permission for this operation.' in str(cm.exception), 'Unexpected Exception: %s' % str(cm.exception)) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() def test004(self): self.setupClient(False) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, '-P', self.p4server.p4api.user, '-F', 'submit', '-t', '#async', '4']) result = p4submit.submitShelf(args) self.assertIsNone(result) p4Test = p4submit.getP4Connection(args) p4Test.run_login() result = p4Test.run_describe(args.shelvedChange[0]) self.assertIn('shelved', result[0], 'change is not shelved') logger.debug(result) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() def test005(self): self.setupClient(False) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, '-P', self.p4server.p4api.user, '-F', 'submit', '-t', '#async', '5']) result = p4submit.submitShelf(args) self.assertIsNotNone(result) self.assertEqual('submitted', result[0]['status']) self.assertEqual('p4tester', result[0]['user']) #self.assertEqual(testConfiguration.get('P4CLIENT'), result[0]['client']) args.shelvedChange[0] = int(result[0]['change']) with self.assertRaises(p4submit.P4SubmitError) as cm: p4submit.submitShelf(args) logger.debug(cm.exception) self.assertTrue('no such shelved changelist' in str(cm.exception), 'Unexpected Exception: %s' % str(cm.exception)) self.assertClientSpecs() def test007(self): self.setupClient(True) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, '-l', 'p4submitTest.log', 'submit', '7']) p4Test = p4submit.getP4Connection(args) p4Test.password = p4Test.user p4Test.run_login() result = p4submit.submitShelf(args) self.assertEqual('submitted', result[0]['status']) self.assertEqual('p4tester', result[0]['user']) #self.assertEqual(testConfiguration.get('P4CLIENT'), result[0]['client']) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() def test008(self): self.setupClient(True) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', 'p4tester2', '-p', self.p4server.p4api.port, 'submit', '8']) p4Test = p4submit.getP4Connection(args) p4Test.password = p4Test.user p4Test.run_login() with self.assertRaises(P4.P4Exception) as cm: p4submit.submitShelf(args) logger.debug(cm.exception) self.assertTrue('You don\'t have permission for this operation.' in str(cm.exception), 'Unexpected Exception: %s' % str(cm.exception)) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() def test009(self): self.setupClient(True) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, '-P', self.p4server.p4api.user, '-F', 'submit', '-t', '#async', '9']) result = p4submit.submitShelf(args) self.assertIsNone(result) p4Test = p4submit.getP4Connection(args) p4Test.run_login() result = p4Test.run_describe(args.shelvedChange[0]) self.assertIn('shelved', result[0], 'change is not shelved') logger.debug(result) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() @unittest.skip("This test is no longer necessary") def test098(self): self.setupClient(False) args = p4submit.processInputParams(['-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'submit', '3']) p4Test = p4submit.getP4Connection(args) p4Test.password = p4Test.user p4Test.client = 'badclientname' p4Test.run_login() with self.assertRaises(P4.P4Exception) as cm: p4submit.submitShelf(args) logger.debug(cm.exception) self.assertTrue('unknown - use \'client\' command to create it.' in str(cm.exception), 'Unexpected Exception: %s' % str(cm.exception)) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() def test099(self): self.setupClient(False) args = p4submit.processInputParams(['-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'submit', '3']) p4Test = p4submit.getP4Connection(args) p4Test.disconnect() with self.assertRaises(P4.P4Exception) as cm: p4submit.submitShelf(args) logger.debug(cm.exception) self.assertTrue('Perforce password (P4PASSWD) invalid or unset.' in str(cm.exception), 'Unexpected Exception: %s' % str(cm.exception)) self.assertClientSpecs() def test100(self): self.setupClient(False) args = p4submit.processInputParams(['-c', testConfiguration.get('P4CLIENT'), '-u', self.p4server.p4api.user, '-p', self.p4server.p4api.port, 'submit', '100']) p4Test = p4submit.getP4Connection(args) p4Test.password = p4Test.user p4Test.run_login() with self.assertRaises(P4.P4Exception) as cm: p4submit.submitShelf(args) logger.debug(cm.exception) self.assertTrue('[Warning]: \'100 - no such changelist.\'' in str(cm.exception), 'Unexpected Exception: %s' % str(cm.exception)) p4Test.run_logout() p4Test.disconnect() self.assertClientSpecs() if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main()