|
13 | 13 | # limitations under the License. |
14 | 14 | # |
15 | 15 |
|
16 | | -from f5.bigip.tm.sys.failover import InvalidParameterValue |
| 16 | +from f5.bigip.resource import BooleansToReduceHaveSameValue |
| 17 | +from f5.multi_device.utils import get_device_info |
| 18 | +from f5.multi_device.utils import pollster |
17 | 19 | from pprint import pprint as pp |
18 | 20 |
|
19 | 21 | import pytest |
20 | | -import time |
| 22 | + |
| 23 | + |
| 24 | +def get_activation_state(device): |
| 25 | + '''Get the activation state for a device.''' |
| 26 | + |
| 27 | + device_name = get_device_info(device).name |
| 28 | + act = device.tm.cm.devices.device.load( |
| 29 | + name=device_name, |
| 30 | + partition='Common' |
| 31 | + ) |
| 32 | + return act.failoverState |
| 33 | + |
| 34 | + |
| 35 | +def check_device_state_as_expected(device, expected_state): |
| 36 | + assert get_activation_state(device).lower() == expected_state.lower() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def teardown_device_failover_state(request, mgmt_root): |
| 41 | + def teardown(): |
| 42 | + if get_activation_state(mgmt_root).lower() != 'active': |
| 43 | + f = mgmt_root.tm.sys.failover |
| 44 | + f.exec_cmd('run', utilCmdArgs='online') |
| 45 | + request.addfinalizer(teardown) |
21 | 46 |
|
22 | 47 |
|
23 | 48 | class TestFailover(object): |
@@ -47,41 +72,45 @@ def test_toggle_standby(self, bigip): |
47 | 72 | assert f.command == u"run" |
48 | 73 | pp('************') |
49 | 74 | f.refresh() |
| 75 | + pp('after refresh') |
50 | 76 | pp(f.raw) |
51 | 77 | assert 'Failover active' in f.apiRawValues['apiAnonymous'] |
52 | 78 |
|
53 | 79 | def test_attribute_values(self, bigip): |
54 | 80 | fl = bigip.sys.failover |
55 | 81 | # Testing both conditions |
56 | | - with pytest.raises(InvalidParameterValue): |
| 82 | + with pytest.raises(BooleansToReduceHaveSameValue) as ex1: |
57 | 83 | fl.exec_cmd('run', online=True, offline=True) |
58 | | - with pytest.raises(InvalidParameterValue): |
| 84 | + assert 'online and offline, have same value: True' \ |
| 85 | + in ex1.value.message |
| 86 | + with pytest.raises(BooleansToReduceHaveSameValue) as ex2: |
59 | 87 | fl.exec_cmd('run', online=False, offline=False) |
| 88 | + assert 'online and offline, have same value: False' \ |
| 89 | + in ex2.value.message |
60 | 90 |
|
61 | | - def test_exec_cmd(self, bigip): |
62 | | - fl = bigip.sys.failover.load() |
63 | | - f = bigip.sys.failover |
| 91 | + def test_exec_cmd(self, mgmt_root, teardown_device_failover_state): |
| 92 | + fl = mgmt_root.tm.sys.failover.load() |
| 93 | + f = mgmt_root.tm.sys.failover |
64 | 94 | f.exec_cmd('run', offline=True) |
65 | | - time.sleep(4) |
| 95 | + get_activation_state(mgmt_root) |
| 96 | + pollster(check_device_state_as_expected)(mgmt_root, 'forced-offline') |
66 | 97 | fl.refresh() |
| 98 | + pp(fl.raw) |
67 | 99 | assert 'Failover forced_offline' in fl.apiRawValues['apiAnonymous'] |
68 | 100 | f.exec_cmd('run', offline=False, online=True) |
69 | | - # We need this 2 sec delay as sometimes the status does not change |
70 | | - # straight away, causing the assertion to fail. |
71 | | - time.sleep(2) |
| 101 | + pollster(check_device_state_as_expected)(mgmt_root, 'active') |
72 | 102 | fl.refresh() |
| 103 | + pp(fl.raw) |
73 | 104 | assert 'Failover active' in fl.apiRawValues['apiAnonymous'] |
74 | 105 |
|
75 | | - def test_exec_cmd_cmdargs(self, bigip): |
76 | | - fl = bigip.sys.failover.load() |
77 | | - f = bigip.sys.failover |
| 106 | + def test_exec_cmd_cmdargs(self, mgmt_root, teardown_device_failover_state): |
| 107 | + fl = mgmt_root.tm.sys.failover.load() |
| 108 | + f = mgmt_root.tm.sys.failover |
78 | 109 | f.exec_cmd('run', utilCmdArgs='offline') |
79 | | - time.sleep(4) |
| 110 | + pollster(check_device_state_as_expected)(mgmt_root, 'forced-offline') |
80 | 111 | fl.refresh() |
81 | 112 | assert 'Failover forced_offline' in fl.apiRawValues['apiAnonymous'] |
82 | 113 | f.exec_cmd('run', utilCmdArgs='online') |
83 | | - # We need this 2 sec delay as sometimes the status does not change |
84 | | - # straight away, causing the assertion to fail. |
85 | | - time.sleep(2) |
| 114 | + pollster(check_device_state_as_expected)(mgmt_root, 'active') |
86 | 115 | fl.refresh() |
87 | 116 | assert 'Failover active' in fl.apiRawValues['apiAnonymous'] |
0 commit comments