class TestTwistedZmqAuthenticator_stop(TestCase): """ This class contains all methods to unit test TwistedZmqAuthenticator.stop() """ def setUp(self): with patch('voltha.adapters.adtran_olt.net.adtran_zmq.structlog.get_logger'), \ patch('voltha.adapters.adtran_olt.net.adtran_zmq.zmq_factory'): # Create TwistedZmqAuthenticator instance for test self.twisted_zmq_authenticator = TwistedZmqAuthenticator() # Test stop() for successful execution where pipe exists and needs to be closed properly def test_stop_pipe_exists(self, mk_defer, mk_succeed): # Create mocks and save a reference for later because source code clears the pipe/thread attributes self.mk_pipe = self.twisted_zmq_authenticator.pipe = MagicMock() self.mk_thread = self.twisted_zmq_authenticator.thread = MagicMock() self.twisted_zmq_authenticator.thread.is_alive.return_value = True _ = self.twisted_zmq_authenticator.stop() self.mk_pipe.send.assert_called_once_with(b'TERMINATE') self.mk_pipe.shutdown.assert_called_once_with() self.mk_thread.is_alive.assert_called_once_with() mk_defer.assert_called_once_with( TwistedZmqAuthenticator._do_thread_join, self.mk_thread) # Test stop() for successful execution where pipe doesn't exist def test_stop_pipe_doesnt_exist(self, mk_defer, mk_succeed): self.twisted_zmq_authenticator.pipe = None _ = self.twisted_zmq_authenticator.stop() self.assertEqual(self.twisted_zmq_authenticator.pipe, None) self.assertEqual(self.twisted_zmq_authenticator.thread, None)
def test_do_thread_start_success(self, mk_sys): mk_sys.version_info = (2, 7) mk_thread = MagicMock() mk_thread.started.wait.return_value = True TwistedZmqAuthenticator._do_thread_start(mk_thread, 20) mk_thread.start.assert_called_once_with() mk_thread.started.wait.assert_called_once_with(timeout=20)
class TestTwistedZmqAuthenticator_configure_curve(TestCase): """ This class contains all methods to unit test TwistedZmqAuthenticator.configure_curve() """ def setUp(self): with patch('voltha.adapters.adtran_olt.net.adtran_zmq.structlog.get_logger'), \ patch('voltha.adapters.adtran_olt.net.adtran_zmq.zmq_factory'): # Create TwistedZmqAuthenticator instance for test self.twisted_zmq_authenticator = TwistedZmqAuthenticator() self.twisted_zmq_authenticator.pipe = MagicMock() # Test configure_curve() for successful curve security configuration def test_configure_curve_success(self): self.twisted_zmq_authenticator.configure_curve('x', 'anywhere') self.twisted_zmq_authenticator.pipe.send.assert_called_once_with( [b'CURVE', b'x', b'anywhere']) # Test configure_curve() for failed call to send() with TypeError return def test_configure_curve_failure(self): self.twisted_zmq_authenticator.pipe.send.side_effect = TypeError self.twisted_zmq_authenticator.configure_curve() self.twisted_zmq_authenticator.log.exception.assert_called_once() def tearDown(self): self.twisted_zmq_authenticator.pipe = None
def test_do_thread_start_failure(self, mk_sys): mk_sys.version_info = (2, 7) mk_thread = MagicMock() mk_thread.started.wait.return_value = False with self.assertRaises(RuntimeError): TwistedZmqAuthenticator._do_thread_start(mk_thread) mk_thread.start.assert_called_once_with() mk_thread.started.wait.assert_called_once_with(timeout=10)
class TestTwistedZmqAuthenticator_configure_plain(TestCase): """ This class contains all methods to unit test TwistedZmqAuthenticator.configure_plain() """ def setUp(self): with patch('voltha.adapters.adtran_olt.net.adtran_zmq.structlog.get_logger'), \ patch('voltha.adapters.adtran_olt.net.adtran_zmq.zmq_factory'): # Create TwistedZmqAuthenticator instance for test self.twisted_zmq_authenticator = TwistedZmqAuthenticator() self.twisted_zmq_authenticator.pipe = MagicMock() # Test configure_plain() for successful plain security configuration with basic password def test_configure_plain_success_with_pswd(self, mk_dumps): mk_dumps.return_value = '{"passwords": ["topsecret"]}' self.twisted_zmq_authenticator.configure_plain( passwords={'passwords': ['topsecret']}) self.twisted_zmq_authenticator.pipe.send.assert_called_once_with( [b'PLAIN', b'*', b'{"passwords": ["topsecret"]}']) # Test configure_plain() for successful plain security configuration with no password def test_configure_plain_success_without_pswd(self, mk_dumps): mk_dumps.return_value = '{}' # 'passwords' parameter defaults to None self.twisted_zmq_authenticator.configure_plain() self.twisted_zmq_authenticator.pipe.send.assert_called_once_with( [b'PLAIN', b'*', b'{}']) # Test configure_plain() for failed call to send() with TypeError return def test_configure_plain_failure(self, mk_dumps): self.twisted_zmq_authenticator.pipe.send.side_effect = TypeError self.twisted_zmq_authenticator.configure_plain() self.twisted_zmq_authenticator.log.exception.assert_called_once() def tearDown(self): self.twisted_zmq_authenticator.pipe = None
class TestTwistedZmqAuthenticator_is_alive(TestCase): """ This class contains all methods to unit test TwistedZmqAuthenticator.is_alive() """ def setUp(self): with patch('voltha.adapters.adtran_olt.net.adtran_zmq.structlog.get_logger'), \ patch('voltha.adapters.adtran_olt.net.adtran_zmq.zmq_factory'): # Create TwistedZmqAuthenticator instance for test self.twisted_zmq_authenticator = TwistedZmqAuthenticator() # Test is_alive() to return True def test_is_alive_true(self): self.twisted_zmq_authenticator.thread = MagicMock() self.twisted_zmq_authenticator.thread.is_alive.return_value = True response = self.twisted_zmq_authenticator.is_alive() self.assertTrue(response) # Test is_alive() to return False def test_is_alive_false(self): self.twisted_zmq_authenticator.thread = MagicMock() self.twisted_zmq_authenticator.thread.is_alive.return_value = False response = self.twisted_zmq_authenticator.is_alive() self.assertFalse(response)
class TestTwistedZmqAuthenticator_start(TestCase): """ This class contains all methods to unit test TwistedZmqAuthenticator.start() """ def setUp(self): with patch('voltha.adapters.adtran_olt.net.adtran_zmq.structlog.get_logger'), \ patch('voltha.adapters.adtran_olt.net.adtran_zmq.zmq_factory'): # Create TwistedZmqAuthenticator instance for test self.twisted_zmq_authenticator = TwistedZmqAuthenticator() # Test start() for successful execution def test_start_success(self, mk_zmq_pair_conn, mk_local_auth_thread, mk_defer): _ = self.twisted_zmq_authenticator.start() self.assertEqual(self.twisted_zmq_authenticator.pipe.onReceive, AdtranZmqClient.rx_nop) # Test start() for failure due to artificial exception def test_start_failure(self, mk_zmq_pair_conn, mk_local_auth_thread, mk_defer): mk_defer.side_effect = TypeError self.twisted_zmq_authenticator.start() self.twisted_zmq_authenticator.log.exception.assert_called_once()
class TestTwistedZmqAuthenticator_deny(TestCase): """ This class contains all methods to unit test TwistedZmqAuthenticator.deny() """ def setUp(self): with patch('voltha.adapters.adtran_olt.net.adtran_zmq.structlog.get_logger'), \ patch('voltha.adapters.adtran_olt.net.adtran_zmq.zmq_factory'): # Create TwistedZmqAuthenticator instance for test self.twisted_zmq_authenticator = TwistedZmqAuthenticator() self.twisted_zmq_authenticator.pipe = MagicMock() # Test deny() for successfully sending a DENY message with no IP addresses specified def test_deny_success_no_ip(self): self.twisted_zmq_authenticator.deny() self.twisted_zmq_authenticator.pipe.send.assert_called_once_with( [b'DENY']) # Test deny() for successfully sending a DENY message to deny one IP address def test_deny_success_one_ip(self): self.twisted_zmq_authenticator.deny('1.2.3.4') self.twisted_zmq_authenticator.pipe.send.assert_called_once_with( [b'DENY', b'1.2.3.4']) # Test deny() for successfully sending a DENY message to deny multiple IP addresses def test_deny_success_mult_ips(self): self.twisted_zmq_authenticator.deny('1.2.3.4', '5.6.7.8') self.twisted_zmq_authenticator.pipe.send.assert_called_once_with( [b'DENY', b'1.2.3.4', b'5.6.7.8']) # Test deny() for sending a DENY message that results in an exception def test_deny_failure(self): self.twisted_zmq_authenticator.deny(1234, 5678) self.twisted_zmq_authenticator.pipe.send.assert_not_called() def tearDown(self): self.twisted_zmq_authenticator.pipe = None
def setUp(self): with patch('voltha.adapters.adtran_olt.net.adtran_zmq.structlog.get_logger'), \ patch('voltha.adapters.adtran_olt.net.adtran_zmq.zmq_factory'): # Create TwistedZmqAuthenticator instance for test self.twisted_zmq_authenticator = TwistedZmqAuthenticator()
def test_do_thread_join_timeout_10(self): mk_thread = MagicMock() TwistedZmqAuthenticator._do_thread_join(mk_thread, 10) mk_thread.join.assert_called_once_with(10)
def test_do_thread_start_success_v26(self, mk_sys): mk_sys.version_info = (2, 6) mk_thread = MagicMock() TwistedZmqAuthenticator._do_thread_start(mk_thread) mk_thread.start.assert_called_once_with() mk_thread.started.wait.assert_called_once_with(timeout=10)