1from mc_bin_client import MemcachedClient
2import logger
3log = logger.Logger.get_logger()
4from couchbase.bucket import Bucket
5import couchbase.subdocument as SD
6
7
8class TestMemcachedClient():
9
10    def connection(self, client_ip, bucket_name, user,password, port=11210):
11        log.info("Bucket name for connection is ---- {0}, username -- {1}, ----- password -- {2}".format(bucket_name,user, \
12                                                                                                         password))
13        try:
14            mc = MemcachedClient(host=client_ip, port=port)
15            mc.sasl_auth_plain(user,password)
16            mc.bucket_select(bucket_name)
17            return mc, True
18        except Exception as e:
19            log.info( "Exception is from connection function {0}".format(e))
20            return False, False
21
22    def write_data(self,mc):
23        try:
24            prefix = "test-"
25            number_of_items = 10
26            keys = ["{0}-{1}".format(prefix, i) for i in range(0, number_of_items)]
27            for k in keys:
28                mc.set(k, 0, 0, str(k + "body"))
29            return True
30        except Exception as e:
31            log.info( "Exception is from write_data function {0}".format(e))
32            return False
33
34    def read_data(self,client_ip, mc,bucket_name):
35        try:
36            mc_temp, status = self.connection(client_ip, bucket_name,'Administrator','password')
37            self.write_data(mc_temp)
38            test = mc.get("test--0")
39            return True
40        except Exception as e:
41            log.info( "Exception is from read_data function {0}".format(e))
42            return False
43
44    def read_stats(self, mc):
45        try:
46            test = mc.stats('warmup')
47            return True
48        except Exception as e:
49            log.info( "Exception is {0}".format(e))
50            return False
51
52    def get_meta(self, client_ip, mc, bucket_name):
53        try:
54            mc_temp, status = self.connection(client_ip, bucket_name, 'Administrator', 'password')
55            self.write_data(mc_temp)
56            test = mc.getMeta("test--0")
57            return True
58        except Exception as e:
59            log.info( "Exception is from get_meata function {0}".format(e))
60            return False
61
62
63    def set_meta(self, client_ip, mc, bucket_name):
64        try:
65            mc_temp, status = self.connection(client_ip, bucket_name, 'Administrator', 'password')
66            self.write_data(mc_temp)
67            rc = mc_temp.getMeta("test--0")
68            cas = rc[4] + 1
69            rev_seqno = rc[3]
70            set_with_meta_resp = mc.setWithMeta("test--0", '123456789', 0, 0, 123, cas)
71            return True
72        except Exception as e:
73            log.info( "Exception is from set_meta function {0}".format(e))
74            return False
75
76
77class TestSDK():
78    def connection(self, client_ip, bucket_name, user, password):
79        log.info(
80            "Bucket name for connection is ---- {0}, username -- {1}, ----- password -- {2}".format(bucket_name, user, \
81                                                                                                    password))
82        result = False
83        connection_string = 'couchbase://' + client_ip + '/' + bucket_name + '?username=' + user + '&select_bucket=true'
84        log.info (" Value of connection string is - {0}".format(connection_string))
85        try:
86            cb = Bucket(connection_string, password=password)
87            if cb is not None:
88                result = True
89                return cb, result
90        except Exception, ex:
91            return result
92
93    def set_xattr(self, sdk_conn):
94        try:
95            k = "sdk_1"
96            sdk_conn.upsert(k, {})
97            sdk_conn.mutate_in(k, SD.upsert('my', {'value': 1}, xattr=True))
98            return True
99        except Exception as e:
100            log.info("Exception is from set_xattr function {0}".format(e))
101            return False
102
103    def get_xattr(self, client_ip, sdk_conn, bucket_name):
104        try:
105            temp_conn, result = self.connection(client_ip, bucket_name, 'Administrator', 'password')
106            self.set_xattr(temp_conn)
107            k = 'sdk_1'
108            rv = sdk_conn.lookup_in(k, SD.get('my', xattr=True))
109            return True
110        except Exception as e:
111            log.info("Exception is from get_xattr function {0}".format(e))
112            return False