* feat: multi-instance NPM configuration Adds NPM_INSTANCES env var (JSON map) to manage multiple NPM servers from a single MCP instance. Every tool gains an optional 'instance' parameter. Legacy single-instance config (NPM_API_URL/IDENTITY/SECRET) still works unchanged. Includes list_instances tool, per-instance proxy_defaults and log_dir, and full backward compatibility. * fix(config): add explicit type validation for NPM_INSTANCES fields * fix(multi-instance): fix cross-instance log leakage and order-dependent default selection * chore(multi-instance): document NPM_DEFAULT_INSTANCE and harden logs fallback logic against cross-instance leakage --------- Co-authored-by: b3nw <189466+b3nw@users.noreply.github.com>
133 lines
5.1 KiB
Python
133 lines
5.1 KiB
Python
"""Tests for multi-instance configuration."""
|
|
|
|
import pytest
|
|
|
|
from npm_mcp.config import Settings
|
|
|
|
|
|
class TestMultiInstance:
|
|
def test_single_instance_legacy(self):
|
|
"""Legacy single-instance config creates a 'default' instance."""
|
|
s = Settings(api_url="http://npm:81/api", identity="a@b", secret="x")
|
|
assert s.list_instances() == ["default"]
|
|
inst = s.get_instance()
|
|
assert inst.api_url == "http://npm:81/api"
|
|
assert inst.identity == "a@b"
|
|
|
|
def test_multi_instance_from_dict(self):
|
|
s = Settings(
|
|
identity="ignored", secret="ignored",
|
|
instances={
|
|
"prod": {"api_url": "http://prod:81/api", "identity": "a@b", "secret": "p"},
|
|
"staging": {"api_url": "http://staging:81/api", "identity": "c@d", "secret": "s"},
|
|
},
|
|
)
|
|
assert set(s.list_instances()) == {"prod", "staging"}
|
|
assert s.get_instance("prod").api_url == "http://prod:81/api"
|
|
assert s.get_instance("staging").identity == "c@d"
|
|
|
|
def test_default_instance_is_first(self):
|
|
s = Settings(
|
|
identity="i", secret="s",
|
|
instances={
|
|
"alpha": {"api_url": "http://a:81/api", "identity": "x", "secret": "y"},
|
|
"beta": {"api_url": "http://b:81/api", "identity": "x", "secret": "y"},
|
|
},
|
|
)
|
|
default = s.get_instance()
|
|
assert default.name == "alpha"
|
|
|
|
def test_unknown_instance_raises(self):
|
|
s = Settings(identity="a", secret="b")
|
|
with pytest.raises(ValueError, match="Unknown instance"):
|
|
s.get_instance("nonexistent")
|
|
|
|
def test_no_instances_raises(self):
|
|
s = Settings(identity="", secret="")
|
|
with pytest.raises(ValueError, match="No NPM instances configured"):
|
|
s.get_instance()
|
|
|
|
def test_instance_proxy_defaults(self):
|
|
s = Settings(
|
|
identity="i", secret="s",
|
|
instances={
|
|
"prod": {
|
|
"api_url": "http://p:81/api",
|
|
"identity": "x",
|
|
"secret": "y",
|
|
"proxy_defaults": {"certificate_id": 42},
|
|
},
|
|
},
|
|
)
|
|
defaults = s.get_instance("prod").get_proxy_defaults()
|
|
assert defaults["certificate_id"] == 42
|
|
assert defaults["ssl_forced"] is True # base default preserved
|
|
|
|
def test_instances_json_env(self, monkeypatch):
|
|
monkeypatch.setenv("NPM_IDENTITY", "x")
|
|
monkeypatch.setenv("NPM_SECRET", "y")
|
|
monkeypatch.setenv(
|
|
"NPM_INSTANCES",
|
|
'{"site1":{"api_url":"http://s1:81/api","identity":"a","secret":"b"}}',
|
|
)
|
|
s = Settings()
|
|
assert s.list_instances() == ["site1"]
|
|
|
|
def test_missing_required_field_raises(self):
|
|
with pytest.raises(ValueError, match="missing required field"):
|
|
Settings(
|
|
identity="i", secret="s",
|
|
instances={"bad": {"identity": "a", "secret": "b"}},
|
|
)
|
|
|
|
def test_default_instance_name_property(self):
|
|
s = Settings(identity="a", secret="b")
|
|
assert s.default_instance_name == "default"
|
|
|
|
def test_instances_invalid_types_raise(self):
|
|
# Invalid api_url type (int)
|
|
with pytest.raises(ValueError, match="must be a string"):
|
|
Settings(
|
|
identity="i", secret="s",
|
|
instances={"site1": {"api_url": 123, "identity": "a", "secret": "b"}}
|
|
)
|
|
|
|
# Invalid log_dir type (list)
|
|
with pytest.raises(ValueError, match="must be a string"):
|
|
Settings(
|
|
identity="i", secret="s",
|
|
instances={"site1": {"api_url": "http://npm:81/api", "identity": "a", "secret": "b", "log_dir": []}}
|
|
)
|
|
|
|
# Invalid proxy_defaults type (str)
|
|
with pytest.raises(ValueError, match="must be an object"):
|
|
Settings(
|
|
identity="i", secret="s",
|
|
instances={"site1": {"api_url": "http://npm:81/api", "identity": "a", "secret": "b", "proxy_defaults": "invalid"}}
|
|
)
|
|
|
|
def test_default_instance_selection(self):
|
|
# Explicit default instance configuration
|
|
s = Settings(
|
|
identity="i", secret="s",
|
|
default_instance="beta",
|
|
instances={
|
|
"alpha": {"api_url": "http://a:81/api", "identity": "x", "secret": "y"},
|
|
"beta": {"api_url": "http://b:81/api", "identity": "x", "secret": "y"},
|
|
},
|
|
)
|
|
assert s.default_instance_name == "beta"
|
|
assert s.get_instance().name == "beta"
|
|
|
|
def test_default_instance_missing_raises(self):
|
|
# Explicit default instance configured but not present in instances list
|
|
with pytest.raises(ValueError, match="is not in configured instances"):
|
|
Settings(
|
|
identity="i", secret="s",
|
|
default_instance="gamma",
|
|
instances={
|
|
"alpha": {"api_url": "http://a:81/api", "identity": "x", "secret": "y"},
|
|
"beta": {"api_url": "http://b:81/api", "identity": "x", "secret": "y"},
|
|
},
|
|
)
|