Skip to content

Commit aa4d1d6

Browse files
authored
Merge pull request #11 from NETWAYS/chore/extend-tests
Extend tests
2 parents ba72621 + 08f952f commit aa4d1d6

4 files changed

Lines changed: 38 additions & 11 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
lint:
44
python -m pylint check_vmware_nsxt.py
55
test:
6-
env TZ=UTC python -m unittest -v -b test_check_vmware_nsxt.py
6+
env TZ=UTC python -m unittest -v test_check_vmware_nsxt.py
77
coverage:
88
env TZ=UTC python -m coverage run -m unittest test_check_vmware_nsxt.py
99
env TZ=UTC python -m coverage report -m --include check_vmware_nsxt.py

check_vmware_nsxt.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,22 @@
5858
}
5959

6060

61-
def fix_tls_cert_store():
61+
def fix_tls_cert_store(cafile_path):
6262
"""
6363
Ensure we are using the system certstore by default
6464
6565
See https://github.com/psf/requests/issues/2966
6666
Inspired by https://github.com/psf/requests/issues/2966#issuecomment-614323746
6767
"""
6868

69-
try:
70-
system_ca_store = ssl.get_default_verify_paths().cafile
71-
if os.stat(system_ca_store).st_size > 0:
72-
requests.utils.DEFAULT_CA_BUNDLE_PATH = system_ca_store
73-
requests.adapters.DEFAULT_CA_BUNDLE_PATH = system_ca_store
74-
except:
75-
pass
69+
# Check if we got a CA file path
70+
if not cafile_path:
71+
return
72+
73+
# If CA file contains something, set as default
74+
if os.stat(cafile_path).st_size > 0:
75+
requests.utils.DEFAULT_CA_BUNDLE_PATH = cafile_path
76+
requests.adapters.DEFAULT_CA_BUNDLE_PATH = cafile_path
7677

7778

7879
class CriticalException(Exception):
@@ -406,7 +407,7 @@ def commandline(args):
406407

407408

408409
def main(args):
409-
fix_tls_cert_store()
410+
fix_tls_cert_store(ssl.get_default_verify_paths().cafile)
410411

411412
if args.insecure:
412413
urllib3.disable_warnings()

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
requests>=1
1+
requests>=2

test_check_vmware_nsxt.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
sys.path.append('..')
1212

13+
from check_vmware_nsxt import main
14+
from check_vmware_nsxt import fix_tls_cert_store
1315
from check_vmware_nsxt import commandline
1416
from check_vmware_nsxt import worst_state
1517
from check_vmware_nsxt import time_iso
@@ -19,6 +21,16 @@
1921

2022
os.environ["TZ"] = "UTC"
2123

24+
class MainTesting(unittest.TestCase):
25+
26+
@mock.patch('check_vmware_nsxt.Client')
27+
def test_main(self, mock_client):
28+
29+
args = commandline(['-A', 'api', '-u', 'user', '-p', 'password', '-m', 'alarms'])
30+
main(args)
31+
32+
mock_client.assert_called_with('api', 'user', 'password', verify=True, max_age=5)
33+
2234
class CLITesting(unittest.TestCase):
2335

2436
def test_commandline(self):
@@ -62,6 +74,20 @@ def test_time_iso(self):
6274
expected = datetime.datetime(1970, 1, 20, 11, 46, 28, 760000)
6375
self.assertEqual(actual, expected)
6476

77+
@mock.patch('os.stat')
78+
def test_fix_tls_cert_store(self, mock_os):
79+
80+
self.assertIsNone(fix_tls_cert_store(None))
81+
82+
m = mock.MagicMock()
83+
m.st_size = 10
84+
mock_os.return_value = m
85+
86+
fix_tls_cert_store("/tmp/foo")
87+
88+
mock_os.assert_called_with("/tmp/foo")
89+
90+
6591
class ClientTesting(unittest.TestCase):
6692

6793
@mock.patch('requests.request')

0 commit comments

Comments
 (0)