def setUp(self): config = Config('test', 'hyperv', server='localhost', username='******', password='******', owner='owner', env='env') self.hyperv = HyperV(self.logger, config)
class TestHyperV(TestBase): def setUp(self): config = Config('test', 'hyperv', server='localhost', username='******', password='******', owner='owner', env='env') self.hyperv = HyperV(self.logger, config) def run_once(self, queue=None): ''' Run Hyper-V in oneshot mode ''' self.hyperv._oneshot = True self.hyperv._queue = queue or Queue() self.hyperv._terminate_event = Event() self.hyperv._interval = 0 self.hyperv._run() @patch('requests.Session') def test_connect(self, session): session.return_value.post.side_effect = HyperVMock.post self.run_once() session.assert_called_with() session.return_value.post.assert_called_with( 'http://*****:*****@patch('requests.Session') def test_connection_refused(self, session): session.return_value.post.side_effect = requests.ConnectionError self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_invalid_login(self, session): session.return_value.post.return_value.status_code = 401 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_404(self, session): session.return_value.post.return_value.text = '' session.return_value.post.return_value.status_code = 404 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_500(self, session): session.return_value.post.return_value.text = '' session.return_value.post.return_value.status_code = 500 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_wrong_namespace(self, session): session.return_value.post.return_value.text = ''' <s:Envelope xml:lang="en-US" xmlns:s="http://www.w3.org/2003/05/soap-envelope"> <s:Body> <s:Fault> <s:Detail> <p:MSFT_WmiError xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/MSFT_WmiError"> <p:error_Code>2150858778</p:error_Code> </p:MSFT_WmiError> </s:Detail> </s:Fault> </s:Body> </s:Envelope>''' session.return_value.post.return_value.status_code = 500 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_fault(self, session): session.return_value.post.return_value.text = ''' <s:Envelope xml:lang="en-US" xmlns:s="http://www.w3.org/2003/05/soap-envelope"></s:Envelope>''' session.return_value.post.return_value.status_code = 500 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_getHostGuestMapping(self, session): expected_hostname = 'hostname.domainname' expected_hypervisorId = '12345678-90AB-CDEF-1234-567890ABCDEF' expected_guestId = '12345678-90AB-CDEF-1234-567890ABCDEF' expected_guest_state = Guest.STATE_UNKNOWN session.return_value.post.side_effect = HyperVMock.post expected_result = Hypervisor(hypervisorId=expected_hypervisorId, name=expected_hostname, guestIds=[ Guest( expected_guestId, self.hyperv, expected_guest_state, ) ], facts={ Hypervisor.CPU_SOCKET_FACT: '1', Hypervisor.HYPERVISOR_TYPE_FACT: 'hyperv', Hypervisor.HYPERVISOR_VERSION_FACT: '0.1.2345.67890', }) result = self.hyperv.getHostGuestMapping()['hypervisors'][0] assert expected_result.toDict() == result.toDict() def test_proxy(self): proxy = Proxy() self.addCleanup(proxy.terminate) proxy.start() oldenv = os.environ.copy() self.addCleanup(lambda: setattr(os, 'environ', oldenv)) os.environ['http_proxy'] = proxy.address self.assertRaises(VirtError, self.run_once) self.assertIsNotNone(proxy.last_path, "Proxy was not called") self.assertEqual(proxy.last_path, 'http://localhost:5985/wsman')
class TestHyperV(TestBase): def setUp(self): config = Config('test', 'hyperv', server='localhost', username='******', password='******', owner='owner', env='env') self.hyperv = HyperV(self.logger, config) def run_once(self, queue=None): ''' Run Hyper-V in oneshot mode ''' self.hyperv._oneshot = True self.hyperv._queue = queue or Queue() self.hyperv._terminate_event = Event() self.hyperv._interval = 0 self.hyperv._run() @patch('requests.Session') def test_connect(self, session): session.return_value.post.side_effect = HyperVMock.post self.run_once() session.assert_called_with() session.return_value.post.assert_called_with('http://*****:*****@patch('requests.Session') def test_connection_refused(self, session): session.return_value.post.side_effect = requests.ConnectionError self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_invalid_login(self, session): session.return_value.post.return_value.status_code = 401 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_404(self, session): session.return_value.post.return_value.text = '' session.return_value.post.return_value.status_code = 404 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_500(self, session): session.return_value.post.return_value.text = '' session.return_value.post.return_value.status_code = 500 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_wrong_namespace(self, session): session.return_value.post.return_value.text = ''' <s:Envelope xml:lang="en-US" xmlns:s="http://www.w3.org/2003/05/soap-envelope"> <s:Body> <s:Fault> <s:Detail> <p:MSFT_WmiError xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/MSFT_WmiError"> <p:error_Code>2150858778</p:error_Code> </p:MSFT_WmiError> </s:Detail> </s:Fault> </s:Body> </s:Envelope>''' session.return_value.post.return_value.status_code = 500 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_fault(self, session): session.return_value.post.return_value.text = ''' <s:Envelope xml:lang="en-US" xmlns:s="http://www.w3.org/2003/05/soap-envelope"></s:Envelope>''' session.return_value.post.return_value.status_code = 500 self.assertRaises(VirtError, self.run_once) @patch('requests.Session') def test_getHostGuestMapping(self, session): expected_hostname = 'hostname.domainname' expected_hypervisorId = '12345678-90AB-CDEF-1234-567890ABCDEF' expected_guestId = '12345678-90AB-CDEF-1234-567890ABCDEF' expected_guest_state = Guest.STATE_UNKNOWN session.return_value.post.side_effect = HyperVMock.post expected_result = Hypervisor( hypervisorId=expected_hypervisorId, name=expected_hostname, guestIds=[ Guest( expected_guestId, self.hyperv, expected_guest_state, ) ], facts={ Hypervisor.CPU_SOCKET_FACT: '1', Hypervisor.HYPERVISOR_TYPE_FACT: 'hyperv', Hypervisor.HYPERVISOR_VERSION_FACT: '0.1.2345.67890', } ) result = self.hyperv.getHostGuestMapping()['hypervisors'][0] assert expected_result.toDict() == result.toDict() def test_proxy(self): proxy = Proxy() self.addCleanup(proxy.terminate) proxy.start() oldenv = os.environ.copy() self.addCleanup(lambda: setattr(os, 'environ', oldenv)) os.environ['http_proxy'] = proxy.address self.assertRaises(VirtError, self.run_once) self.assertIsNotNone(proxy.last_path, "Proxy was not called") self.assertEqual(proxy.last_path, 'http://localhost:5985/wsman')