From 2cb831fac5a564e785d667827bad7be72892af2a Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Mon, 24 May 2021 13:05:41 +0200 Subject: [PATCH] Reenabling extension tests (#929) * Fixed and enabled extension tests * Narrowed types for display_mode * Updated package-lock.json * Removed storage.rst --- .github/workflows/run-tests.yaml | 2 +- crawler.py | 5 ++++- openwpm/Extension/firefox/feature.js/loggingdb.js | 2 +- openwpm/config.py | 4 ++-- openwpm/task_manager.py | 1 - test/conftest.py | 6 +++--- test/extension/test_logging.py | 2 +- test/extension/test_startup_timeout.py | 12 ++++-------- test/openwpmtest.py | 4 ++-- test/test_http_instrumentation.py | 12 ++++-------- 10 files changed, 22 insertions(+), 28 deletions(-) diff --git a/.github/workflows/run-tests.yaml b/.github/workflows/run-tests.yaml index 720d5c8b..fcc235cc 100644 --- a/.github/workflows/run-tests.yaml +++ b/.github/workflows/run-tests.yaml @@ -48,7 +48,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - test-groups: ["test/test_[a-e]*", "test/test_[f-h]*", "test/test_[i-o,q-r,t-z]*", "test/test_[p]*", "test/test_[s]*", "test/storage/*"] + test-groups: ["test/test_[a-e]*", "test/test_[f-h]*", "test/test_[i-o,q-r,t-z]*", "test/test_[p]*", "test/test_[s]*", "test/storage/*", "test/extension/*"] fail-fast: false steps: # All of these steps are just setup, maybe we should wrap them in an action diff --git a/crawler.py b/crawler.py index 8780bae9..26bbc0c6 100644 --- a/crawler.py +++ b/crawler.py @@ -4,9 +4,10 @@ import os import signal import sys import time +import typing from pathlib import Path from threading import Lock -from typing import Any, Callable, List +from typing import Any, Callable, List, Literal import sentry_sdk @@ -36,6 +37,8 @@ AUTH_TOKEN = os.getenv("GCP_AUTH_TOKEN", "cloud") # Browser Params DISPLAY_MODE = os.getenv("DISPLAY_MODE", "headless") +assert DISPLAY_MODE in ["headless", "xvfb", "native"] +DISPLAY_MODE = typing.cast(Literal["headless", "xvfb", "native"], DISPLAY_MODE) HTTP_INSTRUMENT = os.getenv("HTTP_INSTRUMENT", "1") == "1" COOKIE_INSTRUMENT = os.getenv("COOKIE_INSTRUMENT", "1") == "1" NAVIGATION_INSTRUMENT = os.getenv("NAVIGATION_INSTRUMENT", "1") == "1" diff --git a/openwpm/Extension/firefox/feature.js/loggingdb.js b/openwpm/Extension/firefox/feature.js/loggingdb.js index 77d58b5f..b6034f0b 100644 --- a/openwpm/Extension/firefox/feature.js/loggingdb.js +++ b/openwpm/Extension/firefox/feature.js/loggingdb.js @@ -70,7 +70,7 @@ export let open = async function(storageControllerAddress, logAddress, curr_craw // Listen for incoming urls as visit ids listeningSocket = new socket.ListeningSocket(listeningSocketCallback); console.log("Starting socket listening for incoming connections."); - listeningSocket.startListening().then(() => { + await listeningSocket.startListening().then(() => { browser.profileDirIO.writeFile("extension_port.txt", `${listeningSocket.port}`); }); }; diff --git a/openwpm/config.py b/openwpm/config.py index 57dfe706..05e89ed9 100644 --- a/openwpm/config.py +++ b/openwpm/config.py @@ -2,7 +2,7 @@ import os from dataclasses import dataclass, field from json import JSONEncoder from pathlib import Path -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, List, Literal, Optional, Tuple, Union from dataclasses_json import DataClassJsonMixin from dataclasses_json import config as DCJConfig @@ -92,7 +92,7 @@ class BrowserParams(DataClassJsonMixin): seed_tar: Optional[Path] = field( default=None, metadata=DCJConfig(encoder=path_to_str, decoder=str_to_path) ) - display_mode: str = "native" + display_mode: Literal["native", "headless", "xvfb"] = "native" browser: str = "firefox" prefs: dict = field(default_factory=dict) tp_cookies: str = "always" diff --git a/openwpm/task_manager.py b/openwpm/task_manager.py index e321851c..8c5017bd 100644 --- a/openwpm/task_manager.py +++ b/openwpm/task_manager.py @@ -89,7 +89,6 @@ class TaskManager: if not os.path.exists(manager_params.source_dump_path): os.makedirs(manager_params.source_dump_path) - # Check size of parameter dictionary self.num_browsers = manager_params.num_browsers # Parse and flesh out js_instrument_settings diff --git a/test/conftest.py b/test/conftest.py index c9e7086a..7d146fd9 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -2,7 +2,7 @@ import logging import os import subprocess from pathlib import Path -from typing import Any, Callable, Generator, List, Tuple +from typing import Any, Callable, Generator, List, Literal, Tuple import pytest @@ -93,13 +93,13 @@ def task_manager_creator( @pytest.fixture() def http_params( default_params: Tuple[ManagerParams, List[BrowserParams]], -) -> Callable[[str], Tuple[ManagerParams, List[BrowserParams]]]: +) -> Callable[[Literal["headless", "xvfb"]], Tuple[ManagerParams, List[BrowserParams]]]: manager_params, browser_params = default_params for browser_param in browser_params: browser_param.http_instrument = True def parameterize( - display_mode: str = "headless", + display_mode: Literal["headless", "xvfb"] = "headless", ) -> Tuple[ManagerParams, List[BrowserParams]]: for browser_param in browser_params: browser_param.display_mode = display_mode diff --git a/test/extension/test_logging.py b/test/extension/test_logging.py index 1eb5a759..9b13e873 100644 --- a/test/extension/test_logging.py +++ b/test/extension/test_logging.py @@ -19,4 +19,4 @@ def test_extension_logging(task_manager_creator, default_params, tmp_path): if test_msg in log_line: log_list.append(log_line) # We expect to see it once when printing the config and once when printing the log message - assert len(log_line) == 2 * manager_params.num_browsers + assert len(log_list) == 2 * manager_params.num_browsers diff --git a/test/extension/test_startup_timeout.py b/test/extension/test_startup_timeout.py index e6542188..be7f8153 100644 --- a/test/extension/test_startup_timeout.py +++ b/test/extension/test_startup_timeout.py @@ -10,14 +10,10 @@ def test_extension_startup_timeout(task_manager_creator, default_params): browser_param.custom_params[ "pre_instrumentation_code" ] = """ - (async () => { - const myPromise = new Promise((resolve, reject) => { - setTimeout(() => { - resolve(); - }, 20000); // Delaying for 20 seconds - }); - await myPromise; - })() + const startTime = Date.now(); + while (Date.now() - startTime < 20000) { // Delaying for 20s + console.log("delaying startup"); + }; """ with pytest.raises(BrowserConfigError) as error: tm, _ = task_manager_creator((manager_params, browser_params[:1])) diff --git a/test/openwpmtest.py b/test/openwpmtest.py index 74fe78ac..6a788cfb 100644 --- a/test/openwpmtest.py +++ b/test/openwpmtest.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import List, Optional, Tuple +from typing import List, Literal, Optional, Tuple import pytest @@ -51,7 +51,7 @@ class OpenWPMTest: self, data_dir: Optional[Path] = None, num_browsers: int = NUM_BROWSERS, - display_mode: str = "headless", + display_mode: Literal["headless", "xvfb"] = "headless", ) -> Tuple[ManagerParams, List[BrowserParams]]: """Load and return the default test parameters.""" if not data_dir: diff --git a/test/test_http_instrumentation.py b/test/test_http_instrumentation.py index 62f8da23..842f9853 100644 --- a/test/test_http_instrumentation.py +++ b/test/test_http_instrumentation.py @@ -822,14 +822,10 @@ def test_page_visit(task_manager_creator, http_params, delayed): browser_param.custom_params[ "pre_instrumentation_code" ] = """ - (async () => { - const myPromise = new Promise((resolve, reject) => { - setTimeout(() => { - resolve(); - }, 5000); // Delaying for 5 seconds - }); - await myPromise; - })() + const startTime = Date.now(); + while (Date.now() - startTime < 5000) { // Delaying for 5s + console.log("delaying startup"); + }; """ tm, db = task_manager_creator((manager_params, browser_params))