1import unittest
2import uuid
3import TestInput
4import logger
5import datetime
6from membase.api.rest_client import RestConnection
7from membase.helper.bucket_helper import BucketOperationHelper
8
9
10class RecreateMembaseBuckets(unittest.TestCase):
11    version = None
12    servers = None
13    log = None
14    input = TestInput.TestInput
15
16    #as part of the setup let's delete all the existing buckets
17    def setUp(self):
18        self.log = logger.Logger.get_logger()
19        self.input = TestInput.TestInputSingleton.input
20        self.assertTrue(self.input, msg="input parameters missing...")
21        self.servers = self.input.servers
22        BucketOperationHelper.delete_all_buckets_or_assert(self.servers, test_case=self)
23        self._log_start()
24
25    def tearDown(self):
26        BucketOperationHelper.delete_all_buckets_or_assert(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    #create bucket-load some keys-delete bucket-recreate bucket
45    def test_default_moxi(self):
46        name = 'default'
47        serverInfo = self.servers[0]
48        if serverInfo.ip != "":
49            rest = RestConnection(serverInfo)
50            replicaNumber = 1
51            proxyPort = rest.get_nodes_self().moxi
52            rest.create_bucket(bucket=name,
53                               ramQuotaMB=200,
54                               replicaNumber=replicaNumber,
55                               proxyPort=proxyPort)
56            msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
57            self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
58            ready = BucketOperationHelper.wait_for_memcached(serverInfo, name)
59            self.assertTrue(ready, "wait_for_memcached failed")
60            inserted_keys = BucketOperationHelper.load_some_data(serverInfo, 1, name)
61            self.assertTrue(inserted_keys, 'unable to insert any key to memcached')
62            verified = BucketOperationHelper.verify_data(serverInfo, inserted_keys, True, False, self, bucket=name)
63            self.assertTrue(verified, msg='verified all the keys stored')
64            #verify keys
65            rest.delete_bucket(name)
66            msg = 'bucket "{0}" was not deleted even after waiting for two minutes'.format(name)
67            self.assertTrue(BucketOperationHelper.wait_for_bucket_deletion(name, rest, timeout_in_seconds=60), msg=msg)
68
69            rest.create_bucket(bucket=name,
70                               ramQuotaMB=200,
71                               replicaNumber=replicaNumber,
72                               proxyPort=proxyPort)
73            msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
74            self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
75            BucketOperationHelper.wait_for_memcached(serverInfo, name)
76            #now let's recreate the bucket
77            self.log.info('recreated the default bucket...')
78            #loop over the keys make sure they dont exist
79            self.assertTrue(BucketOperationHelper.keys_dont_exist(serverInfo, inserted_keys, name),
80                            msg='at least one key found in the bucket')
81
82    def test_default_dedicated(self):
83        name = 'recreate-non-default-{0}'.format(uuid.uuid4())
84        serverInfo = self.servers[0]
85        if serverInfo.ip != "":
86            rest = RestConnection(serverInfo)
87            replicaNumber = 1
88            proxyPort = rest.get_nodes_self().memcached + 2000
89            rest.create_bucket(bucket=name,
90                               ramQuotaMB=200,
91                               replicaNumber=replicaNumber,
92                               proxyPort=proxyPort)
93            msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
94            self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
95            ready = BucketOperationHelper.wait_for_memcached(serverInfo, name)
96            self.assertTrue(ready, "wait_for_memcached failed")
97            inserted_keys = BucketOperationHelper.load_some_data(serverInfo, 1, name)
98            self.assertTrue(inserted_keys, 'unable to insert any key to memcached')
99            verified = BucketOperationHelper.verify_data(serverInfo, inserted_keys, True, False, self, bucket=name)
100            self.assertTrue(verified, msg='verified all the keys stored')
101            #verify keys
102            rest.delete_bucket(name)
103            msg = 'bucket "{0}" was not deleted even after waiting for two minutes'.format(name)
104            self.assertTrue(BucketOperationHelper.wait_for_bucket_deletion(name, rest, timeout_in_seconds=60), msg=msg)
105
106            rest.create_bucket(bucket=name,
107                               ramQuotaMB=200,
108                               replicaNumber=replicaNumber,
109                               proxyPort=proxyPort)
110            msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
111            self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
112            ready = BucketOperationHelper.wait_for_memcached(serverInfo, name)
113            self.assertTrue(ready, "wait_for_memcached failed")
114            #now let's recreate the bucket
115            self.log.info('recreated the default bucket...')
116            #loop over the keys make sure they dont exist
117            self.assertTrue(BucketOperationHelper.keys_dont_exist(serverInfo, inserted_keys, name),
118                            msg='at least one key found in the bucket')
119
120
121    def test_default_moxi_sasl(self):
122        name = 'new-bucket-{0}'.format(uuid.uuid4())
123        serverInfo = self.servers[0]
124        if serverInfo.ip != "":
125            rest = RestConnection(serverInfo)
126            replicaNumber = 1
127            proxyPort = rest.get_nodes_self().moxi
128            rest.create_bucket(bucket=name,
129                               ramQuotaMB=200,
130                               replicaNumber=replicaNumber,
131                               proxyPort=proxyPort,
132                               authType="sasl",
133                               saslPassword='password')
134            msg = 'create_bucket succeeded but bucket {0} does not exist'.format(name)
135            self.assertTrue(BucketOperationHelper.wait_for_bucket_creation(name, rest), msg=msg)
136            ready = BucketOperationHelper.wait_for_memcached(serverInfo, name)
137            self.assertTrue(ready, "wait_for_memcached failed")
138            rest.delete_bucket(name)
139            msg = 'bucket "{0}" was not deleted even after waiting for two minutes'.format(name)
140            self.assertTrue(BucketOperationHelper.wait_for_bucket_deletion(name, rest, timeout_in_seconds=30), msg=msg)