-
Notifications
You must be signed in to change notification settings - Fork 133
Issue #538 - Adding bash utility #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
caphrim007
merged 4 commits into
F5Networks:development
from
jasonrahm:feature.util_bash
Sep 15, 2016
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2ad5ec
Issue #538 - Adding bash utility
f5-rahm 83a5d32
Issue #538 - Restricted utilCmdArgs submissions to "-c" bash argument
f5-rahm b661d93
Issue #538 - fixed flake8 errors
f5-rahm 5c10bc9
Issue #538 - Added 2 test cases for test coverage
f5-rahm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # coding=utf-8 | ||
| # | ||
| # Copyright 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. | ||
| # | ||
| """BIG-IP® utility module | ||
|
|
||
| REST URI | ||
| ``http://localhost/mgmt/tm/util/bash`` | ||
|
|
||
| GUI Path | ||
| N/A | ||
|
|
||
| REST Kind | ||
| ``tm:util:bash:*`` | ||
| """ | ||
|
|
||
| from f5.bigip.mixins import CommandExecutionMixin | ||
| from f5.bigip.resource import UnnamedResource | ||
| from f5.utils.util_exceptions import UtilError | ||
|
|
||
|
|
||
| class Bash(UnnamedResource, CommandExecutionMixin): | ||
| """BIG-IP® utility command | ||
|
|
||
| .. note:: | ||
|
|
||
| This is an unnamed resource so it has not ~Partition~Name pattern | ||
| at the end of its URI. | ||
| """ | ||
|
|
||
| def __init__(self, util): | ||
| super(Bash, self).__init__(util) | ||
| self._meta_data['required_command_parameters'].update(('utilCmdArgs',)) | ||
| self._meta_data['required_json_kind'] = 'tm:util:bash:runstate' | ||
| self._meta_data['allowed_commands'].append('run') | ||
|
|
||
| def _exec_cmd(self, command, **kwargs): | ||
| kwargs['command'] = command | ||
| self._check_exclusive_parameters(**kwargs) | ||
| requests_params = self._handle_requests_params(kwargs) | ||
| self._check_command_parameters(**kwargs) | ||
|
|
||
| if not kwargs['utilCmdArgs'].startswith("-c"): | ||
| raise UtilError( | ||
| 'Required format is "-c <bash command and arguments>"') | ||
|
|
||
| session = self._meta_data['bigip']._meta_data['icr_session'] | ||
| response = session.post( | ||
| self._meta_data['uri'], json=kwargs, **requests_params) | ||
| self._local_update(response.json()) | ||
|
|
||
| if 'commandResult' in self.__dict__: | ||
| if self.commandResult.startswith('/bin/bash'): | ||
| raise UtilError('%s' % self.commandResult.split(' ', 1)[1]) | ||
| else: | ||
| return self | ||
| else: | ||
| return self | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Copyright 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. | ||
| # | ||
|
|
||
| import mock | ||
| import pytest | ||
|
|
||
| from f5.bigip import ManagementRoot | ||
| from f5.bigip.tm.util.Bash import Bash | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def FakeBash(): | ||
| fake_sys = mock.MagicMock() | ||
| fake_bash = Bash(fake_sys) | ||
| return fake_bash | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def FakeiControl(fakeicontrolsession): | ||
| mr = ManagementRoot('host', 'fake_admin', 'fake_admin') | ||
| mock_session = mock.MagicMock() | ||
| mock_session.post.return_value.json.return_value = {} | ||
| mr._meta_data['icr_session'] = mock_session | ||
| return mr.tm.util.bash | ||
|
|
||
|
|
||
| class TestBashCommand(object): | ||
| def test_command_bash(self, FakeiControl): | ||
| FakeiControl.exec_cmd('run', | ||
| utilCmdArgs='-c "df -k"') | ||
| session = FakeiControl._meta_data['bigip']._meta_data['icr_session'] | ||
| assert session.post.call_args == mock.call( | ||
| 'https://host:443/mgmt/tm/util/bash/', | ||
| json={'utilCmdArgs': '-c "df -k"', 'command': 'run'} | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
|
|
||
| # Copyright 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. | ||
| # | ||
|
|
||
| import pytest | ||
|
|
||
| from f5.bigip.resource import MissingRequiredCommandParameter | ||
| from f5.utils.util_exceptions import UtilError | ||
| from icontrol.session import iControlUnexpectedHTTPError | ||
|
|
||
|
|
||
| def test_E_bash(mgmt_root): | ||
|
|
||
| with pytest.raises(MissingRequiredCommandParameter) as err: | ||
| mgmt_root.tm.util.bash.exec_cmd('run') | ||
| assert "Missing required params: ['utilCmdArgs']" in err.response.text | ||
|
|
||
| # use bash to create a test file | ||
| bash1 = mgmt_root.tm.util.bash.exec_cmd( | ||
| 'run', | ||
| utilCmdArgs='-c "echo hello >> /var/tmp/test.txt"') | ||
|
|
||
| # commandResult should not be present if this was successful | ||
| assert 'commandResult' not in bash1.__dict__ | ||
|
|
||
| bash2 = mgmt_root.tm.util.bash.exec_cmd( | ||
| 'run', | ||
| utilCmdArgs='-c df -k') | ||
|
|
||
| # commandResult should b present with data from 'df -k' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "be". Again, we'll fix it in a follow-up PR |
||
| assert 'commandResult' in bash2.__dict__ | ||
|
|
||
| # UtilError should be raised if -c is not specified | ||
| with pytest.raises(UtilError) as err: | ||
| mgmt_root.tm.util.bash.exec_cmd('run', | ||
| utilCmdArgs='-9 hello') | ||
| assert 'Required format is "-c <bash command and arguments>"'\ | ||
| in err.response.text | ||
|
|
||
| # UtilError should be raised if command isn't found | ||
| with pytest.raises(UtilError) as err: | ||
| mgmt_root.tm.util.bash.exec_cmd('run', | ||
| utilCmdArgs='-c hello') | ||
| assert 'command not found' in err.response.text | ||
|
|
||
| # clean up test file | ||
| mgmt_root.tm.util.unix_rm.exec_cmd('run', utilCmdArgs='/var/tmp/test.txt') | ||
|
|
||
| # iControlUnexpectedHTTPError should be raised if quotes don't match | ||
| with pytest.raises(iControlUnexpectedHTTPError) as err: | ||
| mgmt_root.tm.util.bash.exec_cmd('run', | ||
| utilCmdArgs='-c "df -k') | ||
| assert err.response.status_code == 400 | ||
| assert 'quotes are not balanced' in err.response.text | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"no" instead of "not". We will fix this in a follow-up PR