コード例 #1
0
class TestIpsetManager(BaseTestCase):
    def setUp(self):
        super(TestIpsetManager, self).setUp()
        self.reset()

    def reset(self):
        self.created_refs = defaultdict(list)
        self.acquired_refs = {}
        self.mgr = IpsetManager(IPV4)
        self.m_create = Mock(spec=self.mgr._create,
                             side_effect = self.m_create)
        self.mgr._create = self.m_create

    def m_create(self, tag_id):
        _log.info("Creating ipset %s", tag_id)
        ipset = Mock(spec=TagIpset)

        ipset._manager = None
        ipset._id = None
        ipset.ref_mgmt_state = CREATED
        ipset.ref_count = 0
        ipset.owned_ipset_names.return_value = ["felix-v4-" + tag_id,
                                                "felix-v4-tmp-" + tag_id]

        ipset.tag = tag_id
        self.created_refs[tag_id].append(ipset)
        return ipset

    def test_tag_then_endpoint(self):
        # Send in the messages.
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        # Let the actor process them.
        self.step_mgr()
        self.assert_one_ep_one_tag()

    def test_endpoint_then_tag(self):
        # Send in the messages.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        # Let the actor process them.
        self.step_mgr()
        self.assert_one_ep_one_tag()

    def test_endpoint_then_tag_idempotent(self):
        for _ in xrange(3):
            # Send in the messages.
            self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
            self.mgr.on_tags_update("prof1", ["tag1"], async=True)
            # Let the actor process them.
            self.step_mgr()
            self.assert_one_ep_one_tag()

    def assert_one_ep_one_tag(self):
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })
        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": {
                    "prof1": set([
                        EP_ID_1_1
                    ])
                }
            }
        })

    def test_change_ip(self):
        # Initial set-up.
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.step_mgr()
        # Update the endpoint's IPs:
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1_NEW_IP, async=True)
        self.step_mgr()

        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.2": {
                    "prof1": set([
                        EP_ID_1_1
                    ])
                },
                "10.0.0.3": {
                    "prof1": set([
                        EP_ID_1_1
                    ])
                }
            }
        })

    def test_tag_updates(self):
        # Initial set-up.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.step_mgr()

        # Add a tag, keep a tag.
        self.mgr.on_tags_update("prof1", ["tag1", "tag2"], async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": {
                    "prof1": set([
                        EP_ID_1_1
                    ])
                }
            },
            "tag2": {
                "10.0.0.1": {
                    "prof1": set([
                        EP_ID_1_1
                    ])
                }
            }
        })
        self.assertEqual(self.mgr.tags_by_prof_id, {"prof1": ["tag1", "tag2"]})

        # Remove a tag.
        self.mgr.on_tags_update("prof1", ["tag2"], async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag2": {
                "10.0.0.1": {
                    "prof1": set([
                        EP_ID_1_1
                    ])
                }
            }
        })

        # Delete the tags:
        self.mgr.on_tags_update("prof1", None, async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.ip_owners_by_tag, {})
        self.assertEqual(self.mgr.tags_by_prof_id, {})

    def step_mgr(self):
        self.step_actor(self.mgr)
        self.assertEqual(self.mgr._dirty_tags, set())

    def test_update_profile_and_ips(self):
        # Initial set-up.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_tags_update("prof3", ["tag3"], async=True)
        self.step_mgr()

        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1_NEW_PROF_IP, async=True)
        self.step_mgr()

        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag3": {
                "10.0.0.3": {
                    "prof3": set([
                        EP_ID_1_1
                    ])
                }
            }
        })
        self.assertEqual(self.mgr.endpoint_ids_by_profile_id, {
            "prof3": set([EP_ID_1_1])
        })

    def test_optimize_out_v6(self):
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1_IPV6, async=True)
        self.step_mgr()
        # Index should contain only 1_1:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })

    def test_optimize_out_no_nets(self):
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1_NO_NETS, async=True)
        self.step_mgr()
        # Index should contain only 1_1:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })
        # Should be happy to then add it in.
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1, async=True)
        self.step_mgr()
        # Index should contain both:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
            EP_ID_2_1: EP_DATA_2_1,
        })

    def test_duplicate_ips(self):
        # Add in two endpoints with the same IP.
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1, async=True)
        self.step_mgr()
        # Index should contain both:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
            EP_ID_2_1: EP_DATA_2_1,
        })
        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": {
                    "prof1": set([
                        EP_ID_1_1,
                        EP_ID_2_1,
                    ])
                }
            }
        })

        # Second profile tags arrive:
        self.mgr.on_tags_update("prof2", ["tag1", "tag2"], async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": {
                    "prof1": set([
                        EP_ID_1_1,
                        EP_ID_2_1,
                    ]),
                    "prof2": set([
                        EP_ID_1_1,
                    ])
                }
            },
            "tag2": {
                "10.0.0.1": {
                    "prof2": set([
                        EP_ID_1_1,
                    ])
                }
            },
        })

        # Remove one, check the index gets updated.
        self.mgr.on_endpoint_update(EP_ID_2_1, None, async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })
        self.assertEqual(self.mgr.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": {
                    "prof1": set([
                        EP_ID_1_1,
                    ]),
                    "prof2": set([
                        EP_ID_1_1,
                    ])
                }
            },
            "tag2": {
                "10.0.0.1": {
                    "prof2": set([
                        EP_ID_1_1,
                    ])
                }
            },
        })

        # Remove the other, index should get completely cleaned up.
        self.mgr.on_endpoint_update(EP_ID_1_1, None, async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {})
        self.assertEqual(self.mgr.ip_owners_by_tag, {})

    def on_ref_acquired(self, tag_id, ipset):
        self.acquired_refs[tag_id] = ipset

    @patch("calico.felix.ipsets.list_ipset_names", autospec=True)
    @patch("calico.felix.futils.check_call", autospec=True)
    def test_cleanup(self, m_check_call, m_list_ipsets):
        # Start with a couple ipsets.
        self.mgr.get_and_incref("foo", callback=self.on_ref_acquired,
                                async=True)
        self.mgr.get_and_incref("bar", callback=self.on_ref_acquired,
                                async=True)
        self.step_mgr()
        self.assertEqual(set(self.created_refs.keys()),
                         set(["foo", "bar"]))

        # Notify ready so that the ipsets are marked as started.
        self._notify_ready(["foo", "bar"])
        self.step_mgr()

        # Then decref "bar" so that it gets marked as stopping.
        self.mgr.decref("bar", async=True)
        self.step_mgr()
        self.assertEqual(
            self.mgr.stopping_objects_by_id,
            {"bar": set(self.created_refs["bar"])}
        )

        # Return mix of expected and unexpected ipsets.
        m_list_ipsets.return_value = [
            "not-felix-foo",
            "felix-v6-foo",
            "felix-v6-bazzle",
            "felix-v4-foo",
            "felix-v4-bar",
            "felix-v4-baz",
            "felix-v4-biff",
        ]
        m_check_call.side_effect = iter([
            # Exception on any individual call should be ignored.
            FailedSystemCall("Dummy", [], None, None, None),
            None,
        ])
        self.mgr.cleanup(async=True)
        self.step_mgr()

        # Explicitly check that exactly the right delete calls were made.
        # assert_has_calls would ignore extra calls.
        self.assertEqual(sorted(m_check_call.mock_calls),
                         sorted([
                             call(["ipset", "destroy", "felix-v4-biff"]),
                             call(["ipset", "destroy", "felix-v4-baz"]),
                         ]))

    def test_apply_snapshot_mainline(self):
        self.mgr.apply_snapshot(
            {"prof1": ["tag1"], "prof2": ["B"], "prof3": ["B"]},
            {EP_ID_1_1: EP_1_1,
             EP_ID_2_1: EP_2_1},
            async=True,
        )
        self.mgr.get_and_incref("tag1",
                                callback=self.on_ref_acquired,
                                async=True)
        self.step_mgr()
        self.mgr.on_object_startup_complete("tag1",
                                            self.created_refs["tag1"][0],
                                            async=True)
        self.step_mgr()
        self.mgr.apply_snapshot(
            {"prof1": ["tag1", "tag2"]},
            {EP_ID_1_1: EP_1_1},
            async=True,
        )
        self.step_mgr()
        self.assertEqual(self.mgr.tags_by_prof_id,
                         {"prof1": ["tag1", "tag2"]})
        self.assertEqual(self.mgr.endpoint_data_by_ep_id,
                         {EP_ID_1_1: EP_DATA_1_1})
        ipset = self.acquired_refs["tag1"]
        self.assertEqual(
            ipset.replace_members.mock_calls,
            [
                call(set(['10.0.0.1']), force_reprogram=True, async=True),
                call(set(['10.0.0.1']), force_reprogram=True, async=True),
            ]
        )

    def test_apply_snapshot_forces_reprogram(self):
        # Apply a snapshot but mock the finish call so that we can check that
        # apply_snapshot set the flag...
        self.mgr.apply_snapshot(
            {"prof1": ["A"], "prof2": ["B"]},
            {EP_ID_1_1: EP_1_1,
             EP_ID_2_1: EP_2_1},
            async=True,
        )
        # noinspection PyUnresolvedReferences
        with patch.object(self.mgr, "_finish_msg_batch"):
            self.step_actor(self.mgr)
        self.assertTrue(self.mgr._force_reprogram)

    def test_finish_msg_batch_clears_reprogram_flag(self):
        # Apply a snapshot and step the actor for real, should clear the flag.
        self.mgr.apply_snapshot(
            {"prof1": ["A"]},
            {EP_ID_1_1: EP_1_1},
            async=True,
        )
        self.step_mgr()
        self.assertFalse(self.mgr._force_reprogram)

    def _notify_ready(self, tags):
        for tag in tags:
            self.mgr.on_object_startup_complete(tag, self.created_refs[tag][0],
                                                async=True)
        self.step_mgr()
