def test_create_concurrent(self): o = offer.Offer(self.context, **self.fake_offer) o2 = offer.Offer(self.context, **self.fake_offer) o2.id = 28 with mock.patch.object(self.db_api, 'offer_create', autospec=True) as mock_offer_create: with mock.patch.object(self.db_api, 'offer_verify_resource_availability', autospec=True) as mock_ovra: def update_mock(updates): mock_ovra.side_effect = Exception("bad") mock_offer_create.side_effect = update_mock thread = threading.Thread(target=o.create) thread2 = threading.Thread(target=o2.create) thread.start() thread2.start() thread.join() thread2.join() assert mock_ovra.call_count == 2 mock_offer_create.assert_called_once()
def setUp(self): super(TestOffersController, self).setUp() start = datetime.datetime(2016, 7, 16) self.test_offer = offer.Offer(resource_type='test_node', resource_uuid=uuidutils.generate_uuid(), name='test_offer', uuid=uuidutils.generate_uuid(), status=statuses.AVAILABLE, start_time=start, end_time=start + datetime.timedelta(days=100), project_id=self.context.project_id, parent_lease_uuid=None) self.test_offer_drt = offer.Offer( resource_type='ironic_node', resource_uuid=uuidutils.generate_uuid(), name='test_offer', uuid=uuidutils.generate_uuid(), status=statuses.AVAILABLE, start_time=start, end_time=start + datetime.timedelta(days=100), project_id=self.context.project_id, parent_lease_uuid=None) self.test_offer_lessee = offer.Offer( resource_type='test_node', resource_uuid=uuidutils.generate_uuid(), name='test_offer', uuid=uuidutils.generate_uuid(), lessee_id='lessee-uuid', status=statuses.AVAILABLE, start_time=start, end_time=start + datetime.timedelta(days=100), project_id=self.context.project_id, parent_lease_uuid=None) self.test_offer_2 = offer.Offer( resource_type='test_node', resource_uuid=uuidutils.generate_uuid(), name='test_offer2', uuid=uuidutils.generate_uuid(), status=statuses.DELETED, start_time=start, end_time=start + datetime.timedelta(days=100), project_id=self.context.project_id, parent_lease_uuid=None) self.test_offer_with_parent = offer.Offer( resource_type='test_node', resource_uuid=uuidutils.generate_uuid(), name='test_offer', uuid=uuidutils.generate_uuid(), status=statuses.AVAILABLE, start_time=start, end_time=start + datetime.timedelta(days=100), project_id=self.context.project_id, parent_lease_uuid='parent-lease-uuid')
def test_get_availabilities_offer_in_future(self, mock_datetime, mock_ogct): o = offer.Offer(self.context, **self.test_offer_data) # test offer start_time > now mock_datetime.datetime.now = mock.Mock( return_value=o.start_time + datetime.timedelta(days=-5)) mock_ogct.return_value = [ [o.start_time + datetime.timedelta(days=10), o.start_time + datetime.timedelta(days=20)], [o.start_time + datetime.timedelta(days=20), o.start_time + datetime.timedelta(days=30)], [o.start_time + datetime.timedelta(days=50), o.start_time + datetime.timedelta(days=60)] ] expect = [ [o.start_time, o.start_time + datetime.timedelta(days=10)], [o.start_time + datetime.timedelta(days=30), o.start_time + datetime.timedelta(days=50)], [o.start_time + datetime.timedelta(days=60), o.end_time] ] a = o.get_availabilities() self.assertEqual(a, expect) mock_ogct.return_value = [[o.start_time, o.end_time]] expect = [] a = o.get_availabilities() self.assertEqual(a, expect) mock_ogct.return_value = [] expect = [[o.start_time, o.end_time]] a = o.get_availabilities() self.assertEqual(a, expect)
def create_test_offer(context): o = offer.Offer(resource_type='test_node', resource_uuid='1234567890', start_date=datetime.datetime(2016, 7, 16, 19, 20, 30), end_date=datetime.datetime(2016, 8, 16, 19, 20, 30)) o.create(context) return o
def post(self, new_offer): request = pecan.request.context cdict = request.to_policy_values() policy.authorize('esi_leap:offer:create', cdict, cdict) offer_dict = new_offer.to_dict() offer_dict['project_id'] = request.project_id OffersController._verify_resource_permission(cdict, offer_dict) offer_dict['uuid'] = uuidutils.generate_uuid() if 'start_time' not in offer_dict: offer_dict['start_time'] = datetime.datetime.now() if 'end_time' not in offer_dict: offer_dict['end_time'] = datetime.datetime.max if offer_dict['start_time'] >= offer_dict['end_time']: raise exception.\ InvalidTimeRange(resource="an offer", start_time=str(offer_dict['start_time']), end_time=str(offer_dict['end_time'])) o = offer.Offer(**offer_dict) o.create(request) o = OffersController._add_offer_availabilities(o) return Offer(**o)
def test_get_availabilities_conflicts_current(self, mock_datetime, mock_ogct): o = offer.Offer(self.context, **self.test_offer_data) # test a conflict is happening right now now = o.start_time + datetime.timedelta(days=10) mock_datetime.datetime.now = mock.Mock(return_value=now) mock_ogct.return_value = [ [o.start_time, o.start_time + datetime.timedelta(days=4)], [o.start_time + datetime.timedelta(days=5), o.start_time + datetime.timedelta(days=15)], [o.start_time + datetime.timedelta(days=20), o.start_time + datetime.timedelta(days=30)], [o.start_time + datetime.timedelta(days=50), o.start_time + datetime.timedelta(days=60)] ] expect = [ [o.start_time + datetime.timedelta(days=15), o.start_time + datetime.timedelta(days=20)], [o.start_time + datetime.timedelta(days=30), o.start_time + datetime.timedelta(days=50)], [o.start_time + datetime.timedelta(days=60), o.end_time] ] a = o.get_availabilities() self.assertEqual(a, expect)
def test_offer_get_dict_with_added_info(self, mock_get_availabilities, mock_gpn): mock_get_availabilities.return_value = [] mock_gpn.return_value = 'project-name' start = datetime.datetime(2016, 7, 16) o = offer.Offer(resource_type='test_node', resource_uuid='1234567890', name='o', status=statuses.AVAILABLE, start_time=start, end_time=start + datetime.timedelta(days=100), project_id=uuidutils.generate_uuid(), lessee_id=None) o_dict = utils.offer_get_dict_with_added_info(o) expected_offer_dict = { 'resource_type': o.resource_type, 'resource_uuid': o.resource_uuid, 'resource_class': 'fake', 'resource': 'test-node-1234567890', 'name': o.name, 'project_id': o.project_id, 'project': 'project-name', 'lessee_id': None, 'lessee': 'project-name', 'start_time': o.start_time, 'end_time': o.end_time, 'status': o.status, 'availabilities': [], } self.assertEqual(expected_offer_dict, o_dict) self.assertEqual(2, mock_gpn.call_count)
def test__add_offer_availabilities(self, mock_get_availabilities): mock_get_availabilities.return_value = [] o = offer.Offer( resource_type='test_node', resource_uuid='1234567890', name="o", status=statuses.AVAILABLE, start_time=start, end_time=end, project_id=owner_ctx.project_id ) o_dict = OffersController._add_offer_availabilities(o) expected_offer_dict = { 'resource_type': o.resource_type, 'resource_uuid': o.resource_uuid, 'name': o.name, 'project_id': o.project_id, 'start_time': o.start_time, 'end_time': o.end_time, 'status': o.status, 'availabilities': [], } self.assertEqual(o_dict, expected_offer_dict)
def test_send_to_flocx_market(self): o = offer.Offer(self.context, **self.fake_offer) with mock.patch.object(flocx_market_client.FlocxMarketClient, 'send_offer', autospec=True) as mock_send_offer: mock_send_offer.return_value = 201 res_status_code = o.send_to_flocx_market() mock_send_offer.assert_called_once() self.assertEqual(res_status_code, 201)
def setUp(self): super(TestListOffers, self).setUp() self.test_offer = offer.Offer(resource_type='test_node', resource_uuid='1234567890', start_time=start, end_time=end, project_id=owner_ctx.project_id)
def post(self, new_offer): request = pecan.request.context cdict = request.to_policy_values() policy.authorize('esi_leap:offer:create', cdict, cdict) o = offer.Offer(**new_offer.to_dict()) o.create(request) return Offer(**o.to_dict())
def test_destroy(self): o = offer.Offer(self.context, **self.fake_offer) with mock.patch.object(self.db_api, 'offer_destroy', autospec=True) as mock_offer_cancel: o.destroy() mock_offer_cancel.assert_called_once_with(o.uuid)
def create_test_offer(context): o = offer.Offer(resource_type='test_node', resource_uuid='1234567890', uuid='aaaaaaaa', start_time=datetime.datetime(2016, 7, 16, 19, 20, 30), end_time=datetime.datetime(2016, 8, 16, 19, 20, 30), project_id="111111111111") o.create(context) return o
def test_create(self): o = offer.Offer(self.context, **self.fake_offer) with mock.patch.object(self.db_api, 'offer_create', autospec=True) as mock_offer_create: mock_offer_create.return_value = get_test_offer() o.create(self.context) mock_offer_create.assert_called_once_with(get_test_offer())
def test_create(self, mock_oc, mock_rva): o = offer.Offer(self.context, **self.test_offer_create_data) mock_oc.return_value = self.test_offer_data o.create(self.context) mock_rva.assert_called_once_with(o.resource_type, o.resource_uuid, o.start_time, o.end_time) mock_oc.assert_called_once_with(self.test_offer_create_data)
def test_create_concurrent(self, mock_oc, mock_rva): o = offer.Offer(self.context, **self.test_offer_create_data) o2 = offer.Offer(self.context, **self.test_offer_create_data) o2.id = 28 def update_mock(updates): mock_rva.side_effect = Exception('bad') mock_oc.side_effect = update_mock thread = threading.Thread(target=o.create) thread2 = threading.Thread(target=o2.create) thread.start() thread2.start() thread.join() thread2.join() self.assertEqual(mock_rva.call_count, 2) mock_oc.assert_called_once()
def test_create(self): o = offer.Offer(self.context, **self.fake_offer) with mock.patch.object(self.db_api, 'offer_create', autospec=True) as mock_offer_create,\ mock.patch.object(offer.Offer, 'send_to_flocx_market', autospec=True) as mock_offer_send: mock_offer_create.return_value = get_test_offer() mock_offer_send.return_value = 201 o.create(self.context) mock_offer_create.assert_called_once_with(self.context, get_test_offer())
def test_create_with_parent_lease_expired(self, mock_oc, mock_lg, mock_lvca): o = offer.Offer(self.context, **self.test_offer_create_parent_lease_data) mock_lg.return_value = self.test_parent_lease_expired mock_oc.return_value = self.test_offer_data self.assertRaises(exception.LeaseNotActive, o.create, self.context) mock_lg.assert_called_once_with('parent-lease-uuid') mock_lvca.assert_not_called() mock_oc.assert_not_called()
def test_create(self): o = offer.Offer(self.context, **self.fake_offer) with mock.patch.object(self.db_api, 'offer_create', autospec=True) as mock_offer_create: with mock.patch( 'esi_leap.objects.offer.uuidutils.generate_uuid') \ as mock_uuid: mock_uuid.return_value = '534653c9-880d-4c2d-6d6d-' \ '11111111111' mock_offer_create.return_value = get_test_offer() o.create(self.context) mock_offer_create.assert_called_once_with(get_test_offer())
def post(self, new_offer): request = pecan.request.context cdict = request.to_policy_values() policy.authorize('esi_leap:offer:create', cdict, cdict) offer_dict = new_offer.to_dict() offer_dict['project_id'] = request.project_id OffersController._verify_resource_permission(cdict, offer_dict) o = offer.Offer(**offer_dict) o.create(request) o = OffersController._add_offer_availabilities(o) return Offer(**o)
def test_create_with_parent_lease(self, mock_oc, mock_lg, mock_lvca): o = offer.Offer(self.context, **self.test_offer_create_parent_lease_data) mock_lg.return_value = self.test_parent_lease mock_oc.return_value = self.test_offer_data o.create(self.context) mock_lg.assert_called_once_with('parent-lease-uuid') mock_lvca.assert_called_once_with(self.test_parent_lease, o.start_time, o.end_time) mock_oc.assert_called_once_with( self.test_offer_create_parent_lease_data)
def test_get_availabilities_conflicts_past(self, mock_datetime, mock_ogct): o = offer.Offer(self.context, **self.test_offer_data) # test all conflicts happened in the past now = o.start_time + datetime.timedelta(days=10) mock_datetime.datetime.now = mock.Mock(return_value=now) mock_ogct.return_value = [ [o.start_time, o.start_time + datetime.timedelta(days=4)], [o.start_time + datetime.timedelta(days=5), o.start_time + datetime.timedelta(days=10)] ] expect = [[now, o.end_time]] a = o.get_availabilities() self.assertEqual(a, expect)
def test_check_resource_admin_admin(self, mock_authorize, mock_ra): mock_ra.return_value = owner_ctx_2.project_id bad_test_offer = offer.Offer(resource_type='test_node', resource_uuid=test_node_2._uuid, project_id=owner_ctx.project_id) utils.check_resource_admin(admin_ctx.to_policy_values(), test_node_2, bad_test_offer.project_id) mock_ra.assert_called_once() mock_authorize.assert_called_once_with('esi_leap:offer:offer_admin', admin_ctx.to_policy_values(), admin_ctx.to_policy_values(), 'test_node', test_node_2._uuid)
def test_create_invalid_time(self): start = self.test_offer_data['start_time'] bad_offer = { 'name': 'o', 'project_id': '0wn5r', 'resource_type': 'dummy_node', 'resource_uuid': '1718', 'start_time': start + datetime.timedelta(days=100), 'end_time': start, 'status': statuses.AVAILABLE, 'properties': {'floor_price': 3}, } o = offer.Offer(self.context, **bad_offer) self.assertRaises(exception.InvalidTimeRange, o.create)
def test_get_availabilities(self): o = offer.Offer(self.context, **self.fake_offer) with mock.patch.object(self.db_api, 'offer_get_conflict_times', autospec=True) as mock_offer_get_conflict_times: mock_offer_get_conflict_times.return_value = [ [ o.start_time + datetime.timedelta(days=10), o.start_time + datetime.timedelta(days=20) ], [ o.start_time + datetime.timedelta(days=20), o.start_time + datetime.timedelta(days=30) ], [ o.start_time + datetime.timedelta(days=50), o.start_time + datetime.timedelta(days=60) ] ] expect = [ [o.start_time, o.start_time + datetime.timedelta(days=10)], [ o.start_time + datetime.timedelta(days=30), o.start_time + datetime.timedelta(days=50) ], [o.start_time + datetime.timedelta(days=60), o.end_time], ] a = o.get_availabilities() self.assertEqual(a, expect) mock_offer_get_conflict_times.return_value = [ [o.start_time, o.end_time], ] expect = [] a = o.get_availabilities() self.assertEqual(a, expect) mock_offer_get_conflict_times.return_value = [] expect = [ [o.start_time, o.end_time], ] a = o.get_availabilities() self.assertEqual(a, expect)
def test__verify_resource_permission_admin(self, mock_gro, mock_authorize, mock_is_resource_admin): bad_test_offer = offer.Offer(resource_type='test_node', resource_uuid=test_node_2._uuid, project_id=owner_ctx.project_id) assert OffersController._verify_resource_permission( admin_ctx.to_policy_values(), bad_test_offer.to_dict()) is None mock_gro.assert_called_once_with(bad_test_offer.resource_type, bad_test_offer.resource_uuid) mock_is_resource_admin.assert_called_once_with( bad_test_offer.project_id) mock_authorize.assert_called_once_with('esi_leap:offer:offer_admin', admin_ctx.to_policy_values(), admin_ctx.to_policy_values())
def post(self, new_offer): request = pecan.request.context cdict = request.to_policy_values() utils.policy_authorize('esi_leap:offer:create', cdict, cdict) offer_dict = new_offer.to_dict() offer_dict['project_id'] = request.project_id offer_dict['uuid'] = uuidutils.generate_uuid() if 'resource_type' not in offer_dict: offer_dict['resource_type'] = CONF.api.default_resource_type resource = get_resource_object(offer_dict['resource_type'], offer_dict['resource_uuid']) offer_dict['resource_uuid'] = resource.get_resource_uuid() if 'lessee_id' in offer_dict: offer_dict['lessee_id'] = keystone.get_project_uuid_from_ident( offer_dict['lessee_id']) if 'start_time' not in offer_dict: offer_dict['start_time'] = datetime.datetime.now() if 'end_time' not in offer_dict: offer_dict['end_time'] = datetime.datetime.max if offer_dict['start_time'] >= offer_dict['end_time']: raise exception.InvalidTimeRange( resource='an offer', start_time=str(offer_dict['start_time']), end_time=str(offer_dict['end_time'])) try: utils.check_resource_admin(cdict, resource, request.project_id) except exception.HTTPResourceForbidden: parent_lease_uuid = utils.check_resource_lease_admin( cdict, resource, request.project_id, offer_dict.get('start_time'), offer_dict.get('end_time')) if parent_lease_uuid is None: raise offer_dict['parent_lease_uuid'] = parent_lease_uuid o = offer_obj.Offer(**offer_dict) o.create() return Offer(**utils.offer_get_dict_with_added_info(o))
def test_save(self, mock_offer_update): o = offer.Offer(self.context, **self.test_offer_data) new_status = statuses.DELETED updated_at = datetime.datetime(2006, 12, 11, 0, 0) updated_offer = self.test_offer_data.copy() updated_offer['status'] = new_status updated_offer['updated_at'] = updated_at mock_offer_update.return_value = updated_offer o.status = new_status o.save(self.context) updated_values = self.test_offer_data.copy() updated_values['status'] = new_status mock_offer_update.assert_called_once_with(o.uuid, updated_values) self.assertEqual(self.context, o._context) self.assertEqual(updated_at, o.updated_at)
def test_save(self): o = offer.Offer(self.context, **self.fake_offer) new_status = statuses.CANCELLED updated_at = datetime.datetime(2006, 12, 11, 0, 0) with mock.patch.object(self.db_api, 'offer_update', autospec=True) as mock_offer_update: updated_offer = get_test_offer() updated_offer['status'] = new_status updated_offer['updated_at'] = updated_at mock_offer_update.return_value = updated_offer o.status = new_status o.save(self.context) updated_values = get_test_offer() updated_values['status'] = new_status mock_offer_update.assert_called_once_with(o.uuid, updated_values) self.assertEqual(self.context, o._context) self.assertEqual(updated_at, o.updated_at)
def test_check_resource_admin_invalid_owner(self, mock_authorize, mock_ra): mock_ra.return_value = owner_ctx_2.project_id mock_authorize.side_effect = exception.HTTPResourceForbidden( resource_type='test_node', resource=test_node_2._uuid) bad_test_offer = offer.Offer(resource_type='test_node', resource_uuid=test_node_2._uuid, project_id=owner_ctx.project_id) self.assertRaises(exception.HTTPResourceForbidden, utils.check_resource_admin, owner_ctx_2.to_policy_values(), test_node_2, bad_test_offer.project_id) mock_ra.assert_called_once() mock_authorize.assert_called_once_with('esi_leap:offer:offer_admin', owner_ctx_2.to_policy_values(), owner_ctx_2.to_policy_values(), 'test_node', test_node_2._uuid)