chore: migrate tokenserver tests to pytest with junit output

* add pytest and script to make for running tests with pytest

* add conftest for pytest path handling

* move store test results to capture integration results

* remove unneeded run_tests and fix warnings about assert_()

Closes SYNC-4612
This commit is contained in:
Nick Shirley 2025-03-17 22:36:42 -06:00 committed by GitHub
parent 324a6d8144
commit 15840c5ecf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 32 additions and 48 deletions

View File

@ -116,13 +116,24 @@ commands:
name: Merge llvm-cov results
command: make merge_coverage_results
store-unit-test-results:
store-test-results:
steps:
- store_test_results:
path: workflow/test-results
- store_artifacts:
path: workflow/test-results
run-tokenserver-integration-tests:
steps:
- run:
when: always
name: Tokenserver integration tests
command: |
# NOTE: Python3.12 requires `--break-system-packages`.
# This command is run on the circleci/rust image, which is running python 3.10
make run_token_server_integration_tests
environment:
SYNCSTORAGE_RS_IMAGE: app:build
run-e2e-mysql-tests:
steps:
- run:
@ -137,17 +148,6 @@ commands:
environment:
SYNCSTORAGE_RS_IMAGE: app:build
run-tokenserver-scripts-tests:
steps:
- run:
name: Tokenserver scripts tests
command: |
# NOTE: Python3.12 requires `--break-system-packages`.
# This command is run on the circleci/rust image, which is running python 3.10
pip3 install -r tools/tokenserver/requirements.txt
python3 tools/tokenserver/run_tests.py
environment:
SYNCSTORAGE_RS_IMAGE: app:build
run-e2e-spanner-tests:
steps:
@ -240,11 +240,11 @@ jobs:
- make-test-dir
- run-unit-tests
- merge-unit-test-coverage
- store-unit-test-results
# if the above tests don't run tokenserver-db tests (i.e. using --workspace)
# then run-tokenserver-scripts-tests will fail. These tests expect the db to be
# configured already, and it appears unit-tests modify the db to the expected state
- run-tokenserver-scripts-tests
- run-tokenserver-integration-tests
- store-test-results
#- save-sccache-cache
build-mysql-image:
docker:

View File

@ -16,11 +16,11 @@ PATH_TO_GRPC_CERT = ../server-syncstorage/local/lib/python2.7/site-packages/grpc
WORKFLOW := build-deploy
EPOCH_TIME := $(shell date +"%s")
TEST_RESULTS_DIR ?= workflow/test-results
TEST_PROFILE := $(if $(CIRCLECI),ci,default)
TEST_FILE_PREFIX := $(if $(CIRCLECI),$(CIRCLE_BUILD_NUM)__$(EPOCH_TIME)__$(CIRCLE_PROJECT_REPONAME)__$(WORKFLOW)__)
UNIT_JUNIT_XML := $(TEST_RESULTS_DIR)/$(TEST_FILE_PREFIX)unit__results.xml
UNIT_COVERAGE_JSON := $(TEST_RESULTS_DIR)/$(TEST_FILE_PREFIX)unit__coverage.json
INTEGRATION_JUNIT_XML := $(TEST_RESULTS_DIR)/$(TEST_FILE_PREFIX)integration__results.xml
SYNC_SYNCSTORAGE__DATABASE_URL ?= mysql://sample_user:sample_password@localhost/syncstorage_rs
SYNC_TOKENSERVER__DATABASE_URL ?= mysql://sample_user:sample_password@localhost/tokenserver_rs
@ -61,7 +61,6 @@ python:
python3 -m venv venv
venv/bin/python -m pip install -r requirements.txt
run_mysql: python
PATH="./venv/bin:$(PATH)" \
# See https://github.com/PyO3/pyo3/issues/1741 for discussion re: why we need to set the
@ -100,4 +99,9 @@ test_with_coverage:
exit $$exit_code
merge_coverage_results:
cargo llvm-cov report --summary-only --json --output-path ${UNIT_COVERAGE_JSON}
cargo llvm-cov report --summary-only --json --output-path ${UNIT_COVERAGE_JSON}
.ONESHELL:
run_token_server_integration_tests:
pip3 install -r tools/tokenserver/requirements.txt
pytest tools/tokenserver --junit-xml=${INTEGRATION_JUNIT_XML}

View File

@ -0,0 +1,4 @@
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

View File

@ -6,6 +6,7 @@ sqlalchemy==1.4.46
testfixtures
tokenlib==2.0.0
PyBrowserID==0.14.0
pytest==8.3.5
datadog
backoff

View File

@ -1,25 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import sys
import unittest
from test_database import TestDatabase
from test_process_account_events import TestProcessAccountEvents
from test_purge_old_records import TestPurgeOldRecords
from test_scripts import TestScripts
if __name__ == "__main__":
loader = unittest.TestLoader()
test_cases = [TestDatabase, TestPurgeOldRecords, TestProcessAccountEvents,
TestScripts]
res = 0
for test_case in test_cases:
suite = loader.loadTestsFromTestCase(test_case)
runner = unittest.TextTestRunner()
if not runner.run(suite).wasSuccessful():
res = 1
sys.exit(res)

View File

@ -467,23 +467,23 @@ class TestDatabase(unittest.TestCase):
def test_build_old_range(self):
params = dict()
sql = self.database._build_old_user_query(None, params)
self.assert_(sql.text.find("uid > :start") < 0)
self.assert_(sql.text.find("uid < :end") < 0)
self.assertTrue(sql.text.find("uid > :start") < 0)
self.assertTrue(sql.text.find("uid < :end") < 0)
self.assertIsNone(params.get("start"))
self.assertIsNone(params.get("end"))
params = dict()
rrange = (None, "abcd")
sql = self.database._build_old_user_query(rrange, params)
self.assert_(sql.text.find("uid > :start") < 0)
self.assert_(sql.text.find("uid < :end") > 0)
self.assertTrue(sql.text.find("uid > :start") < 0)
self.assertTrue(sql.text.find("uid < :end") > 0)
self.assertIsNone(params.get("start"))
self.assertEqual(params.get("end"), rrange[1])
params = dict()
rrange = ("1234", "abcd")
sql = self.database._build_old_user_query(rrange, params)
self.assert_(sql.text.find("uid > :start") > 0)
self.assert_(sql.text.find("uid < :end") > 0)
self.assertTrue(sql.text.find("uid > :start") > 0)
self.assertTrue(sql.text.find("uid < :end") > 0)
self.assertEqual(params.get("start"), rrange[0])
self.assertEqual(params.get("end"), rrange[1])