コード例 #1
0
 def test_fail_to_maintain(self):
     # Become the master after one round
     LOG.debug("test_become_master_first_time")
     etcdv3._client = client = stub_etcd.Client()
     client.add_read_exception(etcdv3.KeyNotFound())
     client.add_write_exception(None)
     client.add_write_exception(e3e.ConnectionFailedError())
     elector = election.Elector("test_basic", "/bloop", interval=5, ttl=15)
     self._wait_and_stop(client, elector)
コード例 #2
0
 def test_become_master_implausible(self):
     # Become the master after key vanishes
     LOG.debug("test_become_master_implausible")
     etcdv3._client = client = stub_etcd.Client()
     client.add_read_result(key="/bloop", value="value")
     client.add_read_result(key="/bloop", value="value")
     client.add_read_exception(etcdv3.KeyNotFound())
     client.add_write_result()
     client.add_write_result()
     elector = election.Elector("test_basic", "/bloop", interval=5, ttl=15)
     self._wait_and_stop(client, elector)
コード例 #3
0
 def test_become_master_first_time(self):
     # Become the master after one round
     LOG.debug("test_become_master_first_time")
     etcdv3._client = client = stub_etcd.Client()
     client.add_read_exception(etcdv3.KeyNotFound())
     client.add_write_exception(None)
     client.add_write_exception(None)
     client.add_write_exception(None)
     elector = election.Elector("test_basic",
                                "/bloop",
                                old_key="/legacy",
                                interval=5,
                                ttl=15)
     self._wait_and_stop(client, elector)
     client.assert_key_written("/legacy")
コード例 #4
0
    def test_mainline(self):
        # Set up 3 iterations through the watcher's main loop.
        #
        # 1. No data for snapshot.  Watch throws exception.
        #
        # 2. Data for snapshot.  Watch throws exception.
        #
        # 3. Throw ExpectedException(), to exit.
        status = {'header': {'cluster_id': '1234', 'revision': '10'}}
        self.m_client.status.side_effect = iter([
            # Iteration 1.
            status,
            # Iteration 2.
            status,
            # Iteration 3.
            status,
        ])
        rsp1 = Response(action='set',
                        key='foo',
                        value='bar',
                        mod_revision='12')
        self.m_client.get.side_effect = iter([[], [_rsp_to_tuple(rsp1)],
                                              ExpectedException()])
        self.m_client.watch_prefix.side_effect = etcdv3.KeyNotFound()

        with patch.object(self.watcher, "_pre_snapshot_hook",
                          autospec=True) as m_pre:
            m_pre.return_value = None
            with patch.object(self.watcher,
                              "_post_snapshot_hook",
                              autospec=True) as m_post:
                self.assertRaises(ExpectedException, self.watcher.start)

        # _pre_snapshot_hook() called 3 times.
        self.assertEqual(m_pre.mock_calls, [call(), call(), call()])

        # _post_snapshot_hook() called twice.
        self.assertEqual(m_post.mock_calls, [call(None), call(None)])

        # watch_prefix called twice.
        self.assertEqual(self.m_client.watch_prefix.mock_calls, [
            call('/calico', start_revision='11'),
            call('/calico', start_revision='11')
        ])

        # Snapshot event dispatched once.
        self.assertEqual(self.m_dispatcher.handle_event.mock_calls,
                         [call(rsp1)])
コード例 #5
0
    def test_master_failure(self):
        LOG.debug("test_master_failure")

        etcdv3._client = client = stub_etcd.Client()
        client.add_read_exception(etcdv3.KeyNotFound())
        # Now become the master but fail
        client.add_write_exception(e3e.ConnectionFailedError())
        client.add_read_result(key="/bloop", value="value")
        client.add_read_result(key="/bloop", value=None, action="delete")
        # Now become the master but fail again
        client.add_write_exception(e3e.InternalServerError())
        # Go back to the beginning again.
        client.add_read_result(key="/bloop", value="value")
        client.add_read_result(key="/bloop", value=None, action="delete")
        client.add_write_exception(None)
        client.add_write_exception(None)
        elector = election.Elector("test_basic", "/bloop", interval=5, ttl=15)
        self._wait_and_stop(client, elector)

        # We are no longer the master, after being told to stop.
        self.assertFalse(elector.master())