|
| 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.datacenter import Datacenter |
| 20 | +from requests.exceptions import HTTPError |
| 21 | + |
| 22 | + |
| 23 | +def delete_dc(mgmt_root, name, partition): |
| 24 | + r = mgmt_root.tm.gtm.datacenters.datacenter |
| 25 | + try: |
| 26 | + r.load(name=name, partition=partition) |
| 27 | + except HTTPError as err: |
| 28 | + if err.response.status_code != 404: |
| 29 | + raise |
| 30 | + return |
| 31 | + r.delete() |
| 32 | + |
| 33 | + |
| 34 | +def setup_create_test(request, mgmt_root, name, partition): |
| 35 | + def teardown(): |
| 36 | + delete_dc(mgmt_root, name, partition) |
| 37 | + request.addfinalizer(teardown) |
| 38 | + |
| 39 | + |
| 40 | +def setup_basic_test(request, mgmt_root, name, partition): |
| 41 | + def teardown(): |
| 42 | + delete_dc(mgmt_root, name, partition) |
| 43 | + |
| 44 | + dc1 = mgmt_root.tm.gtm.datacenters.datacenter |
| 45 | + dc1.create(name=name, partition=partition) |
| 46 | + request.addfinalizer(teardown) |
| 47 | + return dc1 |
| 48 | + |
| 49 | + |
| 50 | +class TestCreate(object): |
| 51 | + def test_create_no_args(self, mgmt_root): |
| 52 | + dc1 = mgmt_root.tm.gtm.datacenters.datacenter |
| 53 | + with pytest.raises(MissingRequiredCreationParameter): |
| 54 | + dc1.create() |
| 55 | + |
| 56 | + def test_create(self, request, mgmt_root): |
| 57 | + setup_create_test(request, mgmt_root, 'dc1', 'Common') |
| 58 | + dc1 = mgmt_root.tm.gtm.datacenters.datacenter |
| 59 | + dc1.create(name='dc1', partition='Common') |
| 60 | + assert dc1.name == 'dc1' |
| 61 | + assert dc1.partition == 'Common' |
| 62 | + assert dc1.generation and isinstance(dc1.generation, int) |
| 63 | + assert dc1.kind == 'tm:gtm:datacenter:datacenterstate' |
| 64 | + assert dc1.selfLink.startswith( |
| 65 | + 'https://localhost/mgmt/tm/gtm/datacenter/~Common~dc1') |
| 66 | + |
| 67 | + def test_create_optional_args(self, request, mgmt_root): |
| 68 | + setup_create_test(request, mgmt_root, 'dc1', 'Common') |
| 69 | + dc1 = mgmt_root.tm.gtm.datacenters.datacenter |
| 70 | + dc1.create(name='dc1', |
| 71 | + partition='Common', |
| 72 | + enabled=False, |
| 73 | + contact="admin@root.local", |
| 74 | + description="A datacenter is fine too", |
| 75 | + location="Between the earth and the moon") |
| 76 | + assert False == dc1.enabled |
| 77 | + assert "admin@root.local" == dc1.contact |
| 78 | + assert "A datacenter is fine too" == dc1.description |
| 79 | + assert "Between the earth and the moon" == dc1.location |
| 80 | + |
| 81 | + def test_create_duplicate(self, request, mgmt_root): |
| 82 | + setup_create_test(request, mgmt_root, 'dc1', 'Common') |
| 83 | + dc1 = mgmt_root.tm.gtm.datacenters.datacenter |
| 84 | + dc1.create(name='dc1', partition='Common') |
| 85 | + dc2 = mgmt_root.tm.gtm.datacenters.datacenter |
| 86 | + with pytest.raises(HTTPError) as err: |
| 87 | + dc2.create(name='dc1', partition='Common') |
| 88 | + assert err.response.status_code == 400 |
| 89 | + |
| 90 | + |
| 91 | +class TestRefresh(object): |
| 92 | + def test_refresh(self, request, mgmt_root): |
| 93 | + setup_basic_test(request, mgmt_root, 'dc1', 'Common') |
| 94 | + d1 = mgmt_root.tm.gtm.datacenters.datacenter.load( |
| 95 | + name='dc1', partition='Common') |
| 96 | + d2 = mgmt_root.tm.gtm.datacenters.datacenter.load( |
| 97 | + name='dc1', partition='Common') |
| 98 | + assert True == d1.enabled |
| 99 | + assert True == d2.enabled |
| 100 | + |
| 101 | + d2.update(enabled=False) |
| 102 | + assert False == d2.enabled |
| 103 | + assert True == d1.enabled |
| 104 | + |
| 105 | + d1.refresh() |
| 106 | + assert False == d1.enabled |
| 107 | + |
| 108 | + |
| 109 | +class TestLoad(object): |
| 110 | + def test_load_no_object(self, mgmt_root): |
| 111 | + with pytest.raises(HTTPError) as err: |
| 112 | + mgmt_root.tm.gtm.datacenters.datacenter.load( |
| 113 | + name='dc1', partition='Common') |
| 114 | + assert err.response.status_code == 404 |
| 115 | + |
| 116 | + def test_load(self, request, mgmt_root): |
| 117 | + setup_basic_test(request, mgmt_root, 'dc1', 'Common') |
| 118 | + dc1 = mgmt_root.tm.gtm.datacenters.datacenter.load( |
| 119 | + name='dc1', partition='Common') |
| 120 | + assert True == dc1.enabled |
| 121 | + dc1.update(enabled=False) |
| 122 | + dc2 = mgmt_root.tm.gtm.datacenters.datacenter.load( |
| 123 | + name='dc1', partition='Common') |
| 124 | + assert False == dc1.enabled |
| 125 | + assert False == dc2.enabled |
| 126 | + |
| 127 | + |
| 128 | +class TestUpdate(object): |
| 129 | + def test_update(self, request, mgmt_root): |
| 130 | + dc1 = setup_basic_test(request, mgmt_root, 'dc1', 'Common') |
| 131 | + assert True == dc1.enabled |
| 132 | + assert False == dc1.disabled |
| 133 | + dc1.update(enabled=False) |
| 134 | + assert False == dc1.enabled |
| 135 | + assert True == dc1.disabled |
| 136 | + |
| 137 | + def test_update_samevalue(self, request, mgmt_root): |
| 138 | + dc1 = setup_basic_test(request, mgmt_root, 'dc1', 'Common') |
| 139 | + dc1.update(enabled=True) |
| 140 | + assert False != dc1.enabled |
| 141 | + |
| 142 | + |
| 143 | +class TestDelete(object): |
| 144 | + def test_delete(self, request, mgmt_root): |
| 145 | + dc1 = setup_basic_test(request, mgmt_root, 'dc1', 'Common') |
| 146 | + dc1.delete() |
| 147 | + with pytest.raises(HTTPError) as err: |
| 148 | + mgmt_root.tm.gtm.datacenters.datacenter.load( |
| 149 | + name='dc1', partition='Common') |
| 150 | + assert err.response.status_code == 404 |
| 151 | + |
| 152 | + |
| 153 | +class TestDatacenterCollection(object): |
| 154 | + def test_datacenter_collection(self, request, mgmt_root): |
| 155 | + setup_create_test(request, mgmt_root, 'dc1', 'Common') |
| 156 | + dc1 = mgmt_root.tm.gtm.datacenters.datacenter |
| 157 | + dc1.create(name='dc1', partition='Common') |
| 158 | + assert dc1.name == 'dc1' |
| 159 | + assert dc1.partition == 'Common' |
| 160 | + assert dc1.generation and isinstance(dc1.generation, int) |
| 161 | + assert dc1.fullPath == '/Common/dc1' |
| 162 | + assert dc1.kind == 'tm:gtm:datacenter:datacenterstate' |
| 163 | + assert dc1.selfLink.startswith( |
| 164 | + 'https://localhost/mgmt/tm/gtm/datacenter/~Common~dc1') |
| 165 | + |
| 166 | + rc = mgmt_root.tm.gtm.datacenters.get_collection() |
| 167 | + assert isinstance(rc, list) |
| 168 | + assert len(rc) |
| 169 | + assert isinstance(rc[0], Datacenter) |
0 commit comments