Skip to content

Commit 0d94796

Browse files
authored
Merge pull request #662 from jasonrahm/bugfix.datagroup_tests
Fixes # 637 bugfix.datagroup_tests added functional tests for datagroups
2 parents 72af69e + eac418c commit 0d94796

7 files changed

Lines changed: 322 additions & 2 deletions

File tree

conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,17 @@ def IFILE(mgmt_root):
237237
i = mgmt_root.tm.sys.file.ifiles.ifile.create(name=ntf_basename,
238238
sourcePath=tpath_name)
239239
return i
240+
241+
242+
@pytest.fixture
243+
def DATAGROUP(mgmt_root):
244+
ntf = NamedTemporaryFile(delete=False)
245+
ntf_basename = os.path.basename(ntf.name)
246+
ntf.write('"name1" := "value1",')
247+
ntf.seek(0)
248+
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
249+
tpath_name = 'file:/var/config/rest/downloads/{0}'.format(ntf_basename)
250+
dg = mgmt_root.tm.sys.file.data_groups.data_group.create(
251+
name=ntf_basename, type='string', sourcePath=tpath_name)
252+
253+
return dg

f5/bigip/tm/ltm/data_group.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
``tm:ltm:data-group*``
2727
"""
2828

29+
from distutils.version import LooseVersion
2930
from f5.bigip.resource import Collection
3031
from f5.bigip.resource import OrganizingCollection
3132
from f5.bigip.resource import Resource
@@ -87,3 +88,12 @@ def __init__(self, externals):
8788
self._meta_data['required_creation_parameters'].update(
8889
('name', 'externalFileName')
8990
)
91+
92+
def update(self, **kwargs):
93+
if LooseVersion(self._meta_data['bigip']._meta_data['tmos_version']) \
94+
< LooseVersion('12.0.0'):
95+
if 'externalFileName' in self.__dict__:
96+
del self.__dict__['externalFileName']
97+
if 'type' in self.__dict__:
98+
del self.__dict__['type']
99+
return self._update(**kwargs)

f5/bigip/tm/ltm/test/test_datagroup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ def FakeData_Group_External():
3939

4040
class TestCreate(object):
4141
def test_create_internal_two(self, fakeicontrolsession):
42-
mgmt = ManagementRoot('172.16.44.15', 'admin', 'admin')
42+
mgmt = ManagementRoot('localhost', 'admin', 'admin')
4343
dg1 = mgmt.tm.ltm.data_group.internals.internal
4444
dg2 = mgmt.tm.ltm.data_group.internals.internal
4545
assert dg1 is not dg2
4646

4747
def test_create_external_two(self, fakeicontrolsession):
48-
mgmt = ManagementRoot('172.16.44.15', 'admin', 'admin')
48+
mgmt = ManagementRoot('localhost', 'admin', 'admin')
4949
dg1 = mgmt.tm.ltm.data_group.externals.external
5050
dg2 = mgmt.tm.ltm.data_group.externals.external
5151
assert dg1 is not dg2

f5/bigip/tm/sys/file.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
``tm:sys:file:*``
2727
"""
2828

29+
from distutils.version import LooseVersion
2930
from f5.bigip.resource import Collection
3031
from f5.bigip.resource import OrganizingCollection
3132
from f5.bigip.resource import Resource
@@ -60,6 +61,21 @@ def __init__(self, data_groups):
6061
self._meta_data['required_creation_parameters'].update(
6162
('name', 'sourcePath', 'type'))
6263

64+
def modify(self, **kwargs):
65+
'''Modify is not supported for iFiles
66+
67+
:raises: UnsupportedOperation
68+
'''
69+
raise UnsupportedMethod(
70+
"%s does not support the update method" % self.__class__.__name__)
71+
72+
def update(self, **kwargs):
73+
if LooseVersion(self._meta_data['bigip']._meta_data['tmos_version']) \
74+
< LooseVersion('12.0.0'):
75+
if 'type' in self.__dict__:
76+
del self.__dict__['type']
77+
return self._update(**kwargs)
78+
6379

