Multiple commands in a single command object
This patch makes it possible to define multiple commands in a single object. - type: command command: - cmd1 - cmd2 to_file: result.txt Change-Id: I4c2052551aeddae78e6d6bdedf8080a0389aa4b3 Related-Bug: #1515517
This commit is contained in:
parent
b7224bd244
commit
b4a1c1fa2b
|
@ -266,17 +266,24 @@ class Command(Driver):
|
|||
|
||||
def __init__(self, data, conf):
|
||||
super(Command, self).__init__(data, conf)
|
||||
self.cmdname = data["command"]
|
||||
self.to_file = data["to_file"]
|
||||
if isinstance(data["command"], list):
|
||||
self.cmds = data["command"]
|
||||
else:
|
||||
self.cmds = [data["command"]]
|
||||
self.to_file = data.get("to_file", "/dev/null")
|
||||
self.target_path = os.path.join(
|
||||
self.conf.target, self.host, "commands", self.to_file)
|
||||
|
||||
def snapshot(self):
|
||||
out = self.command(self.cmdname)
|
||||
for cmd in self.cmds:
|
||||
self._snapshot_single(cmd)
|
||||
|
||||
def _snapshot_single(self, cmd):
|
||||
out = self.command(cmd)
|
||||
utils.execute('mkdir -p "{0}"'.format(os.path.dirname(
|
||||
self.target_path)))
|
||||
with open(self.target_path, "w") as f:
|
||||
f.write("===== COMMAND =====: {0}\n".format(self.cmdname))
|
||||
with open(self.target_path, "a") as f:
|
||||
f.write("===== COMMAND =====: {0}\n".format(cmd))
|
||||
f.write("===== RETURN CODE =====: {0}\n".format(out.return_code))
|
||||
f.write("===== STDOUT =====:\n")
|
||||
if out.stdout:
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import os
|
||||
import random
|
||||
import sys
|
||||
|
||||
|
@ -271,6 +272,66 @@ class TestFile(base.BaseTestCase):
|
|||
mremove.assert_called_with(dir_driver.full_dst_path, data['exclude'])
|
||||
|
||||
|
||||
class TestCommand(base.BaseTestCase):
|
||||
def setUp(self):
|
||||
self.conf = mock.Mock()
|
||||
self.conf.target = '/some/dir'
|
||||
|
||||
def test_init(self):
|
||||
data = {
|
||||
"host": {"hostname": "somehost"},
|
||||
"command": "some command",
|
||||
"to_file": "some_command.txt"
|
||||
}
|
||||
driver_inst = shotgun.driver.Command(data, self.conf)
|
||||
self.assertListEqual(["some command"], driver_inst.cmds)
|
||||
self.assertEqual("some_command.txt", driver_inst.to_file)
|
||||
self.assertEqual(os.path.join("/some/dir", "somehost",
|
||||
"commands", "some_command.txt"),
|
||||
driver_inst.target_path)
|
||||
data = {
|
||||
"host": {"hostname": "somehost"},
|
||||
"command": ["cmd1", "cmd2"],
|
||||
"to_file": "some_command.txt"
|
||||
}
|
||||
driver_inst = shotgun.driver.Command(data, self.conf)
|
||||
self.assertListEqual(["cmd1", "cmd2"], driver_inst.cmds)
|
||||
|
||||
@mock.patch('shotgun.driver.Command._snapshot_single')
|
||||
def test_snapshot(self, msnap_sing):
|
||||
data = {
|
||||
"command": ["cmd1", "cmd2"],
|
||||
}
|
||||
driver_inst = shotgun.driver.Command(data, self.conf)
|
||||
driver_inst.snapshot()
|
||||
expected = [mock.call("cmd1"), mock.call("cmd2")]
|
||||
self.assertListEqual(expected, msnap_sing.call_args_list)
|
||||
|
||||
@mock.patch('shotgun.driver.open', create=True,
|
||||
new_callable=mock.mock_open)
|
||||
@mock.patch('shotgun.driver.Command.command')
|
||||
@mock.patch('shotgun.utils.execute')
|
||||
def test_snapshot_single(self, mexec, mcom, mopen):
|
||||
mout = mock.Mock()
|
||||
mout.return_code = 0
|
||||
mout.stdout = "stdout"
|
||||
mout.stderr = "stderr"
|
||||
mcom.return_value = mout
|
||||
driver_inst = shotgun.driver.Command({"command": "cmd"}, self.conf)
|
||||
driver_inst._snapshot_single("cmd")
|
||||
expected_write = [
|
||||
mock.call("===== COMMAND =====: cmd\n"),
|
||||
mock.call("===== RETURN CODE =====: 0\n"),
|
||||
mock.call("===== STDOUT =====:\n"),
|
||||
mock.call("stdout"),
|
||||
mock.call("\n===== STDERR =====:\n"),
|
||||
mock.call("stderr"),
|
||||
]
|
||||
file_handle_mock = mopen.return_value.__enter__.return_value
|
||||
self.assertListEqual(expected_write,
|
||||
file_handle_mock.write.call_args_list)
|
||||
|
||||
|
||||
class TestOffline(base.BaseTestCase):
|
||||
|
||||
@mock.patch('shotgun.driver.open', create=True,
|
||||
|
|
Loading…
Reference in New Issue