def test_exists(self, ip_wrap, exists, mocket):
        socket_path = '/path/haproxy_stats.sock'
        mock_ns = ip_wrap.return_value
        mock_socket = mocket.return_value
        self.driver._get_state_file_path = mock.Mock(return_value=socket_path)
        mock_ns.netns.exists.return_value = False
        exists.return_value = False

        def reset():
            ip_wrap.reset_mock()
            self.driver._get_state_file_path.reset_mock()
            mock_ns.reset_mock()
            exists.reset_mock()
            mocket.reset_mock()
            mock_socket.reset_mock()

        ret_exists = self.driver.exists(self.lb.id)
        ip_wrap.assert_called_once_with()
        self.driver._get_state_file_path.assert_called_once_with(
            self.lb.id, 'haproxy_stats.sock', False)
        mock_ns.netns.exists.assert_called_once_with(
            namespace_driver.get_ns_name(self.lb.id))
        self.assertFalse(exists.called)
        self.assertFalse(mocket.called)
        self.assertFalse(mock_socket.connect.called)
        self.assertFalse(ret_exists)

        reset()
        mock_ns.netns.exists.return_value = True
        exists.return_value = False
        ret_exists = self.driver.exists(self.lb.id)
        ip_wrap.assert_called_once_with()
        self.driver._get_state_file_path.assert_called_once_with(
            self.lb.id, 'haproxy_stats.sock', False)
        mock_ns.netns.exists.assert_called_once_with(
            namespace_driver.get_ns_name(self.lb.id))
        exists.assert_called_once_with(socket_path)
        self.assertFalse(mocket.called)
        self.assertFalse(mock_socket.connect.called)
        self.assertFalse(ret_exists)

        reset()
        mock_ns.netns.exists.return_value = True
        exists.return_value = True
        ret_exists = self.driver.exists(self.lb.id)
        ip_wrap.assert_called_once_with()
        self.driver._get_state_file_path.assert_called_once_with(
            self.lb.id, 'haproxy_stats.sock', False)
        mock_ns.netns.exists.assert_called_once_with(
            namespace_driver.get_ns_name(self.lb.id))
        exists.assert_called_once_with(socket_path)
        mocket.assert_called_once_with(socket.AF_UNIX, socket.SOCK_STREAM)
        mock_socket.connect.assert_called_once_with(socket_path)
        self.assertTrue(ret_exists)
 def test_create(self):
     self.driver._plug = mock.Mock()
     self.driver._spawn = mock.Mock()
     self.driver.create(self.lb)
     self.driver._plug.assert_called_once_with(
         namespace_driver.get_ns_name(self.lb.id), self.lb.vip_port)
     self.driver._spawn.assert_called_once_with(self.lb)
    def test_undeploy_instance(self, mock_shutil, mock_isdir, mock_dirname,
                               mock_makedirs, mock_ip_wrap):
        self.driver._get_state_file_path = mock.Mock(return_value='/path')
        self.driver._unplug = mock.Mock()
        mock_dirname.return_value = '/path/' + self.lb.id
        mock_isdir.return_value = False
        self.driver.undeploy_instance(self.lb.id)
        calls = [mock.call(self.lb.id, 'pid'), mock.call(self.lb.id, '')]
        self.driver._get_state_file_path.has_calls(calls)
        self.assertFalse(self.driver._unplug.called)
        self.assertFalse(mock_ip_wrap.called)
        mock_isdir.assert_called_once_with('/path/' + self.lb.id)
        self.assertFalse(mock_shutil.called)

        self.driver.deployed_loadbalancers[self.lb.id] = self.lb
        mock_isdir.return_value = True
        mock_isdir.reset_mock()
        mock_ns = mock_ip_wrap.return_value
        mock_ns.get_devices.return_value = [
            collections.namedtuple('Device', ['name'])(name='test_device')
        ]
        self.driver.undeploy_instance(self.lb.id,
                                      cleanup_namespace=True,
                                      delete_namespace=True)
        ns = namespace_driver.get_ns_name(self.lb.id)
        calls = [mock.call(self.lb.id, 'pid'), mock.call(self.lb.id, '')]
        self.driver._get_state_file_path.has_calls(calls)
        self.driver._unplug.assert_called_once_with(ns, self.lb.vip_port)
        ip_wrap_calls = [mock.call(namespace=ns), mock.call(namespace=ns)]
        mock_ip_wrap.has_calls(ip_wrap_calls)
        mock_ns.get_devices.assert_called_once_with(exclude_loopback=True)
        self.vif_driver.unplug.assert_called_once_with('test_device',
                                                       namespace=ns)
        mock_shutil.assert_called_once_with('/path/' + self.lb.id)
        mock_ns.garbage_collect_namespace.assert_called_once_with()
    def test_undeploy_instance(self, mock_shutil, mock_isdir, mock_dirname, mock_ip_wrap):
        self.driver._get_state_file_path = mock.Mock(return_value="/path")
        namespace_driver.kill_pids_in_file = mock.Mock()
        self.driver._unplug = mock.Mock()
        mock_dirname.return_value = "/path/" + self.lb.id
        mock_isdir.return_value = False

        self.driver.undeploy_instance(self.lb.id)
        namespace_driver.kill_pids_in_file.assert_called_once_with("/path")
        calls = [mock.call(self.lb.id, "pid"), mock.call(self.lb.id, "")]
        self.driver._get_state_file_path.has_calls(calls)
        self.assertFalse(self.driver._unplug.called)
        self.assertFalse(mock_ip_wrap.called)
        mock_isdir.assert_called_once_with("/path/" + self.lb.id)
        self.assertFalse(mock_shutil.called)

        self.driver.deployed_loadbalancers[self.lb.id] = self.lb
        mock_isdir.return_value = True
        namespace_driver.kill_pids_in_file.reset_mock()
        mock_isdir.reset_mock()
        mock_ns = mock_ip_wrap.return_value
        mock_ns.get_devices.return_value = [collections.namedtuple("Device", ["name"])(name="test_device")]
        self.driver.undeploy_instance(self.lb.id, cleanup_namespace=True, delete_namespace=True)
        ns = namespace_driver.get_ns_name(self.lb.id)
        namespace_driver.kill_pids_in_file.assert_called_once_with("/path")
        calls = [mock.call(self.lb.id, "pid"), mock.call(self.lb.id, "")]
        self.driver._get_state_file_path.has_calls(calls)
        self.driver._unplug.assert_called_once_with(ns, self.lb.vip_port)
        ip_wrap_calls = [mock.call(namespace=ns), mock.call(namespace=ns)]
        mock_ip_wrap.has_calls(ip_wrap_calls)
        mock_ns.get_devices.assert_called_once_with(exclude_loopback=True)
        self.vif_driver.unplug.assert_called_once_with("test_device", namespace=ns)
        mock_shutil.assert_called_once_with("/path/" + self.lb.id)
        mock_ns.garbage_collect_namespace.assert_called_once_with()
 def test_create(self):
     self.driver._plug = mock.Mock()
     self.driver._spawn = mock.Mock()
     self.driver.create(self.lb)
     self.driver._plug.assert_called_once_with(
         namespace_driver.get_ns_name(self.lb.id), self.lb.vip_port)
     self.driver._spawn.assert_called_once_with(self.lb)
 def test_spawn(self, ip_wrap, jinja_save, ensure_dir):
     mock_ns = ip_wrap.return_value
     self.driver._spawn(self.lb)
     conf_dir = self.driver.state_path + "/" + self.lb.id + "/%s"
     jinja_save.assert_called_once_with(
         conf_dir % "haproxy.conf", self.lb, conf_dir % "haproxy_stats.sock", "test_group", conf_dir % ""
     )
     ip_wrap.assert_called_once_with(namespace=namespace_driver.get_ns_name(self.lb.id))
     mock_ns.netns.execute.assert_called_once_with(
         ["haproxy", "-f", conf_dir % "haproxy.conf", "-p", conf_dir % "haproxy.pid"]
     )
     self.assertIn(self.lb.id, self.driver.deployed_loadbalancers)
     self.assertEqual(self.lb, self.driver.deployed_loadbalancers[self.lb.id])
