1import unittest
2import uuid
3from TestInput import TestInputSingleton
4import logger
5import time
6import datetime
7from membase.api.rest_client import RestConnection
8from membase.helper.bucket_helper import BucketOperationHelper
9from remote.remote_util import RemoteMachineShellConnection
10
11class DeleteMembaseBuckets(unittest.TestCase):
12
13    servers = None
14    input = None
15    log = None
16
17    def setUp(self):
18        self.log = logger.Logger.get_logger()
19        self.input = TestInputSingleton.input
20        self.assertTrue(self.input, msg="input parameters missing...")
21        self.servers = self.input.servers
22        BucketOperationHelper.delete_all_buckets_or_assert(servers=self.servers, test_case=self)
23        self._log_start()
24
25    def tearDown(self):
26        BucketOperationHelper.delete_all_buckets_or_assert(servers=self.servers, test_case=self)
27        self._log_finish()
28
29    def _log_start(self):
30        try:
31            msg = "{0} : {1} started ".format(datetime.datetime.now(), self._testMethodName)
32            RestConnection(self.servers[0]).log_client_error(msg)
33        except:
34            pass
35
36
37    def _log_finish(self):
38        try:
39            msg = "{0} : {1} finished ".format(datetime.datetime.now(), self._testMethodName)
40            RestConnection(self.servers[0]).log_client_error(msg)
41        except:
42            pass
43
44    def test_default_moxi(self):
45        name = 'default'
46        for serverInfo in self.servers:
47            rest = RestConnection(serverInfo)
48            replicas = [0, 1, 2, 3]
49            for replicaNumber in replicas:
50                proxyPort = rest.get_nodes_self().moxi
51                rest.create_bucket(bucket=name,
52                                   ramQuotaMB=200,
53                                   replicaNumber=replicaNumber,
54                                   proxyPort=proxyPort)
55                remote = RemoteMachineShellConnection(serverInfo)
56
57                msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
58                self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
59                rest.delete_bucket(name)
60                msg = 'bucket "{0}" was not deleted even after waiting for two minutes'.format(name)
61                self.assertTrue(BucketOperationHelper.wait_for_bucket_deletion(name, rest, timeout_in_seconds=30), msg=msg)
62                msg = 'bucket {0} data files are not deleted after bucket deleted from membase'.format(name)
63                self.assertTrue(
64                    self.wait_for_data_files_deletion(name,
65                                                      remote_connection=remote,
66                                                      rest=rest, timeout_in_seconds=20), msg=msg)
67
68    def test_non_default_moxi(self):
69        name = 'new-bucket-{0}'.format(uuid.uuid4())
70        for serverInfo in self.servers:
71            replicas = [0, 1, 2, 3]
72            for replicaNumber in replicas:
73                rest = RestConnection(serverInfo)
74                proxyPort = rest.get_nodes_self().moxi + 2000
75                rest.create_bucket(bucket=name,
76                                   ramQuotaMB=200,
77                                   replicaNumber=replicaNumber,
78                                   proxyPort=proxyPort)
79                remote = RemoteMachineShellConnection(serverInfo)
80                msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
81                self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
82                rest.delete_bucket(name)
83                msg = 'bucket "{0}" was not deleted even after waiting for 30 seconds'.format(name)
84                self.assertTrue(BucketOperationHelper.wait_for_bucket_deletion(name, rest, timeout_in_seconds=30), msg=msg)
85                msg = 'bucket {0} data files are not deleted after bucket deleted from membase'.format(name)
86                self.assertTrue(
87                    self.wait_for_data_files_deletion(name,
88                                                      remote_connection=remote,
89                                                      rest=rest, timeout_in_seconds=20), msg=msg)
90                BucketOperationHelper.delete_bucket_or_assert(serverInfo, name, self)
91
92
93    #TODO: move these methods to a helper class
94    def wait_for_data_files_deletion(self,
95                                     bucket,
96                                     remote_connection,
97                                     rest,
98                                     timeout_in_seconds=120):
99        self.log.info('waiting for bucket data files deletion from the disk ....')
100        start = time.time()
101        while (time.time() - start) <= timeout_in_seconds:
102            if self.verify_data_files_deletion(bucket, remote_connection, rest):
103                return True
104            else:
105                data_file = '{0}-data'.format(bucket)
106                self.log.info("still waiting for deletion of {0} ...".format(data_file))
107                time.sleep(2)
108        return False
109
110
111    def verify_data_files_deletion(self,
112                                   bucket,
113                                   remote_connection,
114                                   rest):
115        node = rest.get_nodes_self()
116        for item in node.storage:
117            #get the path
118            data_file = '{0}-data'.format(bucket)
119            if remote_connection.file_exists(item.path, data_file):
120                return False
121        return True
122