|
| 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