def setUp(self): self._tmp_dir = tempfile.mkdtemp() self._ha_conf_file = os.path.join(self._tmp_dir, 'test_ha.conf') self._ha_config_mgr = HAConfigManager(mock.Mock(), mock.Mock(), ha_conf_file=self._ha_conf_file)
class TestHA(unittest.TestCase): def setUp(self): self._tmp_dir = tempfile.mkdtemp() self._ha_conf_file = os.path.join(self._tmp_dir, 'test_ha.conf') self._ha_config_mgr = HAConfigManager(mock.Mock(), mock.Mock(), ha_conf_file=self._ha_conf_file) def tearDown(self): shutil.rmtree(self._tmp_dir) def _create_tmp_conf_file(self, content): with open(self._ha_conf_file, 'wb') as fobj: fobj.write(content) def test_read_conf(self): content = """\ { "node_type" : "master", "remote_address" : "10.0.0.1" } """ expected_ha_config = new_master_ha_config('10.0.0.1') fobj = StringIO(content) ha_config = self._ha_config_mgr._read_ha_config_from_fobj(fobj) self.assertEqual(expected_ha_config, ha_config) def test_write_conf(self): ha_config = new_master_ha_config('10.0.0.1') expected_content = '{"node_type": "master", "remote_address": "10.0.0.1"}' fobj = StringIO() self._ha_config_mgr._write_ha_config_to_fobj(ha_config, fobj) content = fobj.getvalue() self.assertEqual(expected_content, content) def test_get_ha_config_no_file(self): expected_ha_config = new_disabled_ha_config() ha_config = self._ha_config_mgr.get_ha_config(None, None) self.assertEqual(expected_ha_config, ha_config) def test_get_ha_config(self): content = """\ { "node_type" : "master", "remote_address" : "10.0.0.1" } """ expected_ha_config = new_master_ha_config('10.0.0.1') self._create_tmp_conf_file(content) ha_config = self._ha_config_mgr.get_ha_config(None, None) self.assertEqual(expected_ha_config, ha_config) def test_update_ha_config(self): ha_config = new_master_ha_config('10.0.0.1') self._ha_config_mgr._manage_services = mock.Mock() self._ha_config_mgr.update_ha_config(ha_config, None) expected_ha_config = self._ha_config_mgr._read_ha_config() self.assertEqual(expected_ha_config, ha_config) @mock_subprocess_check_call def test_manage_services_disabled(self): ha_config = new_disabled_ha_config() self._ha_config_mgr._manage_services(ha_config) subprocess.check_call.assert_called_with(['/usr/sbin/xivo-manage-slave-services', 'start'], close_fds=True) @mock_subprocess_check_call def test_manage_services_master(self): ha_config = new_master_ha_config('10.0.0.1') self._ha_config_mgr._manage_services(ha_config) subprocess.check_call.assert_called_with(['/usr/sbin/xivo-manage-slave-services', 'start'], close_fds=True) @mock_subprocess_check_call def test_manage_services_slave(self): ha_config = new_slave_ha_config('10.0.0.1') self._ha_config_mgr._manage_services(ha_config) self.assertFalse(subprocess.check_call.called)