Esempio n. 1
0
    def test_is_in_transaction(self):
        # shared mocks
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive('get_transaction_path') \
          .and_return('/transaction/path')

        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        # test when the transaction is running
        zk.ZKTransaction.should_receive('is_blacklisted').and_return(False)
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(True, transaction.is_in_transaction(self.appid, 1))

        # and when it's not
        zk.ZKTransaction.should_receive('is_blacklisted').and_return(False)
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(False)
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(False, transaction.is_in_transaction(self.appid, 1))

        # and when it's blacklisted
        zk.ZKTransaction.should_receive('is_blacklisted').and_return(True)
        fake_transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertRaises(zk.ZKTransactionException,
                          transaction.is_in_transaction, self.appid, 1)
Esempio n. 2
0
  def test_acquire_lock(self):
    # mock out waitForConnect
    flexmock(zk.ZKTransaction)
    zk.ZKTransaction.should_receive('get_lock_root_path').\
       and_return('/lock/root/path')
    zk.ZKTransaction.should_receive('get_transaction_prefix_path').\
       and_return('/rootpath/' + self.appid)
    fake_zookeeper = flexmock(name='fake_zoo', get='get',
      connected=lambda: True)
    fake_zookeeper.should_receive('start')
    fake_zookeeper.should_receive('retry')

    flexmock(kazoo.client)
    kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

    # first, test out getting a lock for a regular transaction, that we don't
    # already have the lock for
    zk.ZKTransaction.should_receive('is_in_transaction').and_return(False)
    zk.ZKTransaction.should_receive('acquire_additional_lock').and_return(True)

    transaction = zk.ZKTransaction(host="something")
    self.assertEquals(True, transaction.acquire_lock(self.appid, "txid",
      "somekey"))

    # next, test when we're in a transaction and we already have the lock
    zk.ZKTransaction.should_receive('is_in_transaction').and_return(True)
    zk.ZKTransaction.should_receive('get_transaction_lock_list_path').\
       and_return('/rootpath/' + self.appid + "/tx1")
    fake_zookeeper.should_receive('retry').with_args('get', str) \
      .and_return(['/lock/root/path'])

    transaction = zk.ZKTransaction(host="something")
    self.assertEquals(True, transaction.acquire_lock(self.appid, "txid",
      "somekey"))

    # next, test when we're in a non-XG transaction and we're not in the lock
    # root path
    zk.ZKTransaction.should_receive('is_in_transaction').and_return(True)
    zk.ZKTransaction.should_receive('get_transaction_lock_list_path').\
       and_return('/rootpath/' + self.appid + "/tx1")
    fake_zookeeper.should_receive('retry').with_args('get', str) \
      .and_return(['/lock/root/path2'])
    zk.ZKTransaction.should_receive('is_xg').and_return(False)

    transaction = zk.ZKTransaction(host="something")
    self.assertRaises(zk.ZKTransactionException, transaction.acquire_lock,
      self.appid, "txid", "somekey")

    # next, test when we're in a XG transaction and we're not in the lock
    # root path
    zk.ZKTransaction.should_receive('is_in_transaction').and_return(True)
    zk.ZKTransaction.should_receive('get_transaction_lock_list_path').\
       and_return('/rootpath/' + self.appid + "/tx1")
    fake_zookeeper.should_receive('retry').with_args('get', str) \
      .and_return(['/lock/root/path2'])
    zk.ZKTransaction.should_receive('is_xg').and_return(True)

    transaction = zk.ZKTransaction(host="something")
    self.assertEquals(True, transaction.acquire_lock(self.appid, "txid",
      "somekey"))
