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_pull_error(self): messages = LogBuilder(exception='Pull Error').get_contents() self.conn.set_responses( Response.with_json(status_code=200, json={'commits': []}), Response.with_json(status_code=500, json=messages)) repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) with self.assertRaises(sap.rest.gcts.GCTSRequestError) as caught: repo.pull() self.assertIsNotNone(repo._data) self.assertEqual(str(caught.exception), 'gCTS exception: Pull Error')
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_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_simple_fetch_error(self): messages = LogBuilder(exception='Fetch Error').get_contents() self.conn.set_responses( Response.with_json(status_code=500, json=messages)) with self.assertRaises(sap.rest.gcts.GCTSRequestError) as caught: sap.rest.gcts.simple_fetch_repos(self.conn) self.assertEqual(str(caught.exception), 'gCTS exception: Fetch Error')
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_properties_fetch_error(self): messages = LogBuilder(exception='Get Repo Error').get_contents() self.conn.set_responses( Response.with_json(status_code=500, json=messages)) repo = sap.rest.gcts.Repository(self.conn, self.repo_name) with self.assertRaises(sap.rest.gcts.GCTSRequestError) as caught: unused = repo.rid self.assertEqual(str(caught.exception), 'gCTS exception: Get Repo Error')
def test_set_config_error(self): messages = LogBuilder(exception='Set Config Error').get_contents() self.conn.set_responses( Response.with_json(status_code=500, json=messages)) repo = sap.rest.gcts.Repository(self.conn, self.repo_name) with self.assertRaises(sap.rest.gcts.GCTSRequestError) as caught: repo.set_config('THE_KEY', 'the value') self.assertEqual(str(caught.exception), 'gCTS exception: Set Config Error')
def test_checkout_error(self): messages = LogBuilder(exception='Checkout Error').get_contents() self.conn.set_responses( Response.with_json(status_code=500, json=messages)) repo = sap.rest.gcts.Repository(self.conn, self.repo_name, data=self.repo_server_data) with self.assertRaises(sap.rest.gcts.GCTSRequestError) as caught: repo.checkout('the_other_branch') self.assertIsNotNone(repo._data) self.assertEqual(str(caught.exception), 'gCTS exception: Checkout Error')
def test_simple_clone_without_params_create_fail(self): log_builder = LogBuilder() messages = log_builder.log_error( make_gcts_log_error('Failure')).log_exception( 'Message', 'EERROR').get_contents() self.conn.set_responses( [Response.with_json(status_code=500, json=messages)]) with self.assertRaises(sap.rest.gcts.GCTSRequestError) as caught: sap.rest.gcts.simple_clone(self.conn, self.repo_url, self.repo_name) self.assertEqual(str(caught.exception), 'gCTS exception: Message')
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_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_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_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_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_simple_clone_without_params_create_exists(self): 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.conn.set_responses( [Response.with_json(status_code=500, json=messages)]) with self.assertRaises( sap.rest.gcts.GCTSRepoAlreadyExistsError) as caught: sap.rest.gcts.simple_clone(self.conn, self.repo_url, self.repo_name) self.assertEqual(str(caught.exception), 'gCTS exception: Cannot create')
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 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)
from mock import Response GCTS_RESPONSE_REPO_NOT_EXISTS = Response.with_json( status_code=500, json={'exception': 'No relation between system and repository'})
def test_simple_fetch_no_repo(self): self.conn.set_responses(Response.with_json(status_code=200, json={})) repos = sap.rest.gcts.simple_fetch_repos(self.conn) self.assertEqual(len(repos), 0)