106a7d5d9Sfarshidimport unittest
206a7d5d9Sfarshidimport uuid
306a7d5d9Sfarshidfrom TestInput import TestInputSingleton
406a7d5d9Sfarshidimport logger
506a7d5d9Sfarshidimport time
626cc6c4eSMike Wiederholdimport datetime
706a7d5d9Sfarshidfrom membase.api.rest_client import RestConnection
806a7d5d9Sfarshidfrom membase.helper.bucket_helper import BucketOperationHelper
906a7d5d9Sfarshidfrom remote.remote_util import RemoteMachineShellConnection
1006a7d5d9Sfarshid
1106a7d5d9Sfarshidclass DeleteMembaseBuckets(unittest.TestCase):
1206a7d5d9Sfarshid
1306a7d5d9Sfarshid    servers = None
1406a7d5d9Sfarshid    input = None
1506a7d5d9Sfarshid    log = None
1606a7d5d9Sfarshid
1706a7d5d9Sfarshid    def setUp(self):
1806a7d5d9Sfarshid        self.log = logger.Logger.get_logger()
1906a7d5d9Sfarshid        self.input = TestInputSingleton.input
2006a7d5d9Sfarshid        self.assertTrue(self.input, msg="input parameters missing...")
2106a7d5d9Sfarshid        self.servers = self.input.servers
2206a7d5d9Sfarshid        BucketOperationHelper.delete_all_buckets_or_assert(servers=self.servers, test_case=self)
23f6e58849Sfarshid        self._log_start()
2406a7d5d9Sfarshid
2506a7d5d9Sfarshid    def tearDown(self):
2606a7d5d9Sfarshid        BucketOperationHelper.delete_all_buckets_or_assert(servers=self.servers, test_case=self)
27f6e58849Sfarshid        self._log_finish()
28f6e58849Sfarshid
29f6e58849Sfarshid    def _log_start(self):
30f6e58849Sfarshid        try:
31f6e58849Sfarshid            msg = "{0} : {1} started ".format(datetime.datetime.now(), self._testMethodName)
32f6e58849Sfarshid            RestConnection(self.servers[0]).log_client_error(msg)
33f6e58849Sfarshid        except:
34f6e58849Sfarshid            pass
35f6e58849Sfarshid
36f6e58849Sfarshid
37f6e58849Sfarshid    def _log_finish(self):
38f6e58849Sfarshid        try:
39f6e58849Sfarshid            msg = "{0} : {1} finished ".format(datetime.datetime.now(), self._testMethodName)
40f6e58849Sfarshid            RestConnection(self.servers[0]).log_client_error(msg)
41f6e58849Sfarshid        except:
42f6e58849Sfarshid            pass
4306a7d5d9Sfarshid
44d7816905SKeith Batten    def test_default_moxi(self):
4506a7d5d9Sfarshid        name = 'default'
4606a7d5d9Sfarshid        for serverInfo in self.servers:
4706a7d5d9Sfarshid            rest = RestConnection(serverInfo)
4806a7d5d9Sfarshid            replicas = [0, 1, 2, 3]
4906a7d5d9Sfarshid            for replicaNumber in replicas:
5006a7d5d9Sfarshid                proxyPort = rest.get_nodes_self().moxi
5106a7d5d9Sfarshid                rest.create_bucket(bucket=name,
5206a7d5d9Sfarshid                                   ramQuotaMB=200,
5306a7d5d9Sfarshid                                   replicaNumber=replicaNumber,
5406a7d5d9Sfarshid                                   proxyPort=proxyPort)
5506a7d5d9Sfarshid                remote = RemoteMachineShellConnection(serverInfo)
5606a7d5d9Sfarshid
5706a7d5d9Sfarshid                msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
5806a7d5d9Sfarshid                self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
5906a7d5d9Sfarshid                rest.delete_bucket(name)
6006a7d5d9Sfarshid                msg = 'bucket "{0}" was not deleted even after waiting for two minutes'.format(name)
6106a7d5d9Sfarshid                self.assertTrue(BucketOperationHelper.wait_for_bucket_deletion(name, rest, timeout_in_seconds=30), msg=msg)
6206a7d5d9Sfarshid                msg = 'bucket {0} data files are not deleted after bucket deleted from membase'.format(name)
6306a7d5d9Sfarshid                self.assertTrue(
6406a7d5d9Sfarshid                    self.wait_for_data_files_deletion(name,
6506a7d5d9Sfarshid                                                      remote_connection=remote,
6606a7d5d9Sfarshid                                                      rest=rest, timeout_in_seconds=20), msg=msg)
6706a7d5d9Sfarshid
68d7816905SKeith Batten    def test_non_default_moxi(self):
6906a7d5d9Sfarshid        name = 'new-bucket-{0}'.format(uuid.uuid4())
7006a7d5d9Sfarshid        for serverInfo in self.servers:
7106a7d5d9Sfarshid            replicas = [0, 1, 2, 3]
7206a7d5d9Sfarshid            for replicaNumber in replicas:
7306a7d5d9Sfarshid                rest = RestConnection(serverInfo)
7406a7d5d9Sfarshid                proxyPort = rest.get_nodes_self().moxi + 2000
7506a7d5d9Sfarshid                rest.create_bucket(bucket=name,
7606a7d5d9Sfarshid                                   ramQuotaMB=200,
7706a7d5d9Sfarshid                                   replicaNumber=replicaNumber,
7806a7d5d9Sfarshid                                   proxyPort=proxyPort)
7906a7d5d9Sfarshid                remote = RemoteMachineShellConnection(serverInfo)
8006a7d5d9Sfarshid                msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
8106a7d5d9Sfarshid                self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
8206a7d5d9Sfarshid                rest.delete_bucket(name)
8306a7d5d9Sfarshid                msg = 'bucket "{0}" was not deleted even after waiting for 30 seconds'.format(name)
8406a7d5d9Sfarshid                self.assertTrue(BucketOperationHelper.wait_for_bucket_deletion(name, rest, timeout_in_seconds=30), msg=msg)
8506a7d5d9Sfarshid                msg = 'bucket {0} data files are not deleted after bucket deleted from membase'.format(name)
8606a7d5d9Sfarshid                self.assertTrue(
8706a7d5d9Sfarshid                    self.wait_for_data_files_deletion(name,
8806a7d5d9Sfarshid                                                      remote_connection=remote,
8906a7d5d9Sfarshid                                                      rest=rest, timeout_in_seconds=20), msg=msg)
9006a7d5d9Sfarshid                BucketOperationHelper.delete_bucket_or_assert(serverInfo, name, self)
9106a7d5d9Sfarshid
9206a7d5d9Sfarshid
9306a7d5d9Sfarshid    #TODO: move these methods to a helper class
9406a7d5d9Sfarshid    def wait_for_data_files_deletion(self,
9506a7d5d9Sfarshid                                     bucket,
9606a7d5d9Sfarshid                                     remote_connection,
9706a7d5d9Sfarshid                                     rest,
9806a7d5d9Sfarshid                                     timeout_in_seconds=120):
9906a7d5d9Sfarshid        self.log.info('waiting for bucket data files deletion from the disk ....')
10006a7d5d9Sfarshid        start = time.time()
10106a7d5d9Sfarshid        while (time.time() - start) <= timeout_in_seconds:
10206a7d5d9Sfarshid            if self.verify_data_files_deletion(bucket, remote_connection, rest):
10306a7d5d9Sfarshid                return True
10406a7d5d9Sfarshid            else:
10506a7d5d9Sfarshid                data_file = '{0}-data'.format(bucket)
10606a7d5d9Sfarshid                self.log.info("still waiting for deletion of {0} ...".format(data_file))
10706a7d5d9Sfarshid                time.sleep(2)
10806a7d5d9Sfarshid        return False
10906a7d5d9Sfarshid
11006a7d5d9Sfarshid
11106a7d5d9Sfarshid    def verify_data_files_deletion(self,
11206a7d5d9Sfarshid                                   bucket,
11306a7d5d9Sfarshid                                   remote_connection,
11406a7d5d9Sfarshid                                   rest):
11506a7d5d9Sfarshid        node = rest.get_nodes_self()
11606a7d5d9Sfarshid        for item in node.storage:
11706a7d5d9Sfarshid            #get the path
11806a7d5d9Sfarshid            data_file = '{0}-data'.format(bucket)
11906a7d5d9Sfarshid            if remote_connection.file_exists(item.path, data_file):
12006a7d5d9Sfarshid                return False
1213fdac395SKeith Batten        return True
122