Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCE Cloud tests #34871

Merged
merged 10 commits into from
Jul 26, 2016
14 changes: 9 additions & 5 deletions salt/cloud/clouds/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
my-gce-config:
# The Google Cloud Platform Project ID
project: "my-project-id"
# The Service ACcount client ID
# The Service Account client ID
service_account_email_address: 1234567890@developer.gserviceaccount.com
# The location of the private key (PEM format)
service_account_private_key: /home/erjohnso/PRIVKEY.pem
Expand Down Expand Up @@ -171,10 +171,14 @@ def get_conn():
driver = get_driver(Provider.GCE)
provider = get_configured_provider()
project = config.get_cloud_config_value('project', provider, __opts__)
email = config.get_cloud_config_value('service_account_email_address',
provider, __opts__)
private_key = config.get_cloud_config_value('service_account_private_key',
provider, __opts__)
email = config.get_cloud_config_value(
'service_account_email_address',
provider,
__opts__)
private_key = config.get_cloud_config_value(
'service_account_private_key',
provider,
__opts__)
gce = driver(email, private_key, project=project)
gce.connection.user_agent_append('{0}/{1}'.format(_UA_PRODUCT,
_UA_VERSION))
Expand Down
40 changes: 21 additions & 19 deletions tests/unit/cloud/clouds/dimensiondata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
# Import Python libs
from __future__ import absolute_import
import libcloud.security
import platform
import os

# Import Salt Libs
from salt.cloud.clouds import dimensiondata
Expand Down Expand Up @@ -39,22 +37,12 @@
}
VM_NAME = 'winterfell'

HAS_CERTS = True
ON_SUSE = True if 'SuSE' in platform.dist() else False
ON_MAC = True if 'Darwin' in platform.system() else False

if not os.path.exists('/etc/ssl/certs/YaST-CA.pem') and ON_SUSE:
if os.path.isfile('/etc/ssl/ca-bundle.pem'):
libcloud.security.CA_CERTS_PATH.append('/etc/ssl/ca-bundle.pem')
else:
HAS_CERTS = False
elif ON_MAC:
if os.path.isfile('/opt/local/share/curl/curl-ca-bundle.crt'):
pass # libcloud will already find this file
elif os.path.isfile('/usr/local/etc/openssl/cert.pem'):
pass # libcloud will already find this file
else:
HAS_CERTS = False
# Use certifi if installed
try:
import certifi
libcloud.security.CA_CERTS_PATH.append(certifi.where())
except ImportError:
pass


class ExtendedTestCase(TestCase):
Expand All @@ -71,7 +59,6 @@ def assertRaisesWithMessage(self, exc_type, exc_msg, func, *args, **kwargs):
self.assertEqual(exc.message, exc_msg)


@skipIf(not HAS_CERTS, 'Cannot find CA cert bundle')
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch('salt.cloud.clouds.dimensiondata.__virtual__', MagicMock(return_value='dimensiondata'))
class DimensionDataTestCase(ExtendedTestCase):
Expand Down Expand Up @@ -149,6 +136,21 @@ def test_avail_sizes(self):
'default'
)

def test_import(self):
"""
Test that the module picks up installed deps
"""
with patch('salt.config.check_driver_dependencies', return_value=True) as p:
get_deps = dimensiondata.get_dependencies()
self.assertEqual(get_deps, True)
p.assert_called_once()

def test_provider_matches(self):
"""
Test that the first configured instance of a dimensiondata driver is matched
"""
p = dimensiondata.get_configured_provider()
self.assertNotEqual(p, None)

if __name__ == '__main__':
from integration import run_tests
Expand Down
105 changes: 37 additions & 68 deletions tests/unit/cloud/clouds/gce_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
# Import Python libs
from __future__ import absolute_import
import libcloud.security
import platform
import os


# Import Salt Libs
from salt.cloud.clouds import gce
from salt.exceptions import SaltCloudSystemExit

# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import MagicMock, NO_MOCK, NO_MOCK_REASON, patch
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, patch
from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../../')
Expand Down Expand Up @@ -46,46 +45,16 @@
'grant_type': 'refresh_token'
}

