def create_test_product(product_name, product_endpoint): # Create a new product on the secondary server. name = base64.b64encode(product_name) return ProductConfiguration(endpoint=product_endpoint, displayedName_b64=name, description_b64=name, connection=DatabaseConnection( engine='sqlite', host='', port=0, username_b64='', password_b64='', database=os.path.join( self.test_workspace_secondary, 'data.sqlite')))
def handle_add_product(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) # Put together the database connection's descriptor. if 'postgresql' in args: db_engine = 'postgresql' db_host = args.dbaddress db_port = args.dbport db_user = args.dbusername db_pass = args.dbpassword db_name = args.dbname else: db_engine = 'sqlite' db_host = "" db_port = 0 db_user = "" db_pass = "" db_name = args.sqlite dbc = DatabaseConnection(engine=db_engine, host=db_host, port=db_port, username_b64=base64.b64encode(db_user), password_b64=base64.b64encode(db_pass), database=db_name) # Put together the product configuration. name = base64.b64encode(args.display_name) \ if 'display_name' in args else None desc = base64.b64encode(args.description) \ if 'description' in args else None prod = ProductConfiguration(endpoint=args.endpoint, displayedName_b64=name, description_b64=desc, connection=dbc) LOG.debug("Sending request to add product...") success = client.addProduct(prod) if success: LOG.info("Product added successfully.") else: LOG.error("Adding the product has failed.") sys.exit(1)
def test_add_invalid_product(self): """ Test the server prohibiting the addition of bogus product configs. """ error = base64.b64encode("bogus") product_cfg = ProductConfiguration(displayedName_b64=error, description_b64=error) # Test setting up product with valid endpoint but no database # connection. with self.assertRaises(RequestFailed): cfg = deepcopy(product_cfg) cfg.endpoint = "valid" self._root_client.addProduct(cfg) # Test some invalid strings based on pattern. dbc = DatabaseConnection(engine='sqlite', host='', port=0, username_b64='', password_b64='', database="valid.sqlite") product_cfg.connection = dbc with self.assertRaises(RequestFailed): product_cfg.endpoint = "_INVALID" self._root_client.addProduct(product_cfg) with self.assertRaises(RequestFailed): product_cfg.endpoint = "0foobar" self._root_client.addProduct(product_cfg) with self.assertRaises(RequestFailed): product_cfg.endpoint = "$$$$$$$" self._root_client.addProduct(product_cfg) # Test some forbidden URI parts. with self.assertRaises(RequestFailed): product_cfg.endpoint = "index.html" self._root_client.addProduct(product_cfg) with self.assertRaises(RequestFailed): product_cfg.endpoint = "CodeCheckerService" self._root_client.addProduct(product_cfg)
def test_read_product_from_another_server(self): """ Test if adding and removing a product is visible from the other server. """ # Check if the main server's product is visible on the second server. self.assertEqual( self._pr_client_2.getProducts('producttest', None)[0].endpoint, 'producttest', "Main server's product was not loaded by the secondary server.") # Create a new product on the secondary server. name = base64.b64encode('producttest_second') product_cfg = ProductConfiguration( endpoint='producttest_second', displayedName_b64=name, description_b64=name, connection=DatabaseConnection(engine='sqlite', host='', port=0, username_b64='', password_b64='', database=os.path.join( self.test_workspace_secondary, 'data.sqlite'))) self.assertTrue(self._pr_client_2.addProduct(product_cfg), "Cannot create product on secondary server.") # Use the same CodeChecker config that was used on the main server, # but store into the secondary one. store_cfg = deepcopy(self.test_cfg['codechecker_cfg']) store_cfg.update({ 'viewer_port': self.codechecker_cfg_2['viewer_port'], 'viewer_product': self.codechecker_cfg_2['viewer_product'] }) codechecker.login(store_cfg, self.test_workspace_secondary, 'root', 'root') store_res = codechecker.store( store_cfg, 'test_proj-secondary', os.path.join(self.test_workspace_main, 'reports')) self.assertEqual(store_res, 0, "Storing the test project failed.") cc_client_2 = env.setup_viewer_client(self.test_workspace_secondary) self.assertEqual(len(cc_client_2.getRunData(None)), 1, "There should be a run present in the new server.") self.assertEqual( len(self._cc_client.getRunData(None)), 1, "There should be a run present in the database when " "connected through the main server.") # Remove the product through the main server. p_id = self._root_client.getProducts('producttest_second', None)[0].id p_id2 = self._pr_client_2.getProducts('producttest_second', None)[0].id self.assertIsNotNone(p_id) self.assertEqual( p_id, p_id2, "The products have different ID across the two " "servers. WHAT? Database isn't shared?!") self.assertTrue(self._root_client.removeProduct(p_id), "Main server reported error while removing product.") self.assertEqual(len(self._pr_client.getProducts('_second', None)), 0, "Secondary server still sees the removed product.") # Try to store into the product just removed through the secondary # server, which still sees the product internally. store_res = codechecker.store( store_cfg, 'test_proj-secondary', os.path.join(self.test_workspace_main, 'reports')) self.assertNotEqual( store_res, 0, "Storing into the server with " "the product missing should've resulted in " "an error.")