def test_warn_bad_oci_image_format_no_layout(self):
        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        exit_mock = MagicMock()
        configure_logging_mock = MagicMock()
        bndl_main.logging_setup.configure_logging(MagicMock(), stdout_mock,
                                                  stderr_mock)
        temp = tempfile.mkdtemp()

        try:
            os.mkdir(os.path.join(temp, 'refs'))
            open(os.path.join(temp, 'refs/latest'), 'a').close()

            with \
                    patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                    patch('sys.stdout.isatty', lambda: False), \
                    patch('conductr_cli.logging_setup.configure_logging', configure_logging_mock), \
                    patch('sys.exit', exit_mock):
                bndl_main.run([
                    '--name', 'test', '-f', 'oci-image', '-t', 'latest', temp
                ])

            self.assertEqual(
                self.output(stderr_mock),
                as_error(
                    'Error: bndl: Invalid OCI Image. Missing oci-layout\n'))

            exit_mock.assert_called_once_with(2)
        finally:
            shutil.rmtree(temp)
    def test_warn_bad_oci_image_format_no_tag(self):
        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        exit_mock = MagicMock()
        configure_logging_mock = MagicMock()
        bndl_main.logging_setup.configure_logging(MagicMock(), stdout_mock,
                                                  stderr_mock)
        temp = tempfile.mkdtemp()

        try:
            with \
                    patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                    patch('sys.stdout.isatty', lambda: False), \
                    patch('conductr_cli.logging_setup.configure_logging', configure_logging_mock), \
                    patch('sys.exit', exit_mock):
                bndl_main.run([
                    '--name', 'test', '-f', 'oci-image', '-t', 'latest', temp
                ])

            self.assertEqual(self.output(stderr_mock),
                             as_error('Error: bndl: Not an OCI Image\n'))

            exit_mock.assert_called_once_with(2)
        finally:
            shutil.rmtree(temp)
    def test_run_dash_rewrite(self):
        bndl_mock = MagicMock()
        exit_mock = MagicMock()

        with \
                patch('conductr_cli.bndl_main.bndl', bndl_mock), \
                patch('sys.stdout.isatty', lambda: False), \
                patch('sys.stdin.isatty', lambda: False), \
                patch('sys.exit', exit_mock):
            bndl_main.run(['-o', '-', '-'])

        self.assertEqual(bndl_mock.call_count, 1)
        self.assertIsNone(bndl_mock.call_args[0][0].output)
        self.assertIsNone(bndl_mock.call_args[0][0].source)
    def test_warn_output_tty(self):
        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        exit_mock = MagicMock()
        configure_logging_mock = MagicMock()
        bndl_main.logging_setup.configure_logging(MagicMock(), stdout_mock,
                                                  stderr_mock)

        with \
                patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b''), 'isatty': MagicMock(return_value=False)})), \
                patch('sys.stdout.isatty', lambda: True), \
                patch('conductr_cli.logging_setup.configure_logging', configure_logging_mock), \
                patch('sys.exit', exit_mock):
            bndl_main.run(['--name', 'test', '--tag', 'latest'])

        self.assertEqual(
            self.output(stderr_mock),
            as_error(
                'Error: bndl: Refusing to write to terminal. Provide -o or redirect elsewhere\n'
            ))

        exit_mock.assert_called_once_with(2)
    def test_bndl_oci_image_missing_args(self):
        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        exit_mock = MagicMock()
        configure_logging_mock = MagicMock()
        bndl_main.logging_setup.configure_logging(MagicMock(), stdout_mock,
                                                  stderr_mock)

        with \
                patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                patch('sys.stdout.isatty', lambda: False), \
                patch('conductr_cli.logging_setup.configure_logging', configure_logging_mock), \
                patch('sys.exit', exit_mock):
            bndl_main.run(['-f', 'oci-image'])

        self.assertEqual(
            self.output(stderr_mock),
            as_error(
                'Error: bndl: OCI Image support requires that you provide a --name argument\n'
            ))

        exit_mock.assert_called_once_with(2)
    def test_warn_bad_file(self):
        stdout_mock = MagicMock()
        stderr_mock = MagicMock()
        exit_mock = MagicMock()
        configure_logging_mock = MagicMock()
        bndl_main.logging_setup.configure_logging(MagicMock(), stdout_mock,
                                                  stderr_mock)

        with \
                patch('sys.stdin', MagicMock(**{'buffer': BytesIO(b'')})), \
                patch('sys.stdout.isatty', lambda: False), \
                patch('conductr_cli.logging_setup.configure_logging', configure_logging_mock), \
                patch('sys.exit', exit_mock):
            bndl_main.run(['--name', 'test', '-f', 'docker', '/some/file'])

        self.assertEqual(
            self.output(stderr_mock),
            as_error(
                'Error: bndl: Unable to read /some/file. Must be the path to a valid file or directory\n'
            ))

        exit_mock.assert_called_once_with(2)
Beispiel #7
0
def main_method():
    bndl_main.run()