HAS_CERTS = True
ON_SUSE = True if 'SuSE' in platform.dist() else False
ON_MAC = True if 'Darwin' in platform.system() else False

if not os.path.exists('/etc/ssl/certs/YaST-CA.pem') and ON_SUSE:
if os.path.isfile('/etc/ssl/ca-bundle.pem'):
libcloud.security.CA_CERTS_PATH.append('/etc/ssl/ca-bundle.pem')
else:
HAS_CERTS = False
elif ON_MAC:
if os.path.isfile('/opt/local/share/curl/curl-ca-bundle.crt'):
pass # libcloud will already find this file
elif os.path.isfile('/usr/local/etc/openssl/cert.pem'):
pass # libcloud will already find this file
else:
HAS_CERTS = False


class ExtendedTestCase(TestCase):
'''
Extended TestCase class containing additional helper methods.
'''
# Use certifi if installed
try:
import certifi
libcloud.security.CA_CERTS_PATH.append(certifi.where())
except ImportError:
pass

def assertRaisesWithMessage(self, exc_type, exc_msg, func, *args, **kwargs):
try:
func(*args, **kwargs)
self.assertFail()
except Exception as exc:
self.assertEqual(type(exc), exc_type)
self.assertEqual(exc.message, exc_msg)


@skipIf(True, 'Test mock token is not properly mocked and occassionally causes the test suite to hang.')
@skipIf(not HAS_CERTS, 'Cannot find CA cert bundle')
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch('salt.cloud.clouds.gce.__virtual__', MagicMock(return_value='gce'))
@patch('libcloud.common.google.GoogleInstalledAppAuthConnection.get_new_token', MagicMock(return_value=DUMMY_TOKEN))
@patch('libcloud.compute.drivers.gce.GCENodeDriver.ex_list_zones', MagicMock(return_value=[]))
@patch('libcloud.compute.drivers.gce.GCENodeDriver.ex_list_regions', MagicMock(return_value=[]))
class GCETestCase(ExtendedTestCase):
class GCETestCase(TestCase):
'''
Unit TestCase for salt.cloud.clouds.gce module.
'''
Expand All @@ -102,34 +71,34 @@ def test_destroy_call(self):
call='function'
)

@patch('libcloud.compute.drivers.gce.GCENodeDriver.list_sizes', MagicMock(return_value=[]))
def test_avail_sizes(self):
'''
Tests that avail_sizes returns an empty dictionary.
'''
sizes = gce.avail_sizes()
self.assertEqual(
sizes,
[]
)

@patch('libcloud.compute.drivers.gce.GCENodeDriver.list_nodes', MagicMock(return_value=[]))
def test_list_nodes(self):
nodes = gce.list_nodes()
self.assertEqual(
nodes,
{}
)

@patch('libcloud.compute.drivers.gce.GCENodeDriver.list_locations', MagicMock(return_value=[]))
def test_list_locations(self):
locations = gce.avail_locations()
self.assertEqual(
locations,
{}
)

def test_fail_virtual_missing_deps(self):
# Missing deps
with patch('salt.config.check_driver_dependencies', return_value=False):
v = gce.__virtual__()
self.assertEqual(v, False)

def test_fail_virtual_deps_missing_config(self):
with patch('salt.config.check_driver_dependencies', return_value=True):
with patch('salt.config.is_provider_configured', return_value=False):
v = gce.__virtual__()
self.assertEqual(v, False)

def test_import(self):
"""
Test that the module picks up installed deps
"""
with patch('salt.config.check_driver_dependencies', return_value=True) as p:
get_deps = gce.get_dependencies()
self.assertEqual(get_deps, True)
p.assert_called_once()

def test_provider_matches(self):
"""
Test that the first configured instance of a gce driver is matched
"""
p = gce.get_configured_provider()
self.assertNotEqual(p, None)

if __name__ == '__main__':
from integration import run_tests
from unit import run_tests
run_tests(GCETestCase, needs_daemon=False)