예제 #1
0
class TestHttpProxy(unittest.TestCase):
    def setUp(self):
        self._proxy_pid = start_proxy(options=_OPTIONS, log_level='error',
                                      log_output='/dev/null',
                                      protocol='http')
        self._web = start_simplehttp_server()
        time.sleep(.3)
        try:
            if self._web.poll():
                raise ValueError("Could not start the proxy")

            self.client = Client()

            assert self.client.get_behavior() == 'dummy'
        except Exception:
            self.tearDown()
            raise

    def tearDown(self):
        stop_proxy(self._proxy_pid)
        self._web.terminate()

    def test_proxy(self):
        # let's do a few simple request first to make sure the proxy works
        self.assertEqual(self.client.get_behavior(), 'dummy')
        times = []

        for i in range(10):
            start = time.time()
            try:
                res = requests.get(_PROXY)
            finally:
                times.append(time.time() - start)

            self.assertEqual(res.status_code, 200)

        fastest = min(times)

        # now let's try the various behaviors
        with self.client.with_behavior('blackout'):
            # oh look we broke it
            self.assertRaises(requests.ConnectionError, requests.get, _PROXY)
            self.assertEqual(self.client.get_behavior(), 'blackout')

        with self.client.with_behavior('delay'):
            # should work but be slower
            start = time.time()
            try:
                res = requests.get(_PROXY)
            finally:
                duration = time.time() - start

            self.assertEqual(res.status_code, 200)
            self.assertTrue(duration > fastest + 1)

        # we should be back to normal
        self.assertEqual(self.client.get_behavior(), 'dummy')
        res = requests.get(_PROXY)
        self.assertEqual(res.status_code, 200)
예제 #2
0
class TestHttpProxy(unittest.TestCase):
    def setUp(self):
        self._proxy_pid = start_proxy(options=_OPTIONS,
                                      log_level='error',
                                      log_output='/dev/null',
                                      protocol='http')
        self._web = start_simplehttp_server()
        time.sleep(.3)
        try:
            if self._web.poll():
                raise ValueError("Could not start the proxy")

            self.client = Client()

            assert self.client.get_behavior() == 'dummy'
        except Exception:
            self.tearDown()
            raise

    def tearDown(self):
        stop_proxy(self._proxy_pid)
        self._web.terminate()

    def test_proxy(self):
        # let's do a few simple request first to make sure the proxy works
        self.assertEqual(self.client.get_behavior(), 'dummy')
        times = []
        for i in range(10):
            start = time.time()
            try:
                res = requests.get(_PROXY)
            finally:
                times.append(time.time() - start)

            self.assertEqual(res.status_code, 200)

        fastest = min(times)

        # now let's try the various behaviors
        with self.client.with_behavior('blackout'):
            # oh look we broke it
            self.assertRaises(requests.ConnectionError, requests.get, _PROXY)
            self.assertEqual(self.client.get_behavior(), 'blackout')

        with self.client.with_behavior('delay'):
            # should work but be slower
            start = time.time()
            try:
                res = requests.get(_PROXY)
            finally:
                duration = time.time() - start

            self.assertEqual(res.status_code, 200)
            self.assertTrue(duration > fastest + 1)

        # we should be back to normal
        self.assertEqual(self.client.get_behavior(), 'dummy')
        res = requests.get(_PROXY)
        self.assertEqual(res.status_code, 200)