Esempio n. 3
0
  def test_acquire_additional_lock(self):
    # mock out waitForConnect
    flexmock(zk.ZKTransaction)
    zk.ZKTransaction.should_receive('check_transaction')
    zk.ZKTransaction.should_receive('get_transaction_path').\
       and_return('/txn/path')
    zk.ZKTransaction.should_receive('get_lock_root_path').\
       and_return('/lock/root/path')
    zk.ZKTransaction.should_receive('get_transaction_prefix_path').\
       and_return('/rootpath/' + self.appid)

    fake_zookeeper = flexmock(name='fake_zoo', create='create',
      create_async='create_async', get='get', set_async='set_async',
      connected=lambda: True)
    fake_zookeeper.should_receive('start')
    fake_zookeeper.should_receive('retry').with_args('create', str, makepath=bool, sequence=bool,
      ephemeral=bool, value=str, acl=None).and_return("/some/lock/path")
    fake_zookeeper.should_receive('retry').with_args('create_async', str, value=str,
      acl=None, ephemeral=bool, makepath=bool, sequence=bool)
    fake_zookeeper.should_receive('retry').with_args('create_async', str, value=str,
      acl=str, ephemeral=bool, makepath=bool, sequence=bool)
    lock_list = ['path1', 'path2', 'path3']
    lock_list_str = zk.LOCK_LIST_SEPARATOR.join(lock_list)
    fake_zookeeper.should_receive('retry').with_args('get', str) \
      .and_return([lock_list_str])
    fake_zookeeper.should_receive('retry').with_args('set_async', str, str)

    flexmock(kazoo.client)
    kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

    transaction = zk.ZKTransaction(host="something")
    self.assertEquals(True, transaction.acquire_additional_lock(self.appid,
      "txid", "somekey", False))

    # Test for when we want to create a new ZK node for the lock path
    self.assertEquals(True, transaction.acquire_additional_lock(self.appid,
      "txid", "somekey", True))

    # Test for existing max groups
    lock_list = ['path' + str(num+1) for num in range(MAX_GROUPS_FOR_XG)]
    lock_list_str = zk.LOCK_LIST_SEPARATOR.join(lock_list)
    fake_zookeeper.should_receive('retry').with_args('get', str) \
      .and_return([lock_list_str])

    transaction = zk.ZKTransaction(host="something")
    self.assertRaises(zk.ZKTransactionException,
      transaction.acquire_additional_lock, self.appid, "txid", "somekey", False)

    # Test for when there is a node which already exists.
    fake_zookeeper.should_receive('retry').with_args('create', str, str, None,
      bool, bool, bool).and_raise(kazoo.exceptions.NodeExistsError)
    transaction = zk.ZKTransaction(host="something")
    self.assertRaises(zk.ZKTransactionException,
      transaction.acquire_additional_lock, self.appid, "txid", "somekey", False)
Esempio n. 4
0
    def test_versions_on_failure(self):
        zoo = zk.ZKTransaction()

        txid = zoo.get_transaction_id(self.appid, is_xg=True)

        zoo.acquire_lock(self.appid, txid, "__allocate11__")
        zoo.acquire_lock(self.appid, txid, "__allocate12__")

        current_id_1 = zoo.get_valid_transaction_id(self.appid, txid,
                                                    "__allocate11__/key")
        current_id_2 = zoo.get_valid_transaction_id(self.appid, txid,
                                                    "__allocate12__/key")

        zoo.register_updated_key(self.appid, current_id_1, txid,
                                 "__allocate11__/key")
        zoo.register_updated_key(self.appid, current_id_2, txid,
                                 "__allocate12__/key")

        zoo.notify_failed_transaction(self.appid, txid)

        self.assertEquals(
            current_id_1,
            zoo.get_valid_transaction_id(self.appid, txid,
                                         "__allocate11__/key"))
        self.assertEquals(
            current_id_2,
            zoo.get_valid_transaction_id(self.appid, txid,
                                         "__allocate12__/key"))

        self.assertRaises(zk.ZKTransactionException, zoo.release_lock,
                          self.appid, txid)
Esempio n. 5
0
    def test_versions_on_success(self):
        zoo = zk.ZKTransaction()

        txid = zoo.get_transaction_id(self.appid, is_xg=True)

        zoo.acquire_lock(self.appid, txid, "__allocate9__")
        zoo.acquire_lock(self.appid, txid, "__allocate10__")

        current_id_1 = zoo.get_valid_transaction_id(self.appid, txid,
                                                    "__allocate9__/key")
        current_id_2 = zoo.get_valid_transaction_id(self.appid, txid,
                                                    "__allocate10__/key")

        zoo.register_updated_key(self.appid, current_id_1, txid,
                                 "__allocate9__/key")
        zoo.register_updated_key(self.appid, current_id_2, txid,
                                 "__allocate10__/key")

        self.assertEquals(
            txid,
            zoo.get_valid_transaction_id(self.appid, txid,
                                         "__allocate9__/key"))
        self.assertEquals(
            txid,
            zoo.get_valid_transaction_id(self.appid, txid,
                                         "__allocate10__/key"))

        self.assertEquals(True, zoo.release_lock(self.appid, txid))
