Skip to content

Commit

Permalink
Merge pull request #11 from NETWAYS/chore/extend-tests
Browse files Browse the repository at this point in the history
Extend tests
  • Loading branch information
martialblog authored Aug 1, 2023
2 parents ba72621 + 08f952f commit aa4d1d6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
lint:
python -m pylint check_vmware_nsxt.py
test:
env TZ=UTC python -m unittest -v -b test_check_vmware_nsxt.py
env TZ=UTC python -m unittest -v test_check_vmware_nsxt.py
coverage:
env TZ=UTC python -m coverage run -m unittest test_check_vmware_nsxt.py
env TZ=UTC python -m coverage report -m --include check_vmware_nsxt.py
19 changes: 10 additions & 9 deletions check_vmware_nsxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,22 @@
}


def fix_tls_cert_store():
def fix_tls_cert_store(cafile_path):
"""
Ensure we are using the system certstore by default
See https://github.com/psf/requests/issues/2966
Inspired by https://github.com/psf/requests/issues/2966#issuecomment-614323746
"""

try:
system_ca_store = ssl.get_default_verify_paths().cafile
if os.stat(system_ca_store).st_size > 0:
requests.utils.DEFAULT_CA_BUNDLE_PATH = system_ca_store
requests.adapters.DEFAULT_CA_BUNDLE_PATH = system_ca_store
except:
pass
# Check if we got a CA file path
if not cafile_path:
return

# If CA file contains something, set as default
if os.stat(cafile_path).st_size > 0:
requests.utils.DEFAULT_CA_BUNDLE_PATH = cafile_path
requests.adapters.DEFAULT_CA_BUNDLE_PATH = cafile_path


class CriticalException(Exception):
Expand Down Expand Up @@ -406,7 +407,7 @@ def commandline(args):


def main(args):
fix_tls_cert_store()
fix_tls_cert_store(ssl.get_default_verify_paths().cafile)

if args.insecure:
urllib3.disable_warnings()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests>=1
requests>=2
26 changes: 26 additions & 0 deletions test_check_vmware_nsxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

sys.path.append('..')

from check_vmware_nsxt import main
from check_vmware_nsxt import fix_tls_cert_store
from check_vmware_nsxt import commandline
from check_vmware_nsxt import worst_state
from check_vmware_nsxt import time_iso
Expand All @@ -19,6 +21,16 @@

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

class MainTesting(unittest.TestCase):

@mock.patch('check_vmware_nsxt.Client')
def test_main(self, mock_client):

args = commandline(['-A', 'api', '-u', 'user', '-p', 'password', '-m', 'alarms'])
main(args)

mock_client.assert_called_with('api', 'user', 'password', verify=True, max_age=5)

class CLITesting(unittest.TestCase):

def test_commandline(self):
Expand Down Expand Up @@ -62,6 +74,20 @@ def test_time_iso(self):
expected = datetime.datetime(1970, 1, 20, 11, 46, 28, 760000)
self.assertEqual(actual, expected)

@mock.patch('os.stat')
def test_fix_tls_cert_store(self, mock_os):

self.assertIsNone(fix_tls_cert_store(None))

m = mock.MagicMock()
m.st_size = 10
mock_os.return_value = m

fix_tls_cert_store("/tmp/foo")

mock_os.assert_called_with("/tmp/foo")


class ClientTesting(unittest.TestCase):

@mock.patch('requests.request')
Expand Down

0 comments on commit aa4d1d6

Please sign in to comment.