Add support for Elastic Cloud Serverless projects (#770)

This commit is contained in:
DVB authored and GitHub committed 2026-05-20 21:36:19 -04:00
1 parent 535d9db1ad
commit bf37ded688
6 files changed
+154 -6

No files matched your search

+54
View File
@@ -2232,6 +2232,60 @@ class TestParseConfigElasticsearch(unittest.TestCase):
_parse_config(cp, _opts())
self.assertIn("hosts", str(ctx.exception))
def test_elasticsearch_serverless_flag(self):
"""``[elasticsearch] serverless = true`` flips ``opts.elasticsearch_serverless``."""
from parsedmarc.cli import _parse_config
cp = _config_with("elasticsearch", {"hosts": "es:9200", "serverless": "true"})
opts = _opts()
_parse_config(cp, opts)
self.assertIs(opts.elasticsearch_serverless, True)
def test_elasticsearch_serverless_passed_to_set_hosts(self):
"""End-to-end: a Serverless config reaches ``elastic.set_hosts(serverless=True)``.
Regression guard: catches anyone who later parses the flag but forgets
to plumb it through to ``set_hosts`` (or vice-versa).
"""
config = """[general]
save_aggregate = true
silent = true
[imap]
host = imap.example.com
user = test-user
password = test-password
[elasticsearch]
hosts = localhost
serverless = true
"""
with tempfile.NamedTemporaryFile(
"w", suffix=".ini", delete=False
) as config_file:
config_file.write(config)
config_path = config_file.name
self.addCleanup(lambda: os.path.exists(config_path) and os.remove(config_path))
with (
patch("parsedmarc.cli.elastic.migrate_indexes"),
patch("parsedmarc.cli.elastic.set_hosts") as mock_set_hosts,
patch(
"parsedmarc.cli.get_dmarc_reports_from_mailbox",
return_value={
"aggregate_reports": [],
"failure_reports": [],
"smtp_tls_reports": [],
},
),
patch("parsedmarc.cli.IMAPConnection", return_value=object()),
patch.object(sys, "argv", ["parsedmarc", "-c", config_path]),
):
parsedmarc.cli._main()
mock_set_hosts.assert_called_once()
self.assertIs(mock_set_hosts.call_args.kwargs.get("serverless"), True)
class TestParseConfigOpenSearch(unittest.TestCase):
def test_opensearch_basic(self):
+48
View File
@@ -320,6 +320,54 @@ class TestCreateIndexes(unittest.TestCase):
self.assertIn("cluster down", str(ctx.exception))
class TestCreateIndexesServerless(unittest.TestCase):
"""Serverless mode strips shard/replica keys but keeps everything else.
Elastic Cloud Serverless rejects ``number_of_shards`` and
``number_of_replicas`` with HTTP 400. Other settings like
``refresh_interval`` are accepted and must pass through unchanged.
"""
def setUp(self):
self._original = elastic_module._SERVERLESS
elastic_module._SERVERLESS = True
def tearDown(self):
elastic_module._SERVERLESS = self._original
def test_serverless_default_skips_settings_entirely(self):
with patch("parsedmarc.elastic.Index") as mock_index_cls:
mock_index = mock_index_cls.return_value
mock_index.exists.return_value = False
create_indexes(["idx"])
mock_index.settings.assert_not_called()
mock_index.create.assert_called_once()
def test_serverless_filters_rejected_keys_and_passes_others_through(self):
with patch("parsedmarc.elastic.Index") as mock_index_cls:
mock_index = mock_index_cls.return_value
mock_index.exists.return_value = False
create_indexes(
["idx"],
settings={
"number_of_shards": 3,
"number_of_replicas": 2,
"refresh_interval": "5s",
},
)
mock_index.settings.assert_called_once_with(refresh_interval="5s")
def test_serverless_skips_settings_when_only_rejected_keys(self):
with patch("parsedmarc.elastic.Index") as mock_index_cls:
mock_index = mock_index_cls.return_value
mock_index.exists.return_value = False
create_indexes(
["idx"], settings={"number_of_shards": 3, "number_of_replicas": 2}
)
mock_index.settings.assert_not_called()
mock_index.create.assert_called_once()
# ---------------------------------------------------------------------------
# migrate_indexes
# ---------------------------------------------------------------------------