Esempio n. 6
0
    def test_create_node(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive(
            'get_transaction_prefix_path').with_args(
                self.appid).and_return('/rootpath')

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  create='create',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('create',
                                                         str,
                                                         value=str,
                                                         acl=None,
                                                         makepath=bool,
                                                         sequence=bool,
                                                         ephemeral=bool)

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        # mock out zookeeper.create for txn id
        path_to_create = "/rootpath/" + self.appid
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(
            None, transaction.create_node('/rootpath/' + self.appid, 'now'))
Esempio n. 7
0
    def test_register_updated_key(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive('get_valid_transaction_path').\
          and_return('/txn/path')
        zk.ZKTransaction.should_receive('get_transaction_path').\
          and_return('/txn/path')

        zk.ZKTransaction.should_receive('get_blacklist_root_path').\
          and_return("bl_root_path")

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  set_async='set_async',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)
        fake_zookeeper.should_receive('retry').with_args('set_async', str, str)

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(
            True,
            transaction.register_updated_key(self.appid, "1", "2", "somekey"))

        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(False)
        self.assertRaises(ZKTransactionException,
                          transaction.register_updated_key, self.appid, "1",
                          "2", "somekey")
Esempio n. 8
0
    def test_increment_and_get_counter(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive(
            'get_transaction_prefix_path').with_args(
                self.appid).and_return('/rootpath')

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  create='create',
                                  delete_async='delete_async',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').and_return(None)

        fake_counter = flexmock(name='fake_counter', value='value')
        fake_counter.value = 1
        fake_counter.should_receive('__add__').and_return(2)
        fake_zookeeper.should_receive("Counter").and_return(fake_counter)
        # mock out deleting the zero id we get the first time around

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        # assert, make sure we got back our id
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals((0, 1),
                          transaction.increment_and_get_counter(self.appid, 1))
Esempio n. 9
0
    def test_check_transaction(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive(
            'get_transaction_prefix_path').with_args(
                self.appid).and_return('/rootpath')
        zk.ZKTransaction.should_receive('is_blacklisted').and_return(False)

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(True, transaction.check_transaction(self.appid, 1))

        # Check to make sure it raises exception for blacklisted transactions.
        zk.ZKTransaction.should_receive('is_blacklisted').and_return(True)
        self.assertRaises(zk.ZKTransactionException,
                          transaction.check_transaction, self.appid, 1)

        zk.ZKTransaction.should_receive('is_blacklisted').and_return(False)
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(False)
        self.assertRaises(zk.ZKTransactionException,
                          transaction.check_transaction, self.appid, 1)
Esempio n. 10
0
    def test_create_sequence_node(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive(
            'get_transaction_prefix_path').with_args(
                self.appid).and_return('/rootpath')

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  create='create',
                                  delete='delete',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')

        # mock out zookeeper.create for txn id
        path_to_create = "/rootpath/" + self.appid
        zero_path = path_to_create + "/0"
        nonzero_path = path_to_create + "/1"


        fake_zookeeper.should_receive('retry').with_args('create', str, value=str,
          acl=None, makepath=bool, sequence=bool, ephemeral=bool).\
          and_return(zero_path).and_return(nonzero_path)

        # mock out deleting the zero id we get the first time around
        fake_zookeeper.should_receive('retry').with_args('delete', zero_path)

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        # assert, make sure we got back our id
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(1, transaction.create_sequence_node('/rootpath/' + \
          self.appid, 'now'))
Esempio n. 11
0
    def test_is_blacklisted(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive('get_blacklist_root_path').\
          and_return("bl_root_path")

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  create='create',
                                  exists='exists',
                                  get_children='get_children',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args(
            'create', str, str, None, bool, bool, bool).and_return()
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)
        fake_zookeeper.should_receive('retry').with_args('get_children', str) \
          .and_return(['1','2'])

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(True, transaction.is_blacklisted(self.appid, 1))
Esempio n. 12
0
def get_zookeeper(zk_location_ips):
  """ Returns a handler for making ZooKeeper operations.
  Args:
    zk_location_ips: A list of ZooKeeper location ips.
  """
  zookeeper_locations = get_zk_locations_string(zk_location_ips)
  zookeeper = zk.ZKTransaction(host=zookeeper_locations, start_gc=False)
  return zookeeper
Esempio n. 13
0
    def test_xg_successful_transaction(self):
        zoo = zk.ZKTransaction()

        txid = zoo.get_transaction_id(self.appid, is_xg=True)

        zoo.acquire_lock(self.appid, txid, "__allocate1__")
        zoo.acquire_lock(self.appid, txid, "__allocate2__")
        zoo.acquire_lock(self.appid, txid, "__allocate3__")

        self.assertEquals(True, zoo.release_lock(self.appid, txid))
Esempio n. 14
0
    def test_is_xg(self):
        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)

        transaction = zk.ZKTransaction(zk_client=fake_zookeeper)
        self.assertEquals(True, transaction.is_xg(self.appid, 1))
Esempio n. 15
0
    def test_try_garbage_collection(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive('update_node')

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  get='get',
                                  get_children='get_children',
                                  create='create',
                                  delete='delete')
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)
        fake_zookeeper.should_receive('retry').with_args('get', str) \
          .and_return([str(time.time() + 10000)])
        fake_zookeeper.should_receive('retry').with_args('get_children', str) \
          .and_return(['1','2','3'])
        fake_zookeeper.should_receive('retry').with_args('create',
                                                         str,
                                                         value=str,
                                                         acl=None,
                                                         ephemeral=bool)
        fake_zookeeper.should_receive('retry').with_args('delete', str)

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        # Put the last time we ran GC way into the future.
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(
            False, transaction.try_garbage_collection(self.appid,
                                                      "/some/path"))

        # Make it so we recently ran the GC
        fake_zookeeper.should_receive('retry').with_args('get', str) \
          .and_return([str(time.time())])
        self.assertEquals(
            False, transaction.try_garbage_collection(self.appid,
                                                      "/some/path"))

        # Make it so we ran the GC a long time ago.
        fake_zookeeper.should_receive('retry').with_args('get', str) \
          .and_return([str(time.time() - 1000)])
        self.assertEquals(
            True, transaction.try_garbage_collection(self.appid, "/some/path"))

        # No node means we have not run the GC before, so run it.
        fake_zookeeper.should_receive('retry').with_args('get', str) \
          .and_raise(kazoo.exceptions.NoNodeError)
        self.assertEquals(
            True, transaction.try_garbage_collection(self.appid, "/some/path"))
Esempio n. 16
0
    def test_xg_failed_transaction(self):
        zoo = zk.ZKTransaction()

        txid = zoo.get_transaction_id(self.appid, is_xg=True)

        zoo.acquire_lock(self.appid, txid, "__allocate4__")
        zoo.acquire_lock(self.appid, txid, "__allocate5__")
        zoo.acquire_lock(self.appid, txid, "__allocate6__")
        zoo.acquire_lock(self.appid, txid, "__allocate7__")
        zoo.acquire_lock(self.appid, txid, "__allocate8__")

        self.assertRaises(zk.ZKTransactionException, zoo.acquire_lock,
                          self.appid, txid, "__allocate9__")
Esempio n. 17
0
    def test_is_xg(self):
        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(True, transaction.is_xg(self.appid, 1))
Esempio n. 18
0
    def test_get_txn_path_before_getting_id(self):
        # mock out initializing a ZK connection
        flexmock(zk.ZKTransaction)

        fake_zookeeper = flexmock(name='fake_zoo')
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry')

        zk.ZKTransaction.should_receive('get_app_root_path').and_return(
            "app_root_path")

        expected = zk.PATH_SEPARATOR.join(
            ["app_root_path", zk.APP_TX_PATH, zk.APP_TX_PREFIX])
        transaction = zk.ZKTransaction(zk_client=fake_zookeeper)
        self.assertEquals(
            expected, transaction.get_txn_path_before_getting_id(self.appid))
Esempio n. 19
0
    def test_release_lock_with_path(self):
        flexmock(zk.ZKTransaction)

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo', delete='delete')
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('delete', str)

        transaction = zk.ZKTransaction(zk_client=fake_zookeeper)
        self.assertEquals(True,
                          transaction.release_lock_with_path('some/path'))

        fake_zookeeper.should_receive('retry').with_args('delete', str). \
          and_raise(kazoo.exceptions.NoNodeError)
        self.assertRaises(ZKTransactionException,
                          transaction.release_lock_with_path, 'some/path')
Esempio n. 20
0
    def test_get_lock_with_path(self):
        flexmock(zk.ZKTransaction)

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo', create='create')
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args(
            'create', str, value=str, acl=None,
            ephemeral=bool).and_return(True)

        transaction = zk.ZKTransaction(zk_client=fake_zookeeper)
        self.assertEquals(True, transaction.get_lock_with_path('path'))

        fake_zookeeper.should_receive('retry').with_args(
            'create', str, value=str, acl=None,
            ephemeral=bool).and_raise(kazoo.exceptions.NodeExistsError)
        self.assertEquals(False, transaction.get_lock_with_path('some/path'))
Esempio n. 21
0
    def test_get_xg_path(self):
        # mock out initializing a ZK connection
        flexmock(zk.ZKTransaction)

        fake_zookeeper = flexmock(name='fake_zoo')
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry')

        tx_id = 100
        tx_str = zk.APP_TX_PREFIX + "%010d" % tx_id
        zk.ZKTransaction.should_receive('get_app_root_path') \
          .and_return("app_root_path")

        expected = zk.PATH_SEPARATOR.join(
            ["app_root_path", zk.APP_TX_PATH, tx_str, zk.XG_PREFIX])

        transaction = zk.ZKTransaction(zk_client=fake_zookeeper)
        self.assertEquals(expected, transaction.get_xg_path("xxx", 100))
Esempio n. 22
0
    def test_release_lock(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive('check_transaction')
        zk.ZKTransaction.should_receive('get_transaction_path').\
          and_return('/rootpath')
        zk.ZKTransaction.should_receive('get_transaction_lock_list_path').\
          and_return('/rootpath')
        zk.ZKTransaction.should_receive('is_xg').and_return(False)

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  get='get',
                                  delete='delete',
                                  delete_async='delete_async',
                                  get_children='get_children',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)
        fake_zookeeper.should_receive('retry').with_args('get', str) \
          .and_return(['/1/2/3'])
        fake_zookeeper.should_receive('retry').with_args('delete_async', str)
        fake_zookeeper.should_receive('retry').with_args('delete', str)
        fake_zookeeper.should_receive('retry').with_args('get_children', str) \
          .and_return(['1','2'])

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(True, transaction.release_lock(self.appid, 1))

        zk.ZKTransaction.should_receive('is_xg').and_return(True)
        self.assertEquals(True, transaction.release_lock(self.appid, 1))

        # Check to make sure it raises exception for blacklisted transactions.
        zk.ZKTransaction.should_receive('is_xg').and_return(False)
        fake_zookeeper.should_receive('retry').with_args('get', str) \
          .and_raise(kazoo.exceptions.NoNodeError)
        self.assertRaises(zk.ZKTransactionException, transaction.release_lock,
                          self.appid, 1)
Esempio n. 23
0
    def test_increment_and_get_counter(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive(
            'get_transaction_prefix_path').with_args(
                self.appid).and_return('/rootpath')

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  create='create',
                                  delete_async='delete_async',
                                  connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').and_return(None)

        flexmock(InspectableCounter).should_receive('__add__').and_return(1)

        # assert, make sure we got back our id
        transaction = zk.ZKTransaction(zk_client=fake_zookeeper)
        self.assertEquals((0, 1),
                          transaction.increment_and_get_counter(self.appid, 1))
Esempio n. 24
0
    def test_execute_garbage_collection(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive('notify_failed_transaction')

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo',
                                  exists='exists',
                                  get='get',
                                  get_children='get_children')
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry').with_args('exists', str) \
          .and_return(True)
        fake_zookeeper.should_receive('retry').with_args('get', str) \
          .and_return([str(time.time() + 10000)])
        fake_zookeeper.should_receive('retry').with_args('get_children', str) \
          .and_return(['1','2','3'])

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        transaction.execute_garbage_collection(self.appid, "some/path")
Esempio n. 25
0
    def test_get_transaction_id(self):
        # mock out getTransactionRootPath
        flexmock(zk.ZKTransaction)
        zk.ZKTransaction.should_receive(
            'get_transaction_prefix_path').with_args(
                self.appid).and_return('/rootpath/' + self.appid)
        path_to_create = "/rootpath/" + self.appid + "/" + zk.APP_TX_PREFIX
        zk.ZKTransaction.should_receive('get_txn_path_before_getting_id') \
          .with_args(self.appid).and_return(path_to_create)

        # mock out time.time
        flexmock(time)
        time.should_receive('time').and_return(1000)

        # mock out initializing a ZK connection
        fake_zookeeper = flexmock(name='fake_zoo', connected=lambda: True)
        fake_zookeeper.should_receive('start')
        fake_zookeeper.should_receive('retry')

        flexmock(kazoo.client)
        kazoo.client.should_receive('KazooClient').and_return(fake_zookeeper)

        # mock out making the txn id
        zk.ZKTransaction.should_receive('create_sequence_node').with_args(
            path_to_create, '1000').and_return(1)

        # mock out zookeeper.create for is_xg
        xg_path = path_to_create + "/1/" + zk.XG_PREFIX
        zk.ZKTransaction.should_receive('get_xg_path').and_return(xg_path)
        zk.ZKTransaction.should_receive('create_node').with_args(
            xg_path, '1000')

        # assert, make sure we got back our id
        transaction = zk.ZKTransaction(host="something", start_gc=False)
        self.assertEquals(
            1, transaction.get_transaction_id(self.appid, is_xg=True))
Esempio n. 26
0
        ret = self.zk.acquire_lock(app, txid2, key)
        self.assertTrue(ret)
        # get previous valid id
        ret = self.zk.get_valid_transaction_id(app, txid, key + "/a")
        self.assertEqual(vid, ret)
        # update previous valid id
        vid = 100L
        self.zk.register_updated_key(app, txid2, vid, key + "/a")
        # try to get updated previous valid id
        ret = self.zk.get_valid_transaction_id(app, txid, key + "/a")
        self.assertEqual(vid, ret)
        self.assertTrue(self.zk.release_lock(app, txid2))

    def setUp(self):
        global zkconnection
        self.zk = zkconnection

    def tearDown(self):
        # for debug
        self.zk.dump_tree("/appscale/apps")

if __name__ == "__main__":
    global zkconnection
    zkconnection = zktransaction.ZKTransaction()
    if len(sys.argv) > 1 and sys.argv[1] == "dump":
        zkconnection.dump_tree("/appscale")
    else:
        zkconnection.deleteRecursive("/appscale/apps")
        test_support.run_unittest(TestZKTransaction)
    zkconnection.close()
Esempio n. 27
0
 def test_no_lock_with_notify_failed_transaction(self):
     zoo = zk.ZKTransaction()
     txid = zoo.get_transaction_id(self.appid, is_xg=False)
     zoo.notify_failed_transaction(self.appid, txid)
     self.assertRaises(zk.ZKTransactionException, zoo.release_lock,
                       self.appid, txid)
Esempio n. 28
0
 def test_two_locks_transaction(self):
     zoo = zk.ZKTransaction()
     txid = zoo.get_transaction_id(self.appid, is_xg=False)
     zoo.acquire_lock(self.appid, txid, "__lock_one__")
     self.assertRaises(zk.ZKTransactionException, zoo.acquire_lock,
                       self.appid, txid, "__lock_two__")
Esempio n. 29
0
 def test_no_lock_transaction(self):
     zoo = zk.ZKTransaction()
     txid = zoo.get_transaction_id(self.appid, is_xg=False)
     self.assertEquals(True, zoo.release_lock(self.appid, txid))