|
| 1 | +# Copyright 2015 F5 Networks Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from f5.bigip.resource import MissingRequiredCreationParameter |
| 19 | +from f5.bigip.tm.gtm.rule import Rule |
| 20 | +from requests.exceptions import HTTPError |
| 21 | + |
| 22 | + |
| 23 | +RULE = '''when LB_SELECTED { |
| 24 | + set wipHost [LB::server addr] |
| 25 | +} |
| 26 | +''' |
| 27 | + |
| 28 | + |
| 29 | +def delete_rule(mgmt_root, name, partition): |
| 30 | + r = mgmt_root.tm.gtm.rules.rule |
| 31 | + try: |
| 32 | + r.load(name=name, partition=partition) |
| 33 | + except HTTPError as err: |
| 34 | + if err.response.status_code != 404: |
| 35 | + raise |
| 36 | + return |
| 37 | + r.delete() |
| 38 | + |
| 39 | + |
| 40 | +def setup_create_test(request, mgmt_root, name, partition): |
| 41 | + def teardown(): |
| 42 | + delete_rule(mgmt_root, name, partition) |
| 43 | + request.addfinalizer(teardown) |
| 44 | + |
| 45 | + |
| 46 | +def setup_basic_test(request, mgmt_root, name, partition): |
| 47 | + def teardown(): |
| 48 | + delete_rule(mgmt_root, name, partition) |
| 49 | + |
| 50 | + rule1 = mgmt_root.tm.gtm.rules.rule |
| 51 | + rule1.create(name=name, partition=partition, apiAnonymous=RULE) |
| 52 | + request.addfinalizer(teardown) |
| 53 | + return rule1 |
| 54 | + |
| 55 | + |
| 56 | +class TestCreate(object): |
| 57 | + def test_create_no_args(self, mgmt_root): |
| 58 | + rule1 = mgmt_root.tm.gtm.rules.rule |
| 59 | + with pytest.raises(MissingRequiredCreationParameter): |
| 60 | + rule1.create() |
| 61 | + |
| 62 | + def test_create_no_apianonymous(self, mgmt_root): |
| 63 | + rule1 = mgmt_root.tm.gtm.rules.rule |
| 64 | + with pytest.raises(MissingRequiredCreationParameter): |
| 65 | + rule1.create(name='rule1', partition='Common') |
| 66 | + |
| 67 | + def test_create(self, request, mgmt_root): |
| 68 | + setup_create_test(request, mgmt_root, 'rule1', 'Common') |
| 69 | + rule1 = mgmt_root.tm.gtm.rules.rule |
| 70 | + rule1.create(name='rule1', partition='Common', apiAnonymous=RULE) |
| 71 | + assert rule1.name == 'rule1' |
| 72 | + assert rule1.partition == 'Common' |
| 73 | + assert rule1.generation and isinstance(rule1.generation, int) |
| 74 | + assert 'LB_SELECTED' in rule1.apiAnonymous |
| 75 | + assert rule1.fullPath == '/Common/rule1' |
| 76 | + assert rule1.kind == 'tm:gtm:rule:rulestate' |
| 77 | + assert rule1.selfLink.startswith( |
| 78 | + 'https://localhost/mgmt/tm/gtm/rule/~Common~rule1') |
| 79 | + |
| 80 | + def test_create_optional_args(self, request, mgmt_root): |
| 81 | + setup_create_test(request, mgmt_root, 'rule1', 'Common') |
| 82 | + rule1 = mgmt_root.tm.gtm.rules.rule |
| 83 | + rule1.create(name='rule1', partition='Common', |
| 84 | + apiAnonymous=RULE, |
| 85 | + check='syntax') |
| 86 | + assert 'check syntax' in rule1.apiAnonymous |
| 87 | + |
| 88 | + def test_create_duplicate(self, request, mgmt_root): |
| 89 | + setup_create_test(request, mgmt_root, 'rule1', 'Common') |
| 90 | + rule1 = mgmt_root.tm.gtm.rules.rule |
| 91 | + rule1.create(name='rule1', partition='Common', |
| 92 | + apiAnonymous=RULE, |
| 93 | + check='syntax') |
| 94 | + rule2 = mgmt_root.tm.gtm.rules.rule |
| 95 | + with pytest.raises(HTTPError) as err: |
| 96 | + rule2.create(name='rule1', partition='Common', |
| 97 | + apiAnonymous=RULE, |
| 98 | + check='syntax') |
| 99 | + assert err.response.status_code == 400 |
| 100 | + |
| 101 | + |
| 102 | +class TestRefresh(object): |
| 103 | + def test_refresh(self, request, mgmt_root): |
| 104 | + setup_basic_test(request, mgmt_root, 'rule1', 'Common') |
| 105 | + r1 = mgmt_root.tm.gtm.rules.rule.load( |
| 106 | + name='rule1', partition='Common') |
| 107 | + r2 = mgmt_root.tm.gtm.rules.rule.load( |
| 108 | + name='rule1', partition='Common') |
| 109 | + assert 'check syntax' not in r1.apiAnonymous |
| 110 | + assert 'check syntax' not in r2.apiAnonymous |
| 111 | + |
| 112 | + r2.update(apiAnonymous='check syntax\n' + RULE) |
| 113 | + assert 'check syntax' in r2.apiAnonymous |
| 114 | + assert 'check syntax' not in r1.apiAnonymous |
| 115 | + |
| 116 | + r1.refresh() |
| 117 | + assert 'check syntax' in r1.apiAnonymous |
| 118 | + |
| 119 | + |
| 120 | +class TestLoad(object): |
| 121 | + def test_load_no_object(self, mgmt_root): |
| 122 | + with pytest.raises(HTTPError) as err: |
| 123 | + mgmt_root.tm.gtm.rules.rule.load( |
| 124 | + name='rule1', partition='Common') |
| 125 | + assert err.response.status_code == 404 |
| 126 | + |
| 127 | + def test_load(self, request, mgmt_root): |
| 128 | + setup_basic_test(request, mgmt_root, 'rule1', 'Common') |
| 129 | + rule1 = mgmt_root.tm.gtm.rules.rule.load( |
| 130 | + name='rule1', partition='Common') |
| 131 | + assert 'check syntax' not in rule1.apiAnonymous |
| 132 | + rule1.update(apiAnonymous='check syntax\n' + RULE) |
| 133 | + rule2 = mgmt_root.tm.gtm.rules.rule.load( |
| 134 | + name='rule1', partition='Common') |
| 135 | + assert 'check syntax' in rule1.apiAnonymous |
| 136 | + assert 'check syntax' in rule2.apiAnonymous |
| 137 | + |
| 138 | + |
| 139 | +class TestUpdate(object): |
| 140 | + def test_update(self, request, mgmt_root): |
| 141 | + rule1 = setup_basic_test(request, mgmt_root, 'rule1', 'Common') |
| 142 | + rule1.update(apiAnonymous='check syntax\n' + RULE) |
| 143 | + assert 'check syntax' in rule1.apiAnonymous |
| 144 | + |
| 145 | + def test_update_samevalue(self, request, mgmt_root): |
| 146 | + rule1 = setup_basic_test(request, mgmt_root, 'rule1', 'Common') |
| 147 | + rule1.update(apiAnonymous='check none\n' + RULE) |
| 148 | + assert 'check syntax' not in rule1.apiAnonymous |
| 149 | + |
| 150 | + |
| 151 | +class TestDelete(object): |
| 152 | + def test_delete(self, request, mgmt_root): |
| 153 | + r1 = setup_basic_test(request, mgmt_root, 'rule1', 'Common') |
| 154 | + r1.delete() |
| 155 | + with pytest.raises(HTTPError) as err: |
| 156 | + mgmt_root.tm.gtm.rules.rule.load( |
| 157 | + name='rule1', partition='Common') |
| 158 | + assert err.response.status_code == 404 |
| 159 | + |
| 160 | + |
| 161 | +class TestRuleCollection(object): |
| 162 | + def test_rule_collection(self, request, mgmt_root): |
| 163 | + setup_create_test(request, mgmt_root, 'rule1', 'Common') |
| 164 | + rule1 = mgmt_root.tm.gtm.rules.rule |
| 165 | + rule1.create(name='rule1', partition='Common', apiAnonymous=RULE) |
| 166 | + assert rule1.name == 'rule1' |
| 167 | + assert rule1.partition == 'Common' |
| 168 | + assert rule1.generation and isinstance(rule1.generation, int) |
| 169 | + assert 'LB_SELECTED' in rule1.apiAnonymous |
| 170 | + assert rule1.fullPath == '/Common/rule1' |
| 171 | + assert rule1.kind == 'tm:gtm:rule:rulestate' |
| 172 | + assert rule1.selfLink.startswith( |
| 173 | + 'https://localhost/mgmt/tm/gtm/rule/~Common~rule1') |
| 174 | + |
| 175 | + rc = mgmt_root.tm.gtm.rules.get_collection() |
| 176 | + assert isinstance(rc, list) |
| 177 | + assert len(rc) |
| 178 | + assert isinstance(rc[0], Rule) |
0 commit comments