Esempio n. 1
0
 def test_mocked_key_not_config(self, mock_stderr):
     with self.assertRaises(SystemExit):
         env.middleware_config(None, "/local/file/path.key")
     mock_stderr.assert_called_once_with(
         "Error: When --booth-key is specified, --booth-conf must be "
         "specified as well\n"
     )
Esempio n. 2
0
    def test_catch_exactly_his_exception(self):
        report_missing = self.setup_patch("env_file.report_missing")
        next_in_line = mock.Mock(side_effect=LibraryEnvError(
            ReportItem.error(report_codes.FILE_DOES_NOT_EXIST, info={
                "file_role": env_file_role_codes.BOOTH_CONFIG,
            }),
            ReportItem.error(report_codes.FILE_DOES_NOT_EXIST, info={
                "file_role": env_file_role_codes.BOOTH_KEY,
            }),
            ReportItem.error("OTHER ERROR", info={}),
        ))
        mock_env = mock.MagicMock()
        self.read.return_value = {"content": None}

        booth_conf_middleware = env.middleware_config(
            "booth-name",
            "/local/file/path.conf",
            "/local/file/path.key",
        )
        raised_exception = []
        def run_middleware():
            try:
                booth_conf_middleware(next_in_line, mock_env)
            except Exception as e:
                raised_exception.append(e)
                raise e
        self.assertRaises(LibraryEnvError, run_middleware)
        self.assertEqual(1, len(raised_exception[0].unprocessed))
        self.assertEqual("OTHER ERROR", raised_exception[0].unprocessed[0].code)
        self.assertEqual(report_missing.mock_calls, [
            mock.call('Booth config file', '/local/file/path.conf'),
            mock.call('Booth key file', '/local/file/path.key'),
        ])
Esempio n. 3
0
    def test_sucessfully_care_about_local_file(self, mock_is_file):
        #setup, fixtures
        def next_in_line(env):
            env.booth["modified_env"] = {
                "config_file": {
                    "content": "file content",
                    "no_existing_file_expected": False,
                },
                "key_file": {
                    "content": "key file content",
                    "no_existing_file_expected": False,
                }
            }
            return "call result"
        mock_is_file.return_value = True
        mock_env = mock.MagicMock()

        mock_open = mock.mock_open()
        with mock.patch(
            "pcs.cli.booth.env.open",
            mock_open,
            create=True
        ):
            #run tested code
            booth_conf_middleware = middleware_config(
                "booth-name",
                "/local/file/path.conf",
                "/local/file/path.key",
            )

            self.assertEqual(
                "call result",
                booth_conf_middleware(next_in_line, mock_env)
            )

        #assertions
        self.assertEqual(mock_is_file.mock_calls,[
            mock.call("/local/file/path.conf"),
            mock.call("/local/file/path.key"),
        ])

        self.assertEqual(mock_env.booth["name"], "booth-name")
        self.assertEqual(mock_env.booth["config_file"], {"content": ""})
        self.assertEqual(mock_env.booth["key_file"], {"content": ""})

        self.assertEqual(mock_open.mock_calls, [
            mock.call(u'/local/file/path.conf'),
            mock.call().read(),
            mock.call(u'/local/file/path.key'),
            mock.call().read(),
            mock.call(u'/local/file/path.key', u'w'),
            mock.call().write(u'key file content'),
            mock.call().close(),
            mock.call(u'/local/file/path.conf', u'w'),
            mock.call().write(u'file content'),
            mock.call().close(),
        ])
