예제 #1
0
 def set_node_service(
     self,
     node_id: int,
     service_name: str,
     dirs: List[str],
     files: List[str],
     startups: List[str],
     validations: List[str],
     shutdowns: List[str],
 ) -> NodeServiceData:
     response = self.client.set_node_service(
         self.session.id,
         node_id,
         service_name,
         directories=dirs,
         files=files,
         startup=startups,
         validate=validations,
         shutdown=shutdowns,
     )
     logging.info(
         "Set %s service for node(%s), files: %s, Startup: %s, "
         "Validation: %s, Shutdown: %s, Result: %s",
         service_name,
         node_id,
         files,
         startups,
         validations,
         shutdowns,
         response,
     )
     response = self.client.get_node_service(self.session.id, node_id,
                                             service_name)
     return NodeServiceData.from_proto(response.service)
예제 #2
0
 def click_apply(self) -> None:
     if (not self.is_custom_command() and not self.is_custom_service_file()
             and not self.has_new_files()
             and not self.is_custom_directory()):
         self.node.service_configs.pop(self.service_name, None)
         self.current_service_color("")
         self.destroy()
         return
     files = set(self.filenames)
     if (self.is_custom_command() or self.has_new_files()
             or self.is_custom_directory()):
         startup, validate, shutdown = self.get_commands()
         files = set(self.filename_combobox["values"])
         service_data = NodeServiceData(
             configs=list(files),
             dirs=self.temp_directories,
             startup=startup,
             validate=validate,
             shutdown=shutdown,
         )
         logger.info("setting service data: %s", service_data)
         self.node.service_configs[self.service_name] = service_data
     for file in self.modified_files:
         if file not in files:
             continue
         file_configs = self.node.service_file_configs.setdefault(
             self.service_name, {})
         file_configs[file] = self.temp_service_files[file]
     self.current_service_color("green")
     self.destroy()
예제 #3
0
 def get_node_service(self, node_id: int,
                      service_name: str) -> NodeServiceData:
     response = self.client.get_node_service(self.session.id, node_id,
                                             service_name)
     logging.debug("get node(%s) %s service, response: %s", node_id,
                   service_name, response)
     return NodeServiceData.from_proto(response.service)
예제 #4
0
    def test_start_session(self, grpc_server: CoreGrpcServer, definition):
        # given
        client = CoreGrpcClient()
        with client.context_connect():
            session = client.create_session()
        position = Position(x=50, y=100)
        node1 = session.add_node(1, position=position)
        position = Position(x=100, y=100)
        node2 = session.add_node(2, position=position)
        position = Position(x=200, y=200)
        wlan_node = session.add_node(3,
                                     _type=NodeType.WIRELESS_LAN,
                                     position=position)
        iface_helper = InterfaceHelper(ip4_prefix="10.83.0.0/16")
        iface1_id = 0
        iface1 = iface_helper.create_iface(node1.id, iface1_id)
        iface2_id = 0
        iface2 = iface_helper.create_iface(node2.id, iface2_id)
        link = Link(node1_id=node1.id,
                    node2_id=node2.id,
                    iface1=iface1,
                    iface2=iface2)
        session.links = [link]
        hook = Hook(state=SessionState.RUNTIME,
                    file="echo.sh",
                    data="echo hello")
        session.hooks = {hook.file: hook}
        location_x = 5
        location_y = 10
        location_z = 15
        location_lat = 20
        location_lon = 30
        location_alt = 40
        location_scale = 5
        session.location = SessionLocation(
            x=location_x,
            y=location_y,
            z=location_z,
            lat=location_lat,
            lon=location_lon,
            alt=location_alt,
            scale=location_scale,
        )

        # setup wlan config
        wlan_config_key = "range"
        wlan_config_value = "333"
        wlan_node.set_wlan({wlan_config_key: wlan_config_value})

        # setup mobility config
        mobility_config_key = "refresh_ms"
        mobility_config_value = "60"
        wlan_node.set_mobility({mobility_config_key: mobility_config_value})

        # setup service config
        service_name = "DefaultRoute"
        service_validate = ["echo hello"]
        node1.service_configs[service_name] = NodeServiceData(
            executables=[],
            dependencies=[],
            dirs=[],
            configs=[],
            startup=[],
            validate=service_validate,
            validation_mode=ServiceValidationMode.NON_BLOCKING,
            validation_timer=0,
            shutdown=[],
            meta="",
        )

        # setup service file config
        service_file = "defaultroute.sh"
        service_file_data = "echo hello"
        node1.service_file_configs[service_name] = {
            service_file: service_file_data
        }

        # setup session option
        option_key = "controlnet"
        option_value = "172.16.0.0/24"
        session.set_options({option_key: option_value})

        # when
        with patch.object(CoreXmlWriter, "write"):
            with client.context_connect():
                client.start_session(session, definition=definition)

        # then
        real_session = grpc_server.coreemu.sessions[session.id]
        if definition:
            state = EventTypes.DEFINITION_STATE
        else:
            state = EventTypes.RUNTIME_STATE
        assert real_session.state == state
        assert node1.id in real_session.nodes
        assert node2.id in real_session.nodes
        assert wlan_node.id in real_session.nodes
        assert iface1_id in real_session.nodes[node1.id].ifaces
        assert iface2_id in real_session.nodes[node2.id].ifaces
        hook_file, hook_data = real_session.hooks[EventTypes.RUNTIME_STATE][0]
        assert hook_file == hook.file
        assert hook_data == hook.data
        assert real_session.location.refxyz == (location_x, location_y,
                                                location_z)
        assert real_session.location.refgeo == (
            location_lat,
            location_lon,
            location_alt,
        )
        assert real_session.location.refscale == location_scale
        set_wlan_config = real_session.mobility.get_model_config(
            wlan_node.id, BasicRangeModel.name)
        assert set_wlan_config[wlan_config_key] == wlan_config_value
        set_mobility_config = real_session.mobility.get_model_config(
            wlan_node.id, Ns2ScriptedMobility.name)
        assert set_mobility_config[
            mobility_config_key] == mobility_config_value
        service = real_session.services.get_service(node1.id,
                                                    service_name,
                                                    default_service=True)
        assert service.validate == tuple(service_validate)
        real_node1 = real_session.get_node(node1.id, CoreNode)
        service_file = real_session.services.get_service_file(
            real_node1, service_name, service_file)
        assert service_file.data == service_file_data
        assert option_value == real_session.options.get_config(option_key)