Exemplo n.º 1
0
    def test_should_return_vcl_list_without_broken_server_items(self):
        first_vcl = Vcl('Test-1', name='test-1')
        second_vcl = Vcl('Test-2', name='test-2')
        cluster_with_partial_reload = LogicalCluster(name='cluster1',
                                                     id=1,
                                                     partial_reload=True)
        server = VarnishServer(ip='127.0.0.1',
                               port='6082',
                               hostname='localhost-1',
                               secret='secret-1',
                               dc=dc,
                               cluster=cluster_with_partial_reload,
                               status='active')

        vcl_list = [(server, first_vcl), (servers[1], second_vcl)]

        with patch.object(VarnishApiProvider,
                          'get_api',
                          side_effect=[VclLoadException, None]):
            with patch.object(VclLoader,
                              'load_new_vcl',
                              return_value=VclStatus.OK):
                # as opposed to test:
                # test_should_raise_custom_exception_if_error_occurred_while_connecting_to_server
                # it DOES NOT raise any exception when cluster allow partial reloads
                # what is being tested implicitly there.
                to_use = ParallelLoader().load_vcl_list(vcl_list)
                assert_equals(len(to_use), 1)
Exemplo n.º 2
0
    def test_vcl_is_not_changed(self, varnish_api_mock):
        varnish_api_mock.vcl_active_name.return_value = 'vcl-current-vol_cfff2'

        vcl = Vcl('vcl content')
        vcl.name = 'vcl-new-vol.cfff2'

        loader = VclLoader(varnish_api_mock)

        assert_false(loader.vcl_has_changed(vcl))
Exemplo n.º 3
0
    def test_vcl_is_not_changed(self, varnish_api_mock):
        varnish_api_mock.vcl_active_name.return_value = 'vcl-current-vol.cfff2'

        vcl = Vcl('vcl content')
        vcl.name = 'vcl-new-vol.cfff2'

        loader = VclLoader(varnish_api_mock)

        assert_false(loader.vcl_has_changed(vcl))
Exemplo n.º 4
0
    def test_should_load_vcl_list_to_associated_servers(self):
        first_vcl = Vcl('Test-1', name='test-1')
        second_vcl = Vcl('Test-2', name='test-2')
        vcl_list = [(servers[0], first_vcl), (servers[1], second_vcl)]

        with patch.object(VarnishApiProvider, 'get_api') as get_api_mock:
            with patch.object(VclLoader, 'load_new_vcl') as load_vcl_mock:
                ParallelLoader().load_vcl_list(vcl_list)
                assert_equals(
                    [call(first_vcl), call(second_vcl)],
                    load_vcl_mock.call_args_list)
                assert_equals(
                    [call(servers[0]), call(servers[1])],
                    get_api_mock.call_args_list)
Exemplo n.º 5
0
    def test_should_load_and_use_only_loaded_vcls(self):
        start_processing_time = timezone.now()
        vcl = Vcl('Test-content', name='test')

        loader_mock = Mock()
        loader_mock.use_vcl = Mock(return_value=VclStatus.OK)
        loader_mock.discard_unused_vcls = Mock()

        rendered_list = [(vcl, servers[0])]
        loaded_list = [(vcl, loader_mock, servers[0])]

        with patch.object(ParallelRenderer,
                          'render_vcl_for_servers',
                          return_value=rendered_list):
            with patch.object(ParallelLoader,
                              'load_vcl_list',
                              return_value=loaded_list):
                with patch.object(ParallelLoader,
                                  'use_vcl_list',
                                  return_value=True) as use_vcl_mock:
                    varnish_cluster = VarnishCluster()
                    assert_true(
                        varnish_cluster.load_vcl(start_processing_time, []))
                    """
                    Here we check if only previously loaded vcl-s are used
                    """
                    assert_list_equal(
                        [call(start_processing_time, loaded_list)],
                        use_vcl_mock.call_args_list)
