This commit is contained in:
Jan-Erik Rediger 2023-10-26 14:42:17 +02:00 коммит произвёл Jan-Erik Rediger
Родитель abee0ddf8c
Коммит 2a437ea2d8
5 изменённых файлов: 95 добавлений и 2 удалений

Просмотреть файл

@ -44,7 +44,8 @@ commands:
sudo apt upgrade -q
sudo apt install \
--yes --no-install-recommends \
openjdk-11-jdk-headless
openjdk-11-jdk-headless \
ruby
make install-kotlin-linters
- run:
name: Install glean_parser

Просмотреть файл

@ -47,7 +47,7 @@ test: ## run tests quickly with the default Python
py.test
test-full: ## run tests, including those with additional dependencies
py.test --run-web-tests --run-node-tests
py.test --run-web-tests --run-node-tests --run-ruby-tests
coverage: ## check code coverage quickly with the default Python
coverage run --source glean_parser -m pytest

Просмотреть файл

@ -14,6 +14,12 @@ def pytest_addoption(parser):
default=False,
help="Run tests that require node.js",
)
parser.addoption(
"--run-ruby-tests",
action="store_true",
default=False,
help="Run tests that require Ruby",
)
def pytest_collection_modifyitems(config, items):
@ -28,3 +34,9 @@ def pytest_collection_modifyitems(config, items):
for item in items:
if "node_dependency" in item.keywords:
item.add_marker(skip_node)
if not config.getoption("--run-ruby-tests"):
skip_ruby = pytest.mark.skip(reason="Need --run-ruby-tests option to run")
for item in items:
if "ruby_dependency" in item.keywords:
item.add_marker(skip_ruby)

Просмотреть файл

@ -0,0 +1,9 @@
require_relative ".//* IMPORT */"
events = Glean::GleanEventsLogger.new(
app_id: "glean.test",
app_display_version: "0.0.1",
app_channel: "testing",
logger_options: $stdout
)
/* CODE */

Просмотреть файл

@ -4,9 +4,14 @@
# http://creativecommons.org/publicdomain/zero/1.0/
from pathlib import Path
import io
import json
import pytest
import subprocess
import glean_parser
from glean_parser import translate
from glean_parser import validate_ping
ROOT = Path(__file__).parent
@ -102,3 +107,69 @@ def test_parser_rb_server(tmpdir):
current_version=f"glean_parser v{glean_parser.__version__}"
)
assert content == compare
def run_logger(code_dir, import_file, code):
"""
Run the Ruby logger with a mocked logger
that just prints the ping payload to STDOUT.
"""
tmpl_code = ""
with open(ROOT / "test-rb" / "test.rb.tmpl", "r") as fp:
tmpl_code = fp.read()
tmpl_code = tmpl_code.replace("/* IMPORT */", import_file).replace(
"/* CODE */", code
)
with open(code_dir / "test.rb", "w") as fp:
fp.write(tmpl_code)
return subprocess.check_output(["ruby", "test.rb"], cwd=code_dir).decode("utf-8")
@pytest.mark.ruby_dependency
def test_run_logging(tmpdir):
tmpdir = Path(str(tmpdir))
translate.translate(
[
ROOT / "data" / "ruby_server_metrics.yaml",
],
"ruby_server",
tmpdir,
)
code = """
events.backend_object_update.record(
object_type: "type",
object_state: "state",
identifiers_fxa_account_id: nil,
user_agent: "glean-test/1.0",
ip_address: "127.0.0.1"
)
"""
logged_output = run_logger(tmpdir, "server_events.rb", code)
logged_output = json.loads(logged_output)
fields = logged_output["Fields"]
payload = fields["payload"]
assert "glean-server-event" == logged_output["Type"]
assert "glean.test" == fields["document_namespace"]
assert "events" == fields["document_type"]
assert "1" == fields["document_version"]
assert "glean-test/1.0" == fields["user_agent"]
schema_url = (
"https://raw.githubusercontent.com/mozilla-services/"
"mozilla-pipeline-schemas/main/"
"schemas/glean/glean/glean.1.schema.json"
)
input = io.StringIO(payload)
output = io.StringIO()
assert (
validate_ping.validate_ping(input, output, schema_url=schema_url) == 0
), output.getvalue()