def test_with_alternate_context(self): ctxt1 = context.RequestContext('foo', 'foo') ctxt2 = context.RequestContext('bar', tenant='alternate') obj = MyObj.query(ctxt1) obj.update_test(ctxt2) self.assertEqual('alternate-context', obj.bar) self.assertRemotes()
def test_neutron_port_update(self): opts = [{ 'opt_name': 'bootfile-name', 'opt_value': 'pxelinux.0' }, { 'opt_name': 'tftp-server', 'opt_value': '1.1.1.1' }, { 'opt_name': 'server-ip-address', 'opt_value': '1.1.1.1' }] port_id = 'fake-port-id' expected = {'port': {'extra_dhcp_opts': opts}} my_context = context.RequestContext(user='******', tenant='test-tenant') with mock.patch.object(client.Client, "__init__") as mock_client_init: mock_client_init.return_value = None api = dhcp_factory.DHCPFactory(token=my_context.auth_token) with mock.patch.object(client.Client, "update_port") as mock_update_port: mock_update_port.return_value = None api = dhcp_factory.DHCPFactory(token=my_context.auth_token) api.provider.update_port_dhcp_opts(port_id, opts) mock_update_port.assert_called_once_with(port_id, expected)
def start(self): super(PeriodicService, self).start() admin_context = context.RequestContext('admin', 'admin', is_admin=True) self.tg.add_dynamic_timer( self.manager.periodic_tasks, periodic_interval_max=cfg.CONF.periodic_interval, context=admin_context)
def test_download_with_retries(self): tries = [0] class MyGlanceStubClient(stubs.StubGlanceClient): """A client that fails the first time, then succeeds.""" def get(self, image_id): if tries[0] == 0: tries[0] = 1 raise exception.ServiceUnavailable('') else: return {} stub_client = MyGlanceStubClient() stub_context = context.RequestContext(auth_token=True) stub_context.user_id = 'fake' stub_context.project_id = 'fake' stub_service = service.Service(stub_client, 1, stub_context) image_id = 1 # doesn't matter writer = NullWriter() # When retries are disabled, we should get an exception self.config(glance_num_retries=0, group='glance') self.assertRaises(exception.GlanceConnectionFailed, stub_service.download, image_id, writer) # Now lets enable retries. No exception should happen now. tries = [0] self.config(glance_num_retries=1, group='glance') stub_service.download(image_id, writer)
def test_invalid_auth_strategy(self): self.config(auth_strategy='wrong_config', group='neutron') token = 'test-token-123' my_context = context.RequestContext(user='******', tenant='test-tenant', auth_token=token) self.assertRaises(exception.ConfigInvalid, neutron.NeutronAPI, my_context)
def test_neutron_address_update(self, mock_client_init, mock_update_port): address = 'fe:54:00:77:07:d9' port_id = 'fake-port-id' expected = {'port': {'mac_address': address}} my_context = context.RequestContext(user='******', tenant='test-tenant') mock_client_init.return_value = None api = dhcp_factory.DHCPFactory(token=my_context.auth_token) mock_update_port.return_value = None api.provider.update_port_address(port_id, address) mock_update_port.assert_called_once_with(port_id, expected)
def test_neutron_address_update(self, mock_client_init, mock_update_port): address = 'fe:54:00:77:07:d9' port_id = 'fake-port-id' expected = {'port': {'mac_address': address}} my_context = context.RequestContext(user='******', tenant='test-tenant') mock_client_init.return_value = None api = neutron.NeutronAPI(my_context) mock_update_port.return_value = None api.update_port_address(port_id, address) mock_update_port.assert_called_once_with(port_id, expected)
def test_neutron_address_update_with_exception(self, mock_client_init, mock_update_port): address = 'fe:54:00:77:07:d9' port_id = 'fake-port-id' my_context = context.RequestContext(user='******', tenant='test-tenant') mock_client_init.return_value = None api = neutron.NeutronAPI(my_context) mock_update_port.side_effect = ( neutron_client_exc.NeutronClientException()) self.assertRaises(exception.FailedToUpdateMacOnPort, api.update_port_address, port_id, address)
def test_neutron_port_update_with_execption(self): opts = [{}] port_id = 'fake-port-id' my_context = context.RequestContext(user='******', tenant='test-tenant') with mock.patch.object(client.Client, "__init__") as mock_client_init: mock_client_init.return_value = None api = neutron.NeutronAPI(my_context) with mock.patch.object(client.Client, "update_port") as mock_update_port: mock_update_port.side_effect = ( neutron_client_exc.NeutronClientException()) self.assertRaises(exception.FailedToUpdateDHCPOptOnPort, api.update_port_dhcp_opts, port_id, opts)
def test_create_noauth(self): self.config(auth_strategy='noauth', group='neutron') my_context = context.RequestContext() expected = { 'ca_cert': 'test-file', 'insecure': False, 'endpoint_url': 'test-url', 'timeout': 30, 'auth_strategy': 'noauth' } with mock.patch.object(client.Client, "__init__") as mock_client_init: mock_client_init.return_value = None neutron.NeutronDHCPApi(token=my_context.auth_token) mock_client_init.assert_called_once_with(**expected)
def start(self): super(RPCService, self).start() admin_context = context.RequestContext('admin', 'admin', is_admin=True) self.tg.add_dynamic_timer( self.manager.periodic_tasks, periodic_interval_max=cfg.CONF.periodic_interval, context=admin_context) self.manager.init_host() LOG.debug("Creating RPC server for service %s", self.topic) target = messaging.Target(topic=self.topic, server=self.host) endpoints = [self.manager] serializer = objects_base.IronicObjectSerializer() self.rpcserver = rpc.get_server(target, endpoints, serializer) self.rpcserver.start()
def before(self, state): user_id = state.request.headers.get('X-User-Id') user_id = state.request.headers.get('X-User', user_id) tenant = state.request.headers.get('X-Tenant-Id') tenant = state.request.headers.get('X-Tenant', tenant) auth_token = state.request.headers.get('X-Auth-Token', None) creds = {'roles': state.request.headers.get('X-Roles', '').split(',')} is_admin = policy.check('is_admin', state.request.headers, creds) state.request.context = context.RequestContext( auth_token=auth_token, user=user_id, tenant=tenant, request_id=context.generate_request_id(), is_admin=is_admin)
def test_client_httpnotfound_converts_to_imagenotfound(self): class MyGlanceStubClient(stubs.StubGlanceClient): """A client that raises a HTTPNotFound exception.""" def get(self, image_id): raise exception.HTTPNotFound(image_id) stub_client = MyGlanceStubClient() stub_context = context.RequestContext(auth_token=True) stub_context.user_id = 'fake' stub_context.project_id = 'fake' stub_service = service.Service(stub_client, 1, stub_context) image_id = 1 # doesn't matter writer = NullWriter() self.assertRaises(exception.ImageNotFound, stub_service.download, image_id, writer)
def setUp(self): super(TestGlanceSwiftTempURL, self).setUp() client = stubs.StubGlanceClient() self.context = context.RequestContext() self.service = service.Service(client, 2, self.context) self.config(swift_temp_url_key='correcthorsebatterystaple', group='glance') self.config(swift_endpoint_url='https://swift.example.com', group='glance') self.config(swift_account='AUTH_a422b2-91f3-2f46-74b7-d7c9e8958f5d30', group='glance') self.config(swift_api_version='v1', group='glance') self.config(swift_container='glance', group='glance') self.config(swift_temp_url_duration=1200, group='glance') self.config() self.fake_image = {'id': '757274c4-2856-4bd2-bb20-9a4a231e187b'}
def test_create_without_token(self): my_context = context.RequestContext(user='******', tenant='test-tenant') expected = {'timeout': 30, 'insecure': False, 'ca_cert': 'test-file', 'endpoint_url': 'test-url', 'username': '******', 'tenant_name': 'test-admin-tenant', 'password': '******', 'auth_url': 'test-auth-uri'} with mock.patch.object(client.Client, "__init__") as mock_client_init: mock_client_init.return_value = None neutron.NeutronAPI(my_context) mock_client_init.assert_called_once_with(**expected)
def test_create_with_token(self): token = 'test-token-123' my_context = context.RequestContext(user='******', tenant='test-tenant', auth_token=token) expected = {'timeout': 30, 'insecure': False, 'ca_cert': 'test-file', 'token': token, 'endpoint_url': 'test-url', 'auth_strategy': None} with mock.patch.object(client.Client, "__init__") as mock_client_init: mock_client_init.return_value = None neutron.NeutronAPI(my_context) mock_client_init.assert_called_once_with(**expected)
def setUp(self): super(TestGlanceImageService, self).setUp() client = stubs.StubGlanceClient() self.context = context.RequestContext(auth_token=True) self.context.user_id = 'fake' self.context.project_id = 'fake' self.service = service.Service(client, 1, self.context) self.config(glance_host='localhost', group='glance') try: self.config(auth_strategy='keystone', group='glance') except Exception: opts = [ cfg.StrOpt('auth_strategy', default='keystone'), ] CONF.register_opts(opts) return
def test_download_file_url(self): # NOTE: only in v2 API class MyGlanceStubClient(stubs.StubGlanceClient): """A client that returns a file url.""" (outfd, s_tmpfname) = tempfile.mkstemp(prefix='directURLsrc') outf = os.fdopen(outfd, 'w') inf = open('/dev/urandom', 'r') for i in range(10): _data = inf.read(1024) outf.write(_data) outf.close() def get(self, image_id): return type('GlanceTestDirectUrlMeta', (object, ), {'direct_url': 'file://%s' + self.s_tmpfname}) stub_context = context.RequestContext(auth_token=True) stub_context.user_id = 'fake' stub_context.project_id = 'fake' stub_client = MyGlanceStubClient() (outfd, tmpfname) = tempfile.mkstemp(prefix='directURLdst') writer = os.fdopen(outfd, 'w') stub_service = service.Service(stub_client, context=stub_context, version=2) image_id = 1 # doesn't matter self.config(allowed_direct_url_schemes=['file'], group='glance') stub_service.download(image_id, writer) writer.close() # compare the two files rc = filecmp.cmp(tmpfname, stub_client.s_tmpfname) self.assertTrue( rc, "The file %s and %s should be the same" % (tmpfname, stub_client.s_tmpfname)) os.remove(stub_client.s_tmpfname) os.remove(tmpfname)
def test__download_in_progress_wait(self): try: self.config(auth_strategy='keystone') except Exception: opts = [ cfg.StrOpt('auth_strategy', default='keystone'), ] CONF.register_opts(opts) ctx = context.RequestContext(auth_token=True) uuid = 'node_uuid' temp_dir = tempfile.mkdtemp() master_path = os.path.join(temp_dir, 'master_path') instance_path = os.path.join(temp_dir, 'instance_path') os.mkdir(master_path) os.mkdir(instance_path) lock_file = os.path.join(master_path, 'node_uuid.lock') open(lock_file, 'w').close() class handler_deploying(threading.Thread): def __init__(self, lock_file): threading.Thread.__init__(self) self.lock_file = lock_file def run(self): time.sleep(0.2) open(os.path.join(master_path, 'node_uuid'), 'w').close() pxe._remove_download_in_progress_lock(self.lock_file) handler = handler_deploying(lock_file) handler.start() pxe._get_image(ctx, os.path.join(instance_path, 'node_uuid'), uuid, master_path) self.assertFalse(os.path.exists(lock_file)) self.assertTrue( os.path.exists(os.path.join(instance_path, 'node_uuid'))) self.assertEqual( 2, os.stat(os.path.join(master_path, 'node_uuid')).st_nlink)
def start(self): super(RPCService, self).start() admin_context = context.RequestContext('admin', 'admin', is_admin=True) target = messaging.Target(topic=self.topic, server=self.host) endpoints = [self.manager] serializer = objects_base.IronicObjectSerializer() self.rpcserver = rpc.get_server(target, endpoints, serializer) self.rpcserver.start() self.handle_signal() self.manager.init_host() self.tg.add_dynamic_timer( self.manager.periodic_tasks, periodic_interval_max=cfg.CONF.periodic_interval, context=admin_context) LOG.info( _LI('Created RPC server for service %(service)s on host ' '%(host)s.'), { 'service': self.topic, 'host': self.host })