コード例 #1
0
    def test_command_wrong_type(self):
        @command(command_type='foo')
        def foo(*args, **kwargs):
            pass

        args = ['foo']
        self.assertRaises(CommandTypeError, Main(args).run, args)
コード例 #2
0
    def test_command_wrong_type(self, cli):
        @command(command_type="foo")
        def foo(*args, **kwargs):
            pass

        args = ["foo"]
        with pytest.raises(CommandTypeError):
            Main(args).run(args)

        del command.register["foo"]
コード例 #3
0
    def test_command_with_wrong_args_number(self, cli):
        @command(command_type=Type.PYTHON, args=(((1, 2, 3),)))  # wrong args number
        def foo(*args, **kwargs):
            kwargs["q"].put(kwargs["bar"])

        args = ["foo"]
        with pytest.raises(CommandArgParseError):
            Main(args)

        del command.register["foo"]
コード例 #4
0
    def test_command_python(self):
        @command(command_type=Type.PYTHON)
        def foo(*args, **kwargs):
            kwargs['q'].put(42)

        queue = Queue()
        args = ['foo']
        Main(args).run(q=queue)

        self.assertEqual(queue.get(), 42)
コード例 #5
0
    def test_command(self):
        @command
        def foo(*args, **kwargs):
            kwargs['q'].put(42)

        queue = Queue()
        args = ['foo']
        Main(args).run(q=queue)

        self.assertEqual(queue.get(), 42)
コード例 #6
0
    def test_command_with_unknown_args(self):
        @command  # args
        def foo(*args, **kwargs):
            kwargs['q'].put(args[0])

        queue = Queue()
        args = ['foo']
        Main(args).run('foobar', q=queue)

        self.assertEqual(queue.get(), 'foobar')
コード例 #7
0
    def test_command_with_args_and_opts(self):
        @command(command_type=Type.PYTHON,
                 args=((('-b', '--bar'), {'type': int, 'help': 'bar argument'}),))  # args and opts
        def foo(*args, **kwargs):
            kwargs['q'].put(kwargs['bar'])

        queue = Queue()
        args = ['foo']
        Main(args).run(bar=3, q=queue)

        self.assertEqual(queue.get(), 3)
コード例 #8
0
    def test_command_with_args(self):
        @command(command_type=Type.PYTHON,
                 args=((('-b', '--bar'),),))  # args
        def foo(*args, **kwargs):
            kwargs['q'].put(kwargs['bar'])

        queue = Queue()
        args = ['foo']
        Main(args).run(bar='foobar', q=queue)

        self.assertEqual(queue.get(), 'foobar')
コード例 #9
0
    def test_command_python_async(self, cli):
        @command(command_type=Type.PYTHON)
        async def foo(*args, **kwargs):
            kwargs["q"].put(42)

        queue = Queue()
        args = ["foo"]
        Main(args).run(q=queue)

        assert queue.get() == 42

        del command.register["foo"]
コード例 #10
0
    def test_command_with_unknown_args(self, cli):
        @command  # args
        def foo(*args, **kwargs):
            kwargs["q"].put(args[0])

        queue = Queue()
        args = ["foo"]
        Main(args).run("foobar", q=queue)

        assert queue.get() == "foobar"

        del command.register["foo"]
コード例 #11
0
    def test_command_with_args(self, cli):
        @command(command_type=Type.PYTHON, args=((("-b", "--bar"),),))  # args
        def foo(*args, **kwargs):
            kwargs["q"].put(kwargs["bar"])

        queue = Queue()
        args = ["foo"]
        Main(args).run(bar="foobar", q=queue)

        assert queue.get() == "foobar"

        del command.register["foo"]
コード例 #12
0
    def test_command(self, cli):
        @command
        def foo(*args, **kwargs):
            kwargs["q"].put(42)

        queue = Queue()
        args = ["foo"]
        Main(args).run(q=queue)

        assert queue.get() == 42

        del command.register["foo"]
コード例 #13
0
    def test_command_multiple_shell_failing(self):
        @command(command_type=Type.SHELL)
        def foo(*args, **kwargs):
            return [['foo'], ['bar']]

        args = ['foo']
        main = Main(args)
        with patch('clinner.run.base.Popen') as popen_mock:
            popen_mock.return_value.returncode = 1
            main.run()

        self.assertEqual(popen_mock.call_count, 1)
        self.assertEqual(popen_mock.call_args_list[0][1]['args'], ['foo'])
コード例 #14
0
    def test_command_with_callable_args(self):
        def add_arguments(parser):
            parser.add_argument('-b', '--bar', type=int, help='bar argument')

        @command(args=add_arguments)
        def foo(*args, **kwargs):
            kwargs['q'].put(kwargs['bar'])

        queue = Queue()
        args = ['foo']
        Main(args).run(bar=3, q=queue)

        self.assertEqual(queue.get(), 3)
コード例 #15
0
    def test_command_with_args_and_opts(self, cli):
        @command(
            command_type=Type.PYTHON, args=((("-b", "--bar"), {"type": int, "help": "bar argument"}),)
        )  # args and opts
        def foo(*args, **kwargs):
            kwargs["q"].put(kwargs["bar"])

        queue = Queue()
        args = ["foo"]
        Main(args).run(bar=3, q=queue)

        assert queue.get() == 3

        del command.register["foo"]
コード例 #16
0
    def test_command_with_callable_args(self, cli):
        def add_arguments(parser):
            parser.add_argument("-b", "--bar", type=int, help="bar argument")

        @command(args=add_arguments)
        def foo(*args, **kwargs):
            kwargs["q"].put(kwargs["bar"])

        queue = Queue()
        args = ["foo"]
        Main(args).run(bar=3, q=queue)

        assert queue.get() == 3

        del command.register["foo"]
コード例 #17
0
    def test_command_multiple_shell_failing(self, cli):
        @command(command_type=Type.SHELL)
        def foo(*args, **kwargs):
            return [["foo"], ["bar"]]

        args = ["foo"]
        main = Main(args)
        with patch("clinner.run.base.Popen") as popen_mock:
            popen_mock.return_value.returncode = 1
            main.run()

        assert popen_mock.call_count == 1
        assert popen_mock.call_args_list[0][1]["args"] == ["foo"]

        del command.register["foo"]
コード例 #18
0
ファイル: __main__.py プロジェクト: storewise/docker-sia
    condition = Condition()
    if kwargs["unlock"]:
        unlock_thread = Thread(name="unlock", target=unlock, args=(condition,))
        unlock_thread.start()

    socat_cmd = shlex.split("socat tcp-listen:8000,reuseaddr,fork tcp:localhost:9980")
    siad_cmd = shlex.split(f"siad --sia-directory {os.getcwd()}") + list(args)
    try:
        with subprocess.Popen(socat_cmd) as socat_process:
            with subprocess.Popen(
                siad_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True
            ) as siad_process:
                redirect_output_thread = Thread(
                    name="redirect_output", target=redirect_output, args=(siad_process, condition)
                )
                redirect_output_thread.start()
                siad_process.wait()

            socat_process.terminate()

    except KeyboardInterrupt:
        pass
    finally:
        logger.info("Siad command finished with code %d", siad_process.returncode)
        logger.info("Socat command finished with code %d", socat_process.returncode)


if __name__ == "__main__":
    sys.exit(Main().run())