Exemplo n.º 1
0
class TestIpset(BaseTestCase):
    def setUp(self):
        super(TestIpset, self).setUp()
        self.ipset = Ipset("foo", "foo-tmp", "inet")

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_mainline(self, m_check_call):
        self.ipset.replace_members(set(["10.0.0.1"]))
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet --exist\n'
                      'create foo-tmp hash:ip family inet --exist\n'
                      'flush foo-tmp\n'
                      'add foo-tmp 10.0.0.1\n'
                      'swap foo foo-tmp\n'
                      'destroy foo-tmp\n'
                      'COMMIT\n'
        )

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_ensure_exists(self, m_check_call):
        self.ipset.ensure_exists()
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet --exist\n'
                      'COMMIT\n'
        )

    @patch("calico.felix.futils.call_silent", autospec=True)
    def test_delete(self, m_call_silent):
        self.ipset.delete()
        self.assertEqual(
            m_call_silent.mock_calls,
            [
                call(["ipset", "destroy", "foo"]),
                call(["ipset", "destroy", "foo-tmp"]),
            ]
        )
Exemplo n.º 2
0
class TestIpset(BaseTestCase):
    def setUp(self):
        super(TestIpset, self).setUp()
        self.ipset = Ipset("foo", "foo-tmp", "inet")

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_replace_members(self, m_check_call):
        self.ipset.replace_members(set(["10.0.0.1"]))
        exp_calls = [
            call(["ipset", "destroy", "foo-tmp"]),
            call(["ipset", "list", "foo"]),
            call(["ipset", "restore"],
                 input_str='create foo-tmp hash:ip family inet '
                 'maxelem 1048576 --exist\n'
                 'flush foo-tmp\n'
                 'add foo-tmp 10.0.0.1\n'
                 'swap foo foo-tmp\n'
                 'destroy foo-tmp\n'
                 'COMMIT\n')
        ]
        self.assertEqual(m_check_call.mock_calls, exp_calls)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_replace_members_delete_fails(self, m_check_call):
        m_check_call.side_effect = iter(
            [FailedSystemCall("Blah", [], 1, None, "err"), None, None, None])
        self.ipset.replace_members(set(["10.0.0.1"]))
        exp_calls = [
            call(["ipset", "destroy", "foo-tmp"]),
            call(['ipset', 'list', 'foo-tmp']),
            call(['ipset', 'list', 'foo']),
            call(["ipset", "restore"],
                 input_str='create foo-tmp hash:ip family inet '
                 'maxelem 1048576 --exist\n'
                 'flush foo-tmp\n'
                 'add foo-tmp 10.0.0.1\n'
                 'swap foo foo-tmp\n'
                 'destroy foo-tmp\n'
                 'COMMIT\n')
        ]
        self.assertEqual(m_check_call.mock_calls, exp_calls)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_apply_changes(self, m_check_call):
        added = set(["10.0.0.2"])
        removed = set(["10.0.0.1"])
        self.ipset.apply_changes(added, removed)
        self.assertEqual(m_check_call.mock_calls, [
            call(["ipset", "restore"],
                 input_str='del foo 10.0.0.1\n'
                 'add foo 10.0.0.2\n'
                 'COMMIT\n')
        ])

    @patch("calico.felix.futils.check_call",
           autospec=True,
           side_effect=FailedSystemCall("Blah", [], None, None, "err"))
    def test_apply_changes_err(self, m_check_call):
        # First call to update_members will fail, leading to a retry.
        added = set(["10.0.0.2"])
        removed = set(["10.0.0.1"])
        self.assertRaises(FailedSystemCall, self.ipset.apply_changes, added,
                          removed)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_ensure_exists(self, m_check_call):
        self.ipset.ensure_exists()
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet maxelem 1048576 --exist\n'
            'COMMIT\n')

    @patch("calico.felix.futils.call_silent", autospec=True)
    def test_delete(self, m_call_silent):
        self.ipset.delete()
        self.assertEqual(m_call_silent.mock_calls, [
            call(["ipset", "destroy", "foo"]),
            call(["ipset", "destroy", "foo-tmp"]),
        ])

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_list_ipset_names(self, m_check_call):
        m_check_call.return_value = CommandOutput(IPSET_LIST_OUTPUT, "")
        self.assertEqual(list_ipset_names(),
                         ['felix-v4-calico_net', 'felix-v6-calico_net'])
