def test_simple_clone_success(self): CALL_ID_CREATE = 0 CALL_ID_CLONE = 1 repository = dict(self.repo_server_data) repository['status'] = 'CREATED' self.conn.set_responses( Response.with_json(status_code=201, json={'repository': repository}), Response.ok()) sap.rest.gcts.simple_clone(self.conn, self.repo_url, self.repo_name, vcs_token='THE_TOKEN') data = dict(self.repo_data) data['config'] = [{ 'key': 'VCS_TARGET_DIR', 'value': 'src/' }, { 'key': 'CLIENT_VCS_AUTH_TOKEN', 'value': 'THE_TOKEN' }] request_load = {'repository': self.repo_name, 'data': data} self.assertEqual(len(self.conn.execs), 2) self.conn.execs[CALL_ID_CREATE].assertEqual(Request.post_json( uri='repository', body=request_load, accept='application/json'), self, json_body=True) self.conn.execs[CALL_ID_CLONE].assertEqual( Request.post(uri=f'repository/{self.repo_name}/clone'), self)
def test_commit_transport_full(self): repo_name = 'the_repo' corrnr = 'CORRNR' message = 'Message' description = 'Description' commit_cmd = self.commit_cmd(repo_name, corrnr, '-m', message, '--description', description) commit_cmd.execute(self.fake_connection, commit_cmd) self.fake_connection.execs[0].assertEqual( Request.post_json(uri=f'repository/{repo_name}/commit', body={ 'message': message, 'autoPush': 'true', 'objects': [{ 'object': corrnr, 'type': 'TRANSPORT' }], 'description': description }), self) self.assertConsoleContents( self.console, stdout=f'''The transport "{corrnr}" has been committed\n''')
def test_simple_clone_without_params_create_exists_continue_cloned(self): CALL_ID_FETCH_REPO_DATA = 1 log_builder = LogBuilder() log_builder.log_error( make_gcts_log_error( '20200923111743: Error action CREATE_REPOSITORY Repository already exists' )) log_builder.log_exception('Cannot create', 'EEXIST').get_contents() messages = log_builder.get_contents() self.assertEqual(self.repo_server_data['status'], 'READY') self.conn.set_responses([ Response.with_json(status_code=500, json=messages), Response.with_json(status_code=200, json={'result': self.repo_server_data}), ]) repo = sap.rest.gcts.simple_clone(self.conn, self.repo_url, self.repo_name, error_exists=False) self.assertIsNotNone(repo) self.assertEqual(len(self.conn.execs), 2) self.conn.execs[CALL_ID_FETCH_REPO_DATA].assertEqual( Request.get_json(uri=f'repository/{self.repo_name}'), self)
def test_service_published(self): connection = sample_connection_ok() service_name = 'TEST_SERVICE_2' binding_name = 'TEST_BINDING' binding = sap.adt.businessservice.ServiceBinding(connection, binding_name) binding.fetch() status = binding.publish(binding.services[1]) connection.execs[1].assertEqual( Request.post( uri='/sap/bc/adt/businessservices/odatav2/publishjobs', headers={ 'Accept': 'application/xml, application/vnd.sap.as+xml;charset=UTF-8;dataname=com.sap.adt.StatusMessage', 'Content-Type': 'application/xml' }, params={ 'servicename': 'TEST_BINDING', 'serviceversion': '0002' }, body=SAMPLE_BINDING_OBJECT_REFERENCE ), self ) self.assertEqual(status.SEVERITY, "OK") self.assertEqual(status.SHORT_TEXT, "Local Service Endpoint of service TEST_SERVICE_2 with version 0002 is activated locally") self.assertEqual(status.LONG_TEXT, "")
def test_get_config_no_key_ok(self): self.conn.set_responses( Response.with_json(status_code=200, json={'result': { 'value': 'the value' }})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) # This will fetch the configruation key value from the server value = repo.get_config('THE_KEY') self.assertEqual(value, 'the value') # The second request does not causes an HTTP request value = repo.get_config('THE_KEY') self.assertEqual(value, 'the value') # The update of keys did not break the cache value = repo.get_config('VCS_CONNECTION') self.assertEqual(value, 'SSL') self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual( Request.get_json( uri=f'repository/{self.repo_name}/config/THE_KEY'), self)
def test_delete_transport_recursive(self): self.transport.delete(recursive=True) self.assertEqual(self.connection.execs, [ Request.put(uri='/sap/bc/adt/cts/transportrequests/NPLK007001', body=f'''<?xml version="1.0" encoding="ASCII"?> <tm:root xmlns:tm="http://www.sap.com/cts/adt/tm" tm:number="NPLK007001" tm:useraction="removeobject"> <tm:request> <tm:abap_object tm:name="{self.object_1_1.name}" tm:obj_desc="{self.object_1_1.description}" tm:pgmid="{self.object_1_1.pgmid}" tm:type="{self.object_1_1.type}" tm:position="{self.object_1_1.position}"/> </tm:request> </tm:root>'''), Request.put(uri='/sap/bc/adt/cts/transportrequests/NPLK007001', body=f'''<?xml version="1.0" encoding="ASCII"?> <tm:root xmlns:tm="http://www.sap.com/cts/adt/tm" tm:number="NPLK007001" tm:useraction="removeobject"> <tm:request> <tm:abap_object tm:name="{self.object_1_2.name}" tm:obj_desc="{self.object_1_2.description}" tm:pgmid="{self.object_1_2.pgmid}" tm:type="{self.object_1_2.type}" tm:position="{self.object_1_2.position}"/> </tm:request> </tm:root>'''), Request.delete('/sap/bc/adt/cts/transportrequests/NPLK007001'), Request.put(uri='/sap/bc/adt/cts/transportrequests/NPLK007002', body=f'''<?xml version="1.0" encoding="ASCII"?> <tm:root xmlns:tm="http://www.sap.com/cts/adt/tm" tm:number="NPLK007002" tm:useraction="removeobject"> <tm:request> <tm:abap_object tm:name="{self.object_2_1.name}" tm:obj_desc="{self.object_2_1.description}" tm:pgmid="{self.object_2_1.pgmid}" tm:type="{self.object_2_1.type}" tm:position="{self.object_2_1.position}"/> </tm:request> </tm:root>'''), Request.put(uri='/sap/bc/adt/cts/transportrequests/NPLK007002', body=f'''<?xml version="1.0" encoding="ASCII"?> <tm:root xmlns:tm="http://www.sap.com/cts/adt/tm" tm:number="NPLK007002" tm:useraction="removeobject"> <tm:request> <tm:abap_object tm:name="{self.object_2_2.name}" tm:obj_desc="{self.object_2_2.description}" tm:pgmid="{self.object_2_2.pgmid}" tm:type="{self.object_2_2.type}" tm:position="{self.object_2_2.position}"/> </tm:request> </tm:root>'''), Request.delete('/sap/bc/adt/cts/transportrequests/NPLK007002'), Request.delete('/sap/bc/adt/cts/transportrequests/NPLK007000'), ])
def setUp(self): self.variant = 'ACT_VARIANT' self.worklist_id = 'WORKLIST_ID' self.request_create_worklist = Request( method='POST', adt_uri='/sap/bc/adt/atc/worklists', params={'checkVariant': self.variant}, headers={'Accept': 'text/plain'}, body=None) self.request_run_worklist = Request( method='POST', adt_uri='/sap/bc/adt/atc/runs', params={'worklistId': self.worklist_id}, headers={ 'Accept': 'application/xml', 'Content-Type': 'application/xml' }, body=ADT_XML_ATC_RUN_REQUEST_PACKAGE) self.request_get_worklist = Request( method='GET', adt_uri=f'/sap/bc/adt/atc/worklists/{self.worklist_id}', params={'includeExemptedFindings': 'false'}, headers={'Accept': 'application/atc.worklist.v1+xml'}, body=None) self.conn = Connection([ Response(status_code=200, text=self.worklist_id, headers={'Content-Type': 'text/plain'}), Response(status_code=200, text=ADT_XML_ATC_RUN_RESPONSE_NO_OBJECTS, headers={'Content-Type': 'application/xml'}), Response( status_code=200, text=ADT_XML_ATC_WORKLIST_EMPTY, headers={'Content-Type': 'application/atc.worklist.v1+xml'}) ]) self.checks_runner = sap.adt.atc.ChecksRunner(self.conn, self.variant)
def test_clone_ok(self): repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) repo.clone() self.assertIsNone(repo._data) self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual( Request.post(uri=f'repository/{self.repo_name}/clone'), self)
def test_reassign_transport(self): self.transport.reassign('FILAK') self.maxDiff = None self.assertEqual(self.connection.execs, [ Request.put(uri='/sap/bc/adt/cts/transportrequests/NPLK007000', body=f'''<?xml version="1.0" encoding="ASCII"?> <tm:root xmlns:tm="http://www.sap.com/cts/adt/tm" tm:number="NPLK007000" tm:targetuser="******" tm:useraction="changeowner"/>''') ])
def test_not_json_response(self): req = Request(method='GET', adt_uri='/epic/success', headers=None, body=None, params=None) res = Response(status_code=401, text='Not JSON') orig_error = UnauthorizedError(req, res, 'foo') new_error = sap.rest.gcts.exception_from_http_error(orig_error) self.assertEqual(new_error, orig_error)
def test_fetch_inactive_objects(self): exp_request = Request.get(adt_uri='activation/inactiveobjects') conn = Connection(responses=[(RESPONSE_INACTIVE_OBJECTS_V1, exp_request)]) conn.asserter = self my_inactive_objects = sap.adt.wb.fetch_inactive_objects(conn) self.assertEquals( my_inactive_objects.entries[0].transport.reference.name, 'C50K000377') self.assertEquals(my_inactive_objects.entries[1].object.reference.name, 'CL_HELLO_WORLD')
def test_set_config_success(self): repo = sap.rest.gcts.Repository(self.conn, self.repo_name) repo.set_config('THE_KEY', 'the value') self.assertEqual(repo.get_config('THE_KEY'), 'the value') self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual(Request.post_json( uri=f'repository/{self.repo_name}/config', body={ 'key': 'THE_KEY', 'value': 'the value' }), self, json_body=True)
def test_get_transport_requests(self): connection = Connection([Response(SHORTENED_WORKBENCH_XML, 200, {})]) workbench = sap.adt.cts.Workbench(connection) transport = workbench.get_transport_requests(user='******') self.assertEqual(connection.execs, [ Request('GET', f'/sap/bc/adt/cts/transportrequests', { 'Accept': 'application/vnd.sap.adt.transportorganizer.v1+xml' }, None, sap.adt.cts.workbench_params('FILAK')) ]) self.assert_trasport_equal(transport[0], connection) self.assert_task_equal(transport[0].tasks[0], connection)
def test_delete_ok(self): repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) repo.delete() self.assertIsNone(repo._data) self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual( Request(method='DELETE', adt_uri=f'repository/{self.repo_name}', params=None, headers=None, body=None), self)
def test_repository_does_not_exist(self): messages = {'exception': 'No relation between system and repository'} req = Request(method='GET', adt_uri='/epic/success', headers=None, body=None, params=None) res = Response.with_json(status_code=500, json=messages) orig_error = HTTPRequestError(req, res) new_error = sap.rest.gcts.exception_from_http_error(orig_error) expected_error = sap.rest.gcts.GCTSRepoNotExistsError(messages) self.assertEqual(str(new_error), str(expected_error))
def test_properties_fetch(self): response = {'result': self.repo_server_data} self.conn.set_responses( [Response.with_json(json=response, status_code=200)]) repo = sap.rest.gcts.Repository(self.conn, self.repo_name) self.assertEqual(repo.rid, self.repo_server_data['rid']) self.assertEqual(repo.url, self.repo_server_data['url']) self.assertEqual(repo.branch, self.repo_server_data['branch']) self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual( Request.get_json(uri=f'repository/{self.repo_name}'), self)
def test_create_no_self_data_no_config(self): self.conn.set_responses( Response.with_json(status_code=201, json={'repository': self.repo_server_data})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name) repo.create(self.repo_url, self.repo_vsid) self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual(Request.post_json( uri=f'repository', body=self.repo_request, accept='application/json'), self, json_body=True)
def test_set_config_success_overwrite(self): repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) repo.set_config('VCS_CONNECTION', 'git') self.assertEqual(repo.get_config('VCS_CONNECTION'), 'git') self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual(Request.post_json( uri=f'repository/{self.repo_name}/config', body={ 'key': 'VCS_CONNECTION', 'value': 'git' }), self, json_body=True)
def test_create_with_config_update_instance(self): self.conn.set_responses( Response.with_json(status_code=201, json={'repository': self.repo_server_data})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data={ 'config': [{ 'key': 'first_key', 'value': 'first_value' }, { 'key': 'third_key', 'value': 'third_value' }] }) repo.create(self.repo_url, self.repo_vsid, config={ 'second_key': 'second_value', 'third_key': 'fourth_value' }) repo_request = dict(self.repo_request) repo_request['data']['config'] = [ { 'key': 'first_key', 'value': 'first_value' }, { 'key': 'third_key', 'value': 'fourth_value' }, { 'key': 'second_key', 'value': 'second_value' }, ] self.maxDiff = None self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual(Request.post_json( uri=f'repository', body=repo_request, accept='application/json'), self, json_body=True)
def test_get_config_no_config_ok(self): self.conn.set_responses( Response.with_json(status_code=200, json={'result': self.repo_server_data})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name) # This will fetch repo data from the server value = repo.get_config('VCS_CONNECTION') self.assertEqual(value, 'SSL') # The second request does not causes an HTTP request value = repo.get_config('VCS_CONNECTION') self.assertEqual(value, 'SSL') self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual( Request.get_json(uri=f'repository/{self.repo_name}'), self)
def test_log_ok(self): exp_commits = [{'id': '123'}] self.conn.set_responses( Response.with_json(status_code=200, json={'commits': exp_commits})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) act_commits = repo.log() self.assertIsNotNone(repo._data) self.assertEqual(act_commits, exp_commits) self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual( Request.get_json(uri=f'repository/{self.repo_name}/getCommit'), self)
def test_create_task(self): self.connection.set_responses( Response(status_code=201, text=TASK_CREATE_OK_RESPONSE)) self.task_1._number = None self.task_1._transport = self.transport.number self.task_1.create() self.maxDiff = None self.assertEqual(len(self.connection.execs), 1) self.connection.execs[0].assertEqual(Request.post_text( uri= f'/sap/bc/adt/cts/transportrequests/{self.task_1.transport}/tasks', accept='application/vnd.sap.adt.transportorganizer.v1+xml', body=f'''<?xml version="1.0" encoding="ASCII"?> <tm:root xmlns:tm="http://www.sap.com/cts/adt/tm" tm:number="{self.task_1.transport}" tm:targetuser="******" tm:useraction="newtask"/> '''), asserter=self) self.assertEqual(self.task_1.number, TASK_NUMBER)
def test_pull_no_log_commits(self): exp_log = {'fromCommit': '123', 'toCommit': '456'} self.conn.set_responses( Response.with_json(status_code=200, json={'commits': []}), Response.with_json(status_code=200, json=exp_log)) repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) act_log = repo.pull() self.assertIsNone(repo._data) self.assertEqual(act_log, exp_log) self.assertEqual(len(self.conn.execs), 2) self.conn.execs[1].assertEqual( Request.get_json(uri=f'repository/{self.repo_name}/pullByCommit'), self)
def test_create_with_role_and_type(self): self.conn.set_responses( Response.with_json(status_code=201, json={'repository': self.repo_server_data})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name) repo.create(self.repo_url, self.repo_vsid, role='TARGET', typ='GIT') repo_request = dict(self.repo_request) repo_request['data']['role'] = 'TARGET' repo_request['data']['type'] = 'GIT' self.maxDiff = None self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual(Request.post_json( uri=f'repository', body=repo_request, accept='application/json'), self, json_body=True)
def test_create_transport(self): self.connection.set_responses( Response(status_code=201, text=TRANSPORT_CREATE_OK_RESPONSE)) self.transport._number = None self.transport.create() self.maxDiff = None self.assertEqual(self.connection.execs, [ Request.post_text( uri='/sap/bc/adt/cts/transportrequests', accept='application/vnd.sap.adt.transportorganizer.v1+xml', body=f'''<?xml version="1.0" encoding="UTF-8"?> <tm:root xmlns:tm="http://www.sap.com/cts/adt/tm" tm:useraction="newrequest"> <tm:request tm:desc="{self.transport.description}" tm:type="K" tm:target="{self.transport.target}" tm:cts_project=""> <tm:task tm:owner="{self.transport.owner}"/> </tm:request> </tm:root> ''') ]) self.assertEqual(self.transport.number, TRANSPORT_NUMBER)
def test_create_with_config_instance_none(self): self.conn.set_responses( Response.with_json(status_code=201, json={'repository': self.repo_server_data})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name) repo.create(self.repo_url, self.repo_vsid, config={'THE_KEY': 'THE_VALUE'}) repo_request = dict(self.repo_request) repo_request['data']['config'] = [{ 'key': 'THE_KEY', 'value': 'THE_VALUE' }] self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual(Request.post_json( uri=f'repository', body=repo_request, accept='application/json'), self, json_body=True)
def do_check_release(self, factory): """Check it correctly builds the URL with parameters and returns the expected data. """ connection = Connection([Response(TASK_RELEASE_OK_RESPONSE, 200, {})]) wbr = factory(connection, TASK_NUMBER) resp = wbr.release() self.assertEqual(connection.execs, [ Request( 'POST', f'/sap/bc/adt/cts/transportrequests/{TASK_NUMBER}/newreleasejobs', { 'Accept': 'application/vnd.sap.adt.transportorganizer.v1+xml' }, None, None) ]) self.assertEqual(resp, TASK_RELEASE_OK_RESPONSE)
def test_simple_fetch_ok(self): REPO_ONE_ID = 0 repo_one = dict(self.repo_server_data) repo_one['name'] = repo_one['rid'] = 'one' REPO_TWO_ID = 1 repo_two = dict(self.repo_server_data) repo_two['name'] = repo_two['rid'] = 'two' self.conn.set_responses( Response.with_json(status_code=200, json={'result': [repo_one, repo_two]})) repos = sap.rest.gcts.simple_fetch_repos(self.conn) self.assertEqual(len(repos), 2) self.assertEqual(repos[REPO_ONE_ID].name, 'one') self.assertEqual(repos[REPO_TWO_ID].name, 'two') self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual(Request.get_json(uri=f'repository'), self)
def test_checkout_ok(self): self.conn.set_responses( Response.with_json( status_code=200, json={'result': { 'fromCommit': '123', 'toCommit': '456' }})) repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) repo.checkout('the_other_branch') self.assertIsNone(repo._data) self.assertEqual(len(self.conn.execs), 1) self.conn.execs[0].assertEqual( Request.get( adt_uri= f'repository/{self.repo_name}/branches/the_branch/switch', params={'branch': 'the_other_branch'}), self)
def test_activate(self): self.execute_definition_activate() self.assertConsoleContents(console=self.console, stdout=f'''Activating: * EXAMPLE_CONFIG_SRV Activation has finished Warnings: 0 Errors: 0 ''') self.connection.execs[0].assertEqual( Request.post_xml(uri='/sap/bc/adt/activation', accept='application/xml', params={ 'method': 'activate', 'preauditRequested': 'true' }, body='''<?xml version="1.0" encoding="UTF-8"?> <adtcore:objectReferences xmlns:adtcore="http://www.sap.com/adt/core"> <adtcore:objectReference adtcore:uri="/sap/bc/adt/ddic/srvd/sources/example_config_srv" adtcore:name="EXAMPLE_CONFIG_SRV"/> </adtcore:objectReferences>'''), self)