Skip to content

Commit f5c954e

Browse files
f5-rahmcaphrim007
authored andcommitted
Issue #538 - added unix-ls command (#682)
* Issue #538 - added unix-ls command Updated file: f5/bigip/tm/util/__init__.py Added file: f5/bigip/tm/util/Unix_Ls.py f5/bigip/tm/util/test/test_unix_ls.py test/function/tm/util/test_unix_ls.py * Issue #538 - fixed/expanded test cases * Issue #538 - add tmos version dependency to test case * Issue #538 - fixed incorrect references in header
1 parent 451554c commit f5c954e

4 files changed

Lines changed: 176 additions & 0 deletions

File tree

f5/bigip/tm/util/Unix_Ls.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# coding=utf-8
2+
#
3+
# Copyright 2016 F5 Networks Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
"""BIG-IP® utility module
18+
19+
REST URI
20+
``http://localhost/mgmt/tm/util/unix-ls``
21+
22+
GUI Path
23+
N/A
24+
25+
REST Kind
26+
``tm:util:unix-ls:*``
27+
"""
28+
29+
from f5.bigip.mixins import CommandExecutionMixin
30+
from f5.bigip.resource import UnnamedResource
31+
from f5.utils.util_exceptions import UtilError
32+
33+
34+
class Unix_Ls(UnnamedResource, CommandExecutionMixin):
35+
36+
def __init__(self, util):
37+
super(Unix_Ls, self).__init__(util)
38+
self._meta_data['required_command_parameters'].update(('utilCmdArgs',))
39+
self._meta_data['required_json_kind'] = 'tm:util:unix-ls:runstate'
40+
self._meta_data['allowed_commands'].append('run')
41+
42+
def _exec_cmd(self, command, **kwargs):
43+
kwargs['command'] = command
44+
self._check_exclusive_parameters(**kwargs)
45+
requests_params = self._handle_requests_params(kwargs)
46+
self._check_command_parameters(**kwargs)
47+
session = self._meta_data['bigip']._meta_data['icr_session']
48+
response = session.post(
49+
self._meta_data['uri'], json=kwargs, **requests_params)
50+
self._local_update(response.json())
51+
52+
if 'commandResult' in self.__dict__:
53+
if self.commandResult.startswith('/bin/ls'):
54+
raise UtilError('%s' % self.commandResult.split(' ', 1)[1])
55+
else:
56+
return self
57+
else:
58+
return self

f5/bigip/tm/util/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"""
2929

3030
from f5.bigip.resource import PathElement
31+
from f5.bigip.tm.util.Unix_Ls import Unix_Ls
3132
from f5.bigip.tm.util.Unix_Mv import Unix_Mv
3233
from f5.bigip.tm.util.Unix_Rm import Unix_Rm
3334

@@ -36,6 +37,7 @@ class Util(PathElement):
3637
def __init__(self, bigip):
3738
super(Util, self).__init__(bigip)
3839
self._meta_data['allowed_lazy_attributes'] = [
40+
Unix_Ls,
3941
Unix_Mv,
4042
Unix_Rm
4143
]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 mock
17+
import pytest
18+
19+
from f5.bigip import ManagementRoot
20+
from f5.bigip.tm.util.Unix_Ls import Unix_Ls
21+
22+
23+
@pytest.fixture
24+
def FakeUnixLs():
25+
fake_sys = mock.MagicMock()
26+
fake_ls = Unix_Ls(fake_sys)
27+
return fake_ls
28+
29+
30+
@pytest.fixture
31+
def FakeiControl(fakeicontrolsession):
32+
mr = ManagementRoot('host', 'fake_admin', 'fake_admin')
33+
mock_session = mock.MagicMock()
34+
mock_session.post.return_value.json.return_value = {}
35+
mr._meta_data['icr_session'] = mock_session
36+
return mr.tm.util.unix_ls
37+
38+
39+
class TestUnixLsCommand(object):
40+
def test_command_unix_ls(self, FakeiControl):
41+
FakeiControl.exec_cmd('run',
42+
utilCmdArgs='-als /var/tmp/')
43+
session = FakeiControl._meta_data['bigip']._meta_data['icr_session']
44+
assert session.post.call_args == mock.call(
45+
'https://host:443/mgmt/tm/util/unix-ls/',
46+
json={'utilCmdArgs': '-als /var/tmp/', 'command': 'run'}
47+
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
# Copyright 2016 F5 Networks Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
from distutils.version import LooseVersion
18+
import pytest
19+
20+
from f5.utils.util_exceptions import UtilError
21+
from icontrol.session import iControlUnexpectedHTTPError
22+
import os
23+
from tempfile import NamedTemporaryFile
24+
25+
26+
def test_E_unix_ls(mgmt_root):
27+
ntf = NamedTemporaryFile(delete=False)
28+
ntf_basename = os.path.basename(ntf.name)
29+
ntf.write('text for test file')
30+
ntf.seek(0)
31+
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
32+
tpath_name = '/var/config/rest/downloads/{0}'.format(ntf_basename)
33+
34+
# create
35+
fls1 = mgmt_root.tm.util.unix_ls.exec_cmd('run', utilCmdArgs=tpath_name)
36+
# grab tmos version for later use in version discrepancy
37+
tmos_ver = fls1._meta_data['bigip']._meta_data['tmos_version']
38+
39+
# validate object was created
40+
assert fls1.utilCmdArgs == tpath_name
41+
42+
# commandResult should be present with successful listing
43+
assert 'commandResult' in fls1.__dict__
44+
45+
# commandResult listing should match the file we requested a listing for
46+
assert '{0}\n'.format(fls1.utilCmdArgs) == fls1.commandResult
47+
48+
# UtilError should be raised when non-existent file is mentioned
49+
with pytest.raises(UtilError) as err:
50+
mgmt_root.tm.util.unix_ls.exec_cmd('run',
51+
utilCmdArgs='/configs/testfile.txt')
52+
assert 'No such file or directory' in err.response.text
53+
54+
# clean up created file
55+
mgmt_root.tm.util.unix_rm.exec_cmd('run', utilCmdArgs=tpath_name)
56+
57+
# test that a bad command option errors out
58+
if LooseVersion(tmos_ver) < LooseVersion('12.0.0'):
59+
with pytest.raises(UtilError) as err:
60+
mgmt_root.tm.util.unix_ls.exec_cmd('run',
61+
utilCmdArgs='-9')
62+
assert 'invalid option -- 9' in err.response.text
63+
64+
else:
65+
with pytest.raises(iControlUnexpectedHTTPError) as err:
66+
mgmt_root.tm.util.unix_ls.exec_cmd('run',
67+
utilCmdArgs='-9')
68+
assert err.response.status_code == 400
69+
assert 'unix-ls does not support' in err.response.text

0 commit comments

Comments
 (0)