1""" 2 :codeauthor: Jayesh Kariya <jayeshk@saltstack.com> 3""" 4 5 6import salt.modules.haproxyconn as haproxyconn 7from tests.support.mixins import LoaderModuleMockMixin 8from tests.support.unit import TestCase 9 10 11class Mockcmds: 12 """ 13 Mock of cmds 14 """ 15 16 def __init__(self): 17 self.backend = None 18 self.server = None 19 self.weight = None 20 21 def listServers(self, backend): 22 """ 23 Mock of listServers method 24 """ 25 self.backend = backend 26 return ( 27 "Name: server01 Status: UP Weight: 1 bIn: 22 bOut: 12\n" 28 "Name: server02 Status: MAINT Weight: 2 bIn: 0 bOut: 0" 29 ) 30 31 def enableServer(self, server, backend): 32 """ 33 Mock of enableServer method 34 """ 35 self.backend = backend 36 self.server = server 37 return "server enabled" 38 39 def disableServer(self, server, backend): 40 """ 41 Mock of disableServer method 42 """ 43 self.backend = backend 44 self.server = server 45 return "server disabled" 46 47 def getWeight(self, server, backend, weight=0): 48 """ 49 Mock of getWeight method 50 """ 51 self.backend = backend 52 self.server = server 53 self.weight = weight 54 return "server weight" 55 56 @staticmethod 57 def showFrontends(): 58 """ 59 Mock of showFrontends method 60 """ 61 return "frontend-alpha\nfrontend-beta\nfrontend-gamma" 62 63 @staticmethod 64 def showBackends(): 65 """ 66 Mock of showBackends method 67 """ 68 return "backend-alpha\nbackend-beta\nbackend-gamma" 69 70 71class Mockhaproxy: 72 """ 73 Mock of haproxy 74 """ 75 76 def __init__(self): 77 self.cmds = Mockcmds() 78 79 80class MockHaConn: 81 """ 82 Mock of HaConn 83 """ 84 85 def __init__(self, socket=None): 86 self.ha_cmd = None 87 88 def sendCmd(self, ha_cmd, objectify=False): 89 """ 90 Mock of sendCmd method 91 """ 92 self.ha_cmd = ha_cmd 93 self.objectify = objectify 94 return ha_cmd 95 96 97class HaproxyConnTestCase(TestCase, LoaderModuleMockMixin): 98 """ 99 Test cases for salt.modules.haproxyconn 100 """ 101 102 def setup_loader_modules(self): 103 return {haproxyconn: {"haproxy": Mockhaproxy(), "_get_conn": MockHaConn}} 104 105 # 'list_servers' function tests: 1 106 107 def test_list_servers(self): 108 """ 109 Test list_servers 110 """ 111 self.assertTrue(haproxyconn.list_servers("mysql")) 112 113 # 'enable_server' function tests: 1 114 115 def test_enable_server(self): 116 """ 117 Test enable_server 118 """ 119 self.assertTrue(haproxyconn.enable_server("web1.salt.com", "www")) 120 121 # 'disable_server' function tests: 1 122 123 def test_disable_server(self): 124 """ 125 Test disable_server 126 """ 127 self.assertTrue(haproxyconn.disable_server("db1.salt.com", "mysql")) 128 129 # 'get_weight' function tests: 1 130 131 def test_get_weight(self): 132 """ 133 Test get the weight of a server 134 """ 135 self.assertTrue(haproxyconn.get_weight("db1.salt.com", "mysql")) 136 137 # 'set_weight' function tests: 1 138 139 def test_set_weight(self): 140 """ 141 Test setting the weight of a given server 142 """ 143 self.assertTrue(haproxyconn.set_weight("db1.salt.com", "mysql", weight=11)) 144 145 # 'show_frontends' function tests: 1 146 147 def test_show_frontends(self): 148 """ 149 Test print all frontends received from the HAProxy socket 150 """ 151 self.assertTrue(haproxyconn.show_frontends()) 152 153 def test_list_frontends(self): 154 """ 155 Test listing all frontends 156 """ 157 self.assertEqual( 158 sorted(haproxyconn.list_frontends()), 159 sorted(["frontend-alpha", "frontend-beta", "frontend-gamma"]), 160 ) 161 162 # 'show_backends' function tests: 1 163 164 def test_show_backends(self): 165 """ 166 Test print all backends received from the HAProxy socket 167 """ 168 self.assertTrue(haproxyconn.show_backends()) 169 170 def test_list_backends(self): 171 """ 172 Test listing of all backends 173 """ 174 self.assertEqual( 175 sorted(haproxyconn.list_backends()), 176 sorted(["backend-alpha", "backend-beta", "backend-gamma"]), 177 ) 178 179 def test_get_backend(self): 180 """ 181 Test get_backend and compare returned value 182 """ 183 expected_data = { 184 "server01": {"status": "UP", "weight": 1, "bin": 22, "bout": 12}, 185 "server02": {"status": "MAINT", "weight": 2, "bin": 0, "bout": 0}, 186 } 187 self.assertDictEqual(haproxyconn.get_backend("test"), expected_data) 188 189 def test_wait_state_true(self): 190 """ 191 Test a successful wait for state 192 """ 193 self.assertTrue(haproxyconn.wait_state("test", "server01")) 194 195 def test_wait_state_false(self): 196 """ 197 Test a failed wait for state, with a timeout of 0 198 """ 199 self.assertFalse(haproxyconn.wait_state("test", "server02", "up", 0)) 200