Esempio n. 4
0
    def test_mocked(self, mock_exists, mock_read, mock_write):
        # pylint: disable=too-many-locals
        conf_content = "file content".encode("utf-8")
        key_content = "key file content".encode("utf-8")
        conf_path = "/tmp/pcs_test/file/path.conf"
        key_path = "/tmp/pcs_test/file/path.key"
        new_conf = "new conf".encode("utf-8")
        new_key = "new key".encode("utf-8")

        def next_in_line(_env):
            self.assertEqual(
                _env.booth,
                {
                    "config_data": conf_content,
                    "key_data": key_content,
                    "key_path": key_path,
                },
            )
            _env.booth["modified_env"] = {
                "config_file": {
                    "content": new_conf
                },
                "key_file": {
                    "content": new_key
                },
            }
            return "call result"

        mock_exists.return_value = True

        def read(this):
            if this.metadata.file_type_code == file_type_codes.BOOTH_KEY:
                return key_content
            if this.metadata.file_type_code == file_type_codes.BOOTH_CONFIG:
                return conf_content
            raise AssertionError(f"Unexpected file type: {this.metadata}")

        mock_read.side_effect = read

        def write(this, data, can_overwrite):
            if this.metadata.file_type_code == file_type_codes.BOOTH_KEY:
                self.assertEqual(data, new_key)
                self.assertTrue(can_overwrite)
                return
            if this.metadata.file_type_code == file_type_codes.BOOTH_CONFIG:
                self.assertEqual(data, new_conf)
                self.assertTrue(can_overwrite)
                return
            raise AssertionError(f"Unexpected file type: {this.metadata}")

        mock_write.side_effect = write

        mock_env = mock.MagicMock()
        booth_conf_middleware = env.middleware_config(conf_path, key_path)

        self.assertEqual("call result",
                         booth_conf_middleware(next_in_line, mock_env))
Esempio n. 5
0
    def test_not_mocked(self):
        def next_in_line(_env):
            self.assertEqual(_env.booth, {})
            return "call result"

        mock_env = mock.MagicMock()

        booth_conf_middleware = env.middleware_config(None, None)
        self.assertEqual("call result",
                         booth_conf_middleware(next_in_line, mock_env))
Esempio n. 6
0
    def test_sucessfully_care_about_local_file(self):
        def next_in_line(env):
            env.booth["modified_env"] = {
                "config_file": {
                    "content": "file content",
                    "no_existing_file_expected": False,
                },
                "key_file": {
                    "content": "key file content",
                    "no_existing_file_expected": False,
                }
            }
            return "call result"

        mock_env = mock.MagicMock()
        booth_conf_middleware = env.middleware_config(
            "booth-name",
            "/local/file/path.conf",
            "/local/file/path.key",
        )

        self.assertEqual("call result",
                         booth_conf_middleware(next_in_line, mock_env))

        self.assertEqual(self.read.mock_calls, [
            mock.call('/local/file/path.conf'),
            mock.call('/local/file/path.key', is_binary=True),
        ])

        self.assertEqual(
            self.process_no_existing_file_expectation.mock_calls, [
                mock.call('booth config file', {
                    'content': 'file content',
                    'no_existing_file_expected': False
                }, '/local/file/path.conf'),
                mock.call(
                    'booth key file', {
                        'content': 'key file content',
                        'no_existing_file_expected': False
                    }, '/local/file/path.key'),
            ])

        self.assertEqual(self.write.mock_calls, [
            mock.call(
                {
                    'content': 'key file content',
                    'no_existing_file_expected': False
                }, '/local/file/path.key'),
            mock.call(
                {
                    'content': 'file content',
                    'no_existing_file_expected': False
                }, '/local/file/path.conf')
        ])
