|
8 | 8 | import sys
|
9 | 9 | from pipes import quote
|
10 | 10 | import yaml
|
| 11 | +import warnings |
11 | 12 |
|
12 | 13 | try:
|
13 | 14 | from molecule.api import drivers
|
@@ -38,91 +39,102 @@ def pytest_addoption(parser):
|
38 | 39 |
|
39 | 40 | def pytest_configure(config):
|
40 | 41 |
|
41 |
| - config.option.molecule = {} |
42 |
| - for driver in drivers(): |
43 |
| - config.addinivalue_line( |
44 |
| - "markers", "{0}: mark test to run only when {0} is available".format(driver) |
45 |
| - ) |
46 |
| - config.option.molecule[driver] = {"available": True} |
47 |
| - # TODO(ssbarnea): extend molecule itself to allow it to report usable drivers |
48 |
| - if driver == "docker": |
49 |
| - try: |
50 |
| - import docker |
51 |
| - |
52 |
| - # validate docker connectivity |
53 |
| - # Default docker value is 60s but we want to fail faster |
54 |
| - # With parallel execution 5s proved to give errors. |
55 |
| - c = docker.from_env(timeout=10, version="auto") |
56 |
| - if not c.ping(): |
57 |
| - raise Exception("Failed to ping docker server.") |
58 |
| - |
59 |
| - except Exception as e: |
60 |
| - msg = "Molecule {} driver is not available due to: {}.".format( |
61 |
| - driver, e |
62 |
| - ) |
63 |
| - if config.option.molecule_unavailable_driver: |
64 |
| - msg += " We will tag scenarios using it with '{}' marker.".format( |
65 |
| - config.option.molecule_unavailable_driver |
| 42 | + # We hide DeprecationWarnings thrown by driver loading because these are |
| 43 | + # outside our control and worse: they are displayed even on projects that |
| 44 | + # have no molecule tests at all as pytest_configure() is called during |
| 45 | + # collection, causing spam. |
| 46 | + with warnings.catch_warnings(): |
| 47 | + warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 48 | + |
| 49 | + config.option.molecule = {} |
| 50 | + for driver in drivers(): |
| 51 | + config.addinivalue_line( |
| 52 | + "markers", |
| 53 | + "{0}: mark test to run only when {0} is available".format(driver), |
| 54 | + ) |
| 55 | + config.option.molecule[driver] = {"available": True} |
| 56 | + # TODO(ssbarnea): extend molecule itself to allow it to report usable drivers |
| 57 | + if driver == "docker": |
| 58 | + try: |
| 59 | + import docker |
| 60 | + |
| 61 | + # validate docker connectivity |
| 62 | + # Default docker value is 60s but we want to fail faster |
| 63 | + # With parallel execution 5s proved to give errors. |
| 64 | + c = docker.from_env(timeout=10, version="auto") |
| 65 | + if not c.ping(): |
| 66 | + raise Exception("Failed to ping docker server.") |
| 67 | + |
| 68 | + except Exception as e: |
| 69 | + msg = "Molecule {} driver is not available due to: {}.".format( |
| 70 | + driver, e |
66 | 71 | )
|
67 |
| - logging.getLogger().warning(msg) |
68 |
| - config.option.molecule[driver]["available"] = False |
69 |
| - |
70 |
| - if driver == "delegated": |
71 |
| - # To protect ourselves from case where a molecule scenario using |
72 |
| - # `delegated` is accidentally altering the localhost on a developer |
73 |
| - # machine, we verify run delegated tests only when ansible `zuul` |
74 |
| - # or `use_for_testing` vars are defined. |
75 |
| - cmd = [ |
76 |
| - "ansible", |
77 |
| - "localhost", |
78 |
| - "-e", |
79 |
| - "ansible_connection=local", |
80 |
| - "-o", |
81 |
| - "-m", |
82 |
| - "shell", |
83 |
| - "-a", |
84 |
| - "exit {% if zuul is defined or use_for_testing is defined %}0{% else %}1{% endif %}", |
85 |
| - ] |
86 |
| - try: |
87 |
| - p = subprocess.Popen( |
88 |
| - cmd, |
89 |
| - stdout=subprocess.DEVNULL, |
90 |
| - stderr=subprocess.DEVNULL, |
91 |
| - universal_newlines=True, |
92 |
| - ) |
93 |
| - p.wait() |
94 |
| - if p.returncode != 0: |
95 |
| - raise Exception( |
96 |
| - "Error code %s returned by: %s" % (p.returncode, " ".join(cmd)) |
| 72 | + if config.option.molecule_unavailable_driver: |
| 73 | + msg += " We will tag scenarios using it with '{}' marker.".format( |
| 74 | + config.option.molecule_unavailable_driver |
| 75 | + ) |
| 76 | + logging.getLogger().warning(msg) |
| 77 | + config.option.molecule[driver]["available"] = False |
| 78 | + |
| 79 | + if driver == "delegated": |
| 80 | + # To protect ourselves from case where a molecule scenario using |
| 81 | + # `delegated` is accidentally altering the localhost on a developer |
| 82 | + # machine, we verify run delegated tests only when ansible `zuul` |
| 83 | + # or `use_for_testing` vars are defined. |
| 84 | + cmd = [ |
| 85 | + "ansible", |
| 86 | + "localhost", |
| 87 | + "-e", |
| 88 | + "ansible_connection=local", |
| 89 | + "-o", |
| 90 | + "-m", |
| 91 | + "shell", |
| 92 | + "-a", |
| 93 | + "exit {% if zuul is defined or use_for_testing is defined %}0{% else %}1{% endif %}", |
| 94 | + ] |
| 95 | + try: |
| 96 | + p = subprocess.Popen( |
| 97 | + cmd, |
| 98 | + stdout=subprocess.DEVNULL, |
| 99 | + stderr=subprocess.DEVNULL, |
| 100 | + universal_newlines=True, |
97 | 101 | )
|
98 |
| - except Exception: |
99 |
| - msg = "Molecule {} driver was not enabled because missing zuul.build variable in current inventory.".format( |
100 |
| - driver |
101 |
| - ) |
102 |
| - if config.option.molecule_unavailable_driver: |
103 |
| - msg += " We will tag scenarios using it with '{}' marker.".format( |
104 |
| - config.option.molecule_unavailable_driver |
| 102 | + p.wait() |
| 103 | + if p.returncode != 0: |
| 104 | + raise Exception( |
| 105 | + "Error code %s returned by: %s" |
| 106 | + % (p.returncode, " ".join(cmd)) |
| 107 | + ) |
| 108 | + except Exception: |
| 109 | + msg = "Molecule {} driver was not enabled because missing zuul.build variable in current inventory.".format( |
| 110 | + driver |
105 | 111 | )
|
106 |
| - logging.getLogger().warning(msg) |
107 |
| - config.option.molecule[driver]["available"] = False |
| 112 | + if config.option.molecule_unavailable_driver: |
| 113 | + msg += " We will tag scenarios using it with '{}' marker.".format( |
| 114 | + config.option.molecule_unavailable_driver |
| 115 | + ) |
| 116 | + logging.getLogger().warning(msg) |
| 117 | + config.option.molecule[driver]["available"] = False |
108 | 118 |
|
109 |
| - config.addinivalue_line("markers", "molecule: mark used by all molecule scenarios") |
| 119 | + config.addinivalue_line( |
| 120 | + "markers", "molecule: mark used by all molecule scenarios" |
| 121 | + ) |
110 | 122 |
|
111 |
| - # validate selinux availability |
112 |
| - if sys.platform == "linux" and os.path.isfile("/etc/selinux/config"): |
113 |
| - try: |
114 |
| - import selinux # noqa |
115 |
| - except Exception: |
116 |
| - logging.error( |
117 |
| - "It appears that you are trying to use " |
118 |
| - "molecule with a Python interpreter that does not have the " |
119 |
| - "libselinux python bindings installed. These can only be " |
120 |
| - "installed using your distro package manager and are specific " |
121 |
| - "to each python version. Common package names: " |
122 |
| - "libselinux-python python2-libselinux python3-libselinux" |
123 |
| - ) |
124 |
| - # we do not re-raise this exception because missing or broken |
125 |
| - # selinux bindings are not guaranteed to fail molecule execution. |
| 123 | + # validate selinux availability |
| 124 | + if sys.platform == "linux" and os.path.isfile("/etc/selinux/config"): |
| 125 | + try: |
| 126 | + import selinux # noqa |
| 127 | + except Exception: |
| 128 | + logging.error( |
| 129 | + "It appears that you are trying to use " |
| 130 | + "molecule with a Python interpreter that does not have the " |
| 131 | + "libselinux python bindings installed. These can only be " |
| 132 | + "installed using your distro package manager and are specific " |
| 133 | + "to each python version. Common package names: " |
| 134 | + "libselinux-python python2-libselinux python3-libselinux" |
| 135 | + ) |
| 136 | + # we do not re-raise this exception because missing or broken |
| 137 | + # selinux bindings are not guaranteed to fail molecule execution. |
126 | 138 |
|
127 | 139 |
|
128 | 140 | def pytest_collect_file(parent, path):
|
|
0 commit comments