예제 #3
0
class TestSimpleProxy(unittest.TestCase):
    def setUp(self):
        self._proxy_pid = start_proxy(log_output='/dev/null',
                                      log_level='error')
        self._web = start_simplehttp_server()
        time.sleep(.2)
        try:
            if self._web.poll():
                raise ValueError("Could not start the proxy")

            self.client = Client()

            assert self.client.get_behavior() == 'dummy'
        except Exception:
            self.tearDown()
            raise

    def tearDown(self):
        stop_proxy(self._proxy_pid)
        self._web.terminate()

    def test_existing_behaviors(self):
        wanted = [
            'blackout', 'delay', 'dummy', 'error', 'hang', 'transient', 'abort'
        ]
        self.assertEqual(set(self.client.list_behaviors()), set(wanted))

    def test_proxy(self):
        # let's do a few simple request first to make sure the proxy works
        self.assertEqual(self.client.get_behavior(), 'dummy')
        for i in range(10):
            res = requests.get(_PROXY)
            self.assertEqual(res.status_code, 200)

        # now let's add a bit of havoc
        with self.client.with_behavior('blackout'):
            # oh look we broke it
            self.assertRaises(requests.ConnectionError, requests.get, _PROXY)
            self.assertEqual(self.client.get_behavior(), 'blackout')

        with self.client.with_behavior('abort'):
            # oh look we broke it
            self.assertRaises(requests.ConnectionError, requests.get, _PROXY)
            self.assertEqual(self.client.get_behavior(), 'abort')

        # we should be back to normal
        self.assertEqual(self.client.get_behavior(), 'dummy')
        res = requests.get(_PROXY)
        self.assertEqual(res.status_code, 200)
예제 #4
0
class TestSimpleProxy(unittest.TestCase):
    def setUp(self):
        self._proxy_pid = start_proxy(log_output='/dev/null',
                                      log_level='error')
        self._web = start_simplehttp_server()
        time.sleep(.2)
        try:
            if self._web.poll():
                raise ValueError("Could not start the proxy")

            self.client = Client()

            assert self.client.get_behavior() == 'dummy'
        except Exception:
            self.tearDown()
            raise

    def tearDown(self):
        stop_proxy(self._proxy_pid)
        self._web.terminate()

    def test_existing_behaviors(self):
        wanted = ['blackout', 'delay', 'dummy', 'error', 'hang', 'transient', 'abort']
        self.assertEqual(set(self.client.list_behaviors()), set(wanted))

    def test_proxy(self):
        # let's do a few simple request first to make sure the proxy works
        self.assertEqual(self.client.get_behavior(), 'dummy')
        for i in range(10):
            res = requests.get(_PROXY)
            self.assertEqual(res.status_code, 200)

        # now let's add a bit of havoc
        with self.client.with_behavior('blackout'):
            # oh look we broke it
            self.assertRaises(requests.ConnectionError, requests.get, _PROXY)
            self.assertEqual(self.client.get_behavior(), 'blackout')

        with self.client.with_behavior('abort'):
            # oh look we broke it
            self.assertRaises(requests.ConnectionError, requests.get, _PROXY)
            self.assertEqual(self.client.get_behavior(), 'abort')

        # we should be back to normal
        self.assertEqual(self.client.get_behavior(), 'dummy')
        res = requests.get(_PROXY)
        self.assertEqual(res.status_code, 200)
예제 #5
0
    def test_aws_transient(self):
        """Tests that we retry requests."""
        self._setup_proxy()
        client = VClient()

        with client.with_behavior('transient'):
            result = self._query_ns()
            self._test_result(result)
예제 #6
0
    def test_normal(self):
        # dig A @127.0.0.1 -p 5003 mwhooker.dev.banksimple.com.
        self._setup_proxy()
        client = VClient()

        with client.with_behavior('dummy'):
            result = self._query_ns()
            self._test_result(result)
예제 #7
0
    def test_aws_5xx(self):
        """test that we succeed on 5xx errors if we have a cached
        result."""
        self._setup_proxy()
        client = VClient()
        options = {
            'inject': True
        }

        result = self._query_ns()
        with client.with_behavior('error', **options):
            result = self._query_ns()
            self._test_result(result)
예제 #8
0
    def test_under_partition(self):
        """Test that we succeed on network errors
        if we have a cached result."""
        self._setup_proxy(protocol='tcp')
        client = VClient()
        options = {
            'inject': True
        }

        result = self._query_ns()
        with client.with_behavior('error', **options):
            result = self._query_ns()
            self._test_result(result)