Exemplo n.º 7
0
 def test_spawn(self, ip_wrap, jinja_save, ensure_dir):
     mock_ns = ip_wrap.return_value
     self.driver._spawn(self.lb)
     conf_dir = self.driver.state_path + '/' + self.lb.id + '/%s'
     jinja_save.assert_called_once_with(conf_dir % 'haproxy.conf', self.lb,
                                        conf_dir % 'haproxy_stats.sock',
                                        'test_group', conf_dir % '')
     ip_wrap.assert_called_once_with(
         namespace=namespace_driver.get_ns_name(self.lb.id))
     mock_ns.netns.execute.assert_called_once_with([
         'haproxy', '-f', conf_dir % 'haproxy.conf', '-p',
         conf_dir % 'haproxy.pid'
     ])
     self.assertIn(self.lb.id, self.driver.deployed_loadbalancers)
     self.assertEqual(self.lb,
                      self.driver.deployed_loadbalancers[self.lb.id])
 def test_spawn(self, ip_wrap, jinja_save, ensure_dir):
     mock_ns = ip_wrap.return_value
     self.driver._spawn(self.lb)
     conf_dir = self.driver.state_path + '/' + self.lb.id + '/%s'
     jinja_save.assert_called_once_with(
         conf_dir % 'haproxy.conf',
         self.lb,
         conf_dir % 'haproxy_stats.sock',
         'test_group',
         conf_dir % '')
     ip_wrap.assert_called_once_with(
         namespace=namespace_driver.get_ns_name(self.lb.id))
     mock_ns.netns.execute.assert_called_once_with(
         ['haproxy', '-f', conf_dir % 'haproxy.conf', '-p',
          conf_dir % 'haproxy.pid'])
     self.assertIn(self.lb.id, self.driver.deployed_loadbalancers)
     self.assertEqual(self.lb,
                      self.driver.deployed_loadbalancers[self.lb.id])
 def test_get_ns_name(self):
     ns_name = namespace_driver.get_ns_name('woohoo')
     self.assertEqual(namespace_driver.NS_PREFIX + 'woohoo', ns_name)
 def test_get_ns_name(self):
     ns_name = namespace_driver.get_ns_name("woohoo")
     self.assertEqual(namespace_driver.NS_PREFIX + "woohoo", ns_name)