def test_success(self): auth_file = "auth.file" auth_file_path = os.path.join(settings.booth_config_dir, auth_file) config_content = "authfile={}".format(auth_file_path) auth_file_content = b"auth" (self.config.fs.open(self.config_path, mock.mock_open(read_data=config_content)(), name="open.conf").fs.open( auth_file_path, mock.mock_open(read_data=auth_file_content)(), mode="rb", name="open.authfile", ).corosync_conf.load().http.booth.send_config( self.name, config_content, authfile=auth_file, authfile_data=auth_file_content, node_labels=self.node_list, )) commands.config_sync(self.env_assist.get_env(), self.name) self.env_assist.assert_reports( [fixture.info(report_codes.BOOTH_CONFIG_DISTRIBUTION_STARTED)] + [ fixture.info(report_codes.BOOTH_CONFIG_ACCEPTED_BY_NODE, node=node, name_list=[self.name]) for node in self.node_list ])
def test_authfile_not_in_booth_dir(self): config_file_content = "authfile=/etc/my_booth.conf" (self.config .fs.open( self.config_path, mock.mock_open(read_data=config_file_content)(), name="open.conf" ) .corosync_conf.load() .http.booth.send_config( self.name, config_file_content, node_labels=self.node_list, ) ) commands.config_sync(self.env_assist.get_env(), self.name) self.env_assist.assert_reports( [ fixture.warn(report_codes.BOOTH_UNSUPORTED_FILE_LOCATION), fixture.info(report_codes.BOOTH_CONFIG_DISTRIBUTION_STARTED) ] + [ fixture.info( report_codes.BOOTH_CONFIG_ACCEPTED_BY_NODE, node=node, name_list=[self.name] ) for node in self.node_list ] )
def setUp(self): super(PullConfigWithAuthfile, self).setUp() self.booth_cfg_open_mock = mock.mock_open()() self.authfile = "authfile" self.authfile_path = _get_booth_file_path(self.authfile) self.authfile_data = b"auth" self.pcmk_uid = 2 self.pcmk_gid = 3 (self.config .http.booth.get_config( self.name, self.config_data, authfile=self.authfile, authfile_data=self.authfile_data, node_labels=[self.node_name], ) .fs.exists(self.config_path, False) .fs.open(self.config_path, self.booth_cfg_open_mock, mode="w") .fs.exists(self.authfile_path, False, name="fs.exists.authfile") ) self.addCleanup( lambda: self.booth_cfg_open_mock.write.assert_called_once_with( self.config_data ) )
def test_node_failure_skip_offline(self): (self.config.fs.open( self.config_path, mock.mock_open(read_data="")(), name="open.conf").corosync_conf.load().http.booth.send_config( self.name, "", communication_list=[ dict( label=self.node_list[0], response_code=400, output=self.reason, ), dict(label=self.node_list[1], ) ])) commands.config_sync(self.env_assist.get_env(), self.name, skip_offline_nodes=True) self.env_assist.assert_reports([ fixture.info(report_codes.BOOTH_CONFIG_DISTRIBUTION_STARTED), fixture.info(report_codes.BOOTH_CONFIG_ACCEPTED_BY_NODE, node=self.node_list[1], name_list=[self.name]), fixture.warn( report_codes.NODE_COMMUNICATION_COMMAND_UNSUCCESSFUL, node=self.node_list[0], reason=self.reason, command="remote/booth_set_config", ), ])
def test_authfile_not_accessible(self): auth_file = "auth.file" auth_file_path = os.path.join(settings.booth_config_dir, auth_file) config_content = "authfile={}".format(auth_file_path) (self.config.fs.open(self.config_path, mock.mock_open(read_data=config_content)(), name="open.conf").fs.open( auth_file_path, mode="rb", name="open.authfile", side_effect=EnvironmentError( 0, self.reason, auth_file_path), ).corosync_conf.load().http.booth.send_config( self.name, config_content, node_labels=self.node_list, )) commands.config_sync(self.env_assist.get_env(), self.name) self.env_assist.assert_reports([ fixture.warn( report_codes.FILE_IO_ERROR, reason="{}: '{}'".format(self.reason, auth_file_path), file_role=file_roles.BOOTH_KEY, file_path=auth_file_path, operation="read", ), fixture.info(report_codes.BOOTH_CONFIG_DISTRIBUTION_STARTED) ] + [ fixture.info(report_codes.BOOTH_CONFIG_ACCEPTED_BY_NODE, node=node, name_list=[self.name]) for node in self.node_list ])
def test_node_offline_skip_offline(self): (self.config.fs.open( self.config_path, mock.mock_open(read_data="")(), name="open.conf").corosync_conf.load().http.booth.send_config( self.name, "", communication_list=[ dict( label=self.node_list[0], errno=1, error_msg=self.reason, was_connected=False, ), dict(label=self.node_list[1], ) ], )) commands.config_sync(self.env_assist.get_env(), self.name, skip_offline_nodes=True) self.env_assist.assert_reports([ fixture.info(report_codes.BOOTH_CONFIG_DISTRIBUTION_STARTED), fixture.info(report_codes.BOOTH_CONFIG_ACCEPTED_BY_NODE, node=self.node_list[1], name_list=[self.name]), fixture.warn( report_codes.NODE_COMMUNICATION_ERROR_UNABLE_TO_CONNECT, node=self.node_list[0], reason=self.reason, command="remote/booth_set_config", ), ])
def test_no_authfile(self): (self.config .fs.open( self.config_path, mock.mock_open(read_data="")(), name="open.conf" ) .corosync_conf.load() .http.booth.send_config( self.name, "", node_labels=self.node_list, ) ) commands.config_sync(self.env_assist.get_env(), self.name) self.env_assist.assert_reports( [fixture.info(report_codes.BOOTH_CONFIG_DISTRIBUTION_STARTED)] + [ fixture.info( report_codes.BOOTH_CONFIG_ACCEPTED_BY_NODE, node=node, name_list=[self.name] ) for node in self.node_list ] )
def test_success_read_content_from_file(self): mock_open = mock.mock_open() with mock.patch("pcs.lib.env_file.open", mock_open, create=True): mock_open().read.return_value = "test booth\nconfig" self.assertEqual( "test booth\nconfig", RealFile("some role", "/path/to.file").read() )
def open_authkey(self, pcmk_authkey_content="", fail=False): kwargs = {} if fail: kwargs["side_effect"] = EnvironmentError("open failed") else: kwargs["return_value"] = mock.mock_open( read_data=pcmk_authkey_content)() self.config.fs.open(self.PCMK_AUTHKEY_PATH, mode="rb", **kwargs)
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(), ])
def test_success_write_content_to_path(self): mock_open = mock.mock_open() mock_file_operation = mock.Mock() with patch_env_file("open", mock_open, create=True): env_file.RealFile("some role", CONF_PATH).write( "config content", file_operation=mock_file_operation) mock_open.assert_called_once_with(CONF_PATH, "w") mock_open().write.assert_called_once_with("config content") mock_file_operation.assert_called_once_with(CONF_PATH)
def test_success_write_content_to_path(self): mock_open = mock.mock_open() mock_file_operation = mock.Mock() with mock.patch("pcs.lib.env_file.open", mock_open, create=True): RealFile("some role", "/etc/booth/some-name.conf").write( "config content", file_operation=mock_file_operation) mock_open.assert_called_once_with("/etc/booth/some-name.conf", "w") mock_open().write.assert_called_once_with("config content") mock_file_operation.assert_called_once_with( "/etc/booth/some-name.conf")
def setUp(self): super(PullConfigSuccess, self).setUp() self.booth_cfg_open_mock = mock.mock_open()() (self.config.http.booth.get_config( self.name, self.config_data, node_labels=[self.node_name ]).fs.exists(self.config_path, False).fs.open(self.config_path, self.booth_cfg_open_mock, mode="w")) self.addCleanup(lambda: self.booth_cfg_open_mock.write. assert_called_once_with(self.config_data))
def test_success_binary(self): mock_open = mock.mock_open() mock_file_operation = mock.Mock() with patch_env_file("open", mock_open, create=True): env_file.RealFile("some role", CONF_PATH, is_binary=True).write( "config content".encode("utf-8"), file_operation=mock_file_operation, ) mock_open.assert_called_once_with(CONF_PATH, "wb") mock_open().write.assert_called_once_with( "config content".encode("utf-8")) mock_file_operation.assert_called_once_with(CONF_PATH)
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(), ])
def test_success(self): self.maxDiff = None mock_open = mock.mock_open(read_data="config content") with patch_config_files("open", mock_open, create=True): self.assertEqual("config content", config_files._read_config("my-file.conf")) self.assertEqual([ mock.call(os.path.join(BOOTH_CONFIG_DIR, "my-file.conf"), "r"), mock.call().__enter__(), mock.call().read(), mock.call().__exit__(None, None, None) ], mock_open.mock_calls)
def test_success_write_content_to_path(self): mock_open = mock.mock_open() mock_file_operation = mock.Mock() with mock.patch("pcs.lib.env_file.open", mock_open, create=True): RealFile("some role", "/etc/booth/some-name.conf").write( "config content", file_operation=mock_file_operation ) mock_open.assert_called_once_with("/etc/booth/some-name.conf", "w") mock_open().write.assert_called_once_with("config content") mock_file_operation.assert_called_once_with( "/etc/booth/some-name.conf" )
def test_success_binary(self): mock_open = mock.mock_open() mock_file_operation = mock.Mock() with mock.patch("pcs.lib.env_file.open", mock_open, create=True): RealFile("some role", "/etc/booth/some-name.conf").write( "config content".encode("utf-8"), file_operation=mock_file_operation, is_binary=True) mock_open.assert_called_once_with("/etc/booth/some-name.conf", "wb") mock_open().write.assert_called_once_with( "config content".encode("utf-8")) mock_file_operation.assert_called_once_with( "/etc/booth/some-name.conf")
def test_success(self): path = os.path.join(BOOTH_CONFIG_DIR, "file.key") mock_open = mock.mock_open(read_data="key") with patch_config_files("open", mock_open, create=True): self.assertEqual( "key", config_files.read_authfile(self.mock_reporter, path)) self.assertEqual([ mock.call(path, "rb"), mock.call().__enter__(), mock.call().read(), mock.call().__exit__(None, None, None) ], mock_open.mock_calls) self.assertEqual(0, len(self.mock_reporter.report_item_list))
def setUp(self): super(PullConfigWithAuthfileSuccess, self).setUp() self.booth_authfile_open_mock = mock.mock_open()() (self.config.fs.open(self.authfile_path, self.booth_authfile_open_mock, mode="wb", name="fs.open.authfile.write").fs.chown( self.authfile_path, self.pcmk_uid, self.pcmk_gid).fs.chmod( self.authfile_path, settings.pacemaker_authkey_file_mode)) self.addCleanup(lambda: self.booth_authfile_open_mock.write. assert_called_once_with(self.authfile_data))
def test_success_binary(self): mock_open = mock.mock_open() mock_file_operation = mock.Mock() with mock.patch("pcs.lib.env_file.open", mock_open, create=True): RealFile("some role", "/etc/booth/some-name.conf").write( "config content".encode("utf-8"), file_operation=mock_file_operation, is_binary=True ) mock_open.assert_called_once_with("/etc/booth/some-name.conf", "wb") mock_open().write.assert_called_once_with( "config content".encode("utf-8") ) mock_file_operation.assert_called_once_with( "/etc/booth/some-name.conf" )
def test_read_failure(self, _): path = os.path.join(BOOTH_CONFIG_DIR, "file.key") mock_open = mock.mock_open() mock_open().read.side_effect = EnvironmentError() with patch_config_files("open", mock_open, create=True): return_value = config_files.read_authfile(self.mock_reporter, path) self.assertTrue(return_value is None) assert_report_item_list_equal( self.mock_reporter.report_item_list, [(severities.WARNING, report_codes.FILE_IO_ERROR, { "file_role": file_roles.BOOTH_KEY, "file_path": path, "reason": "reason", "operation": "read", })])
def test_success(self): self.maxDiff = None mock_open = mock.mock_open(read_data="config content") with patch_config_files("open", mock_open, create=True): self.assertEqual( "config content", config_files._read_config("my-file.conf") ) self.assertEqual( [ mock.call(os.path.join(BOOTH_CONFIG_DIR, "my-file.conf"), "r"), mock.call().__enter__(), mock.call().read(), mock.call().__exit__(None, None, None) ], mock_open.mock_calls )
def test_success(self): path = os.path.join(BOOTH_CONFIG_DIR, "file.key") mock_open = mock.mock_open(read_data="key") with patch_config_files("open", mock_open, create=True): self.assertEqual( "key", config_files.read_authfile(self.mock_reporter, path) ) self.assertEqual( [ mock.call(path, "rb"), mock.call().__enter__(), mock.call().read(), mock.call().__exit__(None, None, None) ], mock_open.mock_calls ) self.assertEqual(0, len(self.mock_reporter.report_item_list))
def test_read_failure(self, _): path = os.path.join(BOOTH_CONFIG_DIR, "file.key") mock_open = mock.mock_open() mock_open().read.side_effect = EnvironmentError() with patch_config_files("open", mock_open, create=True): return_value = config_files.read_authfile(self.mock_reporter, path) self.assertTrue(return_value is None) assert_report_item_list_equal( self.mock_reporter.report_item_list, [( severities.WARNING, report_codes.FILE_IO_ERROR, { "file_role": file_roles.BOOTH_KEY, "file_path": path, "reason": "reason", "operation": "read", } )] )
def setUp(self): super(PullConfigWithAuthfileFailure, self).setUp() self.reason = "reason" self.booth_authfile_open_mock = mock.mock_open()()
def test_success_read_content_from_file(self): mock_open = mock.mock_open() with mock.patch("pcs.lib.env_file.open", mock_open, create=True): mock_open().read.return_value = "test booth\nconfig" self.assertEqual("test booth\nconfig", RealFile("some role", "/path/to.file").read())
def assert_read_in_correct_mode(self, real_file, mode): mock_open = mock.mock_open() with patch_env_file("open", mock_open, create=True): mock_open().read.return_value = "test booth\nconfig" self.assertEqual("test booth\nconfig", real_file.read()) mock_open.assert_has_calls([mock.call(FILE_PATH, mode)])
def setUp(self): self.mock_open = mock.mock_open() self.mock_error = self.setup_patch("console_report.error")
def setUp(self): self.is_file = self.setup_patch('os.path.isfile') self.mock_open = mock.mock_open(read_data='filecontent') self.mock_error = self.setup_patch("console_report.error")