Skip to content

Commit

Permalink
Merge pull request #53 from romana/issue-52
Browse files Browse the repository at this point in the history
Fixed check to select actually used route tables.
  • Loading branch information
jbrendel authored Jan 8, 2018
2 parents 48f94e0 + 51bf392 commit 791a247
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 40 deletions.
2 changes: 1 addition & 1 deletion vpcrouter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"""

__version__ = "1.8.6"
__version__ = "1.8.9"
85 changes: 70 additions & 15 deletions vpcrouter/tests/test_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def test_connect(self):

self.assertEqual(
sorted(['subnets', 'route_tables', 'instance_by_id',
'instances', 'subnet_rt_lookup', 'zones', 'vpc']),
'ip_subnet_lookup', 'instances', 'rt_subnet_lookup',
'zones', 'vpc']),
sorted(d.keys()))

self.assertEqual(self.new_vpc.id, d['vpc'].id)
Expand All @@ -178,11 +179,18 @@ def _prepare_mock_env(self):

d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")

rt_id = d['route_tables'][0].id

con.associate_route_table(route_table_id=rt_id,
subnet_id=self.new_subnet_a.id)
con.associate_route_table(route_table_id=rt_id,
subnet_id=self.new_subnet_b.id)

d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")

i1, eni1 = vpc.find_instance_and_eni_by_ip(d, self.i1ip)
i2, eni2 = vpc.find_instance_and_eni_by_ip(d, self.i2ip)

rt_id = d['route_tables'][0].id

return con, d, i1, eni1, i2, eni2, rt_id

@mock_ec2_deprecated
Expand All @@ -193,6 +201,8 @@ def test_process_route_spec_config(self):
u"10.1.0.0/16" : [self.i1ip, self.i2ip]
}

d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
# Process a simple route spec, a route should have been added
self.lc.clear()
vpc.process_route_spec_config(con, d, route_spec, [], [])
Expand All @@ -208,6 +218,8 @@ def test_process_route_spec_config(self):

# One of the two IPs questionable, switch over
d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
self.lc.clear()
vpc.process_route_spec_config(con, d, route_spec, [], [self.i1ip])
self.lc.check(
Expand All @@ -225,6 +237,8 @@ def test_process_route_spec_config(self):

# Now switch back
d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
self.lc.clear()
vpc.process_route_spec_config(con, d, route_spec, [], [self.i2ip])
self.lc.check(
Expand All @@ -243,6 +257,8 @@ def test_process_route_spec_config(self):

# One of the two IPs failed, switch over
d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
self.lc.clear()
vpc.process_route_spec_config(con, d, route_spec, [self.i1ip], [])
self.lc.check(
Expand Down Expand Up @@ -281,6 +297,8 @@ def test_process_route_spec_config(self):
}

d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
self.lc.clear()
vpc.process_route_spec_config(con, d, route_spec, [], [])
self.lc.check(
Expand All @@ -305,6 +323,8 @@ def test_process_route_spec_config(self):
}

d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
self.lc.clear()
vpc.process_route_spec_config(con, d, route_spec, [], [])
# See in the logs that 10.2.0.0/16 wasn't deleted, even though it's not
Expand All @@ -319,6 +339,11 @@ def test_process_route_spec_config(self):
@mock_ec2_deprecated
def test_add_new_route(self):
con, d, i1, eni1, i2, eni2, rt_id = self._prepare_mock_env()
route_spec = {
"10.9.0.0/16" : [self.i1ip]
}
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)

self.lc.clear()
vpc._add_new_route("10.9.0.0/16", self.i1ip, d, con, rt_id)
Expand All @@ -341,9 +366,20 @@ def test_add_new_route(self):
def test_update_route(self):
con, d, i1, eni1, i2, eni2, rt_id = self._prepare_mock_env()

route_spec = {
"10.9.0.0/16" : [self.i1ip]
}
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)

vpc._add_new_route("10.9.0.0/16", self.i1ip, d, con, rt_id)

self.lc.clear()
route_spec = {
"10.9.0.0/16" : [self.i2ip]
}
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
vpc._update_route("10.9.0.0/16", self.i2ip, self.i1ip, d, con, rt_id,
"foobar")
self.lc.check(
Expand Down Expand Up @@ -438,11 +474,14 @@ def test_get_host_for_route(self):
def test_update_existing_routes(self):
con, d, i1, eni1, i2, eni2, rt_id = self._prepare_mock_env()

vpc._add_new_route("10.0.0.0/16", self.i1ip, d, con, rt_id)

route_spec = {
u"10.0.0.0/16" : [self.i1ip]
}

d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
vpc._add_new_route("10.0.0.0/16", self.i1ip, d, con, rt_id)

routes_in_rts = {}

# Test that a protected route doesn't get updated
Expand Down Expand Up @@ -475,11 +514,11 @@ def test_update_existing_routes(self):
self.assertEqual(rt.id, rt_id)

route = rt.routes[0]
# Moto doesn't maintain intance or interface ID in the routes
# Moto doesn't maintain instance or interface ID in the routes
# correctly, so need to set this one manually. This time the route spec
# won't contain eligible hosts.
route.instance_id = i1.id
route.interface_id = eni1.id
route.instance_id = i1.id
route.interface_id = eni1.id
self.lc.clear()
route_spec = {
u"10.0.0.0/16" : []
Expand All @@ -495,23 +534,29 @@ def test_update_existing_routes(self):
# Get a refresh, since deleting via Boto interface doesn't update the
# cached vpc-info
d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
# There shouldn't be any routes left now
rt = d['route_tables'][0]
self.assertFalse(rt.routes)

# Now try again, but with proper route spec. First we need to create
# the route again and manually...
route_spec = {
u"10.0.0.0/16" : [self.i2ip]
}
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
vpc._add_new_route("10.0.0.0/16", self.i1ip, d, con, rt_id)
# ... and update our cached vpc info
d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
rt = d['route_tables'][0]
route = rt.routes[0]
route.instance_id = i1.id
route.interface_id = eni1.id

route_spec = {
u"10.0.0.0/16" : [self.i2ip]
}
# Only IP for spec is in failed IPs, can't do anything
self.lc.clear()
vpc._update_existing_routes(route_spec, [self.i2ip], [],
Expand Down Expand Up @@ -562,6 +607,8 @@ def test_add_missing_routes(self):
self.lc.check()

self.lc.clear()
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
vpc._add_missing_routes(route_spec, [], [], {}, d, con, routes_in_rts)
self.lc.check(
('root', 'INFO',
Expand Down Expand Up @@ -614,12 +661,14 @@ def test_multi_address(self):
private_ip_address="10.9.9.9",
primary=False)
eni1.private_ip_addresses.append(priv)
vpc._make_ip_subnet_lookup(d)

self.lc.clear()
route_spec = {
"10.0.0.0/16" : ["10.9.9.9"]
}
self.lc.clear()
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
vpc._add_missing_routes(route_spec, [], [], {},
d, con, {rt_id : []})
self.lc.check(
Expand All @@ -636,13 +685,19 @@ def test_handle_spec(self):
# output later on
con = vpc.connect_to_region("ap-southeast-2")
d = vpc.get_vpc_overview(con, self.new_vpc.id, "ap-southeast-2")
route_spec = {
u"10.2.0.0/16" : [self.i1ip]
}
d['cluster_node_subnets'] = \
vpc.make_cluster_node_subnet_list(d, route_spec)
i, eni = vpc.find_instance_and_eni_by_ip(d, self.i1ip)

rt_id = d['route_tables'][0].id

route_spec = {
u"10.2.0.0/16" : [self.i1ip]
}
con.associate_route_table(route_table_id=rt_id,
subnet_id=self.new_subnet_a.id)
con.associate_route_table(route_table_id=rt_id,
subnet_id=self.new_subnet_b.id)

# Test handle_spec
vid = self.new_vpc.id
Expand Down
Loading

0 comments on commit 791a247

Please sign in to comment.