/usr/lib/python3/dist-packages/plainbox/impl/commands/test_script.py is in python3-plainbox 0.5.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | # This file is part of Checkbox.
#
# Copyright 2013 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
plainbox.impl.commands.test_script
==================================
Test definitions for plainbox.impl.script module
"""
import argparse
from inspect import cleandoc
from unittest import TestCase
from plainbox.impl.applogic import PlainBoxConfig
from plainbox.impl.commands.script import ScriptInvocation, ScriptCommand
from plainbox.impl.providers.v1 import DummyProvider1
from plainbox.impl.testing_utils import make_job
from plainbox.testing_utils.io import TestIO
from plainbox.vendor import mock
class TestScriptCommand(TestCase):
def setUp(self):
self.parser = argparse.ArgumentParser(prog='test')
self.subparsers = self.parser.add_subparsers()
self.provider_list = [mock.Mock()]
self.config = mock.Mock()
self.ns = mock.Mock()
def test_init(self):
script_cmd = ScriptCommand(self.provider_list, self.config)
self.assertIs(script_cmd.provider_list, self.provider_list)
self.assertIs(script_cmd.config, self.config)
def test_register_parser(self):
ScriptCommand(self.provider_list, self.config).register_parser(
self.subparsers)
with TestIO() as io:
self.parser.print_help()
self.assertIn("script run a command from a job", io.stdout)
with TestIO() as io:
with self.assertRaises(SystemExit):
self.parser.parse_args(['script', '--help'])
self.assertEqual(
io.stdout, cleandoc(
"""
usage: test script [-h] JOB-ID
positional arguments:
JOB-ID Id of the job to run
optional arguments:
-h, --help show this help message and exit
""")
+ "\n")
@mock.patch("plainbox.impl.commands.script.ScriptInvocation")
def test_invoked(self, patched_ScriptInvocation):
retval = ScriptCommand(
self.provider_list, self.config).invoked(self.ns)
patched_ScriptInvocation.assert_called_once_with(
self.provider_list, self.config, self.ns.job_id)
self.assertEqual(
retval, patched_ScriptInvocation(
self.provider_list, self.config,
self.ns.job_id).run.return_value)
class ScriptInvocationTests(TestCase):
JOB_ID = '2013.com.canonical.plainbox::foo'
JOB_PARTIAL_ID = 'foo'
def setUp(self):
self.provider_list = mock.Mock()
self.config = PlainBoxConfig()
self.job_id = mock.Mock()
def assertCommandOutput(self, actual, expected):
self.assertEqual(actual, cleandoc(expected) + '\n')
def test_init(self):
script_inv = ScriptInvocation(
self.provider_list, self.config, self.job_id)
self.assertIs(script_inv.provider_list, self.provider_list)
self.assertIs(script_inv.config, self.config)
self.assertIs(script_inv.job_id, self.job_id)
def test_run_no_such_job(self):
provider_list = [DummyProvider1()]
script_inv = ScriptInvocation(provider_list, self.config, self.JOB_ID)
with TestIO() as io:
retval = script_inv.run()
self.assertCommandOutput(
io.stdout, (
"""
There is no job called '{job_id}'
See `plainbox special --list-jobs` for a list of choices
""").format(job_id=self.JOB_ID))
self.assertEqual(retval, 126)
def test_run_job_without_command(self):
provider_list = [DummyProvider1([make_job(self.JOB_PARTIAL_ID)])]
script_inv = ScriptInvocation(provider_list, self.config, self.JOB_ID)
with TestIO() as io:
retval = script_inv.run()
self.assertCommandOutput(
io.stdout, (
"""
Selected job does not have a command
"""))
self.assertEqual(retval, 125)
@mock.patch('plainbox.impl.ctrl.check_output')
def test_job_with_command(self, mock_check_output):
provider_list = [DummyProvider1([
make_job(self.JOB_PARTIAL_ID, command='echo ok')])]
script_inv = ScriptInvocation(provider_list, self.config, self.JOB_ID)
with TestIO() as io:
retval = script_inv.run()
self.assertCommandOutput(
io.stdout, (
"""
(job {job_id}, <stdout:00001>) ok
job {job_id} returned 0
command: echo ok
""").format(job_id=self.JOB_ID))
self.assertEqual(retval, 0)
@mock.patch('plainbox.impl.ctrl.check_output')
def test_job_with_command_making_files(self, mock_check_output):
provider_list = [DummyProvider1([
make_job(self.JOB_PARTIAL_ID, command='echo ok > file')])]
script_inv = ScriptInvocation(provider_list, self.config, self.JOB_ID)
with TestIO() as io:
retval = script_inv.run()
self.maxDiff = None
self.assertCommandOutput(
io.stdout, (
"""
Leftover file detected: 'files-created-in-current-dir/file':
files-created-in-current-dir/file:1: ok
job {job_id} returned 0
command: echo ok > file
""").format(job_id=self.JOB_ID))
self.assertEqual(retval, 0)
|