Exemplo n.º 3
0
class TestIpset(BaseTestCase):
    def setUp(self):
        super(TestIpset, self).setUp()
        self.ipset = Ipset("foo", "foo-tmp", "inet")

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_replace_members(self, m_check_call):
        self.ipset.replace_members(set(["10.0.0.1"]))
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet --exist\n'
                      'create foo-tmp hash:ip family inet --exist\n'
                      'flush foo-tmp\n'
                      'add foo-tmp 10.0.0.1\n'
                      'swap foo foo-tmp\n'
                      'destroy foo-tmp\n'
                      'COMMIT\n'
        )

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_update_members(self, m_check_call):
        old = set(["10.0.0.2"])
        new = set(["10.0.0.1", "10.0.0.2"])
        self.ipset.update_members(old, new)

        old = set(["10.0.0.1", "10.0.0.2"])
        new = set(["10.0.0.1", "1.2.3.4"])

        self.ipset.update_members(old, new)

        calls = [call(["ipset", "restore"],
                      input_str='add foo 10.0.0.1\nCOMMIT\n'),
                 call(["ipset", "restore"],
                      input_str='del foo 10.0.0.2\n'
                                 'add foo 1.2.3.4\n'
                                 'COMMIT\n')]

        self.assertEqual(m_check_call.call_count, 2)
        m_check_call.assert_has_calls(calls)

    @patch("calico.felix.futils.check_call", autospec=True,
           side_effect=iter([
               FailedSystemCall("Blah", [], None, None, "err"),
               None]))
    def test_update_members_err(self, m_check_call):
        # First call to update_members will fail, leading to a retry.
        old = set(["10.0.0.2"])
        new = set(["10.0.0.1"])
        self.ipset.update_members(old, new)

        calls = [call(["ipset", "restore"],
                      input_str='del foo 10.0.0.2\n'
                                'add foo 10.0.0.1\n'
                                'COMMIT\n'),
                 call(["ipset", "restore"],
                      input_str='create foo hash:ip family inet --exist\n'
                                'create foo-tmp hash:ip family inet --exist\n'
                                'flush foo-tmp\n'
                                'add foo-tmp 10.0.0.1\n'
                                'swap foo foo-tmp\n'
                                'destroy foo-tmp\n'
                                'COMMIT\n')]

        self.assertEqual(m_check_call.call_count, 2)
        m_check_call.assert_has_calls(calls)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_ensure_exists(self, m_check_call):
        self.ipset.ensure_exists()
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet --exist\n'
                      'COMMIT\n'
        )

    @patch("calico.felix.futils.call_silent", autospec=True)
    def test_delete(self, m_call_silent):
        self.ipset.delete()
        self.assertEqual(
            m_call_silent.mock_calls,
            [
                call(["ipset", "destroy", "foo"]),
                call(["ipset", "destroy", "foo-tmp"]),
            ]
        )
Exemplo n.º 4
0
class TestIpset(BaseTestCase):
    def setUp(self):
        super(TestIpset, self).setUp()
        self.ipset = Ipset("foo", "foo-tmp", "inet")

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_replace_members(self, m_check_call):
        self.ipset.replace_members(set(["10.0.0.1"]))
        exp_calls = [
            call(["ipset", "destroy", "foo-tmp"]),
            call(["ipset", "list", "foo"]),
            call(
                ["ipset", "restore"],
                input_str='create foo-tmp hash:ip family inet '
                          'maxelem 1048576 --exist\n'
                          'flush foo-tmp\n'
                          'add foo-tmp 10.0.0.1\n'
                          'swap foo foo-tmp\n'
                          'destroy foo-tmp\n'
                          'COMMIT\n'
            )
        ]
        self.assertEqual(m_check_call.mock_calls, exp_calls)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_replace_members_delete_fails(self, m_check_call):
        m_check_call.side_effect = iter([
            FailedSystemCall("Blah", [], 1, None, "err"),
            None, None, None])
        self.ipset.replace_members(set(["10.0.0.1"]))
        exp_calls = [
            call(["ipset", "destroy", "foo-tmp"]),
            call(['ipset', 'list', 'foo-tmp']),
            call(['ipset', 'list', 'foo']),
            call(
                ["ipset", "restore"],
                input_str='create foo-tmp hash:ip family inet '
                          'maxelem 1048576 --exist\n'
                          'flush foo-tmp\n'
                          'add foo-tmp 10.0.0.1\n'
                          'swap foo foo-tmp\n'
                          'destroy foo-tmp\n'
                          'COMMIT\n'
            )
        ]
        self.assertEqual(m_check_call.mock_calls, exp_calls)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_apply_changes(self, m_check_call):
        added = set(["10.0.0.2"])
        removed = set(["10.0.0.1"])
        self.ipset.apply_changes(added, removed)
        self.assertEqual(
            m_check_call.mock_calls,
            [call(["ipset", "restore"],
                   input_str='del foo 10.0.0.1\n'
                             'add foo 10.0.0.2\n'
                             'COMMIT\n')]
        )

    @patch("calico.felix.futils.check_call", autospec=True,
           side_effect=FailedSystemCall("Blah", [], None, None, "err"))
    def test_apply_changes_err(self, m_check_call):
        # First call to update_members will fail, leading to a retry.
        added = set(["10.0.0.2"])
        removed = set(["10.0.0.1"])
        self.assertRaises(FailedSystemCall,
                          self.ipset.apply_changes,
                          added, removed)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_ensure_exists(self, m_check_call):
        self.ipset.ensure_exists()
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet maxelem 1048576 --exist\n'
                      'COMMIT\n'
        )

    @patch("calico.felix.futils.call_silent", autospec=True)
    def test_delete(self, m_call_silent):
        self.ipset.delete()
        self.assertEqual(
            m_call_silent.mock_calls,
            [
                call(["ipset", "destroy", "foo"]),
                call(["ipset", "destroy", "foo-tmp"]),
            ]
        )

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_list_ipset_names(self, m_check_call):
        m_check_call.return_value = CommandOutput(IPSET_LIST_OUTPUT, "")
        self.assertEqual(list_ipset_names(),
                         ['felix-v4-calico_net', 'felix-v6-calico_net'])
