2016-04-19 15:32:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: `Anthony Shaw <anthonyshaw@apache.org>`
|
|
|
|
|
|
|
|
tests.unit.cloud.clouds.gce_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
2016-07-26 15:24:20 +00:00
|
|
|
|
2016-09-08 17:15:41 +00:00
|
|
|
try:
|
|
|
|
import libcloud.security
|
|
|
|
HAS_LIBCLOUD = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_LIBCLOUD = False
|
2016-04-19 15:32:49 +00:00
|
|
|
|
|
|
|
# Import Salt Libs
|
|
|
|
from salt.cloud.clouds import gce
|
|
|
|
from salt.exceptions import SaltCloudSystemExit
|
2017-03-08 18:25:04 +00:00
|
|
|
from salt.utils.versions import LooseVersion
|
2016-04-19 15:32:49 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
2017-03-06 12:53:00 +00:00
|
|
|
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, __version__ as mock_version
|
2016-04-19 15:32:49 +00:00
|
|
|
|
|
|
|
VM_NAME = 'kings_landing'
|
|
|
|
DUMMY_TOKEN = {
|
|
|
|
'refresh_token': None,
|
|
|
|
'client_id': 'dany123',
|
|
|
|
'client_secret': 'lalalalalalala',
|
|
|
|
'grant_type': 'refresh_token'
|
|
|
|
}
|
|
|
|
|
2016-07-26 15:24:20 +00:00
|
|
|
# Use certifi if installed
|
|
|
|
try:
|
2016-09-08 17:15:41 +00:00
|
|
|
if HAS_LIBCLOUD:
|
|
|
|
import certifi
|
|
|
|
libcloud.security.CA_CERTS_PATH.append(certifi.where())
|
2016-07-26 15:24:20 +00:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2016-04-19 15:32:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-02-19 15:35:30 +00:00
|
|
|
class GCETestCase(TestCase, LoaderModuleMockMixin):
|
2016-04-19 15:32:49 +00:00
|
|
|
'''
|
|
|
|
Unit TestCase for salt.cloud.clouds.gce module.
|
|
|
|
'''
|
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {
|
|
|
|
gce: {
|
|
|
|
'__active_provider_name__': '',
|
|
|
|
'__opts__': {
|
|
|
|
'providers': {
|
|
|
|
'my-google-cloud': {
|
|
|
|
'gce': {
|
|
|
|
'project': 'daenerys-cloud',
|
|
|
|
'service_account_email_address': 'dany@targaryen.westeros.cloud',
|
|
|
|
'service_account_private_key': '/home/dany/PRIVKEY.pem',
|
|
|
|
'driver': 'gce',
|
|
|
|
'ssh_interface': 'public_ips'
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 15:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:32:49 +00:00
|
|
|
def test_destroy_call(self):
|
|
|
|
'''
|
|
|
|
Tests that a SaltCloudSystemExit is raised when trying to call destroy
|
|
|
|
with --function or -f.
|
|
|
|
'''
|
|
|
|
self.assertRaises(
|
|
|
|
SaltCloudSystemExit,
|
|
|
|
gce.destroy,
|
|
|
|
vm_name=VM_NAME,
|
|
|
|
call='function'
|
|
|
|
)
|
|
|
|
|
2016-07-26 15:24:20 +00:00
|
|
|
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)
|
2017-03-06 12:53:00 +00:00
|
|
|
if LooseVersion(mock_version) >= LooseVersion('2.0.0'):
|
2016-09-21 20:46:50 +00:00
|
|
|
p.assert_called_once()
|
2016-07-26 15:24:20 +00:00
|
|
|
|
|
|
|
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)
|