From 027ad7eed50947608738ce0009fccf776936e55c Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Mon, 29 Feb 2016 19:33:28 -0400 Subject: [tests] adapt events tests to recent changes --- src/leap/common/tests/test_events.py | 198 ----------------------------------- 1 file changed, 198 deletions(-) delete mode 100644 src/leap/common/tests/test_events.py (limited to 'src/leap/common/tests') diff --git a/src/leap/common/tests/test_events.py b/src/leap/common/tests/test_events.py deleted file mode 100644 index 2ad097e..0000000 --- a/src/leap/common/tests/test_events.py +++ /dev/null @@ -1,198 +0,0 @@ -# -*- coding: utf-8 -*- -# test_events.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - -import os -import logging -import time - -from twisted.internet.reactor import callFromThread -from twisted.trial import unittest -from twisted.internet import defer - -from leap.common.events import server -from leap.common.events import client -from leap.common.events import flags -from leap.common.events import txclient -from leap.common.events import catalog -from leap.common.events.errors import CallbackAlreadyRegisteredError - - -if 'DEBUG' in os.environ: - logging.basicConfig(level=logging.DEBUG) - - -class EventsGenericClientTestCase(object): - - def setUp(self): - flags.set_events_enabled(True) - self._server = server.ensure_server( - emit_addr="tcp://127.0.0.1:0", - reg_addr="tcp://127.0.0.1:0") - self._client.configure_client( - emit_addr="tcp://127.0.0.1:%d" % self._server.pull_port, - reg_addr="tcp://127.0.0.1:%d" % self._server.pub_port) - - def tearDown(self): - self._client.shutdown() - self._server.shutdown() - flags.set_events_enabled(False) - # wait a bit for sockets to close properly - time.sleep(0.1) - - def test_client_register(self): - """ - Ensure clients can register callbacks. - """ - callbacks = self._client.instance().callbacks - self.assertTrue(len(callbacks) == 0, - 'There should be no callback for this event.') - # register one event - event1 = catalog.CLIENT_UID - - def cbk1(event, _): - return True - - uid1 = self._client.register(event1, cbk1) - # assert for correct registration - self.assertTrue(len(callbacks) == 1) - self.assertTrue(callbacks[event1][uid1] == cbk1, - 'Could not register event in local client.') - # register another event - event2 = catalog.CLIENT_SESSION_ID - - def cbk2(event, _): - return True - - uid2 = self._client.register(event2, cbk2) - # assert for correct registration - self.assertTrue(len(callbacks) == 2) - self.assertTrue(callbacks[event2][uid2] == cbk2, - 'Could not register event in local client.') - - def test_register_signal_replace(self): - """ - Make sure clients can replace already registered callbacks. - """ - event = catalog.CLIENT_UID - d = defer.Deferred() - - def cbk_fail(event, _): - return callFromThread(d.errback, event) - - def cbk_succeed(event, _): - return callFromThread(d.callback, event) - - self._client.register(event, cbk_fail, uid=1) - self._client.register(event, cbk_succeed, uid=1, replace=True) - self._client.emit(event, None) - return d - - def test_register_signal_replace_fails_when_replace_is_false(self): - """ - Make sure clients trying to replace already registered callbacks fail - when replace=False - """ - event = catalog.CLIENT_UID - self._client.register(event, lambda event, _: None, uid=1) - self.assertRaises( - CallbackAlreadyRegisteredError, - self._client.register, - event, lambda event, _: None, uid=1, replace=False) - - def test_register_more_than_one_callback_works(self): - """ - Make sure clients can replace already registered callbacks. - """ - event = catalog.CLIENT_UID - d1 = defer.Deferred() - - def cbk1(event, _): - return callFromThread(d1.callback, event) - - d2 = defer.Deferred() - - def cbk2(event, _): - return d2.callback(event) - - self._client.register(event, cbk1) - self._client.register(event, cbk2) - self._client.emit(event, None) - d = defer.gatherResults([d1, d2]) - return d - - def test_client_receives_signal(self): - """ - Ensure clients can receive signals. - """ - event = catalog.CLIENT_UID - d = defer.Deferred() - - def cbk(events, _): - callFromThread(d.callback, event) - - self._client.register(event, cbk) - self._client.emit(event, None) - return d - - def test_client_unregister_all(self): - """ - Test that the client can unregister all events for one signal. - """ - event1 = catalog.CLIENT_UID - d = defer.Deferred() - # register more than one callback for the same event - self._client.register( - event1, lambda ev, _: callFromThread(d.errback, None)) - self._client.register( - event1, lambda ev, _: callFromThread(d.errback, None)) - # unregister and emit the event - self._client.unregister(event1) - self._client.emit(event1, None) - # register and emit another event so the deferred can succeed - event2 = catalog.CLIENT_SESSION_ID - self._client.register( - event2, lambda ev, _: callFromThread(d.callback, None)) - self._client.emit(event2, None) - return d - - def test_client_unregister_by_uid(self): - """ - Test that the client can unregister an event by uid. - """ - event = catalog.CLIENT_UID - d = defer.Deferred() - # register one callback that would fail - uid = self._client.register( - event, lambda ev, _: callFromThread(d.errback, None)) - # register one callback that will succeed - self._client.register( - event, lambda ev, _: callFromThread(d.callback, None)) - # unregister by uid and emit the event - self._client.unregister(event, uid=uid) - self._client.emit(event, None) - return d - - -class EventsTxClientTestCase(EventsGenericClientTestCase, unittest.TestCase): - - _client = txclient - - -class EventsClientTestCase(EventsGenericClientTestCase, unittest.TestCase): - - _client = client -- cgit v1.2.3 From 7e90eed551bbe847201e5c62edcf0e6493ab2ec3 Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 6 Jul 2016 08:43:49 +0200 Subject: [test] toxify tests --- src/leap/common/tests/__init__.py | 0 src/leap/common/tests/test_certs.py | 99 ----------------------------------- src/leap/common/tests/test_check.py | 53 ------------------- src/leap/common/tests/test_http.py | 75 -------------------------- src/leap/common/tests/test_memoize.py | 76 --------------------------- 5 files changed, 303 deletions(-) delete mode 100644 src/leap/common/tests/__init__.py delete mode 100644 src/leap/common/tests/test_certs.py delete mode 100644 src/leap/common/tests/test_check.py delete mode 100644 src/leap/common/tests/test_http.py delete mode 100644 src/leap/common/tests/test_memoize.py (limited to 'src/leap/common/tests') diff --git a/src/leap/common/tests/__init__.py b/src/leap/common/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/leap/common/tests/test_certs.py b/src/leap/common/tests/test_certs.py deleted file mode 100644 index 8ebc0f4..0000000 --- a/src/leap/common/tests/test_certs.py +++ /dev/null @@ -1,99 +0,0 @@ -# -*- coding: utf-8 -*- -# test_certs.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -""" -Tests for: - * leap/common/certs.py -""" -import os -import time - -try: - import unittest2 as unittest -except ImportError: - import unittest - -from leap.common import certs -from leap.common.testing.basetest import BaseLeapTest - -TEST_CERT_PEM = os.path.join( - os.path.split(__file__)[0], - '..', 'testing', "leaptest_combined_keycert.pem") - -# Values from the test cert file: -# Not Before: Sep 3 17:52:16 2013 GMT -# Not After : Sep 1 17:52:16 2023 GMT -CERT_NOT_BEFORE = (2013, 9, 3, 17, 52, 16, 1, 246, 0) -CERT_NOT_AFTER = (2023, 9, 1, 17, 52, 16, 4, 244, 0) - - -class CertsTest(BaseLeapTest): - - def setUp(self): - self.setUpEnv() - - def tearDown(self): - self.tearDownEnv() - - def test_should_redownload_if_no_cert(self): - self.assertTrue(certs.should_redownload(certfile="")) - - def test_should_redownload_if_invalid_pem(self): - cert_path = self.get_tempfile('test_pem_file.pem') - - with open(cert_path, 'w') as f: - f.write('this is some invalid data for the pem file') - - self.assertTrue(certs.should_redownload(cert_path)) - - def test_should_redownload_if_before(self): - def new_now(): - time.struct_time(CERT_NOT_BEFORE) - self.assertTrue(certs.should_redownload(TEST_CERT_PEM, now=new_now)) - - def test_should_redownload_if_after(self): - def new_now(): - time.struct_time(CERT_NOT_AFTER) - self.assertTrue(certs.should_redownload(TEST_CERT_PEM, now=new_now)) - - def test_not_should_redownload(self): - self.assertFalse(certs.should_redownload(TEST_CERT_PEM)) - - def test_is_valid_pemfile(self): - with open(TEST_CERT_PEM) as f: - cert_data = f.read() - - self.assertTrue(certs.is_valid_pemfile(cert_data)) - - def test_not_is_valid_pemfile(self): - cert_data = 'this is some invalid data for the pem file' - - self.assertFalse(certs.is_valid_pemfile(cert_data)) - - def test_get_cert_time_boundaries(self): - """ - This test ensures us that the returned values are returned in UTC/GMT. - """ - with open(TEST_CERT_PEM) as f: - cert_data = f.read() - - valid_from, valid_to = certs.get_cert_time_boundaries(cert_data) - self.assertEqual(tuple(valid_from), CERT_NOT_BEFORE) - self.assertEqual(tuple(valid_to), CERT_NOT_AFTER) - - -if __name__ == "__main__": - unittest.main() diff --git a/src/leap/common/tests/test_check.py b/src/leap/common/tests/test_check.py deleted file mode 100644 index cd488ff..0000000 --- a/src/leap/common/tests/test_check.py +++ /dev/null @@ -1,53 +0,0 @@ -# -*- coding: utf-8 -*- -# test_check.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -""" -Tests for: - * leap/common/check.py -""" -try: - import unittest2 as unittest -except ImportError: - import unittest - -import mock - -from leap.common import check - - -class CheckTests(unittest.TestCase): - def test_raises_on_false_condition(self): - with self.assertRaises(AssertionError): - check.leap_assert(False, "Condition") - - def test_raises_on_none_condition(self): - with self.assertRaises(AssertionError): - check.leap_assert(None, "Condition") - - def test_suceeds_with_good_condition(self): - check.leap_assert(True, "") - - def test_raises_on_bad_type(self): - with self.assertRaises(AssertionError): - check.leap_assert_type(42, str) - - def test_succeeds_on_good_type(self): - check.leap_assert_type(42, int) - - @mock.patch("traceback.extract_stack", mock.MagicMock(return_value=None)) - def test_does_not_raise_on_bug(self): - with self.assertRaises(AssertionError): - check.leap_assert(False, "") diff --git a/src/leap/common/tests/test_http.py b/src/leap/common/tests/test_http.py deleted file mode 100644 index f44550f..0000000 --- a/src/leap/common/tests/test_http.py +++ /dev/null @@ -1,75 +0,0 @@ -# -*- coding: utf-8 -*- -# test_http.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -""" -Tests for: - * leap/common/http.py -""" -import os -try: - import unittest2 as unittest -except ImportError: - import unittest - -from leap.common import http -from leap.common.testing.basetest import BaseLeapTest - -TEST_CERT_PEM = os.path.join( - os.path.split(__file__)[0], - '..', 'testing', "leaptest_combined_keycert.pem") - - -class HTTPClientTest(BaseLeapTest): - - def setUp(self): - pass - - def tearDown(self): - pass - - def test_agents_sharing_pool_by_default(self): - client = http.HTTPClient() - client2 = http.HTTPClient(TEST_CERT_PEM) - self.assertNotEquals( - client._agent, client2._agent, "Expected dedicated agents") - self.assertEquals( - client._agent._pool, client2._agent._pool, - "Pool was not reused by default") - - def test_agent_can_have_dedicated_custom_pool(self): - custom_pool = http._HTTPConnectionPool( - None, - timeout=10, - maxPersistentPerHost=42, - persistent=False - ) - self.assertEquals(custom_pool.maxPersistentPerHost, 42, - "Custom persistent connections " - "limit is not being respected") - self.assertFalse(custom_pool.persistent, - "Custom persistence is not being respected") - default_client = http.HTTPClient() - custom_client = http.HTTPClient(pool=custom_pool) - - self.assertNotEquals( - default_client._agent, custom_client._agent, - "No agent reuse is expected") - self.assertEquals( - custom_pool, custom_client._agent._pool, - "Custom pool usage was not respected") - -if __name__ == "__main__": - unittest.main() diff --git a/src/leap/common/tests/test_memoize.py b/src/leap/common/tests/test_memoize.py deleted file mode 100644 index c923fc5..0000000 --- a/src/leap/common/tests/test_memoize.py +++ /dev/null @@ -1,76 +0,0 @@ -# -*- coding: utf-8 -*- -# test_check.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -""" -Tests for: - * leap/common/decorators._memoized -""" -try: - import unittest2 as unittest -except ImportError: - import unittest - -from time import sleep - -import mock - -from leap.common.decorators import _memoized - - -class MemoizeTests(unittest.TestCase): - - def test_memoized_call(self): - """ - Test that a memoized function only calls once. - """ - witness = mock.Mock() - - @_memoized - def callmebaby(): - return witness() - - for i in range(10): - callmebaby() - witness.assert_called_once_with() - - def test_cache_invalidation(self): - """ - Test that time makes the cache invalidation expire. - """ - witness = mock.Mock() - - cache_with_alzheimer = _memoized - cache_with_alzheimer.CACHE_INVALIDATION_DELTA = 1 - - @cache_with_alzheimer - def callmebaby(*args): - return witness(*args) - - for i in range(10): - callmebaby() - witness.assert_called_once_with() - - sleep(2) - callmebaby("onemoretime") - - expected = [mock.call(), mock.call("onemoretime")] - self.assertEqual( - witness.call_args_list, - expected) - - -if __name__ == "__main__": - unittest.main() -- cgit v1.2.3