コード例 #2
0
class TestIpsetManager(BaseTestCase):
    def setUp(self):
        super(TestIpsetManager, self).setUp()
        self.reset()

    def reset(self):
        self.created_refs = defaultdict(list)
        self.acquired_refs = {}
        self.config = Mock()
        self.config.MAX_IPSET_SIZE = 1234
        self.mgr = IpsetManager(IPV4, self.config)
        self.m_create = Mock(spec=self.mgr._create,
                             side_effect = self.m_create)
        self.real_create = self.mgr._create
        self.mgr._create = self.m_create

    def m_create(self, tag_or_sel):
        _log.info("Creating ipset %s", tag_or_sel)

        # Do the real creation, to kick off selector indexing, for example.
        with patch("calico.felix.ipsets.RefCountedIpsetActor", autospec=True):
            self.real_create(tag_or_sel)

        # But return a mock...
        ipset = Mock(spec=RefCountedIpsetActor)

        ipset._manager = None
        ipset._id = None
        ipset.ref_mgmt_state = CREATED
        ipset.ref_count = 0
        if isinstance(tag_or_sel, SelectorExpression):
            name_stem = tag_or_sel.unique_id[:8]
        else:
            name_stem = tag_or_sel
        ipset.owned_ipset_names.return_value = ["felix-v4-" + name_stem,
                                                "felix-v4-tmp-" + name_stem]
        ipset.name_stem = name_stem
        self.created_refs[tag_or_sel].append(ipset)
        return ipset

    def test_create(self):
        with patch("calico.felix.ipsets.Ipset") as m_Ipset:
            mgr = IpsetManager(IPV4, self.config)
            tag_ipset = mgr._create("tagid")
        self.assertEqual(tag_ipset.name_stem, "tagid")
        m_Ipset.assert_called_once_with('felix-v4-tagid',
                                        'felix-tmp-v4-tagid',
                                        'inet', 'hash:ip',
                                        max_elem=1234)

    def test_maybe_start_gates_on_in_sync(self):
        with patch("calico.felix.refcount.ReferenceManager."
                   "_maybe_start") as m_maybe_start:
            self.mgr._maybe_start("tag-123")
            self.assertFalse(m_maybe_start.called)
            self.mgr.on_datamodel_in_sync(async=True)
            self.mgr.on_datamodel_in_sync(async=True)  # No-op
            self.step_mgr()
            self.mgr._maybe_start("tag-123")
            self.assertEqual(m_maybe_start.mock_calls,
                             [call("tag-123")])

    def test_tag_then_endpoint(self):
        # Send in the messages.
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        # Let the actor process them.
        self.step_mgr()
        self.assert_one_ep_one_tag()
        # Undo our messages to check that the index is correctly updated,
        self.mgr.on_tags_update("prof1", None, async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, None, async=True)
        self.step_mgr()
        self.assert_index_empty()

    def test_endpoint_then_tag(self):
        # Send in the messages.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        # Let the actor process them.
        self.step_mgr()
        self.assert_one_ep_one_tag()

    def test_endpoint_then_tag_idempotent(self):
        for _ in xrange(3):
            # Send in the messages.
            self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
            self.mgr.on_tags_update("prof1", ["tag1"], async=True)
            # Let the actor process them.
            self.step_mgr()
            self.assert_one_ep_one_tag()

    def assert_one_ep_one_tag(self):
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": ("prof1", EP_ID_1_1),
            }
        })

    def test_selector_then_endpoint(self):
        # Send in the messages.  this selector should match even though there
        # are no labels in the endpoint.
        selector = parse_selector("all()")
        self.mgr.get_and_incref(selector,
                                callback=self.on_ref_acquired,
                                async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        # Let the actor process them.
        self.step_mgr()

        self.assert_one_selector_one_ep(selector)

        # Undo our messages to check that the index is correctly updated.
        self.mgr.decref(selector, async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, None, async=True)
        self.step_mgr()
        self.assert_index_empty()

    def test_endpoint_then_selector(self):
        # Send in the messages.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        selector = parse_selector("all()")
        self.mgr.get_and_incref(selector,
                                callback=self.on_ref_acquired,
                                async=True)
        # Let the actor process them.
        self.step_mgr()

        self.assert_one_selector_one_ep(selector)

        # Undo our messages to check that the index is correctly updated.
        self.mgr.on_endpoint_update(EP_ID_1_1, None, async=True)
        self.mgr.decref(selector, async=True)
        self.step_mgr()
        self.assert_index_empty()

    def test_non_trivial_selector_parent_match(self):
        """
        Test a selector that relies on both directly-set labels and
        inherited ones.
        """
        # Send in the messages.  this selector should match even though there
        # are no labels in the endpoint.
        selector = parse_selector("a == 'a1' && p == 'p1'")
        self.mgr.get_and_incref(selector,
                                callback=self.on_ref_acquired,
                                async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1_LABELS, async=True)
        # Let the actor process them.
        self.step_mgr()

        # Should be no match yet.
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {})

        # Now fire in a parent label.
        self.mgr.on_prof_labels_set("prof1", {"p": "p1"}, async=True)
        self.step_mgr()

        # Should now have a match.
        self.assert_one_selector_one_ep(selector)

        # Undo our messages to check that the index is correctly updated.
        self.mgr.on_prof_labels_set("prof1", None, async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {})
        self.mgr.decref(selector, async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, None, async=True)
        self.step_mgr()
        self.assert_index_empty()

    def test_endpoint_ip_update_with_selector_match(self):
        """
        Test a selector that relies on both directly-set labels and
        inherited ones.
        """
        # Send in the messages.  this selector should match even though there
        # are no labels in the endpoint.
        selector = parse_selector("a == 'a1'")
        self.mgr.get_and_incref(selector,
                                callback=self.on_ref_acquired,
                                async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1_LABELS, async=True)
        self.step_mgr()

        # Should now have a match.
        self.assert_one_selector_one_ep(selector)

        # Now update the IPs, should update the index.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1_LABELS_NEW_IP,
                                    async=True)
        self.step_mgr()

        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            selector: {
                "10.0.0.2": ("dummy", EP_ID_1_1),
            }
        })

        # Undo our messages to check that the index is correctly updated.
        self.mgr.decref(selector, async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, None, async=True)
        self.step_mgr()
        self.assert_index_empty()

    def assert_one_selector_one_ep(self, selector):
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            selector: {
                "10.0.0.1": ("dummy", EP_ID_1_1),
            }
        })

    def assert_index_empty(self):
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {})
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {})

    def test_change_ip(self):
        # Initial set-up.
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.step_mgr()
        # Update the endpoint's IPs:
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1_NEW_IP, async=True)
        self.step_mgr()

        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.2": ("prof1", EP_ID_1_1),
                "10.0.0.3": ("prof1", EP_ID_1_1),
            }
        })

    def test_tag_updates(self):
        # Initial set-up.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.step_mgr()

        # Add a tag, keep a tag.
        self.mgr.on_tags_update("prof1", ["tag1", "tag2"], async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": ("prof1", EP_ID_1_1),
            },
            "tag2": {
                "10.0.0.1": ("prof1", EP_ID_1_1),
            }
        })
        self.assertEqual(self.mgr.tags_by_prof_id, {"prof1": ["tag1", "tag2"]})

        # Remove a tag.
        self.mgr.on_tags_update("prof1", ["tag2"], async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag2": {
                "10.0.0.1": ("prof1", EP_ID_1_1),
            }
        })

        # Delete the tags:
        self.mgr.on_tags_update("prof1", None, async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {})
        self.assertEqual(self.mgr.tags_by_prof_id, {})

    def step_mgr(self):
        self.step_actor(self.mgr)

    def test_update_profile_and_ips(self):
        # Initial set-up.
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_tags_update("prof3", ["tag3"], async=True)
        self.step_mgr()

        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1_NEW_PROF_IP, async=True)
        self.step_mgr()

        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag3": {
                "10.0.0.3": ("prof3", EP_ID_1_1)
            }
        })
        self.assertEqual(self.mgr.endpoint_ids_by_profile_id, {
            "prof3": set([EP_ID_1_1])
        })

    def test_optimize_out_v6(self):
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1_IPV6, async=True)
        self.step_mgr()
        # Index should contain only 1_1:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })

    def test_optimize_out_no_nets(self):
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1_NO_NETS, async=True)
        self.step_mgr()
        # Index should contain only 1_1:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })
        # Should be happy to then add it in.
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1, async=True)
        self.step_mgr()
        # Index should contain both:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
            EP_ID_2_1: EP_DATA_2_1,
        })

    def test_duplicate_ips(self):
        # Add in two endpoints with the same IP.
        self.mgr.on_tags_update("prof1", ["tag1"], async=True)
        self.mgr.on_endpoint_update(EP_ID_1_1, EP_1_1, async=True)
        self.mgr.on_endpoint_update(EP_ID_2_1, EP_2_1, async=True)
        self.step_mgr()
        # Index should contain both:
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
            EP_ID_2_1: EP_DATA_2_1,
        })
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": set([
                    ("prof1", EP_ID_1_1),
                    ("prof1", EP_ID_2_1),
                ])
            }
        })

        # Second profile tags arrive:
        self.mgr.on_tags_update("prof2", ["tag1", "tag2"], async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": set([
                    ("prof1", EP_ID_1_1),
                    ("prof1", EP_ID_2_1),
                    ("prof2", EP_ID_1_1),
                ])
            },
            "tag2": {
                "10.0.0.1": ("prof2", EP_ID_1_1),
            },
        })

        # Remove one, check the index gets updated.
        self.mgr.on_endpoint_update(EP_ID_2_1, None, async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {
            EP_ID_1_1: EP_DATA_1_1,
        })
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {
            "tag1": {
                "10.0.0.1": set([
                    ("prof1", EP_ID_1_1),
                    ("prof2", EP_ID_1_1),
                ])
            },
            "tag2": {
                "10.0.0.1": ("prof2", EP_ID_1_1),
            },
        })

        # Remove the other, index should get completely cleaned up.
        self.mgr.on_endpoint_update(EP_ID_1_1, None, async=True)
        self.step_mgr()
        self.assertEqual(self.mgr.endpoint_data_by_ep_id, {})
        self.assertEqual(self.mgr.tag_membership_index.ip_owners_by_tag, {},
                         "ip_owners_by_tag should be empty, not %s" %
                         pformat(self.mgr.tag_membership_index.ip_owners_by_tag))

    def on_ref_acquired(self, tag_id, ipset):
        self.acquired_refs[tag_id] = ipset

    @patch("calico.felix.ipsets.list_ipset_names", autospec=True)
    @patch("calico.felix.futils.check_call", autospec=True)
    def test_cleanup(self, m_check_call, m_list_ipsets):
        # We're testing the in-sync processing
        self.mgr.on_datamodel_in_sync(async=True)
        # Start with a couple ipsets.
        self.mgr.get_and_incref("foo", callback=self.on_ref_acquired,
                                async=True)
        self.mgr.get_and_incref("bar", callback=self.on_ref_acquired,
                                async=True)
        self.step_mgr()
        self.assertEqual(set(self.created_refs.keys()),
                         set(["foo", "bar"]))

        # Notify ready so that the ipsets are marked as started.
        self._notify_ready(["foo", "bar"])
        self.step_mgr()

        # Then decref "bar" so that it gets marked as stopping.
        self.mgr.decref("bar", async=True)
        self.step_mgr()
        self.assertEqual(
            self.mgr.stopping_objects_by_id,
            {"bar": set(self.created_refs["bar"])}
        )

        # Return mix of expected and unexpected ipsets.
        m_list_ipsets.return_value = [
            "not-felix-foo",
            "felix-v6-foo",
            "felix-v6-bazzle",
            "felix-v4-foo",
            "felix-v4-bar",
            "felix-v4-baz",
            "felix-v4-biff",
        ]
        m_check_call.side_effect = iter([
            # Exception on any individual call should be ignored.
            FailedSystemCall("Dummy", [], None, None, None),
            None,
        ])
        self.mgr.cleanup(async=True)
        self.step_mgr()

        # Explicitly check that exactly the right delete calls were made.
        # assert_has_calls would ignore extra calls.
        self.assertEqual(sorted(m_check_call.mock_calls),
                         sorted([
                             call(["ipset", "destroy", "felix-v4-biff"]),
                             call(["ipset", "destroy", "felix-v4-baz"]),
                         ]))

    def test_update_dirty(self):
        self.mgr._datamodel_in_sync = True
        m_ipset = Mock(spec=RefCountedIpsetActor)
        self.mgr.objects_by_id["tag-123"] = m_ipset
        with patch.object(self.mgr, "_is_starting_or_live",
                          autospec=True) as m_sol:
            m_sol.return_value = True
            with patch.object(self.mgr.tag_membership_index,
                              "get_and_reset_changes_by_tag",
                              autospec=True) as m_get_and_reset:
                m_get_and_reset.return_value = ({"tag-123": set(["10.0.0.1"])},
                                                {"tag-123": set(["10.0.0.2"])})
                self.mgr._update_dirty_active_ipsets()
                self.assertEqual(
                    m_ipset.add_members.mock_calls,
                    [call(set(["10.0.0.1"]), async=True)]
                )
                self.assertEqual(
                    m_ipset.remove_members.mock_calls,
                    [call(set(["10.0.0.2"]), async=True)]
                )

    def _notify_ready(self, tags):
        for tag in tags:
            self.mgr.on_object_startup_complete(tag, self.created_refs[tag][0],
                                                async=True)
        self.step_mgr()