forked from F5Networks/f5-common-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
128 lines (100 loc) · 3.97 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright 2015-2016 F5 Networks Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from f5.bigip import BigIP
import pytest
def pytest_addoption(parser):
parser.addoption("--bigip", action="store",
help="BIG-IP hostname or IP address")
parser.addoption("--username", action="store", help="BIG-IP REST username",
default="admin")
parser.addoption("--password", action="store", help="BIG-IP REST password",
default="admin")
parser.addoption("--port", action="store", help="BIG-IP port",
default=443)
parser.addoption("--peer", action="store",
help="Peer BIG-IP hostname or IP address", default='none')
parser.addoption("--release", action="store",
help="TMOS version, in dotted format, eg. 12.0.0",
default='11.6.0')
@pytest.fixture
def opt_bigip(request):
return request.config.getoption("--bigip")
@pytest.fixture
def opt_username(request):
return request.config.getoption("--username")
@pytest.fixture
def opt_password(request):
return request.config.getoption("--password")
@pytest.fixture
def opt_port(request):
return request.config.getoption("--port")
@pytest.fixture
def bigip(opt_bigip, opt_username, opt_password, opt_port, scope="module"):
'''bigip fixture'''
b = BigIP(opt_bigip, opt_username, opt_password, port=opt_port)
return b
@pytest.fixture
def opt_release(request):
return request.config.getoption("--release")
@pytest.fixture
def opt_peer(request):
return request.config.getoption("--peer")
@pytest.fixture
def peer(opt_peer, opt_username, opt_password, scope="module"):
'''peer bigip fixture'''
p = BigIP(opt_peer, opt_username, opt_password)
return p
@pytest.fixture
def NAT(bigip):
n = bigip.ltm.nats.nat
return n
@pytest.fixture
def USER(bigip):
n = bigip.auth.users.user
return n
def _delete_pools_members(bigip, pool_records):
for pr in pool_records:
if bigip.ltm.pools.pool.exists(partition=pr.partition, name=pr.name):
pool_inst = bigip.ltm.pools.pool.load(partition=pr.partition,
name=pr.name)
members_list = pool_inst.members_s.get_collection()
pool_inst.delete()
for mem_inst in members_list:
mem_inst.delete()
@pytest.fixture
def pool_factory():
def _setup_boilerplate(bigip, request, pool_records):
_delete_pools_members(bigip, pool_records)
pool_registry = {}
members_registry = {}
for pr in pool_records:
pool_registry[pr.name] =\
bigip.ltm.pools.pool.create(partition=pr.partition,
name=pr.name)
if pr.memberconfigs != (tuple(),):
members_collection = pool_registry[pr.name].members_s
for memconf in pr.memberconfigs:
members_registry[memconf.memname] =\
members_collection.members\
.create(partition=memconf.mempartition,
name=memconf.memname)
def deleter():
for member_instance in members_registry.values():
member_instance.delete()
for pool_instance in pool_registry.values():
pool_instance.delete()
request.addfinalizer(deleter)
return pool_registry, members_registry
return _setup_boilerplate