|
| 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 | +import pytest |
| 18 | + |
| 19 | +from f5.bigip.resource import MissingRequiredCommandParameter |
| 20 | +from f5.utils.util_exceptions import UtilError |
| 21 | +from icontrol.session import iControlUnexpectedHTTPError |
| 22 | + |
| 23 | + |
| 24 | +def test_E_bash(mgmt_root): |
| 25 | + |
| 26 | + with pytest.raises(MissingRequiredCommandParameter) as err: |
| 27 | + mgmt_root.tm.util.bash.exec_cmd('run') |
| 28 | + assert "Missing required params: ['utilCmdArgs']" in err.response.text |
| 29 | + |
| 30 | + # use bash to create a test file |
| 31 | + bash1 = mgmt_root.tm.util.bash.exec_cmd( |
| 32 | + 'run', |
| 33 | + utilCmdArgs='-c "echo hello >> /var/tmp/test.txt"') |
| 34 | + |
| 35 | + # commandResult should not be present if this was successful |
| 36 | + assert 'commandResult' not in bash1.__dict__ |
| 37 | + |
| 38 | + bash2 = mgmt_root.tm.util.bash.exec_cmd( |
| 39 | + 'run', |
| 40 | + utilCmdArgs='-c df -k') |
| 41 | + |
| 42 | + # commandResult should b present with data from 'df -k' |
| 43 | + assert 'commandResult' in bash2.__dict__ |
| 44 | + |
| 45 | + # UtilError should be raised if -c is not specified |
| 46 | + with pytest.raises(UtilError) as err: |
| 47 | + mgmt_root.tm.util.bash.exec_cmd('run', |
| 48 | + utilCmdArgs='-9 hello') |
| 49 | + assert 'Required format is "-c <bash command and arguments>"'\ |
| 50 | + in err.response.text |
| 51 | + |
| 52 | + # UtilError should be raised if command isn't found |
| 53 | + with pytest.raises(UtilError) as err: |
| 54 | + mgmt_root.tm.util.bash.exec_cmd('run', |
| 55 | + utilCmdArgs='-c hello') |
| 56 | + assert 'command not found' in err.response.text |
| 57 | + |
| 58 | + # clean up test file |
| 59 | + mgmt_root.tm.util.unix_rm.exec_cmd('run', utilCmdArgs='/var/tmp/test.txt') |
| 60 | + |
| 61 | + # iControlUnexpectedHTTPError should be raised if quotes don't match |
| 62 | + with pytest.raises(iControlUnexpectedHTTPError) as err: |
| 63 | + mgmt_root.tm.util.bash.exec_cmd('run', |
| 64 | + utilCmdArgs='-c "df -k') |
| 65 | + assert err.response.status_code == 400 |
| 66 | + assert 'quotes are not balanced' in err.response.text |
0 commit comments