def test_start(self): network_list_mock = self.mocker.mock() network_start_mock = self.mocker.mock() def check_definition(args): cmd, subcommand, template_path = args network_definition = open(template_path).read() self.assertIn('<name>foobar</name>', network_definition) self.assertIn('<bridge name="vbr-foobar-%d" />', network_definition) self.assertIn( '<range start="192.168.131.2" end="192.168.131.254" />', network_definition) self.assertIn( '<ip address="192.168.131.1" netmask="255.255.255.0">', network_definition) return True self.patch(subprocess, "check_output", network_list_mock) self.patch(subprocess, "check_call", network_start_mock) network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result(network_list_default_output) network_start_mock(MATCH(check_definition)) network_start_mock(["virsh", "net-start", "foobar"]) self.mocker.replay() network = Network("foobar") yield network.start()
def test_is_running(self): network_list_mock = self.mocker.mock() self.patch(subprocess, "check_output", network_list_mock) # Network on network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % (dict(name="foobar", state="active", autostart="off"))) # Network off network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % (dict(name="foobar", state="inactive", autostart="off"))) # Non existent network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % (dict(name="magic", state="inactive", autostart="off"))) self.mocker.replay() network = Network("foobar") self.assertTrue((yield network.is_running())) self.assertFalse((yield network.is_running())) self.assertFalse((yield network.is_running()))
def test_start(self): network_list_mock = self.mocker.mock() network_start_mock = self.mocker.mock() def check_definition(args): cmd, subcommand, template_path = args network_definition = open(template_path).read() self.assertIn( '<name>foobar</name>', network_definition) self.assertIn( '<bridge name="vbr-foobar-%d" />', network_definition) self.assertIn( '<range start="192.168.131.2" end="192.168.131.254" />', network_definition) self.assertIn( '<ip address="192.168.131.1" netmask="255.255.255.0">', network_definition) return True self.patch(subprocess, "check_output", network_list_mock) self.patch(subprocess, "check_call", network_start_mock) network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result(network_list_default_output) network_start_mock(MATCH(check_definition)) network_start_mock(["virsh", "net-start", "foobar"]) self.mocker.replay() network = Network("foobar") yield network.start()
def test_is_running(self): network_list_mock = self.mocker.mock() self.patch(subprocess, "check_output", network_list_mock) # Network on network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % ( dict(name="foobar", state="active", autostart="off"))) # Network off network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % ( dict(name="foobar", state="inactive", autostart="off"))) # Non existent network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % ( dict(name="magic", state="inactive", autostart="off"))) self.mocker.replay() network = Network("foobar") self.assertTrue((yield network.is_running())) self.assertFalse((yield network.is_running())) self.assertFalse((yield network.is_running()))
def test_get_network_attributes(self): network_dumpxml_mock = self.mocker.mock() self.patch(subprocess, "check_output", network_dumpxml_mock) network_dumpxml_mock(["virsh", "net-dumpxml", "default"], env={"LC_ALL": "C"}) self.mocker.result(network_dump_xml_default_output) self.mocker.replay() network = Network("default") attributes = yield network.get_attributes() self.assertEqual( dict(name="default", ip=dict(address="192.168.122.1", netmask="255.255.255.0"), bridge="virbr0"), attributes)
def test_start_started_network(self): network_list_mock = self.mocker.mock() self.patch(subprocess, "check_output", network_list_mock) network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result(network_list_output_template % dict(name="zoo", state="active", autostart="no")) # patch to catch any errant calls network_start_mock = self.mocker.mock() self.patch(subprocess, "check_call", network_start_mock) self.mocker.replay() network = Network("zoo") yield network.start()
def test_start_stoppped_network(self): network_list_mock = self.mocker.mock() self.patch(subprocess, "check_output", network_list_mock) network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result(network_list_output_template % dict(name="juju", state="inactive", autostart="no")) network_start_mock = self.mocker.mock() self.patch(subprocess, "check_call", network_start_mock) network_start_mock(["virsh", "net-start", "juju"]) self.mocker.replay() network = Network("juju") yield network.start()
def test_start_started_network(self): network_list_mock = self.mocker.mock() self.patch(subprocess, "check_output", network_list_mock) network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % dict( name="zoo", state="active", autostart="no")) # patch to catch any errant calls network_start_mock = self.mocker.mock() self.patch(subprocess, "check_call", network_start_mock) self.mocker.replay() network = Network("zoo") yield network.start()
def test_start_stoppped_network(self): network_list_mock = self.mocker.mock() self.patch(subprocess, "check_output", network_list_mock) network_list_mock(["virsh", "net-list", "--all"], env={"LC_ALL": "C"}) self.mocker.result( network_list_output_template % dict( name="juju", state="inactive", autostart="no")) network_start_mock = self.mocker.mock() self.patch(subprocess, "check_call", network_start_mock) network_start_mock(["virsh", "net-start", "juju"]) self.mocker.replay() network = Network("juju") yield network.start()
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))