6480
class Ifiles(Collection):
6581
def __init__(self, File):

f5/bigip/tm/sys/test/test_file.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import pytest
1818

1919
from f5.bigip.resource import MissingRequiredCreationParameter
20+
from f5.bigip.tm.sys.file import Data_Group
2021
from f5.bigip.tm.sys.file import Ifile
2122
from f5.bigip.tm.sys.file import Ssl_Cert
2223
from f5.bigip.tm.sys.file import Ssl_Crl
@@ -25,6 +26,30 @@
2526
from f5.sdk_exception import UnsupportedMethod
2627

2728

29+
@pytest.fixture
30+
def FakeSysDatagroup():
31+
fake_dg_s = mock.MagicMock()
32+
fake_dg = Data_Group(fake_dg_s)
33+
return fake_dg
34+
35+
36+
def test_dg_create_no_args(FakeSysDatagroup):
37+
with pytest.raises(MissingRequiredCreationParameter):
38+
FakeSysDatagroup.create()
39+
40+
41+
def test_dg_create_missing_arg(FakeSysDatagroup):
42+
with pytest.raises(MissingRequiredCreationParameter) as ex:
43+
FakeSysDatagroup.create(name='test_dg')
44+
assert 'sourcePath' in ex.value.message
45+
assert 'type' in ex.value.message
46+
47+
48+
def test_dg_modify(FakeSysDatagroup):
49+
with pytest.raises(UnsupportedMethod):
50+
FakeSysDatagroup.modify(value='Fake')
51+
52+
2853
@pytest.fixture
2954
def FakeSysIfile():
3055
fake_ifile_s = mock.MagicMock()
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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'

test/functional/tm/sys/test_file.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,53 @@ def create_sslfiles():
7070
return key, csr, cert
7171

7272

73+
def setup_datagroup_test(request, mgmt_root, name, sourcepath, **kwargs):
74+
dg1 = mgmt_root.tm.sys.file.data_groups.data_group.create(
75+
name=name, type='string', sourcePath=sourcepath, **kwargs)
76+
77+
def teardown():
78+
# Remove the external dg.
79+
try:
80+
dg1.delete()
81+
except HTTPError as err:
82+
if err.response.status_code != 404:
83+
raise
84+
request.addfinalizer(teardown)
85+
86+
return dg1
87+
88+
89+
def test_CURDL_datagroup(request, mgmt_root):
90+
# Create
91+
ntf = NamedTemporaryFile(delete=False)
92+
ntf_basename = os.path.basename(ntf.name)
93+
ntf.write('"name1" := "value1",')
94+
ntf.seek(0)
95+
# Upload the file
96+
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
97+
tpath_name = 'file:/var/config/rest/downloads/{0}'.format(ntf_basename)
98+
dg1 = setup_datagroup_test(request, mgmt_root, ntf_basename, tpath_name,
99+
)
100+
assert dg1.name == ntf_basename
101+
102+
# Load Object
103+
dg2 = mgmt_root.tm.sys.file.data_groups.data_group.load(name=ntf_basename)
104+
assert dg1.name == dg2.name
105+
106+
# Rewrite the contents and update the object
107+
ntf.write('"name2" := "value2",')
108+
ntf.seek(0)
109+
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
110+
111+
dg3 = mgmt_root.tm.sys.file.data_groups.data_group.load(name=ntf_basename)
112+
dg3.update(sourcePath=tpath_name)
113+
assert dg1.revision != dg3.revision
114+
115+
# Refresh dg2 and make sure revision matches dg3
116+
dg2.refresh()
117+
assert dg2.revision == dg3.revision
118+
119+
73120
def setup_ifile_test(request, mgmt_root, name, sourcepath, **kwargs):
74121
if1 = mgmt_root.tm.sys.file.ifiles.ifile.create(name=name,
75122
sourcePath=sourcepath,

0 commit comments

Comments
 (0)