|
| 1 | +# Copyright 2016 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 copy |
| 17 | +from f5.bigip.resource import MissingRequiredCreationParameter |
| 18 | +import pytest |
| 19 | +from requests.exceptions import HTTPError |
| 20 | + |
| 21 | + |
| 22 | +def delete_external_datagroup(mgmt_root, name, partition, DATAGROUP): |
| 23 | + try: |
| 24 | + dg = mgmt_root.tm.ltm.data_group.externals.external.load( |
| 25 | + name=name, partition=partition) |
| 26 | + |
| 27 | + except HTTPError as err: |
| 28 | + if err.response.status_code != 404: |
| 29 | + raise |
| 30 | + return |
| 31 | + dg.delete() |
| 32 | + |
| 33 | + try: |
| 34 | + DATAGROUP.delete() |
| 35 | + except HTTPError as err: |
| 36 | + if err.response.status_code != 404: |
| 37 | + raise |
| 38 | + return |
| 39 | + |
| 40 | + |
| 41 | +def delete_internal_datagroup(mgmt_root, name, partition): |
| 42 | + try: |
| 43 | + dg = mgmt_root.tm.ltm.data_group.internals.internal.load( |
| 44 | + name=name, partition=partition) |
| 45 | + |
| 46 | + except HTTPError as err: |
| 47 | + if err.response.status_code != 404: |
| 48 | + raise |
| 49 | + return |
| 50 | + dg.delete() |
| 51 | + |
| 52 | + |
| 53 | +def setup_create_test_edg(request, mgmt_root, name, partition, DATAGROUP): |
| 54 | + def teardown(): |
| 55 | + delete_external_datagroup(mgmt_root, name, partition, DATAGROUP) |
| 56 | + |
| 57 | + request.addfinalizer(teardown) |
| 58 | + |
| 59 | + |
| 60 | +def setup_basic_test_edg(request, mgmt_root, name, partition, DATAGROUP, |
| 61 | + **kwargs): |
| 62 | + def teardown(): |
| 63 | + delete_external_datagroup(mgmt_root, name, partition, DATAGROUP) |
| 64 | + |
| 65 | + dg1 = mgmt_root.tm.ltm.data_group.externals.external.create( |
| 66 | + name='dg1', partition='Common', |
| 67 | + externalFileName=DATAGROUP.name, **kwargs) |
| 68 | + |
| 69 | + request.addfinalizer(teardown) |
| 70 | + return dg1 |
| 71 | + |
| 72 | + |
| 73 | +def setup_create_test_idg(request, mgmt_root, name, partition): |
| 74 | + def teardown(): |
| 75 | + delete_internal_datagroup(mgmt_root, name, partition) |
| 76 | + |
| 77 | + request.addfinalizer(teardown) |
| 78 | + |
| 79 | + |
| 80 | +def setup_basic_test_idg(request, mgmt_root, name, partition, **kwargs): |
| 81 | + def teardown(): |
| 82 | + delete_internal_datagroup(mgmt_root, name, partition) |
| 83 | + |
| 84 | + dg1 = mgmt_root.tm.ltm.data_group.internals.internal.create( |
| 85 | + name='dg1', partition='Common', type='string', |
| 86 | + records=[{'name': 'a', 'data': '1'}], **kwargs) |
| 87 | + |
| 88 | + request.addfinalizer(teardown) |
| 89 | + return dg1 |
| 90 | + |
| 91 | + |
| 92 | +class TestExternalDatagroup(object): |
| 93 | + def test_create_no_args(self, mgmt_root): |
| 94 | + with pytest.raises(MissingRequiredCreationParameter): |
| 95 | + mgmt_root.tm.ltm.data_group.externals.external.create() |
| 96 | + |
| 97 | + def test_create_no_filename(self, mgmt_root): |
| 98 | + with pytest.raises(MissingRequiredCreationParameter): |
| 99 | + mgmt_root.tm.ltm.data_group.externals.external.create( |
| 100 | + name='dg1', type='string', partition='Common') |
| 101 | + |
| 102 | + def test_create_no_type(self, mgmt_root, DATAGROUP): |
| 103 | + with pytest.raises(MissingRequiredCreationParameter): |
| 104 | + mgmt_root.tm.ltm.data_group.externals.external.create( |
| 105 | + name='dg1', partition='Common', fileName=DATAGROUP.name) |
| 106 | + |
| 107 | + def test_create(self, request, mgmt_root, DATAGROUP): |
| 108 | + setup_create_test_edg(request, mgmt_root, 'dg1', 'Common', DATAGROUP) |
| 109 | + dg1 = mgmt_root.tm.ltm.data_group.externals.external.create( |
| 110 | + name='dg1', partition='Common', |
| 111 | + externalFileName=DATAGROUP.name) |
| 112 | + |
| 113 | + assert dg1.name == 'dg1' |
| 114 | + assert dg1.partition == 'Common' |
| 115 | + assert dg1.type == 'string' |
| 116 | + |
| 117 | + def test_delete(self, request, mgmt_root, DATAGROUP): |
| 118 | + dg1 = setup_basic_test_edg(request, mgmt_root, 'dg1', 'Common', |
| 119 | + DATAGROUP) |
| 120 | + dg1.delete() |
| 121 | + with pytest.raises(HTTPError) as err: |
| 122 | + mgmt_root.tm.ltm.data_group.externals.external.load( |
| 123 | + name='dg1', partition='Common') |
| 124 | + assert err.response.status_code == 404 |
| 125 | + |
| 126 | + try: |
| 127 | + DATAGROUP.delete() |
| 128 | + except HTTPError as err: |
| 129 | + if err.response.status_code != 404: |
| 130 | + raise |
| 131 | + return |
| 132 | + |
| 133 | + def test_modify(self, request, mgmt_root, DATAGROUP): |
| 134 | + dg1 = setup_basic_test_edg(request, mgmt_root, 'dg1', 'Common', |
| 135 | + DATAGROUP, description='first_fake') |
| 136 | + assert dg1.description == 'first_fake' |
| 137 | + original_dict = copy.copy(dg1.__dict__) |
| 138 | + desc = 'description' |
| 139 | + dg1.modify(description='CustomFake') |
| 140 | + for k, v in original_dict.items(): |
| 141 | + if k != desc: |
| 142 | + original_dict[k] = dg1.__dict__[k] |
| 143 | + elif k == desc: |
| 144 | + assert dg1.__dict__[k] == 'CustomFake' |
| 145 | + |
| 146 | + def test_update(self, request, mgmt_root, DATAGROUP): |
| 147 | + dg1 = setup_basic_test_edg(request, mgmt_root, 'dg1', 'Common', |
| 148 | + DATAGROUP, description='first_fake') |
| 149 | + assert dg1.description == 'first_fake' |
| 150 | + dg1.description = 'CustomFake' |
| 151 | + dg1.update() |
| 152 | + assert dg1.description == 'CustomFake' |
| 153 | + |
| 154 | + |
| 155 | +class TestInternalDatagroup(object): |
| 156 | + def test_create_no_args(self, mgmt_root): |
| 157 | + with pytest.raises(MissingRequiredCreationParameter): |
| 158 | + mgmt_root.tm.ltm.data_group.internals.internal.create() |
| 159 | + |
| 160 | + def test_create_no_records(self, mgmt_root): |
| 161 | + with pytest.raises(MissingRequiredCreationParameter): |
| 162 | + mgmt_root.tm.ltm.data_group.internals.internal.create( |
| 163 | + name='dg1', type='string', partition='Common') |
| 164 | + |
| 165 | + def test_create_no_type(self, mgmt_root): |
| 166 | + with pytest.raises(MissingRequiredCreationParameter): |
| 167 | + mgmt_root.tm.ltm.data_group.internals.internal.create( |
| 168 | + name='dg1', partition='Common', |
| 169 | + records=[{'name': 'a', 'data': '1'}]) |
| 170 | + |
| 171 | + def test_create(self, request, mgmt_root): |
| 172 | + setup_create_test_idg(request, mgmt_root, 'dg1', 'Common') |
| 173 | + dg1 = mgmt_root.tm.ltm.data_group.internals.internal.create( |
| 174 | + name='dg1', partition='Common', type='string', |
| 175 | + records=[{'name': 'a', 'data': '1'}]) |
| 176 | + |
| 177 | + assert dg1.name == 'dg1' |
| 178 | + assert dg1.partition == 'Common' |
| 179 | + assert dg1.type == 'string' |
| 180 | + |
| 181 | + def test_delete(self, request, mgmt_root): |
| 182 | + dg1 = setup_basic_test_idg(request, mgmt_root, 'dg1', 'Common') |
| 183 | + dg1.delete() |
| 184 | + with pytest.raises(HTTPError) as err: |
| 185 | + mgmt_root.tm.ltm.data_group.internals.internal.load( |
| 186 | + name='dg1', partition='Common') |
| 187 | + assert err.response.status_code == 404 |
| 188 | + |
| 189 | + def test_modify(self, request, mgmt_root): |
| 190 | + dg1 = setup_basic_test_idg(request, mgmt_root, 'dg1', 'Common', |
| 191 | + description='first_fake') |
| 192 | + assert dg1.description == 'first_fake' |
| 193 | + original_dict = copy.copy(dg1.__dict__) |
| 194 | + desc = 'description' |
| 195 | + dg1.modify(description='CustomFake') |
| 196 | + for k, v in original_dict.items(): |
| 197 | + if k != desc: |
| 198 | + original_dict[k] = dg1.__dict__[k] |
| 199 | + elif k == desc: |
| 200 | + assert dg1.__dict__[k] == 'CustomFake' |
| 201 | + |
| 202 | + def test_update(self, request, mgmt_root): |
| 203 | + dg1 = setup_basic_test_idg(request, mgmt_root, 'dg1', 'Common', |
| 204 | + description='first_fake') |
| 205 | + assert dg1.description == 'first_fake' |
| 206 | + dg1.description = 'CustomFake' |
| 207 | + dg1.update() |
| 208 | + assert dg1.description == 'CustomFake' |
0 commit comments