Ejemplo n.º 1
0
def test_update_config():
    """Test we get the expected modifications for a haproxy configuration
       file"""

    utils.create_test_tmpdir()
    utils.copy_to_testdir('%s/haproxy.cfg' % utils.get_data_path())
    config = utils.get_config('%s/haproxy_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorHaproxy('%s/ip_data.cfg' %
                                        utils.get_data_path())
    gen.set_config_values(config)
    with pytest.raises(ServiceAccessGeneratorServiceRestartError):
        gen.update_config()

    # Load the reference result data
    ref_result_file = ('%s/haproxy.cfg' % utils.get_reference_result_path())
    ref_result = open(ref_result_file).read()

    # Load the generated result
    gen_result_file = ('%s//haproxy.cfg' % utils.get_test_tmpdir())
    gen_result = open(gen_result_file).read()

    if ref_result == gen_result:
        # success
        utils.remove_test_tmpdir()
    else:
        msg = 'Test failed, not removing test directory '
        msg += '"%s" to aid debugging ' % utils.get_test_tmpdir()
        assert 1 == 0, msg
Ejemplo n.º 2
0
def test_update_config():
    """Test we get the expected modifications for a haproxy configuration
       file"""

    utils.create_test_tmpdir()
    utils.copy_to_testdir('%s/haproxy.cfg' % utils.get_data_path())
    config = utils.get_config('%s/haproxy_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorHaproxy(
        '%s/ip_data.cfg' % utils.get_data_path()
    )
    gen.set_config_values(config)
    with pytest.raises(ServiceAccessGeneratorServiceRestartError) as excinfo:
        gen.update_config()

    # Load the reference result data
    ref_result_file = (
        '%s/haproxy.cfg' % utils.get_reference_result_path()
    )
    ref_result = open(ref_result_file).read()

    # Load the generated result
    gen_result_file = (
        '%s//haproxy.cfg' % utils.get_test_tmpdir()
    )
    gen_result = open(gen_result_file).read()

    if ref_result == gen_result:
        # success
        utils.remove_test_tmpdir()
    else:
        msg = 'Test failed, not removing test directory '
        msg += '"%s" to aid debugging ' % utils.get_test_tmpdir()
        assert 1 == 0, msg
