def test_invoke_with_function_alias(self, mock_create_execution, mock_create_context): exec_mock = mock_create_execution.return_value exec_mock.id = "fake_id" db_api.increase_function_version(self.func_id, 0) alias_name = self.rand_name(name="alias", prefix=self.prefix) body = { 'function_id': self.func_id, 'function_version': 1, 'name': alias_name } db_api.create_function_alias(**body) webhook = self.create_webhook(function_alias=alias_name) resp = self.app.post_json('/v1/webhooks/%s/invoke' % webhook.id, {}) context.set_ctx(self.ctx) self.assertEqual(202, resp.status_int) params = { 'function_id': self.func_id, 'function_version': 1, 'sync': False, 'input': json.dumps({}), 'description': constants.EXECUTION_BY_WEBHOOK % webhook.id } mock_create_execution.assert_called_once_with(mock.ANY, params)
def test_job_handler_with_alias(self, mock_next_time): e_client = mock.Mock() now = datetime.utcnow() # It doesn't matter what's the returned value, but need to be in # datetime type. mock_next_time.return_value = now + timedelta(seconds=1) # Create a alias for a function. alias_name = self.rand_name(name="alias", prefix=self.prefix) db_func = self.create_function() function_id = db_func.id db_api.create_function_alias(name=alias_name, function_id=function_id) self.create_job( function_alias=alias_name, status=status.RUNNING, next_execution_time=now, ) periodics.handle_job(e_client) context.set_ctx(self.ctx) # Create function version 1 and update the alias. db_api.increase_function_version(function_id, 0) db_api.update_function_alias(alias_name, function_version=1) periodics.handle_job(e_client) context.set_ctx(self.ctx) db_func = db_api.get_function(function_id) self.assertEqual(1, db_func.count) db_version = db_api.get_function_version(function_id, 1) self.assertEqual(1, db_version.count) db_execs = db_api.get_executions(function_id=function_id) self.assertEqual(2, len(db_execs))
def test_update_function_alias_1(self): # Create webhook using function alias db_api.increase_function_version(self.func_id, 0) name = self.rand_name(name="alias", prefix=self.prefix) body = { 'function_id': self.func_id, 'function_version': 1, 'name': name } db_api.create_function_alias(**body) webhook = self.create_webhook(function_alias=name) db_api.increase_function_version(self.func_id, 1) new_name = self.rand_name(name="alias", prefix=self.prefix) body = { 'function_id': self.func_id, 'function_version': 2, 'name': new_name } db_api.create_function_alias(**body) # Update webhook with the new alias resp = self.app.put_json('/v1/webhooks/%s' % webhook.id, {'function_alias': new_name}) self.assertEqual(200, resp.status_int) self.assertEqual(new_name, resp.json.get("function_alias")) self.assertIsNone(resp.json.get("function_id")) self.assertIsNone(resp.json.get("function_version"))
def test_delete_with_alias(self): db_func = self.create_function(runtime_id=self.runtime_id) func_id = db_func.id name = self.rand_name(name="alias", prefix=self.prefix) body = {'function_id': func_id, 'name': name} db_api.create_function_alias(**body) resp = self.app.delete('/v1/functions/%s' % func_id, expect_errors=True) self.assertEqual(403, resp.status_int)
def test_post_with_alias(self): db_api.increase_function_version(self.func_id, 0) name = self.rand_name(name="alias", prefix=self.prefix) body = { 'function_id': self.func_id, 'function_version': 1, 'name': name } db_api.create_function_alias(**body) webhook_body = {'function_alias': name, 'description': 'webhook test'} resp = self.app.post_json('/v1/webhooks', webhook_body) self.assertEqual(201, resp.status_int) self.assertEqual(1, resp.json.get("function_version"))
def test_delete_with_alias(self): db_api.increase_function_version(self.func_id, 0, description="version 1") name = self.rand_name(name="alias", prefix=self.prefix) body = { 'function_id': self.func_id, 'function_version': 1, 'name': name } db_api.create_function_alias(**body) resp = self.app.delete('/v1/functions/%s/versions/1' % self.func_id, expect_errors=True) self.assertEqual(403, resp.status_int)
def post(self, body): """Create a new alias for the specified function. The supported body params: - function_id: Required. Function id the alias points to. - name: Required. Alias name, must be unique within the project. - function_version: Optional. Version number the alias points to. - description: Optional. The description of the new alias. """ ctx = context.get_ctx() acl.enforce('function_alias:create', ctx) params = body.to_dict() if not POST_REQUIRED.issubset(set(params.keys())): raise exc.InputException( 'Required param is missing. Required: %s' % POST_REQUIRED ) LOG.info("Creating Alias, params: %s", params) values = { 'function_id': params.get('function_id'), 'name': params.get('name'), 'function_version': params.get('function_version'), 'description': params.get('description'), } alias = db_api.create_function_alias(**values) LOG.info("New alias created.") return resources.FunctionAlias.from_db_obj(alias)
def test_create_with_alias(self): name = self.rand_name(name="alias", prefix=self.prefix) body = {'function_id': self.function_id, 'name': name} db_api.create_function_alias(**body) job_body = { 'name': self.rand_name('job', prefix=self.prefix), 'first_execution_time': str(datetime.utcnow() + timedelta(hours=1)), 'function_alias': name } resp = self.app.post_json('/v1/jobs', job_body) self.assertEqual(201, resp.status_int) self.assertEqual(name, resp.json.get('function_alias')) self.assertIsNone(resp.json.get('function_id'))
def test_get(self): name = 'TestAlias' function_version = 0 body = { 'function_id': self.func_id, 'function_version': function_version, 'name': name, 'description': 'new alias' } db_api.create_function_alias(**body) resp = self.app.get('/v1/aliases/%s' % name) context.set_ctx(self.ctx) self.assertEqual(200, resp.status_int) self.assertEqual("new alias", resp.json.get('description'))
def test_get_all(self): name = self.rand_name(name="alias", prefix=self.prefix) body = {'function_id': self.func_id, 'name': name} db_api.create_function_alias(**body) resp = self.app.get('/v1/aliases') self.assertEqual(200, resp.status_int) expected = { "name": name, 'function_id': self.func_id, 'function_version': 0, "project_id": unit_base.DEFAULT_PROJECT_ID, } actual = self._assert_single_item(resp.json['function_aliases'], name=name) self._assertDictContainsSubset(actual, expected)
def test_put_without_optional_params(self): name = self.rand_name(name="alias", prefix=self.prefix) function_version = 1 body = { 'function_id': self.func_id, 'function_version': function_version, 'name': name, 'description': 'new alias' } db_api.create_function_alias(**body) update_body = {} resp = self.app.put_json('/v1/aliases/%s' % name, update_body) self.assertEqual(200, resp.status_int) self._assertDictContainsSubset(resp.json, body)
def test_delete(self): name = self.rand_name(name="alias", prefix=self.prefix) function_version = 0 body = { 'function_id': self.func_id, 'function_version': function_version, 'name': name, 'description': 'new alias' } db_api.create_function_alias(**body) resp = self.app.delete('/v1/aliases/%s' % name) self.assertEqual(204, resp.status_int) context.set_ctx(self.ctx) self.assertRaises(exc.DBEntityNotFoundError, db_api.get_function_alias, name)
def test_post_with_alias(self, mock_rpc): db_api.increase_function_version(self.func_id, 0, description="version 1") name = self.rand_name(name="alias", prefix=self.prefix) body = { 'function_id': self.func_id, 'function_version': 1, 'name': name } db_api.create_function_alias(**body) execution_body = {'function_alias': name} resp = self.app.post_json('/v1/executions', execution_body) self.assertEqual(201, resp.status_int) resp = self.app.get('/v1/functions/%s' % self.func_id) self.assertEqual(0, resp.json.get('count')) resp = self.app.get('/v1/functions/%s/versions/1' % self.func_id) self.assertEqual(1, resp.json.get('count'))