def test_new_binding_non_existing_json_file(self): register_uri(POST, self.base_url + 'bindings', body=json.dumps(TestCli.RESPONSE_BINDING_CREATED), content_type='application/json') sys.argv = [ 'sd-cli', 'bindings', 'create', 'fake', 'fakeorigin', 'fake-not-existing.json' ] with patch('com.tdigital.sd.cli.cli._get_config') as get_config_mock,\ patch('com.tdigital.sd.cli.cli._format') as format_mock,\ patch('com.tdigital.sd.cli.cli.exists') as exists_mock,\ patch('__builtin__.print') as print_mock: # import doing here to use patch exists_mock.return_value = False get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count) print_mock.assert_has_calls([ call('[CLI Error]:'), call('Not existing file fake-not-existing.json') ])
def test_update_instance(self): register_uri(GET, self.base_url + 'classes/fake/instances/fake_id', body=json.dumps(TestCli.RESPONSE_INSTANCE_CREATED), content_type='application/json') register_uri(PUT, self.base_url + 'classes/fake/instances/fake_id', body=json.dumps(TestCli.RESPONSE_INSTANCE_CREATED), content_type='application/json') sys.argv = [ 'sd-cli', 'instances', 'update', 'fake', 'fake_id', 'version=v1.0' ] with patch('com.tdigital.sd.cli.cli._get_config') as get_config_mock,\ patch('com.tdigital.sd.cli.cli._format') as format_mock,\ patch('__builtin__.print') as print_mock: # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count) print_mock.assert_called_once_with('Updated instance: fake_id')
def test_update_instance_attrs(self): register_uri(GET, self.base_url + 'classes/fake/instances/fake_id', body=json.dumps(TestCli.RESPONSE_INSTANCE_CREATED), content_type='application/json') instance_upd = self.RESPONSE_INSTANCE_CREATED.copy() instance_upd['attributes']['protocol'] = 'https' register_uri(PUT, self.base_url + 'classes/fake/instances/fake_id', body=json.dumps(TestCli.RESPONSE_INSTANCE_CREATED), content_type='application/json') sys.argv = [ 'sd-cli', 'instances', 'update-attrs', 'fake', 'fake_id', 'protocol=v1.0' ] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count)
def test_update_binding_invalid_binding_should_print_generic_problem(self): register_uri(GET, self.base_url + 'bindings', body=json.dumps([{ 'origin': 'tugo' }]), content_type='application/json') register_uri(PUT, self.base_url + 'bindings/fake_id', body=json.dumps(TestCli.RESPONSE_BINDING_CREATED), content_type='application/json') sys.argv = [ 'sd-cli', 'bindings', 'update', 'fake', 'fakeorigin', 'fake.json', '--debug' ] with patch('com.tdigital.sd.cli.cli._get_config') as get_config_mock,\ patch('com.tdigital.sd.cli.cli._format') as format_mock,\ patch('__builtin__.open', mock_open(read_data=json.dumps(TestCli.RULES)), create=True) as file_mock,\ patch('com.tdigital.sd.cli.cli.exists') as exists_mock,\ patch('__builtin__.print') as print_mock: # import doing here to use patch exists_mock.return_value = True get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() file_mock.assert_called_once_with('fake.json', "r") self.assertTrue(print_mock.call_count >= 20) self.assertTrue( call( 'It seems that something goes wrong. Contact the operator for further assistance.' ) in print_mock.mock_calls)
def test_update_nonexisting_binding(self): register_uri(GET, self.base_url + 'bindings', status=200, body=[], content_type='application/json') sys.argv = [ 'sd-cli', 'bindings', 'update', 'fake', 'fakeorigin', 'fake.json' ] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format'), patch('__builtin__.open', mock_open(read_data=json.dumps(TestCli.RULES)), create=True), patch('com.tdigital.sd.cli.cli.exists')) as (get_config_mock, format_mock, file_mock, exists_mock): # import doing here to use patch exists_mock.return_value = True get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count) file_mock.assert_called_once_with('fake.json', "r")
def test_delete_binding(self): register_uri(GET, self.base_url + 'bindings', body=json.dumps([TestCli.RESPONSE_BINDING_CREATED]), content_type='application/json') register_uri(DELETE, self.base_url + 'bindings/fake_id', body=None, status=204, content_type='application/json') sys.argv = ['sd-cli', 'bindings', 'delete', 'fake', 'fakeorigin'] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count, 'Format should not be called')
def test_update_api(self): register_uri(POST, self.base_url + 'classes/fake', body=json.dumps(TestCli.RESPONSE_API_CREATED), status=200, content_type='application/json') register_uri(GET, self.base_url + 'classes/fake', body=json.dumps(TestCli.RESPONSE_API_CREATED), status=200, content_type='application/json') sys.argv = [ 'sd-cli', 'classes', 'update', 'fake', 'default_version=v1.0' ] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count)
def test_new_binding_invalid_object_json_file(self): register_uri(POST, self.base_url + 'bindings', body=json.dumps(TestCli.RESPONSE_BINDING_CREATED), content_type='application/json') sys.argv = [ 'sd-cli', 'bindings', 'create', 'fake', 'fakeorigin', 'fake-not-good.json' ] with patch('com.tdigital.sd.cli.cli._get_config') as get_config_mock,\ patch('com.tdigital.sd.cli.cli._format') as format_mock,\ patch('com.tdigital.sd.cli.cli.exists') as exists_mock,\ patch('__builtin__.open', mock_open(read_data='[{"origin": "origin"}]'), create=True) as file_mock,\ patch('__builtin__.print') as print_mock: # import doing here to use patch exists_mock.return_value = True get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() file_mock.assert_called_once_with('fake-not-good.json', "r") self.assertEquals(0, format_mock.call_count) print_mock.assert_has_calls([ call('[CLI Error]:'), call('Json file should be a valid object (not array or null)') ])
def test_show_default_config(self): sys.argv = ['sd-cli', '-s'] with patch('__builtin__.open', mock_open(read_data=json.dumps(TestCli.RULES)), create=True) as file_mock: cli.command() file_mock.assert_called_once_with(ANY) sys.argv = ['sd-cli', '--show-default-config'] with patch('__builtin__.open', mock_open(read_data=json.dumps(TestCli.RULES)), create=True) as file_mock: cli.command() file_mock.assert_called_once_with(ANY)
def test_find_apis(self): register_uri(GET, self.base_url + 'classes', body=json.dumps(list(TestCli.RESPONSE_API_CREATED)), status=200, content_type='application/json') sys.argv = ['sd-cli', 'classes', 'find'] with patch('com.tdigital.sd.cli.cli._get_config') as get_config_mock: # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } cli.command()
def test_find_apis_no_result(self): register_uri(GET, self.base_url + 'classes', body=json.dumps([]), status=200, content_type='application/json') sys.argv = ['sd-cli', 'classes', 'find'] with patch('com.tdigital.sd.cli.cli._get_config') as get_config_mock,\ patch('__builtin__.print') as print_mock: # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } cli.command() print_mock.assert_called_once_with( 'No class matching these filter criteria')
def test_get_binding(self): register_uri(GET, self.base_url + 'bindings', body=json.dumps([TestCli.RESPONSE_BINDING_CREATED]), content_type='application/json') sys.argv = ['sd-cli', 'bindings', 'get', 'fake', 'fakeorigin'] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() format_mock.assert_called_once_with(self.RESPONSE_BINDING_CREATED)
def test_get_binding_unexisting(self): register_uri(GET, self.base_url + 'bindings', body=json.dumps([]), content_type='application/json') sys.argv = ['sd-cli', 'bindings', 'get', 'fake_not', 'origin_not'] with patch('com.tdigital.sd.cli.cli._get_config') as get_config_mock,\ patch('com.tdigital.sd.cli.cli._format') as format_mock,\ patch('__builtin__.print') as print_mock: # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count) self.assertEquals(2, print_mock.call_count)
def test_delete_api(self): register_uri(DELETE, self.base_url + 'classes/fake', body=None, status=204, content_type='application/json') sys.argv = ['sd-cli', 'classes', 'delete', 'fake'] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count)
def test_delete_nonexisting_binding(self): register_uri(GET, self.base_url + 'bindings', body=[], content_type='application/json') sys.argv = ['sd-cli', 'bindings', 'delete', 'fake', 'fakeorigin'] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count, 'Format should not be called')
def test_new_instance(self): register_uri(POST, self.base_url + 'classes/fake/instances', body=json.dumps(TestCli.RESPONSE_INSTANCE_CREATED), content_type='application/json') sys.argv = [ 'sd-cli', 'instances', 'create', 'fake', 'v1.0', 'http://fake', 'dev', 'protocol=https', 'custom=fake' ] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch get_config_mock.return_value = { 'url': self.base_url, 'username': '******', 'password': '******' } format_mock.return_value = 'Format Mock' cli.command() self.assertEquals(0, format_mock.call_count)
def test_info(self): register_uri(GET, 'http://sd_fake.com/sd/info', body=json.dumps({'version': '1.0'}), status=200, content_type='application/json') sys.argv = ['sd-cli', 'info', '-d'] with nested( patch('com.tdigital.sd.cli.cli._get_config'), patch('com.tdigital.sd.cli.cli._format')) as (get_config_mock, format_mock): # import doing here to use patch config_dict = { 'url': self.base_url, 'username': '******', 'password': '******' } get_config_mock.return_value = config_dict format_mock.return_value = 'Format Mock' cli.command() format_mock.assert_has_calls( [call(config_dict), call({'version': '1.0'})])