Ejemplo n.º 1
0
    def test_reset(self, client):
        # Read
        s = client.config.get(Filter(names=["my-int-config"]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "12"

        # Set new value
        s = client.config.set(
            ConfigUpdate(
                items=[ConfigItemUpdate(name="my-int-config", value="777")]))

        # Read
        s = client.config.get(Filter(names=["my-int-config"]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "777"

        # Reset
        s = client.config.reset(Filter(names=["my-int-config"]))

        # Read
        s = client.config.get(Filter(names=["my-int-config"]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "12"
Ejemplo n.º 2
0
 def test_set_empty_list(self, client):
     # Try to set with an empty request
     try:
         client.config.set(ConfigUpdate())
         raise AssertionError("Shouldn't get here")
     except RpcException as e:
         assert e.rc == ResultCode.ERROR_PARAM_MISSING
Ejemplo n.º 3
0
 def test_set_empty_value(self, client):
     # Try to set with an empty value while not authorized
     try:
         client.config.set(
             ConfigUpdate(items=[ConfigItemUpdate(name="my-int-config")]))
         raise AssertionError("Shouldn't get here")
     except RpcException as e:
         assert e.rc == ResultCode.ERROR_PARAM_MISSING
Ejemplo n.º 4
0
 def test_set_bad_value(self, client):
     # Try to set with a bad value
     try:
         client.config.set(
             ConfigUpdate(items=[
                 ConfigItemUpdate(name="my-int-config", value="foo")
             ]))
         raise AssertionError("Shouldn't get here")
     except RpcException as e:
         assert e.rc == ResultCode.ERROR_PARAM_INVALID
Ejemplo n.º 5
0
    def test_proxy_config_conflict(self, proxy_server, client, another_server):
        # Register proxies
        proxy_server.client.srv.proxy_register(
            ProxyRegisterRequest(names=["sample"],
                                 version="123",
                                 port=self.rpc_port))

        # Update only on one proxied server
        s = another_server.client.config.set(
            ConfigUpdate(
                items=[ConfigItemUpdate(name="my-int-config", value="1024")]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "1024"

        # Read while values are different in proxied servers: conflict
        try:
            proxy_server.client.config.get(Filter(names=["my-int-config"]))
            raise AssertionError("shouldn't get here")
        except RpcException as e:
            assert e.rc == ResultCode.ERROR_ITEM_CONFLICT
Ejemplo n.º 6
0
    def test_proxy_config_set_n_reset(self, proxy_server, client,
                                      another_server):
        # Register proxies
        proxy_server.client.srv.proxy_register(
            ProxyRegisterRequest(names=["sample"],
                                 version="123",
                                 port=self.rpc_port))

        # Set in proxy
        s = proxy_server.client.config.set(
            ConfigUpdate(
                items=[ConfigItemUpdate(name="my-int-config", value="789")]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "789"

        # Read (all values shall be the same)
        for c in (client, another_server.client, proxy_server.client):
            s = c.config.get(Filter(names=["my-int-config"]))
            assert len(s.items) == 1
            item = s.items[0]
            assert item.name == "my-int-config"
            assert item.value == "789"

        # Reset
        s = proxy_server.client.config.reset(Filter(names=["my-int-config"]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "12"

        # Read (all values shall be the same)
        for c in (client, another_server.client, proxy_server.client):
            s = c.config.get(Filter(names=["my-int-config"]))
            assert len(s.items) == 1
            item = s.items[0]
            assert item.name == "my-int-config"
            assert item.value == "12"
Ejemplo n.º 7
0
    def set(self, request: ConfigUpdate) -> ConfigStatus:  # NOQA:A003
        """
        Update configuration items, according to input request
        """

        with self.lock:
            # Basic checks
            req_map = {r.name: r.value for r in request.items}
            merged_items = self.__merged_items(req_map.keys(), False)
            self.__check_items(req_map.keys(), merged_items,
                               request.ignore_unknown)

            # Validate local items new values
            for name, value in filter(lambda tpl: tpl[0] in self.user_items,
                                      req_map.items()):
                self.user_items[name].validate(name, value)

            # Delegate to proxied servers
            for client in self.__proxied_config_clients:
                self.logger.debug(
                    f"Set items on remote ({client.target_host})")
                client.config.set(
                    ConfigUpdate(items=list(request.items),
                                 ignore_unknown=True))

            # Finally, update local values
            for name, value in filter(lambda tpl: tpl[0] in self.user_items,
                                      req_map.items()):
                self.user_items[name].update(value)

            # Persist updated local values
            self.__persist()

            # Get again to build returned values
            merged_items = self.__merged_items(req_map.keys(), True)
            return ConfigStatus(items=merged_items.values())
Ejemplo n.º 8
0
    def test_set_ok(self, client):
        # Verify file is not persisted yet
        wks = self.test_folder / "wks"
        cfg = wks / "config.json"
        assert not cfg.is_file()

        # Set new value
        s = client.config.set(
            ConfigUpdate(
                items=[ConfigItemUpdate(name="my-int-config", value="999")]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "999"

        # File is persisted
        assert cfg.is_file()

        # Read again to make sure :)
        s = client.config.get(Filter(names=["my-int-config"]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "999"

        # Reload to verify persistence
        self.shutdown_server_instance()
        self.new_server_instance()

        # Read again
        s = client.config.get(Filter(names=["my-int-config"]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "999"

        # Reload to verify ignored persistence if no workspace (but update logs folder anyway)
        self.shutdown_server_instance()
        os.environ["RPC_LOGS_FOLDER"] = (self.test_folder /
                                         "custom_log_full_path").as_posix()
        self.new_server_instance(with_workspace=False)
        cfg.unlink()

        # Set new value; will not be persisted
        s = client.config.set(
            ConfigUpdate(
                items=[ConfigItemUpdate(name="my-int-config", value="888")]))
        assert not cfg.is_file()

        # Write an invalid persisted value
        with cfg.open("w") as f:
            json.dump({"my-int-config": "invalid string"}, f)

        # Reload to verify invalid value being ignored (and restore default logs folder)
        self.shutdown_server_instance()
        del os.environ["RPC_LOGS_FOLDER"]
        self.new_server_instance()
        self.check_logs(
            "Can't load invalid persisted value 'invalid string' for config item my-int-config"
        )

        # Read again (should be default value)
        s = client.config.get(Filter(names=["my-int-config"]))
        assert len(s.items) == 1
        item = s.items[0]
        assert item.name == "my-int-config"
        assert item.value == "12"