| 1 |
# All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from neutron_lib.callbacks import events |
| 18 |
from neutron_lib.callbacks import resources |
| 19 |
from neutron_lib import fixture |
| 20 |
from neutron_lib import worker |
| 21 |
|
| 22 |
from neutron_lib.tests import _base as base |
| 23 |
|
| 24 |
|
| 25 |
class _BaseWorker(worker.BaseWorker): |
| 26 |
|
| 27 |
def reset(self): |
| 28 |
pass |
| 29 |
|
| 30 |
def stop(self): |
| 31 |
pass |
| 32 |
|
| 33 |
def wait(self): |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
# Same as _BaseWorker, but looks like a process launch instead of eventlet |
| 38 |
class _ProcWorker(_BaseWorker): |
| 39 |
|
| 40 |
def __init__(self, worker_process_count=1, set_proctitle='on'): |
| 41 |
super().__init__(worker_process_count, set_proctitle) |
| 42 |
self._my_pid = -1 # make it appear to be a separate process |
| 43 |
|
| 44 |
|
| 45 |
class TestBaseWorker(base.BaseTestCase): |
| 46 |
|
| 47 |
def setUp(self): |
| 48 |
super().setUp() |
| 49 |
self._reg = mock.Mock() |
| 50 |
self.useFixture(fixture.CallbackRegistryFixture( |
| 51 |
callback_manager=self._reg)) |
| 52 |
|
| 53 |
def test_worker_process_count(self): |
| 54 |
self.assertEqual(9, _BaseWorker( |
| 55 |
worker_process_count=9).worker_process_count) |
| 56 |
|
| 57 |
def test_start_callback_event(self): |
| 58 |
base_worker = _BaseWorker() |
| 59 |
base_worker.start() |
| 60 |
self._reg.publish.assert_called_once_with( |
| 61 |
resources.PROCESS, events.AFTER_INIT, base_worker.start, |
| 62 |
payload=mock.ANY) |
| 63 |
|
| 64 |
# Forked workers, should call setproctitle |
| 65 |
|
| 66 |
def test_proctitle_default(self): |
| 67 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 68 |
_ProcWorker().start() |
| 69 |
self.assertRegex(spt.call_args[0][0], |
| 70 |
'^neutron-server: _ProcWorker \\(.*python.*\\)$') |
| 71 |
|
| 72 |
def test_proctitle_custom_desc(self): |
| 73 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 74 |
_ProcWorker().start(desc="fancy title") |
| 75 |
self.assertRegex(spt.call_args[0][0], |
| 76 |
'^neutron-server: fancy title \\(.*python.*\\)$') |
| 77 |
|
| 78 |
def test_proctitle_custom_name(self): |
| 79 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 80 |
_ProcWorker().start(name="tardis") |
| 81 |
self.assertRegex(spt.call_args[0][0], |
| 82 |
'^tardis: _ProcWorker \\(.*python.*\\)$') |
| 83 |
|
| 84 |
def test_proctitle_empty(self): |
| 85 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 86 |
_ProcWorker().start(desc="") |
| 87 |
self.assertRegex(spt.call_args[0][0], |
| 88 |
'^neutron-server: _ProcWorker \\(.*python.*\\)$') |
| 89 |
|
| 90 |
def test_proctitle_nonstring(self): |
| 91 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 92 |
_ProcWorker().start(desc=2) |
| 93 |
self.assertRegex(spt.call_args[0][0], |
| 94 |
'^neutron-server: 2 \\(.*python.*\\)$') |
| 95 |
|
| 96 |
def test_proctitle_both_empty(self): |
| 97 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 98 |
_ProcWorker().start(name="", desc="") |
| 99 |
self.assertRegex(spt.call_args[0][0], |
| 100 |
'^: _ProcWorker \\(.*python.*\\)$') |
| 101 |
|
| 102 |
def test_proctitle_name_none(self): |
| 103 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 104 |
_ProcWorker().start(name=None) |
| 105 |
self.assertRegex(spt.call_args[0][0], |
| 106 |
'^None: _ProcWorker \\(.*python.*\\)$') |
| 107 |
|
| 108 |
# Forked, but proctitle disabled |
| 109 |
|
| 110 |
def test_proctitle_off(self): |
| 111 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 112 |
_ProcWorker(set_proctitle='off').start() |
| 113 |
self.assertIsNone(spt.call_args) |
| 114 |
|
| 115 |
# Eventlet style worker, should never call setproctitle |
| 116 |
|
| 117 |
def test_proctitle_same_process(self): |
| 118 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 119 |
_BaseWorker().start() |
| 120 |
self.assertIsNone(spt.call_args) |
| 121 |
|
| 122 |
def test_setproctitle_on(self): |
| 123 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 124 |
_ProcWorker(set_proctitle='on').start(name="foo", desc="bar") |
| 125 |
self.assertRegex(spt.call_args[0][0], |
| 126 |
'^foo: bar \\(.*python.*\\)$') |
| 127 |
|
| 128 |
def test_setproctitle_off(self): |
| 129 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 130 |
_ProcWorker(set_proctitle='off').start(name="foo", desc="bar") |
| 131 |
self.assertIsNone(spt.call_args) |
| 132 |
|
| 133 |
def test_setproctitle_brief(self): |
| 134 |
with mock.patch('setproctitle.setproctitle') as spt: |
| 135 |
_ProcWorker(set_proctitle='brief').start(name="foo", desc="bar") |
| 136 |
self.assertEqual(spt.call_args[0][0], 'foo: bar') |