1010# License for the specific language governing permissions and limitations
1111# under the License.
1212
13- from unittest import mock
14-
1513import fixtures
1614
15+ from unittest import mock
16+
1717from nova import context as nova_context
1818from nova import exception
1919from nova import objects
2020from nova .tests import fixtures as nova_fixtures
2121from nova .tests .fixtures import libvirt as fakelibvirt
22+ from nova .tests .functional .api import client
2223from nova .tests .functional .libvirt import base
2324from nova .virt import hardware
2425from nova .virt .libvirt .cpu import api as cpu_api
@@ -240,7 +241,6 @@ def setUp(self):
240241 cpu_cores = 5 , cpu_threads = 2 )
241242 self .compute1 = self .start_compute (host_info = self .host_info ,
242243 hostname = 'compute1' )
243-
244244 # All cores are shutdown at startup, let's check.
245245 cpu_dedicated_set = hardware .get_cpu_dedicated_set ()
246246 self ._assert_cpu_set_state (cpu_dedicated_set , expected = 'offline' )
@@ -266,6 +266,33 @@ def test_create_server(self):
266266 cpu_dedicated_set = hardware .get_cpu_dedicated_set ()
267267 unused_cpus = cpu_dedicated_set - instance_pcpus
268268 self ._assert_cpu_set_state (unused_cpus , expected = 'offline' )
269+ return server
270+
271+ def test_delete_server (self ):
272+ server = self .test_create_server ()
273+ self ._delete_server (server )
274+ # Let's verify that the pinned CPUs are now offline
275+ cpu_dedicated_set = hardware .get_cpu_dedicated_set ()
276+ self ._assert_cpu_set_state (cpu_dedicated_set , expected = 'offline' )
277+
278+ def test_delete_server_device_busy (self ):
279+ server = self .test_create_server ()
280+
281+ inst = objects .Instance .get_by_uuid (self .ctxt , server ['id' ])
282+ instance_pcpus = inst .numa_topology .cpu_pinning
283+ self ._assert_cpu_set_state (instance_pcpus , expected = 'online' )
284+ with mock .patch (
285+ 'nova.filesystem.write_sys' ,
286+ side_effect = exception .FileNotFound (file_path = 'fake' )):
287+ # This is bug 2065927
288+ self .assertRaises (
289+ client .OpenStackApiException , self ._delete_server , server )
290+ cpu_dedicated_set = hardware .get_cpu_dedicated_set ()
291+ # Verify that the unused CPUs are still offline
292+ unused_cpus = cpu_dedicated_set - instance_pcpus
293+ self ._assert_cpu_set_state (unused_cpus , expected = 'offline' )
294+ # but the instance cpus are still online
295+ self ._assert_cpu_set_state (instance_pcpus , expected = 'online' )
269296
270297 def test_create_server_with_emulator_threads_isolate (self ):
271298 server = self ._create_server (
0 commit comments