Exemplo n.º 6
0
    def test_should_return_false_if_vcl_list_is_properly_used(self):
        loader_mock = Mock()
        loader_mock.use_vcl = Mock(return_value=VclStatus.ERROR)
        first_vcl = Vcl('Test-1', name='test-1')
        vcl_loaded_list = [(first_vcl, loader_mock, servers[1])]

        assert_false(ParallelLoader().use_vcl_list('test', vcl_loaded_list))
Exemplo n.º 7
0
    def test_should_raise_custom_exception_if_error_occurred_while_connecting_to_server(
            self):
        first_vcl = Vcl('Test-1', name='test-1')
        vcl_list = [(servers[1], first_vcl)]

        with patch.object(VarnishApiProvider, 'get_api'):
            ParallelLoader().load_vcl_list(vcl_list)
Exemplo n.º 8
0
    def test_should_return_loaded_vcl_list_which_should_be_use_on_servers(
            self):
        first_vcl = Vcl('Test-1', name='test-1')
        second_vcl = Vcl('Test-2', name='test-2')
        vcl_list = [(servers[1], first_vcl), (servers[2], second_vcl)]

        with patch.object(VarnishApiProvider, 'get_api'):
            with patch.object(VclLoader,
                              'load_new_vcl',
                              return_value=VclStatus.OK):
                to_use = ParallelLoader().load_vcl_list(vcl_list)
                assert_equals(len(to_use), 2)
                self.assert_loaded_vcl_contains_proper_vcl_and_server(
                    to_use[0], first_vcl, servers[1])
                self.assert_loaded_vcl_contains_proper_vcl_and_server(
                    to_use[1], second_vcl, servers[2])
Exemplo n.º 9
0
    def test_load_new_vcl(self, varnish_api_mock):
        varnish_api_mock.vcl_content_active.return_value = 'vcl old content'
        varnish_api_mock.vcl_inline.return_value = [[200]]

        loader = VclLoader(varnish_api_mock)

        assert_equals(VclStatus.OK, loader.load_new_vcl(Vcl('vcl content')))
Exemplo n.º 10
0
    def test_use_new_vcl(self, varnish_api_mock):
        varnish_api_mock.vcl_content_active.return_value = 'vcl old content'
        varnish_api_mock.vcl_use.return_value = [[200]]

        loader = VclLoader(varnish_api_mock)

        assert_true(VclStatus.OK, loader.use_vcl(Vcl('vcl content')))
Exemplo n.º 11
0
    def test_do_not_load_new_vcl_if_it_can_not_be_compiled(self, varnish_api_mock):
        varnish_api_mock.vcl_content_active.return_value = 'vcl old content'
        varnish_api_mock.vcl_inline.return_value = [[400]]

        loader = VclLoader(varnish_api_mock)

        assert_equals(VclStatus.ERROR, loader.load_new_vcl(Vcl('vcl content')))
Exemplo n.º 12
0
    def test_should_suppress_varnish_command_execution_exception_if_proper_parameter_is_passed(
            self, varnish_api_mock):
        varnish_api_mock.vcl_content_active.return_value = 'vcl old content'
        varnish_api_mock.vcl_inline.side_effect = AssertionError()
        loader = VclLoader(varnish_api_mock, True)

        assert_equals(VclStatus.NO_CHANGES,
                      loader.load_new_vcl(Vcl('vcl content')))
Exemplo n.º 13
0
 def test_should_raise_custom_exception_if_error_occurred_while_loading_vcl(
         self):
     first_vcl = Vcl('Test-1', name='test-1')
     vcl_list = [(servers[1], first_vcl)]
     with patch.object(VarnishApiProvider, 'get_api'):
         with patch.object(VclLoader,
                           'load_new_vcl',
                           return_value=VclStatus.ERROR):
             ParallelLoader().load_vcl_list(vcl_list)
