Ejemplo n.º 1
0
def test_host_port_remap():
    """
    """
    # Test that providing nothing to NodeManager will not cause error
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=None,
    )
    # Test that providing wrong root level object type will raise config exception. List is expected
    with pytest.raises(RedisClusterConfigError) as excp:
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap={},
        )
    # An empty host_port_remap  list should not raise an error
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=[],
    )
    # A wrong object type inside host_port_remap list shold raise error
    with pytest.raises(RedisClusterConfigError) as excp:
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[None],
        )
    # The correct object typ inside list but empty should not give an error
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=[{}, {}],
    )
    # If we only have either or from_host or to_host set we should get an error
    with pytest.raises(RedisClusterConfigError) as excp:
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'from_host': ''
            }],
        )
    with pytest.raises(RedisClusterConfigError) as excp:
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'to_host': ''
            }],
        )
    # If we only have either or from_port or to_port set we should get an error
    with pytest.raises(RedisClusterConfigError) as excp:
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'from_port': ''
            }],
        )
    with pytest.raises(RedisClusterConfigError) as excp:
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'to_port': ''
            }],
        )

    # Creating a valid config with multiple entries
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=[
            {
                'from_host': '127.0.0.1',
                'to_host': 'localhost',
                'from_port': 7000,
                'to_port': 70001
            },
            {
                'from_host': '172.1.0.1',
                'to_host': 'localhost',
                'from_port': 7000,
                'to_port': 70001
            },
        ],
    )

    # If no host_port_remap is set then a node obj should not be modified in any way when remapping it
    n = NodeManager(host_port_remap=None,
                    startup_nodes=[{
                        "host": "127.0.0.1",
                        "port": 7000
                    }])
    initial_node_obj = ['127.0.0.1', 7000, 'xyz']
    unmodified_remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert unmodified_remapped_obj == initial_node_obj

    # Test that modifying both host and port works
    n = NodeManager(host_port_remap=[{
        'from_host': '127.0.0.1',
        'to_host': 'localhost',
        'from_port': 7000,
        'to_port': 7001
    }],
                    startup_nodes=[{
                        "host": "127.0.0.1",
                        "port": 7000
                    }])
    initial_node_obj = ['127.0.0.1', 7000, 'xyz']
    remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert remapped_obj[0] == 'localhost'
    assert remapped_obj[1] == 7001
Ejemplo n.º 2
0
def test_host_port_remap():
    """
    """
    # Test that providing nothing to NodeManager will not cause error
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=None,
    )
    # Test that providing wrong root level object type will raise config exception. List is expected
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap={},
        )
    # An empty host_port_remap  list should not raise an error
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=[],
    )
    # A wrong object type inside host_port_remap list shold raise error
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[None],
        )
    # The correct object typ inside list but empty should not give an error
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=[{}, {}],
    )
    # If we only have either or from_host or to_host set we should get an error
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'from_host': ''
            }],
        )
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'to_host': ''
            }],
        )
    # If we only have either or from_port or to_port set we should get an error
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'from_port': ''
            }],
        )
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'to_port': ''
            }],
        )
    # Invalid keys in the rules should also raise exception
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'invalid_key': ''
            }],
        )

    # Invalid ips in the rules should raise exception
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'from_host': '127.2.x.w',
                'to_host': '127.0.0.1'
            }],
        )
    # Incomplete ips in the rules should raise exception
    with pytest.raises(RedisClusterConfigError):
        n = NodeManager(
            startup_nodes=[{
                "host": "127.0.0.1",
                "port": 7000
            }],
            host_port_remap=[{
                'from_host': '127.2',
                'to_host': '127.0.0.1'
            }],
        )

    # Creating a valid config with multiple entries
    n = NodeManager(
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
        host_port_remap=[
            {
                'from_host': '127.0.0.1',
                'to_host': '127.0.0.1',
                'from_port': 7000,
                'to_port': 70001
            },
            {
                'from_host': '172.1.0.1',
                'to_host': '127.0.0.1',
                'from_port': 7000,
                'to_port': 70001
            },
        ],
    )

    # If no host_port_remap is set then a node obj should not be modified in any way when remapping it
    n = NodeManager(
        host_port_remap=None,
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
    )
    initial_node_obj = ['127.0.0.1', 7000, 'xyz']
    unmodified_remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert unmodified_remapped_obj == initial_node_obj

    # Test that modifying both host and port works
    n = NodeManager(
        host_port_remap=[
            {
                'from_host': '127.1.1.1',
                'to_host': '128.0.0.1',
                'from_port': 7000,
                'to_port': 7001
            },
            {
                'from_host': '127.2.2.2',
                'to_host': '128.0.0.1',
                'from_port': 7000,
                'to_port': 7005
            },
        ],
        startup_nodes=[{
            "host": "128.0.0.1",
            "port": 7000
        }],
    )
    initial_node_obj = ['127.1.1.1', 7000, 'xyz']
    remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert remapped_obj[0] == '128.0.0.1'
    assert remapped_obj[1] == 7001

    # Validate that ports are NOT remapped in isolation if hosts are also present
    n = NodeManager(
        host_port_remap=[
            {
                'from_host': '127.2.2.2',
                'to_host': '127.0.0.1',
                'from_port': 7000,
                'to_port': 7001
            },
            {
                'from_host': '127.3.3.3',
                'to_host': '127.0.0.1',
                'from_port': 7000,
                'to_port': 7005
            },
        ],
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
    )
    initial_node_obj = ['127.0.0.1', 7000, 'xyz']
    remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert remapped_obj[0] == '127.0.0.1'
    assert remapped_obj[1] == 7000

    # Validate that first applicable rule is applied
    n = NodeManager(
        host_port_remap=[
            {
                'from_host': '127.2.2.2',
                'to_host': '127.0.0.1',
                'from_port': 7000,
                'to_port': 7001
            },
            {
                'from_host': '127.3.3.3',
                'to_host': '127.0.0.1',
                'from_port': 7000,
                'to_port': 7005
            },
            {
                'from_host': '127.2.2.2',
                'to_host': '127.0.0.1',
                'from_port': 7000,
                'to_port': 7006
            },
        ],
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
    )
    initial_node_obj = ['127.2.2.2', 7000, 'xyz']
    remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert remapped_obj[0] == '127.0.0.1'
    assert remapped_obj[1] == 7001

    # Validate just port mapping works
    n = NodeManager(
        host_port_remap=[
            {
                'from_port': 7000,
                'to_port': 7001
            },
            {
                'from_port': 7002,
                'to_port': 7005
            },
        ],
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
    )
    initial_node_obj = ['127.0.0.1', 7000, 'xyz']
    remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert remapped_obj[0] == '127.0.0.1'
    assert remapped_obj[1] == 7001

    # Validate just host mapping works
    n = NodeManager(
        host_port_remap=[
            {
                'from_host': '127.2.2.2',
                'to_host': '127.0.0.1'
            },
            {
                'from_host': '127.3.3.3',
                'to_host': '127.0.0.2'
            },
        ],
        startup_nodes=[{
            "host": "127.0.0.1",
            "port": 7000
        }],
    )
    initial_node_obj = ['127.3.3.3', 7000, 'xyz']
    remapped_obj = n.remap_internal_node_object(initial_node_obj)
    assert remapped_obj[0] == '127.0.0.2'
    assert remapped_obj[1] == 7000