def test_update_config_no_file():
    """Test the generation if the ACL config file if the file does not exist"""

    utils.create_test_tmpdir()
    utils.copy_to_testdir('%s/nginx_setup.cfg' % utils.get_data_path())
    config = utils.get_config('%s/nginx_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorNginx('%s/ip_data.cfg' % utils.get_data_path())
    gen.set_config_values(config)
    with pytest.raises(ServiceAccessGeneratorServiceRestartError):
        gen.update_config()

    # Load the reference result data
    ref_result_file = ('%s/nginx-access.cfg' %
                       utils.get_reference_result_path())
    ref_result = open(ref_result_file).read()

    # Load the generated result
    gen_result_file = ('%s/nginx-access.cfg' % utils.get_test_tmpdir())
    gen_result = open(gen_result_file).read()

    if ref_result == gen_result:
        # success
        utils.remove_test_tmpdir()
    else:
        msg = 'Test failed, not removing test directory '
        msg += '"%s" to aid debugging ' % utils.get_test_tmpdir()
        assert False, msg
Ejemplo n.º 4
0
def test_update_config_v24(mock_glob, mock_osaccess, mock_ospath_apache,
                           mock_ospath_base):
    """Test we get the expected modifications in the config file"""

    utils.create_test_tmpdir()
    utils.copy_to_testdir('%s/apache-vhost.cfg' % utils.get_data_path())
    config = utils.get_config('%s/apache_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorApache('%s/ip_data.cfg' %
                                       utils.get_data_path())
    gen.set_config_values(config)
    with pytest.raises(ServiceAccessGeneratorServiceRestartError) as excinfo:
        gen.update_config()

    # Load the reference result data
    ref_result_file = ('%s/apache-vhost-24.cfg' %
                       utils.get_reference_result_path())
    ref_result = open(ref_result_file).read()

    # Load the generated result
    gen_result_file = ('%s/apache-vhost.cfg' % utils.get_test_tmpdir())
    gen_result = open(gen_result_file).read()

    if ref_result == gen_result:
        # success
        utils.remove_test_tmpdir()
    else:
        msg = 'Test failed, not removing test directory '
        msg += '"%s" to aid debugging ' % utils.get_test_tmpdir()
        assert False, msg
def test_factory_haproxy():
    """Test that we get a genarator for Apache"""

    config = utils.get_config('%s/haproxy_setup.cfg' % utils.get_data_path())
    generators = get_access_rule_generators(config)

    assert len(generators) == 1
    assert generators[0].__class__.__name__ == 'ServiceAccessGeneratorHaproxy'
Ejemplo n.º 6
0
def test_update_config_v24_large_IP_set(
        mock_glob,
        mock_osaccess,
        mock_ospath_apache,
        mock_ospath_base):
    """Test we get the expected modifications in the config file"""

    utils.create_test_tmpdir()
    utils.copy_to_testdir('%s/apache-vhost.cfg' % utils.get_data_path())
    config = utils.get_config('%s/apache_setup.cfg' % utils.get_data_path())
    ip_config = utils.get_config('%s/ip_data.cfg' % utils.get_data_path())
    large_ip_set = '192.168.1.0/24,' * 400
    large_ip_set += '192.168.1.0/24'
    ip_config.set('region3', 'public-ips', large_ip_set)
    large_ip_data = open('%s/large_ip_data.cfg' % utils.get_test_tmpdir(), 'w')
    ip_config.write(large_ip_data)
    large_ip_data.close()

    gen = ServiceAccessGeneratorApache(
        '%s/large_ip_data.cfg' % utils.get_test_tmpdir()
    )
    gen.set_config_values(config)
    with pytest.raises(ServiceAccessGeneratorServiceRestartError) as excinfo:
        gen.update_config()

    # Load the reference result data
    ref_result_file = (
        '%s/apache-vhost-24-large-ip-set.cfg'
        % utils.get_reference_result_path()
    )
    ref_result = open(ref_result_file).read()

    # Load the generated result
    gen_result_file = (
        '%s/apache-vhost.cfg' % utils.get_test_tmpdir()
    )
    gen_result = open(gen_result_file).read()

    if ref_result == gen_result:
        # success
        utils.remove_test_tmpdir()
    else:
        msg = 'Test failed, not removing test directory '
        msg += '"%s" to aid debugging ' % utils.get_test_tmpdir()
        assert False, msg
Ejemplo n.º 7
0
def test_update_config_v24_large_IP_set(
        mock_glob,
        mock_osaccess,
        mock_ospath_apache,
        mock_ospath_base):
    """Test we get the expected modifications in the config file"""

    utils.create_test_tmpdir()
    utils.copy_to_testdir('%s/apache-vhost_24.cfg' % utils.get_data_path())
    config = utils.get_config('%s/apache_setup_24.cfg' % utils.get_data_path())
    ip_config = utils.get_config('%s/ip_data.cfg' % utils.get_data_path())
    large_ip_set = '192.168.1.0/24,' * 400
    large_ip_set += '192.168.1.0/24'
    ip_config.set('region3', 'public-ips', large_ip_set)
    large_ip_data = open('%s/large_ip_data.cfg' % utils.get_test_tmpdir(), 'w')
    ip_config.write(large_ip_data)
    large_ip_data.close()

    gen = ServiceAccessGeneratorApache(
        '%s/large_ip_data.cfg' % utils.get_test_tmpdir()
    )
    gen.set_config_values(config)
    with pytest.raises(ServiceAccessGeneratorServiceRestartError) as excinfo:
        gen.update_config()

    # Load the reference result data
    ref_result_file = (
        '%s/apache-vhost-24-large-ip-set.cfg'
        % utils.get_reference_result_path()
    )
    ref_result = open(ref_result_file).read()

    # Load the generated result
    gen_result_file = (
        '%s/apache-vhost_24.cfg' % utils.get_test_tmpdir()
    )
    gen_result = open(gen_result_file).read()

    if ref_result == gen_result:
        # success
        utils.remove_test_tmpdir()
    else:
        msg = 'Test failed, not removing test directory '
        msg += '"%s" to aid debugging ' % utils.get_test_tmpdir()
        assert False, msg
def test_factory_missing_option(mock_logging):
    """Test that an incomplete configuration file, with the missing 'ipdata'
       option in the 'accessservice' triggers and error"""

    config = utils.get_config('%s/no_service_plugin.cfg' %
                              utils.get_data_path())
    get_access_rule_generators(config)

    assert mock_logging.error.called
def test_factory_multi_services():
    """Test that the factory produces generators for all configured
       services"""

    config = utils.get_config('%s/multi_service_setup.cfg' %
                              utils.get_data_path())
    generators = get_access_rule_generators(config)

    assert len(generators) == 2
Ejemplo n.º 10
0
def test_factory_haproxy():
    """Test that we get a genarator for Apache"""

    config = utils.get_config(
        '%s/haproxy_setup.cfg' % utils.get_data_path()
    )
    generators = get_access_rule_generators(config)

    assert len(generators) == 1
    assert generators[0].__class__.__name__ == 'ServiceAccessGeneratorHaproxy'
Ejemplo n.º 11
0
def test_factory_missing_option(mock_logging):
    """Test that an incomplete configuration file, with the missing 'ipdata'
       option in the 'accessservice' triggers and error"""

    config = utils.get_config(
        '%s/no_service_plugin.cfg' % utils.get_data_path()
    )
    generators = get_access_rule_generators(config)

    assert mock_logging.error.called
Ejemplo n.º 12
0
def test_factory_multi_services():
    """Test that the factory produces generators for all configured
       services"""

    config = utils.get_config(
        '%s/multi_service_setup.cfg' % utils.get_data_path()
    )
    generators = get_access_rule_generators(config)

    assert len(generators) == 2
def test_factory_missing_plugin(mock_logging):
    """Test that a configured service with no equivalent plugin produces
       an error message"""

    config = utils.get_config('%s/missing_ipdata_opt.cfg' %
                              utils.get_data_path())
    with pytest.raises(ServiceAccessGeneratorConfigError):
        get_access_rule_generators(config)

    assert mock_logging.error.called
Ejemplo n.º 14
0
def test_set_config_values_no_service_name():
    """Test configuration with missing 'serviceName' option service and
       section name should be equal"""

    config = utils.get_config('%s/base_no_service_name_config.cfg' %
                              utils.get_data_path())
    gen = ServiceAccessGenerator('%s/ip_data.cfg' % utils.get_data_path())
    result = gen.set_config_values(config, 'example')

    assert gen.service_name == 'example'
    assert result == 1
Ejemplo n.º 15
0
def test_factory_missing_plugin(mock_logging):
    """Test that a configured service with no equivalent plugin produces
       an error message"""

    config = utils.get_config(
        '%s/missing_ipdata_opt.cfg' % utils.get_data_path()
    )
    with pytest.raises(ServiceAccessGeneratorConfigError) as excinfo:
        generators = get_access_rule_generators(config)

    assert mock_logging.error.called
Ejemplo n.º 16
0
def test_set_config_values():
    """Test a properly configured setup"""

    config = utils.get_config('%s/base_config.cfg' % utils.get_data_path())
    gen = ServiceAccessGenerator('%s/ip_data.cfg' % utils.get_data_path())
    result = gen.set_config_values(config, 'example')

    assert gen.service_name == 'foo'
    assert gen.service_config == '/tmp/serviceAccess/foo.cfg'
    assert gen.request == 'http://example.com/service'
    assert gen.interval == '-1'
    assert result == 1
Ejemplo n.º 17
0
def test_get_apache_ip_directive_upgrade_file_22(mock_glob, mock_osaccess,
                                                 mock_ospath):
    """Test we get the expected directive value for Apache 2.2
       when using the upgrade file filename"""

    config = utils.get_config('%s/apache_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorApache('%s/ip_data.cfg' %
                                       utils.get_data_path())
    gen.set_config_values(config)
    ip_directive = gen._get_apache_ip_directive()

    assert ip_directive == 'Allow from'
Ejemplo n.º 18
0
def test_get_apache_ip_directive_apachectl_call_24(mock_ospopen, mock_osaccess,
                                                   mock_ospath):
    """Test we get the expected directive value for Apache 2.4
       when 'apachctl' is called"""

    config = utils.get_config('%s/apache_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorApache('%s/ip_data.cfg' %
                                       utils.get_data_path())
    gen.set_config_values(config)
    ip_directive = gen._get_apache_ip_directive()

    assert ip_directive == 'Require ip'
Ejemplo n.º 19
0
def test_set_config_values_no_service_config(mock_logging):
    """Test configuration with missing 'serviceConfig' option"""

    config = utils.get_config('%s/base_no_service_config.cfg' %
                              utils.get_data_path())
    gen = ServiceAccessGenerator('%s/ip_data.cfg' % utils.get_data_path())
    with pytest.raises(ServiceAccessGeneratorConfigError) as excinfo:
        gen.set_config_values(config, 'example')

    expected_msg = 'Configuration error. A configuration file to modify '
    expected_msg += 'must be specified with the "serviceConfig" option in the '
    expected_msg += '"example" section of the config file.'

    assert mock_logging.error.called
    assert expected_msg == str(excinfo.value)
Ejemplo n.º 20
0
def test_get_apache_ip_directive_upgrade_file_24(
        mock_glob,
        mock_osaccess,
        mock_ospath):
    """Test we get the expected directive value for Apache 2.4
       when using the upgrade file filename"""

    config = utils.get_config('%s/apache_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorApache(
        '%s/ip_data.cfg' % utils.get_data_path()
    )
    gen.set_config_values(config)
    ip_directive = gen._get_apache_ip_directive()

    assert ip_directive == 'Require ip'
Ejemplo n.º 21
0
def test_set_config_values_no_interval(mock_logging):
    """Test configuration with missing 'updateInterval' option"""

    config = utils.get_config('%s/base_no_interval_config.cfg' %
                              utils.get_data_path())
    gen = ServiceAccessGenerator('%s/ip_data.cfg' % utils.get_data_path())
    with pytest.raises(ServiceAccessGeneratorConfigError) as excinfo:
        result = gen.set_config_values(config, 'example')

    expected_msg = 'Configuration error. An update interval '
    expected_msg += 'must be specified with the "updateInterval" option '
    expected_msg += 'in the "example" section of the config file.'

    assert mock_logging.error.called
    assert expected_msg == str(excinfo.value)
Ejemplo n.º 22
0
def test_get_apache_ip_directive_apachectl_call_24(
        mock_ospopen,
        mock_osaccess,
        mock_ospath):
    """Test we get the expected directive value for Apache 2.4
       when 'apachctl' is called"""

    config = utils.get_config('%s/apache_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorApache(
        '%s/ip_data.cfg' % utils.get_data_path()
    )
    gen.set_config_values(config)
    ip_directive = gen._get_apache_ip_directive()

    assert ip_directive == 'Require ip'
Ejemplo n.º 23
0
def test_update_config_v24(
        mock_glob,
        mock_osaccess,
        mock_ospath_apache,
        mock_ospath_base):
    """Test we get the expected modifications in the config file"""

    utils.create_test_tmpdir()
    utils.copy_to_testdir('%s/apache-vhost.cfg' % utils.get_data_path())
    config = utils.get_config('%s/apache_setup.cfg' % utils.get_data_path())
    gen = ServiceAccessGeneratorApache(
        '%s/ip_data.cfg' % utils.get_data_path()
    )
    gen.set_config_values(config)
    with pytest.raises(ServiceAccessGeneratorServiceRestartError) as excinfo:
        gen.update_config()

    # Load the reference result data
    ref_result_file = (
        '%s/apache-vhost-24.cfg' % utils.get_reference_result_path()
    )
    ref_result = open(ref_result_file).read()

    # Load the generated result
    gen_result_file = (
        '%s/apache-vhost.cfg' % utils.get_test_tmpdir()
    )
    gen_result = open(gen_result_file).read()

    if ref_result == gen_result:
        # success
        utils.remove_test_tmpdir()
    else:
        msg = 'Test failed, not removing test directory '
        msg += '"%s" to aid debugging ' % utils.get_test_tmpdir()
        assert False, msg