Skip tests when no libcloud

This commit is contained in:
Daniel A. Wozniak 2019-04-27 09:19:39 +00:00 committed by Pedro Algarvio
parent 2cf4b98952
commit 52ca668360
No known key found for this signature in database
GPG Key ID: BB36BF6584A298FF

View File

@ -17,68 +17,74 @@ from tests.support.mock import (
)
import salt.modules.libcloud_loadbalancer as libcloud_loadbalancer
from libcloud.loadbalancer.base import BaseDriver, LoadBalancer, Algorithm, Member
try:
from libcloud.loadbalancer.base import BaseDriver, LoadBalancer, Algorithm, Member
HAS_LIBCLOUD = True
except ImportError:
HAS_LIBCLOUD = False
MockLBDriver = object
if HASLIBCLOUD:
class MockLBDriver(BaseDriver):
def __init__(self):
self._TEST_BALANCER = LoadBalancer(
id='test_id', name='test_balancer',
state=0, # RUNNING
ip='1.2.3.4',
port=80, driver=self,
extra={})
self._TEST_MEMBER = Member(
id='member_id', ip='12.3.4.5',
port=443, balancer=self._TEST_BALANCER,
extra=None)
class MockLBDriver(BaseDriver):
def __init__(self):
self._TEST_BALANCER = LoadBalancer(
id='test_id', name='test_balancer',
state=0, # RUNNING
ip='1.2.3.4',
port=80, driver=self,
extra={})
self._TEST_MEMBER = Member(
id='member_id', ip='12.3.4.5',
port=443, balancer=self._TEST_BALANCER,
extra=None)
def get_balancer(self, balancer_id):
assert balancer_id == 'test_id'
return self._TEST_BALANCER
def get_balancer(self, balancer_id):
assert balancer_id == 'test_id'
return self._TEST_BALANCER
def list_balancers(self):
return [self._TEST_BALANCER]
def list_balancers(self):
return [self._TEST_BALANCER]
def list_protocols(self):
return ['http', 'https']
def list_protocols(self):
return ['http', 'https']
def create_balancer(self, name, port, protocol, algorithm, members):
assert name == 'new_test_balancer'
assert port == 80
assert protocol == 'http'
assert isinstance(algorithm, (Algorithm, int))
assert isinstance(members, list)
return self._TEST_BALANCER
def create_balancer(self, name, port, protocol, algorithm, members):
assert name == 'new_test_balancer'
assert port == 80
assert protocol == 'http'
assert isinstance(algorithm, (Algorithm, int))
assert isinstance(members, list)
return self._TEST_BALANCER
def destroy_balancer(self, balancer):
assert balancer == self._TEST_BALANCER
return True
def destroy_balancer(self, balancer):
assert balancer == self._TEST_BALANCER
return True
def balancer_attach_member(self, balancer, member):
assert isinstance(balancer, LoadBalancer)
assert isinstance(member, Member)
assert member.id is None
assert balancer.id == 'test_id'
return self._TEST_MEMBER
def balancer_attach_member(self, balancer, member):
assert isinstance(balancer, LoadBalancer)
assert isinstance(member, Member)
assert member.id is None
assert balancer.id == 'test_id'
return self._TEST_MEMBER
def balancer_detach_member(self, balancer, member):
assert isinstance(balancer, LoadBalancer)
assert isinstance(member, Member)
assert member.id == 'member_id'
assert balancer.id == 'test_id'
return True
def balancer_detach_member(self, balancer, member):
assert isinstance(balancer, LoadBalancer)
assert isinstance(member, Member)
assert member.id == 'member_id'
assert balancer.id == 'test_id'
return True
def balancer_list_members(self, balancer):
assert isinstance(balancer, LoadBalancer)
assert balancer.id == 'test_id'
return [self._TEST_MEMBER]
def balancer_list_members(self, balancer):
assert isinstance(balancer, LoadBalancer)
assert balancer.id == 'test_id'
return [self._TEST_MEMBER]
def get_mock_driver():
return MockLBDriver()
@skipIf(not HAS_LIBCLOUD, NO_MOCK_REASON)
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch('salt.modules.libcloud_loadbalancer._get_driver',
MagicMock(return_value=MockLBDriver()))