1"""
2    :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
3"""
4
5
6import salt.modules.modjk as modjk
7from tests.support.mock import patch
8from tests.support.unit import TestCase
9
10
11class ModjkTestCase(TestCase):
12    """
13    Test cases for salt.modules.modjk
14    """
15
16    # 'version' function tests: 1
17
18    def test_version(self):
19        """
20        Test for return the modjk version
21        """
22        with patch.object(
23            modjk, "_do_http", return_value={"worker.jk_version": "mod_jk/1.2.37"}
24        ):
25            self.assertEqual(modjk.version(), "1.2.37")
26
27    # 'get_running' function tests: 1
28
29    def test_get_running(self):
30        """
31        Test for get the current running config (not from disk)
32        """
33        with patch.object(modjk, "_do_http", return_value={}):
34            self.assertDictEqual(modjk.get_running(), {})
35
36    # 'dump_config' function tests: 1
37
38    def test_dump_config(self):
39        """
40        Test for dump the original configuration that was loaded from disk
41        """
42        with patch.object(modjk, "_do_http", return_value={}):
43            self.assertDictEqual(modjk.dump_config(), {})
44
45    # 'list_configured_members' function tests: 1
46
47    def test_list_configured_members(self):
48        """
49        Test for return a list of member workers from the configuration files
50        """
51        with patch.object(modjk, "_do_http", return_value={}):
52            self.assertListEqual(modjk.list_configured_members("loadbalancer1"), [])
53
54        with patch.object(
55            modjk,
56            "_do_http",
57            return_value={"worker.loadbalancer1.balance_workers": "SALT"},
58        ):
59            self.assertListEqual(
60                modjk.list_configured_members("loadbalancer1"), ["SALT"]
61            )
62
63    # 'workers' function tests: 1
64
65    def test_workers(self):
66        """
67        Test for return a list of member workers and their status
68        """
69        with patch.object(
70            modjk, "_do_http", return_value={"worker.list": "Salt1,Salt2"}
71        ):
72            self.assertDictEqual(modjk.workers(), {})
73
74    # 'recover_all' function tests: 1
75
76    def test_recover_all(self):
77        """
78        Test for set the all the workers in lbn to recover and
79        activate them if they are not
80        """
81        with patch.object(modjk, "_do_http", return_value={}):
82            self.assertDictEqual(modjk.recover_all("loadbalancer1"), {})
83
84        with patch.object(
85            modjk,
86            "_do_http",
87            return_value={"worker.loadbalancer1.balance_workers": "SALT"},
88        ):
89            with patch.object(
90                modjk,
91                "worker_status",
92                return_value={"activation": "ACT", "state": "OK"},
93            ):
94                self.assertDictEqual(
95                    modjk.recover_all("loadbalancer1"),
96                    {"SALT": {"activation": "ACT", "state": "OK"}},
97                )
98
99    # 'reset_stats' function tests: 1
100
101    def test_reset_stats(self):
102        """
103        Test for reset all runtime statistics for the load balancer
104        """
105        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
106            self.assertTrue(modjk.reset_stats("loadbalancer1"))
107
108    # 'lb_edit' function tests: 1
109
110    def test_lb_edit(self):
111        """
112        Test for edit the loadbalancer settings
113        """
114        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
115            self.assertTrue(modjk.lb_edit("loadbalancer1", {"vlr": 1, "vlt": 60}))
116
117    # 'bulk_stop' function tests: 1
118
119    def test_bulk_stop(self):
120        """
121        Test for stop all the given workers in the specific load balancer
122        """
123        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
124            self.assertTrue(
125                modjk.bulk_stop(["node1", "node2", "node3"], "loadbalancer1")
126            )
127
128    # 'bulk_activate' function tests: 1
129
130    def test_bulk_activate(self):
131        """
132        Test for activate all the given workers in the specific load balancer
133        """
134        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
135            self.assertTrue(
136                modjk.bulk_activate(["node1", "node2", "node3"], "loadbalancer1")
137            )
138
139    # 'bulk_disable' function tests: 1
140
141    def test_bulk_disable(self):
142        """
143        Test for disable all the given workers in the specific load balancer
144        """
145        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
146            self.assertTrue(
147                modjk.bulk_disable(["node1", "node2", "node3"], "loadbalancer1")
148            )
149
150    # 'bulk_recover' function tests: 1
151
152    def test_bulk_recover(self):
153        """
154        Test for recover all the given workers in the specific load balancer
155        """
156        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
157            self.assertTrue(
158                modjk.bulk_recover(["node1", "node2", "node3"], "loadbalancer1")
159            )
160
161    # 'worker_status' function tests: 1
162
163    def test_worker_status(self):
164        """
165        Test for return the state of the worker
166        """
167        with patch.object(
168            modjk,
169            "_do_http",
170            return_value={"worker.node1.activation": "ACT", "worker.node1.state": "OK"},
171        ):
172            self.assertDictEqual(
173                modjk.worker_status("node1"), {"activation": "ACT", "state": "OK"}
174            )
175
176        with patch.object(modjk, "_do_http", return_value={}):
177            self.assertFalse(modjk.worker_status("node1"))
178
179    # 'worker_recover' function tests: 1
180
181    def test_worker_recover(self):
182        """
183        Test for set the worker to recover this module will fail
184        if it is in OK state
185        """
186        with patch.object(modjk, "_do_http", return_value={}):
187            self.assertDictEqual(modjk.worker_recover("node1", "loadbalancer1"), {})
188
189    # 'worker_disable' function tests: 1
190
191    def test_worker_disable(self):
192        """
193        Test for set the worker to disable state in the lbn load balancer
194        """
195        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
196            self.assertTrue(modjk.worker_disable("node1", "loadbalancer1"))
197
198    # 'worker_activate' function tests: 1
199
200    def test_worker_activate(self):
201        """
202        Test for set the worker to activate state in the lbn load balancer
203        """
204        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
205            self.assertTrue(modjk.worker_activate("node1", "loadbalancer1"))
206
207    # 'worker_stop' function tests: 1
208
209    def test_worker_stop(self):
210        """
211        Test for set the worker to stopped state in the lbn load balancer
212        """
213        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
214            self.assertTrue(modjk.worker_stop("node1", "loadbalancer1"))
215
216    # 'worker_edit' function tests: 1
217
218    def test_worker_edit(self):
219        """
220        Test for edit the worker settings
221        """
222        with patch.object(modjk, "_do_http", return_value={"worker.result.type": "OK"}):
223            self.assertTrue(
224                modjk.worker_edit("node1", "loadbalancer1", {"vwf": 500, "vwd": 60})
225            )
226