예제 #1
0
    def test_delete_with_running_job(self):
        db_api.increase_function_version(self.func_id, 0,
                                         description="version 1")
        self.create_job(
            self.func_id,
            function_version=1,
            status=status.RUNNING,
            first_execution_time=datetime.utcnow(),
            next_execution_time=datetime.utcnow() + timedelta(hours=1),
        )

        resp = self.app.delete(
            '/v1/functions/%s/versions/1' % self.func_id,
            expect_errors=True
        )

        self.assertEqual(403, resp.status_int)
예제 #2
0
    def test_create_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(name, resp.json.get('function_alias'))
        self.assertIsNone(resp.json.get("function_id"))
        self.assertIsNone(resp.json.get("function_version"))
예제 #3
0
    def test_delete(self, mock_package_delete, mock_engine_delete,
                    mock_etcd_delete):
        db_api.increase_function_version(self.func_id,
                                         0,
                                         description="version 1")

        resp = self.app.delete('/v1/functions/%s/versions/1' % self.func_id)

        self.assertEqual(204, resp.status_int)
        mock_engine_delete.assert_called_once_with(self.func_id, version=1)
        mock_etcd_delete.assert_called_once_with(self.func_id, version=1)
        mock_package_delete.assert_called_once_with(
            unit_base.DEFAULT_PROJECT_ID, self.func_id, None, version=1)

        # We need to set context as it was removed after the API call
        context.set_ctx(self.ctx)

        with db_api.transaction():
            func_db = db_api.get_function(self.func_id)
            self.assertEqual(0, len(func_db.versions))
            self.assertEqual(0, func_db.latest_version)
예제 #4
0
    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'))
예제 #5
0
    def test_update_function_alias_2(self):
        # Create webhook using function id
        db_api.increase_function_version(self.func_id, 0)
        webhook = self.create_webhook(function_id=self.func_id,
                                      function_version=1)

        db_api.increase_function_version(self.func_id, 1)
        alias_name = self.rand_name(name="alias", prefix=self.prefix)
        body = {
            'function_id': self.func_id,
            'function_version': 2,
            'name': alias_name
        }
        db_api.create_function_alias(**body)

        # Update webhook with function alias
        resp = self.app.put_json('/v1/webhooks/%s' % webhook.id,
                                 {'function_alias': alias_name})

        self.assertEqual(200, resp.status_int)
        self.assertEqual(alias_name, resp.json.get("function_alias"))
        self.assertIsNone(resp.json.get("function_id"))
        self.assertIsNone(resp.json.get("function_version"))
예제 #6
0
    def _create_function_version(self, project_id, function_id, **kwargs):
        with etcd_util.get_function_version_lock(function_id) as lock:
            if not lock.is_acquired():
                return False

            with db_api.transaction():
                # Get latest function package md5 and version number
                func_db = db_api.get_function(function_id, insecure=False)
                if func_db.code['source'] != constants.PACKAGE_FUNCTION:
                    raise exc.NotAllowedException(
                        "Function versioning only allowed for %s type "
                        "function." %
                        constants.PACKAGE_FUNCTION
                    )

                l_md5 = func_db.code['md5sum']
                l_version = func_db.latest_version

                if len(func_db.versions) >= constants.MAX_VERSION_NUMBER:
                    raise exc.NotAllowedException(
                        'Can not exceed maximum number(%s) of versions' %
                        constants.MAX_VERSION_NUMBER
                    )

                # Check if the latest package changed since last version
                changed = self.storage_provider.changed_since(project_id,
                                                              function_id,
                                                              l_md5,
                                                              l_version)
                if not changed:
                    raise exc.NotAllowedException(
                        'Function package not changed since the latest '
                        'version %s.' % l_version
                    )

                LOG.info("Creating %s, function_id: %s, old_version: %d",
                         self.type, function_id, l_version)

                # Create new version and copy package.
                self.storage_provider.copy(project_id, function_id, l_md5,
                                           l_version)
                version = db_api.increase_function_version(function_id,
                                                           l_version,
                                                           **kwargs)
                func_db.latest_version = l_version + 1

            LOG.info("New version %d for function %s created.", l_version + 1,
                     function_id)
            return version
예제 #7
0
    def test_job_handler_with_version(self, mock_next_time):
        db_func = self.create_function()
        function_id = db_func.id
        new_version = db_api.increase_function_version(function_id, 0)

        self.assertEqual(0, new_version.count)

        now = datetime.utcnow()
        db_job = self.create_job(
            function_id,
            function_version=1,
            status=status.RUNNING,
            next_execution_time=now,
            count=2
        )
        job_id = db_job.id

        e_client = mock.Mock()
        # 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)

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        db_job = db_api.get_job(job_id)
        self.assertEqual(1, db_job.count)
        db_func = db_api.get_function(function_id)
        self.assertEqual(0, 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,
                                         function_version=1)
        self.assertEqual(1, len(db_execs))

        periodics.handle_job(e_client)
        context.set_ctx(self.ctx)

        db_job = db_api.get_job(job_id)
        self.assertEqual(0, db_job.count)
        self.assertEqual(status.DONE, db_job.status)
        db_func = db_api.get_function(function_id)
        self.assertEqual(0, db_func.count)
        db_version = db_api.get_function_version(function_id, 1)
        self.assertEqual(2, db_version.count)
        db_execs = db_api.get_executions(function_id=function_id,
                                         function_version=1)
        self.assertEqual(2, len(db_execs))