From: Iustin Pop <ius...@google.com>

Commit 8b2ec2f added unittests for KVM pinning, but it introduced a non-obvious
local dependency in the tests: the CPU_PINNING_OFF calls work by looking at the
(current) machine's core count, and pinning to all those CPUs. In order to make
this work independently from the test machine, we must also mock the result of
process.cpu_count(). Do this by using a core count that is very much unlikely
to ever be present in the real world.

Signed-off-by: Iustin Pop <ius...@google.com>
---
 test/py/ganeti.hypervisor.hv_kvm_unittest.py | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/test/py/ganeti.hypervisor.hv_kvm_unittest.py 
b/test/py/ganeti.hypervisor.hv_kvm_unittest.py
index c7a53b5..55ffb9b 100755
--- a/test/py/ganeti.hypervisor.hv_kvm_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_kvm_unittest.py
@@ -37,6 +37,7 @@ import socket
 import os
 import struct
 import re
+from contextlib import nested
 
 from ganeti import serializer
 from ganeti import constants
@@ -636,12 +637,13 @@ class TestKvmCpuPinning(testutils.GanetiTestCase):
     cpu_mask = self.params['cpu_mask']
     worker_cpu_mask = self.params['worker_cpu_mask']
     hypervisor = hv_kvm.KVMHypervisor()
-    with mock.patch('psutil.Process', return_value=mock_process):
+    with nested(mock.patch('psutil.Process', return_value=mock_process),
+                mock.patch('psutil.cpu_count', return_value=1237)):
       hypervisor._ExecuteCpuAffinity('test_instance', cpu_mask, 
worker_cpu_mask)
 
     self.assertEqual(mock_process.set_cpu_affinity.call_count, 1)
     self.assertEqual(mock_process.set_cpu_affinity.call_args_list[0],
-                     mock.call(range(0,12)))
+                     mock.call(range(0,1237)))
 
   def testCpuPinningPerVcpu(self):
     mock_process = mock.MagicMock()
@@ -659,11 +661,12 @@ class TestKvmCpuPinning(testutils.GanetiTestCase):
     def get_mock_process(unused_pid):
       return mock_process
 
-    with mock.patch('psutil.Process', get_mock_process):
+    with nested(mock.patch('psutil.Process', get_mock_process),
+                mock.patch('psutil.cpu_count', return_value=1237)):
       hypervisor._ExecuteCpuAffinity('test_instance', cpu_mask, 
worker_cpu_mask)
       self.assertEqual(mock_process.set_cpu_affinity.call_count, 7)
       self.assertEqual(mock_process.set_cpu_affinity.call_args_list[0],
-                       mock.call(range(0,12)))
+                       mock.call(range(0,1237)))
       self.assertEqual(mock_process.set_cpu_affinity.call_args_list[6],
                        mock.call([15, 16, 17]))
 
-- 
2.8.1

Reply via email to