def test_configure_source(self): self.config.return_value = 'testsource' with patch_open() as (_open, _file): trilio._configure_triliovault_source() _open.assert_called_with( "/etc/apt/sources.list.d/trilio-gemfury-sources.list", "w") _file.write.assert_called_once_with('testsource')
def test_pci_rescan(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.pci_rescan() _open.assert_called_with('/sys/bus/pci/rescan', 'w') _file.write.assert_called_with('1')
def test_configure_ca(self): self.patch_target('run_update_certs') with utils.patch_open() as (mock_open, mock_file): self.target.configure_ca('myca') mock_open.assert_called_with( '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt', 'w') mock_file.write.assert_called_with('myca')
def test_pci_rescan(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.pci_rescan() _open.assert_called_with('/sys/bus/pci/rescan', 'w') _file.write.assert_called_with('1')
def test_configure_ca(self): self.patch_target('run_update_certs') with utils.patch_open() as (mock_open, mock_file): self.target.configure_ca('myca') mock_open.assert_called_with( '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt', 'w') mock_file.write.assert_called_with('myca')
def test_configure_source(self): self.config.return_value = 'testsource' self.patch_object(cpl.charms_openstack.charm.HAOpenStackCharm, 'configure_source') with patch_open() as (_open, _file): self.target.configure_source() _open.assert_called_with( "/etc/apt/sources.list.d/trilio-gemfury-sources.list", "w") _file.write.assert_called_once_with('testsource')
def test_configure_ca(self): self.patch_target('run_update_certs') self.patch_target('install_snap_certs') self.patch_object(chm.hookenv, 'service_name', return_value='svc1') with utils.patch_open() as (mock_open, mock_file): self.target.configure_ca('myca') mock_open.assert_called_with( '/usr/local/share/ca-certificates/svc1.crt', 'w') mock_file.write.assert_called_with('myca')
def test_get_sysnet_device_state(self): self.patch_object(pci.PCINetDevice, 'update_attributes') device = pci.PCINetDevice('0000:10:00.1') with utils.patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = mock.MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read state = device.get_sysnet_device_state('/sys/class/net/eth3') self.assertEqual(state, 'down')
def test_bind(self): self.patch_object(pci.PCINetDevice, 'pci_rescan') self.patch_object(pci.PCINetDevice, 'update_attributes') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.bind('enic') _open.assert_called_with('/sys/bus/pci/drivers/enic/bind', 'w') _file.write.assert_called_with('0000:07:00.0') self.pci_rescan.assert_called_with() self.update_attributes.assert_called_with()
def test_bind(self): self.patch_object(pci.PCINetDevice, 'pci_rescan') self.patch_object(pci.PCINetDevice, 'update_attributes') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.bind('enic') _open.assert_called_with('/sys/bus/pci/drivers/enic/bind', 'w') _file.write.assert_called_with('0000:07:00.0') self.pci_rescan.assert_called_with() self.update_attributes.assert_called_with()
def test_get_sysnet_device_state(self): self.patch_object(pci.PCINetDevice, 'update_attributes') device = pci.PCINetDevice('0000:10:00.1') with utils.patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = mock.MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read state = device.get_sysnet_device_state('/sys/class/net/eth3') self.assertEqual(state, 'down')
def test_configure_rabbit_cert(self): rabbit_int_mock = mock.MagicMock() rabbit_int_mock.get_ssl_cert.return_value = 'rabbit_cert' self.patch_object(chm.os.path, 'exists', return_value=True) self.patch_object(chm.os, 'mkdir') self.patch_object(chm.hookenv, 'service_name', return_value='svc1') with utils.patch_open() as (mock_open, mock_file): self.target.configure_rabbit_cert(rabbit_int_mock) mock_open.assert_called_with( '/var/lib/charm/svc1/rabbit-client-ca.pem', 'w') mock_file.write.assert_called_with('rabbit_cert')
def test_unbind(self): self.patch_object(pci.PCINetDevice, 'pci_rescan') self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci.PCINetDevice, 'loaded_kmod', new='igb_uio') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.unbind() _open.assert_called_with('/sys/bus/pci/drivers/igb_uio/unbind', 'w') _file.write.assert_called_with('0000:07:00.0') self.pci_rescan.assert_called_with() self.update_attributes.assert_called_with()
def test_configure_rabbit_cert(self): rabbit_int_mock = mock.MagicMock() rabbit_int_mock.get_ssl_cert.return_value = 'rabbit_cert' self.patch_object(chm.os.path, 'exists', return_value=True) self.patch_object(chm.os, 'mkdir') self.patch_object(chm.hookenv, 'service_name', return_value='svc1') with utils.patch_open() as (mock_open, mock_file): self.target.configure_rabbit_cert(rabbit_int_mock) mock_open.assert_called_with( '/var/lib/charm/svc1/rabbit-client-ca.pem', 'w') mock_file.write.assert_called_with('rabbit_cert')
def test_unbind(self): self.patch_object(pci.PCINetDevice, 'pci_rescan') self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci.PCINetDevice, 'loaded_kmod', new='igb_uio') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.unbind() _open.assert_called_with( '/sys/bus/pci/drivers/igb_uio/unbind', 'w') _file.write.assert_called_with('0000:07:00.0') self.pci_rescan.assert_called_with() self.update_attributes.assert_called_with()
def test_update_modalias_kmod(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') device = pci.PCINetDevice('0000:07:00.0') self.subprocess.check_output.side_effect = mocked_subprocess() with utils.patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = mock.MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read _file.readlines.side_effect = super_fh._getfilecontents_readlines device.update_modalias_kmod() self.assertEqual(device.modalias_kmod, 'enic')
def test_update_modalias_kmod(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') device = pci.PCINetDevice('0000:07:00.0') self.subprocess.check_output.side_effect = mocked_subprocess() with utils.patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = mock.MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read _file.readlines.side_effect = super_fh._getfilecontents_readlines device.update_modalias_kmod() self.assertEqual(device.modalias_kmod, 'enic')
def test_write_configuration_installed_and_running(self): self.patch('zaza.utilities.installers.user_directory', name='mock_user_directory') self.mock_user_directory.return_value = '/some/dir' self.patch_object(self.c, 'install', name='mock_c_install') self.patch_object(self.c, 'is_running', name='mock_c_is_running') self.mock_c_is_running.return_value = True self.patch_object(self.c, 'restart', name='mock_c_restart') mock_scp_fn = mock.Mock() self.c._scp_fn = mock_scp_fn mock_ssh_fn = mock.Mock() self.c._ssh_fn = mock_ssh_fn self.patch('yaml.dump', name='mock_yaml_dump') self.patch('tempfile.TemporaryDirectory', name='mock_TemporaryDirectory') mock_td = mock.MagicMock() mock_td.__enter__.return_value = '/target' self.mock_TemporaryDirectory.return_value = mock_td self.c._installed = True with tests_utils.patch_open() as (mock_open, mock_file): self.c.write_configuration() self.mock_c_install.assert_not_called() mock_open.assert_called_once_with('/target/config.yaml', 'wt') expected_config = { 'name': 'base', 'file-log-path': '/some/dir/conncheck.log', 'collection': 'a-collection', 'log-format': 'InfluxDB', 'listeners': [], 'speakers': [] } self.mock_yaml_dump.assert_called_once_with(expected_config, mock_file) mock_scp_fn.assert_called_once_with('/target/config.yaml', 'config.yaml') mock_ssh_fn.assert_called_once_with( ['sudo', 'mv', 'config.yaml', '/some/dir/config.yaml']) self.mock_c_is_running.assert_called_once_with() self.mock_c_restart.assert_called_once_with()
def test_install(self): mock_ttr = mock.Mock() self.patch('tempfile.TemporaryDirectory', name="mock_tt", return_value=mock_ttr) mock_ttr.__enter__ = mock.Mock(return_value='/some/tempdir/') mock_ttr.__exit__ = mock.Mock(return_value=False) with tests_utils.patch_open() as (mock_open, mock_file): self.sc.install() mock_open.assert_called_once_with('/some/tempdir/control', 'wt') file = installers.SystemdControl.SYSTEMD_FILE.format( name='aname', exec_start='aexec') mock_file.write.assert_called_once_with(file) self.mock_scp_fn.assert_called_once_with( '/some/tempdir/control', '/some/home/aname.service') self.mock_ssh_fn.assert_has_calls((mock.call([ 'sudo', 'mv', '/some/home/aname.service', '/etc/systemd/system/aname.service' ]), mock.call(['sudo', 'systemctl', 'daemon-reload']))) self.assertTrue(self.sc._installed)