Exemplo n.º 5
0
class TestIpset(BaseTestCase):
    def setUp(self):
        super(TestIpset, self).setUp()
        self.ipset = Ipset("foo", "foo-tmp", "inet")

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_replace_members(self, m_check_call):
        self.ipset.replace_members(set(["10.0.0.1"]))
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet --exist\n'
            'create foo-tmp hash:ip family inet --exist\n'
            'flush foo-tmp\n'
            'add foo-tmp 10.0.0.1\n'
            'swap foo foo-tmp\n'
            'destroy foo-tmp\n'
            'COMMIT\n')

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_update_members(self, m_check_call):
        old = set(["10.0.0.2"])
        new = set(["10.0.0.1", "10.0.0.2"])
        self.ipset.update_members(old, new)

        old = set(["10.0.0.1", "10.0.0.2"])
        new = set(["10.0.0.1", "1.2.3.4"])

        self.ipset.update_members(old, new)

        calls = [
            call(["ipset", "restore"], input_str='add foo 10.0.0.1\nCOMMIT\n'),
            call(["ipset", "restore"],
                 input_str='del foo 10.0.0.2\n'
                 'add foo 1.2.3.4\n'
                 'COMMIT\n')
        ]

        self.assertEqual(m_check_call.call_count, 2)
        m_check_call.assert_has_calls(calls)

    @patch("calico.felix.futils.check_call",
           autospec=True,
           side_effect=iter(
               [FailedSystemCall("Blah", [], None, None, "err"), None]))
    def test_update_members_err(self, m_check_call):
        # First call to update_members will fail, leading to a retry.
        old = set(["10.0.0.2"])
        new = set(["10.0.0.1"])
        self.ipset.update_members(old, new)

        calls = [
            call(["ipset", "restore"],
                 input_str='del foo 10.0.0.2\n'
                 'add foo 10.0.0.1\n'
                 'COMMIT\n'),
            call(["ipset", "restore"],
                 input_str='create foo hash:ip family inet --exist\n'
                 'create foo-tmp hash:ip family inet --exist\n'
                 'flush foo-tmp\n'
                 'add foo-tmp 10.0.0.1\n'
                 'swap foo foo-tmp\n'
                 'destroy foo-tmp\n'
                 'COMMIT\n')
        ]

        self.assertEqual(m_check_call.call_count, 2)
        m_check_call.assert_has_calls(calls)

    @patch("calico.felix.futils.check_call", autospec=True)
    def test_ensure_exists(self, m_check_call):
        self.ipset.ensure_exists()
        m_check_call.assert_called_once_with(
            ["ipset", "restore"],
            input_str='create foo hash:ip family inet --exist\n'
            'COMMIT\n')

    @patch("calico.felix.futils.call_silent", autospec=True)
    def test_delete(self, m_call_silent):
        self.ipset.delete()
        self.assertEqual(m_call_silent.mock_calls, [
            call(["ipset", "destroy", "foo"]),
            call(["ipset", "destroy", "foo-tmp"]),
        ])