def test_get_class_path_from_build(self): data_dir = self.makeDir() software_dir = self.makeDir() os.mkdir(os.path.join(software_dir, "build")) lib_dir = os.path.join(software_dir, "build", "lib") os.mkdir(lib_dir) self.makeFile("", path=os.path.join(software_dir, "build", "zookeeper-3.4.0.jar")) for p in [ "jline-0.9.94.jar", "netty-3.2.2.Final.jar", "log4j-1.2.15.jar", "slf4j-log4j12-1.6.1.jar", "slf4j-api-1.6.1.jar" ]: self.makeFile("", path=os.path.join(lib_dir, p)) instance = Zookeeper(data_dir, 12345, zk_location=software_dir) class_path = instance.get_class_path() self.assertEqual(class_path.index(data_dir), 0) self.assertIn(os.path.join(lib_dir, "log4j-1.2.15.jar"), class_path) self.assertIn( os.path.join(software_dir, "build", "zookeeper-3.4.0.jar"), class_path)
def test_get_class_path_from_build(self): data_dir = self.makeDir() software_dir = self.makeDir() os.mkdir(os.path.join(software_dir, "build")) lib_dir = os.path.join(software_dir, "build", "lib") os.mkdir(lib_dir) self.makeFile("", path=os.path.join( software_dir, "build", "zookeeper-3.4.0.jar")) for p in [ "jline-0.9.94.jar", "netty-3.2.2.Final.jar", "log4j-1.2.15.jar", "slf4j-log4j12-1.6.1.jar", "slf4j-api-1.6.1.jar"]: self.makeFile("", path=os.path.join(lib_dir, p)) instance = Zookeeper(data_dir, 12345, zk_location=software_dir) class_path = instance.get_class_path() self.assertEqual(class_path.index(data_dir), 0) self.assertIn( os.path.join(lib_dir, "log4j-1.2.15.jar"), class_path) self.assertIn( os.path.join(software_dir, "build", "zookeeper-3.4.0.jar"), class_path)
def zookeeper_test_context(install_path, port=28181): """Manage context to run/stop a ZooKeeper for testing and related vars. @param install_path: The path to the install for ZK. Bare word "system" causes special behavior to use system conf for ZK @param port: The port to run the managed ZK instance """ global _zookeeper_address saved_zookeeper_address = _zookeeper_address saved_env = os.environ.get("ZOOKEEPER_ADDRESS") test_zookeeper = Zookeeper( tempfile.mkdtemp(), port, zk_location=install_path, use_deferred=False) test_zookeeper.start() os.environ["ZOOKEEPER_ADDRESS"] = test_zookeeper.address _zookeeper_address = test_zookeeper.address try: yield test_zookeeper finally: test_zookeeper.stop() _zookeeper_address = saved_zookeeper_address if saved_env: os.environ["ZOOKEEPER_ADDRESS"] = saved_env else: del os.environ["ZOOKEEPER_ADDRESS"]
def destroy_environment(self): """Shutdown the machine environment. """ # Stop all the containers log.info("Destroying unit containers...") yield self._destroy_containers() # Stop the machine agent log.debug("Stopping machine agent...") agent = ManagedMachineAgent(self._qualified_name) yield agent.stop() # Stop the storage server log.debug("Stopping storage server...") storage_server = StorageServer(self._qualified_name) yield storage_server.stop() # Stop zookeeper log.debug("Stopping zookeeper...") zookeeper_dir = os.path.join(self._directory, "zookeeper") zookeeper = Zookeeper(zookeeper_dir, None) yield zookeeper.stop() # Clean out local state yield self.save_state(False) # Don't stop the network since we're using the default libvirt log.debug("Environment destroyed.")
def destroy_environment(self): """Shutdown the machine environment. """ # Stop all the containers log.info("Destroying unit containers...") yield self._destroy_containers() # Stop the machine agent log.debug("Stopping machine agent...") pid_file = os.path.join(self._directory, "machine-agent.pid") agent = ManagedMachineAgent(pid_file) yield agent.stop() # Stop the storage server log.debug("Stopping storage server...") pid_file = os.path.join(self._directory, "storage-server.pid") storage_server = StorageServer(pid_file) yield storage_server.stop() # Stop zookeeper log.debug("Stopping zookeeper...") zookeeper_dir = os.path.join(self._directory, "zookeeper") zookeeper = Zookeeper(zookeeper_dir, None) yield zookeeper.stop() # Clean out local state yield self.save_state(False) # Don't stop the network since we're using the default libvirt log.debug("Environment destroyed.")
def test_get_class_path_from_package(self): data_dir = self.makeDir() instance = Zookeeper(data_dir, 12345) class_path = instance.get_class_path() self.assertEqual(class_path.index(data_dir), 0) self.assertIn("/usr/share/java/jline.jar", class_path) self.assertIn("/usr/share/java/log4j-1.2.jar", class_path) self.assertIn("/usr/share/java/zookeeper.jar", class_path)
def test_get_zookeeper_variables(self): """All data files are contained in the specified directory. """ data_dir = self.makeDir() instance = Zookeeper(data_dir, 12345) variables = instance.get_zookeeper_variables() variables.pop("class_path") for v in variables.values(): self.assertTrue(v.startswith(data_dir))
def test_get_class_path_from_package_static(self): data_dir = self.makeDir() instance = Zookeeper(data_dir, 12345) instance.package_class_path_file = sample_package_environment_conf class_path = instance.get_class_path() self.assertEqual(class_path.index(data_dir), 0) self.assertIn("/usr/share/java/jline.jar", class_path) self.assertIn("/usr/share/java/log4j-1.2.jar", class_path) self.assertIn("/usr/share/java/zookeeper.jar", class_path)
def test_managed_zookeeper(self): zookeeper.set_debug_level(0) # Start zookeeper data_dir = self.makeDir() instance = Zookeeper(data_dir, 12345) yield instance.start() self.assertTrue(instance.running) # Connect a client client = ZookeeperClient("127.0.1.1:12345") yield client.connect() stat = yield client.exists("/") yield client.close() self.assertTrue(stat) # Stop Instance yield instance.stop() self.assertFalse(instance.running) self.assertFalse(os.path.exists(data_dir))
def zookeeper_test_context(install_path, port=28181): """Manage context to run/stop a ZooKeeper for testing and related vars. @param install_path: The path to the install for ZK. Bare word "system" causes special behavior to use system conf for ZK @param port: The port to run the managed ZK instance """ global _zookeeper_address saved_zookeeper_address = _zookeeper_address saved_env = os.environ.get("ZOOKEEPER_ADDRESS") test_zookeeper = Zookeeper(tempfile.mkdtemp(), port, zk_location=install_path, use_deferred=False) test_zookeeper.start() os.environ["ZOOKEEPER_ADDRESS"] = test_zookeeper.address _zookeeper_address = test_zookeeper.address try: yield test_zookeeper finally: test_zookeeper.stop() _zookeeper_address = saved_zookeeper_address if saved_env: os.environ["ZOOKEEPER_ADDRESS"] = saved_env else: del os.environ["ZOOKEEPER_ADDRESS"]
def bootstrap(self): """Bootstrap a local development environment. """ # Check for existing environment state = yield self.load_state() if state is not False: raise ProviderError("Environment already bootstrapped") # Check for required packages log.info("Checking for required packages...") missing = check_packages(*REQUIRED_PACKAGES) if missing: raise ProviderError("Missing packages %s" % ( ", ".join(sorted(list(missing))))) # Get/create directory for zookeeper and files zookeeper_dir = os.path.join(self._directory, "zookeeper") if not os.path.exists(zookeeper_dir): os.makedirs(zookeeper_dir) # Start networking, and get an open port. log.info("Starting networking...") net = Network("default", subnet=122) # Start is a noop if its already started, which it is by default, # per libvirt-bin package installation yield net.start() net_attributes = yield net.get_attributes() port = get_open_port(net_attributes["ip"]["address"]) # Start zookeeper log.info("Starting zookeeper...") # Run zookeeper as the current user, unless we're being run as root # in which case run zookeeper as the 'zookeeper' user. zookeeper_user = None if os.geteuid() == 0: zookeeper_user = "******" zookeeper = Zookeeper(zookeeper_dir, port=port, host=net_attributes["ip"]["address"], user=zookeeper_user, group=zookeeper_user) yield zookeeper.start() # Starting provider storage server log.info("Starting storage server...") storage_server = StorageServer( pid_file=os.path.join(self._directory, "storage-server.pid"), storage_dir=os.path.join(self._directory, "files"), host=net_attributes["ip"]["address"], port=get_open_port(net_attributes["ip"]["address"]), log_file=os.path.join(self._directory, "storage-server.log")) yield storage_server.start() # Save the zookeeper start to provider storage. yield self.save_state({"zookeeper-instances": ["local"], "zookeeper-address": zookeeper.address}) # Initialize the zookeeper state log.debug("Initializing state...") admin_identity = make_identity( "admin:%s" % self.config["admin-secret"]) client = ZookeeperClient(zookeeper.address) yield client.connect() hierarchy = StateHierarchy(client, admin_identity, "local", "local") yield hierarchy.initialize() # Store user credentials from the running user try: public_key = get_user_authorized_keys(self.config) public_key = public_key.strip() except LookupError, e: raise ProviderError(str(e))