Exemplo n.º 14
0
    def test_should_discard_old_vcls(self):
        loader_mock = Mock()
        loader_mock.use_vcl = Mock(return_value=VclStatus.OK)
        loader_mock.discard_unused_vcls = Mock()
        first_vcl = Vcl('Test-1', name='test-1')
        vcl_loaded_list = [(first_vcl, loader_mock, servers[1])]

        ParallelLoader().use_vcl_list('test', vcl_loaded_list)
        assert_true([call()], loader_mock._discard_unused_vcls.call_args_list)
Exemplo n.º 15
0
    def test_should_return_vcl_list_without_broken_server_items(self):
        first_vcl = Vcl('Test-1', name='test-1')
        vcl_list = [(servers[1], first_vcl)]

        with patch.object(VarnishApiProvider,
                          'get_api',
                          side_effect=VclLoadException):
            # as opposed to test:
            # PartialLoaderTest#test_should_raise_custom_exception_if_error_occurred_while_connecting_to_server
            # it DOES NOT raise any exception what is being tested implicitly there.
            to_use = PartialParallelLoader().load_vcl_list(vcl_list)

            self.assertListEqual([], to_use)
Exemplo n.º 16
0
    def test_should_not_use_vcls_on_error_while_loading_vcl(self):
        vcl = Vcl('Test-content', name='test')

        rendered_list = [(vcl, servers[0])]

        with patch.object(ParallelRenderer,
                          'render_vcl_for_servers',
                          return_value=rendered_list):
            with patch.object(ParallelLoader,
                              'load_vcl_list',
                              side_effect=VclLoadException):
                with patch.object(ParallelLoader,
                                  'use_vcl_list',
                                  return_value=False) as use_vcl_mock:
                    varnish_cluster = VarnishCluster()
                    with self.assertRaises(VclLoadException):
                        varnish_cluster.load_vcl(timezone.now(), [])
                        """
                        Here we check if 'use' command is NOT sent to servers
                        """
                        assert_list_equal([], use_vcl_mock.call_args_list)
Exemplo n.º 17
0
 def test_should_compare_versions(self):
     vcl = Vcl('some_content', 'new-vcl')
     assert_true(vcl.compareVersion('other-vcl-vol.ca5ef'))
Exemplo n.º 18
0
 def test_name_should_contain_version_based_on_digest(self):
     vcl = Vcl('some_content', 'new-vcl')
     assert_equals('new-vcl-vol.ca5ef', vcl.name)
Exemplo n.º 19
0
 def test_should_compare_versions(self):
     vcl = Vcl('some_content', 'new-vcl')
     assert_true(vcl.compareVersion('other-vcl-vol.ca5ef'))
Exemplo n.º 20
0
                  dc=dc,
                  cluster=cluster2,
                  status='active'),
    VarnishServer(ip='127.0.0.3',
                  port='6084',
                  hostname='localhost-3',
                  secret='secret-3',
                  dc=dc,
                  cluster=cluster3,
                  status='active'),
]

query_set = Mock()
query_set.prefetch_related = Mock(return_value=servers)

sample_vcl = Vcl('Test-content', name='test')


class ServerExtractorTest(TestCase):
    @patch('vaas.cluster.cluster.VarnishServer.objects.exclude',
           Mock(return_value=query_set))
    def test_should_extract_servers_by_touched_clusters(self):
        touched_clusters = [cluster1, cluster2]
        expected_extracted_servers = [servers[0], servers[1]]

        assert_equals(
            ServerExtractor().extract_servers_by_clusters(touched_clusters),
            expected_extracted_servers)


class VarnishApiProviderTest(TestCase):
Exemplo n.º 21
0
    def test_vcl_is_changed(self, varnish_api_mock):
        varnish_api_mock.vcl_content_active.return_value = 'vcl old content'

        loader = VclLoader(varnish_api_mock)

        assert_true(loader.vcl_has_changed(Vcl('vcl content')))