salt/tests/unit/modules/test_haproxyconn.py

217 lines
5.4 KiB
Python
Raw Normal View History

2015-02-09 12:53:22 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
2015-02-09 12:53:22 +00:00
# Import Salt Testing Libs
2017-02-19 15:35:30 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
2017-04-10 13:00:57 +00:00
from tests.support.mock import NO_MOCK, NO_MOCK_REASON
2015-02-09 12:53:22 +00:00
# Import Salt Libs
import salt.modules.haproxyconn as haproxyconn
2015-02-09 12:53:22 +00:00
class Mockcmds(object):
'''
2015-02-09 12:53:22 +00:00
Mock of cmds
'''
2015-02-09 12:53:22 +00:00
def __init__(self):
self.backend = None
self.server = None
self.weight = None
def listServers(self, backend):
'''
2015-02-09 12:53:22 +00:00
Mock of listServers method
'''
2015-02-09 12:53:22 +00:00
self.backend = backend
return 'Name: server01 Status: UP Weight: 1 bIn: 22 bOut: 12\n' \
'Name: server02 Status: MAINT Weight: 2 bIn: 0 bOut: 0'
2015-02-09 12:53:22 +00:00
def enableServer(self, server, backend):
'''
2015-02-09 12:53:22 +00:00
Mock of enableServer method
'''
2015-02-09 12:53:22 +00:00
self.backend = backend
self.server = server
return 'server enabled'
def disableServer(self, server, backend):
'''
2015-02-09 12:53:22 +00:00
Mock of disableServer method
'''
2015-02-09 12:53:22 +00:00
self.backend = backend
self.server = server
return 'server disabled'
def getWeight(self, server, backend, weight=0):
'''
2015-02-09 12:53:22 +00:00
Mock of getWeight method
'''
2015-02-09 12:53:22 +00:00
self.backend = backend
self.server = server
self.weight = weight
return 'server weight'
@staticmethod
def showFrontends():
'''
2015-02-09 12:53:22 +00:00
Mock of showFrontends method
'''
return 'frontend-alpha\n' \
'frontend-beta\n' \
'frontend-gamma'
2015-02-09 12:53:22 +00:00
@staticmethod
def showBackends():
'''
2015-02-09 12:53:22 +00:00
Mock of showBackends method
'''
return 'backend-alpha\n' \
'backend-beta\n' \
'backend-gamma'
2015-02-09 12:53:22 +00:00
class Mockhaproxy(object):
'''
2015-02-09 12:53:22 +00:00
Mock of haproxy
'''
2015-02-09 12:53:22 +00:00
def __init__(self):
self.cmds = Mockcmds()
class MockHaConn(object):
'''
2015-02-09 12:53:22 +00:00
Mock of HaConn
'''
2017-04-10 13:00:57 +00:00
def __init__(self, socket=None):
2015-02-09 12:53:22 +00:00
self.ha_cmd = None
2015-03-19 15:56:12 +00:00
def sendCmd(self, ha_cmd, objectify=False):
'''
2015-02-09 12:53:22 +00:00
Mock of sendCmd method
'''
2015-02-09 12:53:22 +00:00
self.ha_cmd = ha_cmd
2015-03-19 15:56:12 +00:00
self.objectify = objectify
return ha_cmd
2015-02-09 12:53:22 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class HaproxyConnTestCase(TestCase, LoaderModuleMockMixin):
2015-02-09 12:53:22 +00:00
'''
Test cases for salt.modules.haproxyconn
'''
def setup_loader_modules(self):
2017-04-10 13:00:57 +00:00
return {haproxyconn: {'haproxy': Mockhaproxy(), '_get_conn': MockHaConn}}
2017-02-19 15:35:30 +00:00
2015-02-09 12:53:22 +00:00
# 'list_servers' function tests: 1
2017-04-10 13:00:57 +00:00
def test_list_servers(self):
2015-02-09 12:53:22 +00:00
'''
Test list_servers
2015-02-09 12:53:22 +00:00
'''
self.assertTrue(haproxyconn.list_servers('mysql'))
# 'enable_server' function tests: 1
2017-04-10 13:00:57 +00:00
def test_enable_server(self):
2015-02-09 12:53:22 +00:00
'''
Test enable_server
2015-02-09 12:53:22 +00:00
'''
self.assertTrue(haproxyconn.enable_server('web1.salt.com', 'www'))
# 'disable_server' function tests: 1
2017-04-10 13:00:57 +00:00
def test_disable_server(self):
2015-02-09 12:53:22 +00:00
'''
Test disable_server
2015-02-09 12:53:22 +00:00
'''
self.assertTrue(haproxyconn.disable_server('db1.salt.com', 'mysql'))
# 'get_weight' function tests: 1
2017-04-10 13:00:57 +00:00
def test_get_weight(self):
2015-02-09 12:53:22 +00:00
'''
Test get the weight of a server
2015-02-09 12:53:22 +00:00
'''
self.assertTrue(haproxyconn.get_weight('db1.salt.com', 'mysql'))
# 'set_weight' function tests: 1
2017-04-10 13:00:57 +00:00
def test_set_weight(self):
2015-02-09 12:53:22 +00:00
'''
Test setting the weight of a given server
2015-02-09 12:53:22 +00:00
'''
self.assertTrue(haproxyconn.set_weight('db1.salt.com', 'mysql',
weight=11))
# 'show_frontends' function tests: 1
2017-04-10 13:00:57 +00:00
def test_show_frontends(self):
2015-02-09 12:53:22 +00:00
'''
Test print all frontends received from the HAProxy socket
2015-02-09 12:53:22 +00:00
'''
self.assertTrue(haproxyconn.show_frontends())
def test_list_frontends(self):
'''
Test listing all frontends
'''
self.assertEqual(
sorted(haproxyconn.list_frontends()),
sorted(['frontend-alpha', 'frontend-beta', 'frontend-gamma'])
)
2015-02-09 12:53:22 +00:00
# 'show_backends' function tests: 1
2017-04-10 13:00:57 +00:00
def test_show_backends(self):
2015-02-09 12:53:22 +00:00
'''
Test print all backends received from the HAProxy socket
2015-02-09 12:53:22 +00:00
'''
self.assertTrue(haproxyconn.show_backends())
def test_list_backends(self):
'''
Test listing of all backends
'''
self.assertEqual(
sorted(haproxyconn.list_backends()),
sorted(['backend-alpha', 'backend-beta', 'backend-gamma'])
)
def test_get_backend(self):
'''
Test get_backend and compare returned value
'''
expected_data = {
'server01': {
'status': 'UP',
'weight': 1,
'bin': 22,
'bout': 12
},
'server02': {
'status': 'MAINT',
'weight': 2,
'bin': 0,
'bout': 0
}
}
self.assertDictEqual(haproxyconn.get_backend('test'), expected_data)
def test_wait_state_true(self):
'''
Test a successful wait for state
'''
self.assertTrue(haproxyconn.wait_state('test', 'server01'))
def test_wait_state_false(self):
'''
Test a failed wait for state, with a timeout of 0
'''
self.assertFalse(haproxyconn.wait_state('test', 'server02', 'up', 0))