pegleg/tests/unit/test_utils.py
Ian H. Pittwood 4480ab5574 Restructure usage of test fixtures
Pytest includes a fixture that can be used to generate temporary
directories. Previously Pegleg had implemented a hombrewed version of a
temporary directory fixture. This change removes the homebrewed version
and replaces it with the tmpdir fixture.

Implement tmpdir fixture in tests

Upgrade all testing packages to use the latest features

Removes unused imports and organizes import lists

Removes mock package requirement and uses unittest.mock, included in
python >3.3

Implements a slightly cleaner method to get proxy info

Change-Id: If66e1cfba858d5fb8948529deb8fb2d32345f630
2019-07-29 11:37:36 -05:00

95 lines
2.8 KiB
Python

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# Copyright 2015 Hewlett-Packard Development Company, L.P.
# Copyright 2017 AT&T Intellectual Property.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import random
import requests
import uuid
def rand_name(name='', prefix='pegleg'):
"""Generate a random name that includes a random number
:param str name: The name that you want to include
:param str prefix: The prefix that you want to include
:return: a random name. The format is
'<prefix>-<name>-<random number>'.
(e.g. 'prefixfoo-namebar-154876201')
:rtype: string
"""
randbits = str(random.randint(1, 0x7fffffff))
rand_name = randbits
if name:
rand_name = name + '-' + rand_name
if prefix:
rand_name = prefix + '-' + rand_name
return rand_name
def get_proxies():
use_proxy = False
http_proxy = None
https_proxy = None
if 'http_proxy' in os.environ:
http_proxy = os.environ['http_proxy']
use_proxy = True
elif 'HTTP_PROXY' in os.environ:
http_proxy = os.environ['HTTP_PROXY']
use_proxy = True
if 'https_proxy' in os.environ:
https_proxy = os.environ['https_proxy']
use_proxy = True
elif 'HTTPS_PROXY' in os.environ:
https_proxy = os.environ['HTTPS_PROXY']
use_proxy = True
return use_proxy, {'http': http_proxy, 'https_proxy': https_proxy}
def is_connected():
"""Verifies whether network connectivity is up.
:returns: True if connected else False.
"""
for _ in range(3):
try:
r = requests.get("http://www.github.com/", proxies={}, timeout=3)
r.raise_for_status()
return True
except requests.exceptions.RequestException:
pass
return False
def is_connected_behind_proxy():
"""Verifies whether network connectivity is up behind given proxy.
:returns: True if connected else False.
"""
for _ in range(3):
try:
r = requests.get(
"http://www.github.com/", proxies=get_proxies()[1], timeout=3)
r.raise_for_status()
return True
except requests.exceptions.RequestException:
pass
return False