def main(): """ The main function. It creates VmaaS application, servers, run everything.""" vmaas_app = Application() server = tornado.httpserver.HTTPServer(vmaas_app) server.bind(PUBLIC_API_PORT) num_servers = int(os.getenv("MAX_VMAAS_SERVERS", MAX_SERVERS)) server.start(num_servers) # start forking here init_logging(num_servers) LOGGER.info("Starting (version %s).", VMAAS_VERSION) # The rest stuff must be done only after forking BaseHandler.db_cache = Cache() BaseHandler.updates_api = UpdatesAPI(BaseHandler.db_cache) BaseHandler.repo_api = RepoAPI(BaseHandler.db_cache) BaseHandler.cve_api = CveAPI(BaseHandler.db_cache) BaseHandler.errata_api = ErrataAPI(BaseHandler.db_cache) BaseHandler.dbchange_api = DBChange(BaseHandler.db_cache) vmaas_app.websocket_reconnect() vmaas_app.reconnect_callback = PeriodicCallback( vmaas_app.websocket_reconnect, WEBSOCKET_RECONNECT_INTERVAL * 1000) vmaas_app.reconnect_callback.start() IOLoop.instance().start()
def load_cache_to_apis(): """Reload cache in APIs.""" BaseHandler.updates_api = UpdatesAPI(BaseHandler.db_cache) BaseHandler.repo_api = RepoAPI(BaseHandler.db_cache) BaseHandler.cve_api = CveAPI(BaseHandler.db_cache) BaseHandler.errata_api = ErrataAPI(BaseHandler.db_cache) BaseHandler.packages_api = PackagesAPI(BaseHandler.db_cache) BaseHandler.vulnerabilities_api = VulnerabilitiesAPI(BaseHandler.db_cache, BaseHandler.updates_api) BaseHandler.dbchange_api = DBChange(BaseHandler.db_cache)
def __init__(self): handlers = [ (r"/api/internal/refresh/?", RefreshHandler), # GET request (r"/api/v1/updates/?", UpdatesHandler), # POST request (r"/api/v1/updates/[a-zA-Z0-9-._:]+", UpdatesHandler), # GET request with package name (r"/api/v1/cves/?", CVEHandler), (r"/api/v1/cves/[a-zA-Z0-9*-]+", CVEHandler), (r"/api/v1/repos/?", ReposHandler), (r"/api/v1/repos/[a-zA-Z0-9*-_]+", ReposHandler), (r"/api/v1/errata/?", ErrataHandler), # POST request (r"/api/v1/errata/[a-zA-Z0-9*-:]+", ErrataHandler) # GET request ] cursor = Database().cursor() self.updatesapi = UpdatesAPI(cursor) self.cveapi = CveAPI(cursor) self.repoapi = RepoAPI(cursor) self.errataapi = ErrataAPI(cursor) tornado.web.Application.__init__(self, handlers)
def setup_api(self, load_cache): """Set RepoAPI object.""" self.repo = RepoAPI(self.cache)
class TestRepoAPI(TestBase): """Test RepoAPI class.""" repo = None # pylint: disable=unused-argument @pytest.fixture(autouse=True) def setup_api(self, load_cache): """Set RepoAPI object.""" self.repo = RepoAPI(self.cache) def test_regex(self): """Test correct repos regex.""" assert self.repo.find_repos_by_regex("rhel-7-server-rpms") == [ "rhel-7-server-rpms" ] assert "rhel-7-server-rpms" in self.repo.find_repos_by_regex( "rhel-[7].*") def test_wrong_regex(self): """Test wrong repos regex.""" with pytest.raises(Exception) as context: self.repo.find_repos_by_regex("*") assert "nothing to repeat" in str(context.value) def test_missing_required(self): """Test missing required property 'repository_list'.""" with pytest.raises(Exception) as context: self.repo.process_list(api_version="v1", data=REPO_JSON_BAD) assert "'repository_list' is a required property" in str(context.value) def test_empty_json(self): """Test repos API with empty JSON.""" with pytest.raises(Exception) as context: self.repo.process_list(api_version="v1", data=REPO_JSON_EMPTY) assert "'repository_list' is a required property" in str(context.value) def test_empty_repository_list(self): """Test repos API with empty 'repository_list'.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_EMPTY_LIST) assert response == EMPTY_RESPONSE def test_non_existing_repo(self): """Test repos API response for non-existent repo.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_NON_EXIST) assert response == EMPTY_RESPONSE def test_schema(self): """Test schema of valid repos API response.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON) assert schemas.repos_schema.validate(response) def test_modified_since(self): """Test repos API with 'modified_since' property.""" response = self.repo.process_list(api_version=1, data=REPO_JSON_MODIFIED_SINCE) assert tools.match(EMPTY_RESPONSE, response) is True def test_page_size(self): """Test repos API page size""" response = self.repo.process_list(api_version="v1", data=REPO_JSON) page_size = 0 for label in response['repository_list']: page_size += len(response['repository_list'][label]) assert response['page_size'] == page_size def test_page_size_mod_since(self): """Test repos API page size with 'modified_since' property""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_MODIFIED_SINCE) page_size = 0 for label in response['repository_list']: page_size += len(response['repository_list'][label]) assert response['page_size'] == page_size
class TestRepoAPI(TestBase): """Test RepoAPI class.""" repo = None # pylint: disable=unused-argument @pytest.fixture(autouse=True) def setup_api(self, load_cache): """Set RepoAPI object.""" self.repo = RepoAPI(self.cache) def test_regex(self): """Test correct repos regex.""" assert self.repo.find_repos_by_regex("rhel-7-server-rpms") == [ "rhel-7-server-rpms" ] assert "rhel-7-server-rpms" in self.repo.find_repos_by_regex( "rhel-[7].*") def test_wrong_regex(self): """Test wrong repos regex.""" with pytest.raises(Exception) as context: self.repo.find_repos_by_regex("*") assert "nothing to repeat" in str(context) def test_missing_required(self): """Test missing required property 'repository_list'.""" with pytest.raises(Exception) as context: self.repo.process_list(api_version="v1", data=REPO_JSON_BAD) assert "'repository_list' is a required property" in str(context) def test_empty_json(self): """Test repos API with empty JSON.""" with pytest.raises(Exception) as context: self.repo.process_list(api_version="v1", data=REPO_JSON_EMPTY) assert "'repository_list' is a required property" in str(context) def test_empty_repository_list(self): """Test repos API with empty 'repository_list'.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_EMPTY_LIST) assert response == EMPTY_RESPONSE def test_non_existing_repo(self): """Test repos API repsonse for non-existent repo.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_NON_EXIST) assert response == EMPTY_RESPONSE def test_schema(self): """Test schema of valid repos API response.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON) assert schemas.repos_schema.validate(response)
class TestRepoAPI(TestBase): """Test RepoAPI class.""" repo = None # pylint: disable=unused-argument @pytest.fixture(autouse=True) def setup_api(self, load_cache): """Set RepoAPI object.""" self.repo = RepoAPI(self.cache) def test_regex(self): """Test correct repos regex.""" assert self.repo.try_expand_by_regex(["rhel-7-server-rpms"]) == ["rhel-7-server-rpms"] assert "rhel-7-server-rpms" in self.repo.try_expand_by_regex(["rhel-[7].*"]) def test_wrong_regex(self): """Test wrong repos regex.""" with pytest.raises(Exception) as context: self.repo.try_expand_by_regex(["*"]) assert "nothing to repeat" in str(context.value) def test_empty_repository_list(self): """Test repos API with empty 'repository_list'.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_EMPTY_LIST) assert response == EMPTY_RESPONSE def test_non_existing_repo(self): """Test repos API response for non-existent repo.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_NON_EXIST) assert response == EMPTY_RESPONSE def test_third_party(self): """Test filtering with third party enabled""" data = {**REPO_JSON_ALL, "third_party": True} response = self.repo.process_list(api_version="v1", data=data) assert len(response["repository_list"]) == 2 def test_non_third_party(self): """Test filtering with third-party content disabled""" data = {**REPO_JSON_ALL} data.pop("third_party", None) response = self.repo.process_list(api_version="v1", data=data) assert len(response["repository_list"]) == 1 def test_schema(self): """Test schema of valid repos API response.""" response = self.repo.process_list(api_version="v1", data=REPO_JSON) assert schemas.repos_schema.validate(response) def test_modified_since(self): """Test repos API with 'modified_since' property.""" response = self.repo.process_list(api_version=1, data=REPO_JSON_MODIFIED_SINCE) assert tools.match(EMPTY_RESPONSE, response) is True def test_page_size(self): """Test repos API page size""" response = self.repo.process_list(api_version="v1", data=REPO_JSON) page_size = 0 for label in response['repository_list']: page_size += len(response['repository_list'][label]) assert response['page_size'] == page_size def test_page_size_mod_since(self): """Test repos API page size with 'modified_since' property""" response = self.repo.process_list(api_version="v1", data=REPO_JSON_MODIFIED_SINCE) page_size = 0 for label in response['repository_list']: page_size += len(response['repository_list'][label]) assert response['page_size'] == page_size