|
| 1 | +# infections.py |
| 2 | +# |
| 3 | +# A utility that provides a list of infections (forced internal errors). |
| 4 | +# Infections are injected into the application via the environment variable |
| 5 | +# 'INFECTIONS', a comma-separated list of infection names. |
| 6 | + |
| 7 | +import os |
| 8 | +from typing import Dict, Set |
| 9 | + |
| 10 | +from api.utils import deployment_mode_is_production |
| 11 | + |
| 12 | +# The built-in set of infections. |
| 13 | +# Must be lowercase, but user can use any case in the environment variable. |
| 14 | +# Define every name as a constant, and add it and a description to the _CATALOGUE. |
| 15 | +INFECTION_STRUCTURE_DOWNLOAD: str = 'structure-download' |
| 16 | + |
| 17 | +# The index is the short-form name of the infection, and the value is the |
| 18 | +# description of the infection. |
| 19 | +_CATALOGUE: Dict[str, str] = { |
| 20 | + INFECTION_STRUCTURE_DOWNLOAD: 'An error in the DownloadStructures view' |
| 21 | +} |
| 22 | + |
| 23 | +# What infection have been set? |
| 24 | +_INFECTIONS: str = os.environ.get('INFECTIONS', '').lower() |
| 25 | + |
| 26 | + |
| 27 | +def have_infection(name: str) -> bool: |
| 28 | + """Returns True if we've been given the named infection. |
| 29 | + Infections are never present in production mode.""" |
| 30 | + return False if deployment_mode_is_production() else name in _get_infections() |
| 31 | + |
| 32 | + |
| 33 | +def _get_infections() -> Set[str]: |
| 34 | + if _INFECTIONS == '': |
| 35 | + return set() |
| 36 | + infections: set[str] = { |
| 37 | + infection for infection in _INFECTIONS.split(',') if infection in _CATALOGUE |
| 38 | + } |
| 39 | + return infections |
0 commit comments