prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  QA: Automate basic server testing
  The default QA test plan includes a basic testing section that mostly checks server configuration. These tests are duplicated in the production testinfra tests, so with some work to get `testinfra` to use production settings where available (via `install_files/ansible-base/group_vars/all/site-specific`), it should be possible to reduce tester workload by removing Basic testing in favour of `testinfra`.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `molecule/testinfra/conftest.py`
  Content:
  ```
  1 """
  2 Configuration for TestInfra test suite for SecureDrop.
  3 Handles importing host-specific test vars, so test functions
  4 can be reused across multiple hosts, with varied targets.
  5 
  6 Vars should be placed in `testinfra/vars/<hostname>.yml`.
  7 """
  8 
  9 import io
  10 import os
  11 import yaml
  12 from typing import Any, Dict
  13 
  14 import testutils
  15 
  16 
  17 # The config tests target staging by default. It's possible to override
  18 # for e.g. prod, but the associated vars files are not yet ported.
  19 target_host = os.environ.get('SECUREDROP_TESTINFRA_TARGET_HOST', 'staging')
  20 
  21 
  22 def securedrop_import_testinfra_vars(hostname, with_header=False):
  23     """
  24     Import vars from a YAML file to populate tests with host-specific
  25     values used in checks. For instance, the SecureDrop docroot will
  26     be under /vagrant in development, but /var/www/securedrop in staging.
  27 
  28     Vars must be stored in `testinfra/vars/<hostname>.yml`.
  29     """
  30     filepath = os.path.join(os.path.dirname(__file__), "vars", hostname+".yml")
  31     with io.open(filepath, 'r') as f:
  32         hostvars = yaml.safe_load(f)
  33 
  34     hostvars['securedrop_venv_site_packages'] = hostvars["securedrop_venv_site_packages"].format("3.8")  # noqa: E501
  35     hostvars['python_version'] = "3.8"
  36     hostvars['apparmor_enforce_actual'] = hostvars['apparmor_enforce']['focal']
  37 
  38     if with_header:
  39         hostvars = dict(securedrop_test_vars=hostvars)
  40 
  41     return hostvars
  42 
  43 
  44 class TestVars(dict):
  45     managed_attrs = {}  # type: Dict[str, Any]
  46 
  47     def __init__(self, initial: Dict[str, Any]) -> None:
  48         self.securedrop_target_distribution = os.environ.get("SECUREDROP_TARGET_DISTRIBUTION")
  49         self.managed_attrs.update(initial)
  50 
  51     def __getattr__(self, name: str) -> Any:
  52         """
  53         If the requested attribute names a dict in managed_attrs and that
  54         contains a key with the name of the target distribution,
  55         e.g. "focal", return that. Otherwise return the entire item
  56         under the requested name.
  57         """
  58         try:
  59             attr = self.managed_attrs[name]
  60             if isinstance(attr, dict) and self.securedrop_target_distribution in attr:
  61                 return attr[self.securedrop_target_distribution]
  62             return attr
  63         except KeyError:
  64             raise AttributeError(name)
  65 
  66     def __str__(self) -> str:
  67         return str(self.managed_attrs)
  68 
  69 
  70 testutils.securedrop_test_vars = TestVars(securedrop_import_testinfra_vars(target_host))
  71
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/molecule/testinfra/conftest.py b/molecule/testinfra/conftest.py
  --- a/molecule/testinfra/conftest.py
  +++ b/molecule/testinfra/conftest.py
  @@ -14,8 +14,7 @@
   import testutils
   
   
  -# The config tests target staging by default. It's possible to override
  -# for e.g. prod, but the associated vars files are not yet ported.
  +# The config tests target staging by default.
   target_host = os.environ.get('SECUREDROP_TESTINFRA_TARGET_HOST', 'staging')
   
   
  @@ -35,6 +34,34 @@
       hostvars['python_version'] = "3.8"
       hostvars['apparmor_enforce_actual'] = hostvars['apparmor_enforce']['focal']
   
  +    # If the tests are run against a production environment, check local config
  +    # and override as necessary.
  +    prod_filepath = os.path.join(os.path.dirname(__file__),
  +                                 "../../install_files/ansible-base/group_vars/all/site-specific")
  +    if os.path.isfile(prod_filepath):
  +        with io.open(prod_filepath, 'r') as f:
  +            prodvars = yaml.safe_load(f)
  +
  +        def _prod_override(vars_key, prod_key):
  +            if prod_key in prodvars:
  +                hostvars[vars_key] = prodvars[prod_key]
  +
  +        _prod_override('app_ip', 'app_ip')
  +        _prod_override('mon_ip', 'monitor_ip')
  +        _prod_override('sasl_domain', 'sasl_domain')
  +        _prod_override('sasl_username', 'sasl_username')
  +        _prod_override('sasl_password', 'sasl_password')
  +        _prod_override('daily_reboot_time', 'daily_reboot_time')
  +
  +        # Check repo targeting, and update vars
  +        repo_filepath = os.path.join(os.path.dirname(__file__),
  +                                 "../../install_files/ansible-base/roles/install-fpf-repo/defaults/main.yml")  # noqa: E501
  +        if os.path.isfile(repo_filepath):
  +            with io.open(repo_filepath, 'r') as f:
  +                repovars = yaml.safe_load(f)
  +                if 'apt_repo_url' in repovars:
  +                    hostvars['fpf_apt_repo_url'] = repovars['apt_repo_url']
  +
       if with_header:
           hostvars = dict(securedrop_test_vars=hostvars)
  
verification_info: {"golden_diff": "diff --git a/molecule/testinfra/conftest.py b/molecule/testinfra/conftest.py\n--- a/molecule/testinfra/conftest.py\n+++ b/molecule/testinfra/conftest.py\n@@ -14,8 +14,7 @@\n import testutils\n \n \n-# The config tests target staging by default. It's possible to override\n-# for e.g. prod, but the associated vars files are not yet ported.\n+# The config tests target staging by default.\n target_host = os.environ.get('SECUREDROP_TESTINFRA_TARGET_HOST', 'staging')\n \n \n@@ -35,6 +34,34 @@\n     hostvars['python_version'] = \"3.8\"\n     hostvars['apparmor_enforce_actual'] = hostvars['apparmor_enforce']['focal']\n \n+    # If the tests are run against a production environment, check local config\n+    # and override as necessary.\n+    prod_filepath = os.path.join(os.path.dirname(__file__),\n+                                 \"../../install_files/ansible-base/group_vars/all/site-specific\")\n+    if os.path.isfile(prod_filepath):\n+        with io.open(prod_filepath, 'r') as f:\n+            prodvars = yaml.safe_load(f)\n+\n+        def _prod_override(vars_key, prod_key):\n+            if prod_key in prodvars:\n+                hostvars[vars_key] = prodvars[prod_key]\n+\n+        _prod_override('app_ip', 'app_ip')\n+        _prod_override('mon_ip', 'monitor_ip')\n+        _prod_override('sasl_domain', 'sasl_domain')\n+        _prod_override('sasl_username', 'sasl_username')\n+        _prod_override('sasl_password', 'sasl_password')\n+        _prod_override('daily_reboot_time', 'daily_reboot_time')\n+\n+        # Check repo targeting, and update vars\n+        repo_filepath = os.path.join(os.path.dirname(__file__),\n+                                 \"../../install_files/ansible-base/roles/install-fpf-repo/defaults/main.yml\")  # noqa: E501\n+        if os.path.isfile(repo_filepath):\n+            with io.open(repo_filepath, 'r') as f:\n+                repovars = yaml.safe_load(f)\n+                if 'apt_repo_url' in repovars:\n+                    hostvars['fpf_apt_repo_url'] = repovars['apt_repo_url']\n+\n     if with_header:\n         hostvars = dict(securedrop_test_vars=hostvars)\n", "issue": "QA: Automate basic server testing\nThe default QA test plan includes a basic testing section that mostly checks server configuration. These tests are duplicated in the production testinfra tests, so with some work to get `testinfra` to use production settings where available (via `install_files/ansible-base/group_vars/all/site-specific`), it should be possible to reduce tester workload by removing Basic testing in favour of `testinfra`.\n", "before_files": [{"content": "\"\"\"\nConfiguration for TestInfra test suite for SecureDrop.\nHandles importing host-specific test vars, so test functions\ncan be reused across multiple hosts, with varied targets.\n\nVars should be placed in `testinfra/vars/<hostname>.yml`.\n\"\"\"\n\nimport io\nimport os\nimport yaml\nfrom typing import Any, Dict\n\nimport testutils\n\n\n# The config tests target staging by default. It's possible to override\n# for e.g. prod, but the associated vars files are not yet ported.\ntarget_host = os.environ.get('SECUREDROP_TESTINFRA_TARGET_HOST', 'staging')\n\n\ndef securedrop_import_testinfra_vars(hostname, with_header=False):\n    \"\"\"\n    Import vars from a YAML file to populate tests with host-specific\n    values used in checks. For instance, the SecureDrop docroot will\n    be under /vagrant in development, but /var/www/securedrop in staging.\n\n    Vars must be stored in `testinfra/vars/<hostname>.yml`.\n    \"\"\"\n    filepath = os.path.join(os.path.dirname(__file__), \"vars\", hostname+\".yml\")\n    with io.open(filepath, 'r') as f:\n        hostvars = yaml.safe_load(f)\n\n    hostvars['securedrop_venv_site_packages'] = hostvars[\"securedrop_venv_site_packages\"].format(\"3.8\")  # noqa: E501\n    hostvars['python_version'] = \"3.8\"\n    hostvars['apparmor_enforce_actual'] = hostvars['apparmor_enforce']['focal']\n\n    if with_header:\n        hostvars = dict(securedrop_test_vars=hostvars)\n\n    return hostvars\n\n\nclass TestVars(dict):\n    managed_attrs = {}  # type: Dict[str, Any]\n\n    def __init__(self, initial: Dict[str, Any]) -> None:\n        self.securedrop_target_distribution = os.environ.get(\"SECUREDROP_TARGET_DISTRIBUTION\")\n        self.managed_attrs.update(initial)\n\n    def __getattr__(self, name: str) -> Any:\n        \"\"\"\n        If the requested attribute names a dict in managed_attrs and that\n        contains a key with the name of the target distribution,\n        e.g. \"focal\", return that. Otherwise return the entire item\n        under the requested name.\n        \"\"\"\n        try:\n            attr = self.managed_attrs[name]\n            if isinstance(attr, dict) and self.securedrop_target_distribution in attr:\n                return attr[self.securedrop_target_distribution]\n            return attr\n        except KeyError:\n            raise AttributeError(name)\n\n    def __str__(self) -> str:\n        return str(self.managed_attrs)\n\n\ntestutils.securedrop_test_vars = TestVars(securedrop_import_testinfra_vars(target_host))\n", "path": "molecule/testinfra/conftest.py"}], "after_files": [{"content": "\"\"\"\nConfiguration for TestInfra test suite for SecureDrop.\nHandles importing host-specific test vars, so test functions\ncan be reused across multiple hosts, with varied targets.\n\nVars should be placed in `testinfra/vars/<hostname>.yml`.\n\"\"\"\n\nimport io\nimport os\nimport yaml\nfrom typing import Any, Dict\n\nimport testutils\n\n\n# The config tests target staging by default.\ntarget_host = os.environ.get('SECUREDROP_TESTINFRA_TARGET_HOST', 'staging')\n\n\ndef securedrop_import_testinfra_vars(hostname, with_header=False):\n    \"\"\"\n    Import vars from a YAML file to populate tests with host-specific\n    values used in checks. For instance, the SecureDrop docroot will\n    be under /vagrant in development, but /var/www/securedrop in staging.\n\n    Vars must be stored in `testinfra/vars/<hostname>.yml`.\n    \"\"\"\n    filepath = os.path.join(os.path.dirname(__file__), \"vars\", hostname+\".yml\")\n    with io.open(filepath, 'r') as f:\n        hostvars = yaml.safe_load(f)\n\n    hostvars['securedrop_venv_site_packages'] = hostvars[\"securedrop_venv_site_packages\"].format(\"3.8\")  # noqa: E501\n    hostvars['python_version'] = \"3.8\"\n    hostvars['apparmor_enforce_actual'] = hostvars['apparmor_enforce']['focal']\n\n    # If the tests are run against a production environment, check local config\n    # and override as necessary.\n    prod_filepath = os.path.join(os.path.dirname(__file__),\n                                 \"../../install_files/ansible-base/group_vars/all/site-specific\")\n    if os.path.isfile(prod_filepath):\n        with io.open(prod_filepath, 'r') as f:\n            prodvars = yaml.safe_load(f)\n\n        def _prod_override(vars_key, prod_key):\n            if prod_key in prodvars:\n                hostvars[vars_key] = prodvars[prod_key]\n\n        _prod_override('app_ip', 'app_ip')\n        _prod_override('mon_ip', 'monitor_ip')\n        _prod_override('sasl_domain', 'sasl_domain')\n        _prod_override('sasl_username', 'sasl_username')\n        _prod_override('sasl_password', 'sasl_password')\n        _prod_override('daily_reboot_time', 'daily_reboot_time')\n\n        # Check repo targeting, and update vars\n        repo_filepath = os.path.join(os.path.dirname(__file__),\n                                 \"../../install_files/ansible-base/roles/install-fpf-repo/defaults/main.yml\")  # noqa: E501\n        if os.path.isfile(repo_filepath):\n            with io.open(repo_filepath, 'r') as f:\n                repovars = yaml.safe_load(f)\n                if 'apt_repo_url' in repovars:\n                    hostvars['fpf_apt_repo_url'] = repovars['apt_repo_url']\n\n    if with_header:\n        hostvars = dict(securedrop_test_vars=hostvars)\n\n    return hostvars\n\n\nclass TestVars(dict):\n    managed_attrs = {}  # type: Dict[str, Any]\n\n    def __init__(self, initial: Dict[str, Any]) -> None:\n        self.securedrop_target_distribution = os.environ.get(\"SECUREDROP_TARGET_DISTRIBUTION\")\n        self.managed_attrs.update(initial)\n\n    def __getattr__(self, name: str) -> Any:\n        \"\"\"\n        If the requested attribute names a dict in managed_attrs and that\n        contains a key with the name of the target distribution,\n        e.g. \"focal\", return that. Otherwise return the entire item\n        under the requested name.\n        \"\"\"\n        try:\n            attr = self.managed_attrs[name]\n            if isinstance(attr, dict) and self.securedrop_target_distribution in attr:\n                return attr[self.securedrop_target_distribution]\n            return attr\n        except KeyError:\n            raise AttributeError(name)\n\n    def __str__(self) -> str:\n        return str(self.managed_attrs)\n\n\ntestutils.securedrop_test_vars = TestVars(securedrop_import_testinfra_vars(target_host))\n", "path": "molecule/testinfra/conftest.py"}]}