Esempio n. 7
0
    def test_sucessfully_care_about_local_file(self, mock_is_file):
        #setup, fixtures
        def next_in_line(env):
            env.booth["modified_env"] = {
                "config_file": {
                    "content": "file content",
                    "no_existing_file_expected": False,
                },
                "key_file": {
                    "content": "key file content",
                    "no_existing_file_expected": False,
                }
            }
            return "call result"

        mock_is_file.return_value = True
        mock_env = mock.MagicMock()

        mock_open = mock.mock_open()
        with mock.patch("pcs.cli.booth.env.open", mock_open, create=True):
            #run tested code
            booth_conf_middleware = middleware_config(
                "booth-name",
                "/local/file/path.conf",
                "/local/file/path.key",
            )

            self.assertEqual("call result",
                             booth_conf_middleware(next_in_line, mock_env))

        #assertions
        self.assertEqual(mock_is_file.mock_calls, [
            mock.call("/local/file/path.conf"),
            mock.call("/local/file/path.key"),
        ])

        self.assertEqual(mock_env.booth["name"], "booth-name")
        self.assertEqual(mock_env.booth["config_file"], {"content": ""})
        self.assertEqual(mock_env.booth["key_file"], {"content": ""})

        self.assertEqual(mock_open.mock_calls, [
            mock.call(u'/local/file/path.conf'),
            mock.call().read(),
            mock.call(u'/local/file/path.key'),
            mock.call().read(),
            mock.call(u'/local/file/path.key', u'w'),
            mock.call().write(u'key file content'),
            mock.call().close(),
            mock.call(u'/local/file/path.conf', u'w'),
            mock.call().write(u'file content'),
            mock.call().close(),
        ])
Esempio n. 8
0
    def test_catch_exactly_his_exception(
        self, mock_is_file, mock_console_report
    ):
        next_in_line = mock.Mock(side_effect=LibraryEnvError(
            ReportItem.error(report_codes.FILE_DOES_NOT_EXIST, "", info={
                "file_role": env_file_role_codes.BOOTH_CONFIG,
            }),
            ReportItem.error(report_codes.FILE_DOES_NOT_EXIST, "", info={
                "file_role": env_file_role_codes.BOOTH_KEY,
            }),
            ReportItem.error("OTHER ERROR", "", info={}),
        ))
        mock_is_file.return_value = False
        mock_env = mock.MagicMock()

        #run tested code
        booth_conf_middleware = middleware_config(
            "booth-name",
            "/local/file/path.conf",
            "/local/file/path.key",
        )
        raised_exception = []
        def run_middleware():
            try:
                booth_conf_middleware(next_in_line, mock_env)
            except Exception as e:
                raised_exception.append(e)
                raise e

        self.assertRaises(LibraryEnvError, run_middleware)
        self.assertEqual(1, len(raised_exception[0].unprocessed))
        self.assertEqual("OTHER ERROR", raised_exception[0].unprocessed[0].code)

        self.assertEqual(mock_console_report.error.mock_calls, [
            mock.call(
                "Booth config file '/local/file/path.conf' does not exist"
            ),
            mock.call(
                "Booth key file '/local/file/path.key' does not exist"
            ),
        ])
Esempio n. 9
0
    def test_catch_exactly_his_exception(self, mock_is_file,
                                         mock_console_report):
        next_in_line = mock.Mock(side_effect=LibraryEnvError(
            ReportItem.error(report_codes.FILE_DOES_NOT_EXIST,
                             info={
                                 "file_role": env_file_role_codes.BOOTH_CONFIG,
                             }),
            ReportItem.error(report_codes.FILE_DOES_NOT_EXIST,
                             info={
                                 "file_role": env_file_role_codes.BOOTH_KEY,
                             }),
            ReportItem.error("OTHER ERROR", info={}),
        ))
        mock_is_file.return_value = False
        mock_env = mock.MagicMock()

        #run tested code
        booth_conf_middleware = middleware_config(
            "booth-name",
            "/local/file/path.conf",
            "/local/file/path.key",
        )
        raised_exception = []

        def run_middleware():
            try:
                booth_conf_middleware(next_in_line, mock_env)
            except Exception as e:
                raised_exception.append(e)
                raise e

        self.assertRaises(LibraryEnvError, run_middleware)
        self.assertEqual(1, len(raised_exception[0].unprocessed))
        self.assertEqual("OTHER ERROR",
                         raised_exception[0].unprocessed[0].code)

        self.assertEqual(mock_console_report.error.mock_calls, [
            mock.call(
                "Booth config file '/local/file/path.conf' does not exist"),
            mock.call("Booth key file '/local/file/path.key' does not exist"),
        ])