def register_user(self, target, db, name, password, channels=list(), roles=list()): data = { "name": name, "password": password, "admin_channels": channels, "admin_roles": roles } resp = requests.put("{0}/{1}/_user/{2}".format(self.admin_url, db, name), headers=self._headers, timeout=settings.HTTP_REQ_TIMEOUT, data=json.dumps(data)) log.info("PUT {}".format(resp.url)) resp.raise_for_status() return User(target, db, name, password, channels)
def rest_scan(sync_gateway, db, online, num_docs, user_name, channels): # Missing ADMIN # TODO: GET /{db}/_session/{session-id} # TODO: POST /{db}/_session # TODO: DELETE /{db}/_session/{session-id} # TODO: DELETE /{db}/_user/{name}/_session/{session-id} # TODO: DELETE /{db}/_user/{name}/_session # TODO: DELETE /{db}/_user/{name} # TODO: POST /{db}/_role/ # TODO: DELETE /{db}/_role/{name} # Missing REST # TODO: POST /{db}/_all_docs # TODO: DELETE /{db}/{doc} # TODO: PUT /{db}/{doc}/{attachment} # TODO: GET /{db}/{doc}/{attachment} # Missing Local Document # TODO: DELETE /{db}/{local-doc-id} # Missing Authentication # TODO: POST /{db}/_facebook_token admin = Admin(sync_gateway=sync_gateway) error_responses = list() # PUT /{db}/_role/{name} try: admin.create_role(db=db, name="radio_stations", channels=["HWOD", "KDWB"]) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db}/_role try: roles = admin.get_roles(db=db) log_info(roles) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db}/_role/{name} try: role = admin.get_role(db=db, name="radio_stations") log_info(role) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # PUT /{db}/_user/{name} try: user = admin.register_user(target=sync_gateway, db=db, name=user_name, password="******", channels=channels) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db}/_user try: users_info = admin.get_users_info(db=db) log_info(users_info) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db}/_user/{name} try: user_info = admin.get_user_info(db=db, name=user_name) log_info(user_info) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db} try: db_info = admin.get_db_info(db=db) if not online: assert db_info["state"] == "Offline" else: assert db_info["state"] == "Online" log_info(db_info) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # Create dummy user to hit endpoint if offline, user creation above will fail if not online: user = User(target=sync_gateway, db=db, name=user_name, password="******", channels=channels) # PUT /{db}/{name} add_docs_errors = user.add_docs(num_docs=num_docs) error_responses.extend(add_docs_errors) # POST /{db}/_bulk_docs bulk_doc_errors = user.add_docs(num_docs=num_docs, bulk=True) error_responses.extend(bulk_doc_errors) # POST /{db}/ for i in range(num_docs): try: user.add_doc() except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db}/{name} # PUT /{db}/{name} if online: update_docs_errors = user.update_docs(num_revs_per_doc=1) error_responses.extend(update_docs_errors) else: try: # Try to hit the GET enpoint for "test-id" user.update_doc("test-id") except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # PUT /{db}/{local-doc-id} local_doc_id = uuid.uuid4() try: doc = user.add_doc("_local/{}".format(local_doc_id), content={"message": "I should not be replicated"}) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db}/{local-doc-id} try: doc = user.get_doc("_local/{}".format(local_doc_id)) assert doc["content"]["message"] == "I should not be replicated" except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # GET /{db}/_all_docs try: all_docs_result = user.get_all_docs() # num_docs /{db}/{doc} PUT + num_docs /{db}/_bulk_docs + num_docs POST /{db}/ assert len(all_docs_result["rows"]) == num_docs * 3 except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # POST /{db}/_bulk_get try: doc_ids = list(user.cache.keys()) first_ten_ids = doc_ids[:10] first_ten = user.get_docs(first_ten_ids) assert len(first_ten) == 10 except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) # wait for changes time.sleep(2) # GET /{db}/_changes try: user.get_changes() # If successful, verify the _changes feed verify_changes(user, expected_num_docs=num_docs * 3, expected_num_revisions=1, expected_docs=user.cache) except HTTPError as e: log_info((e.response.url, e.response.status_code)) error_responses.append((e.response.url, e.response.status_code)) return error_responses
def test_selective_db_delete_and_replication_lifecycle(): try: cluster_conf = os.environ["CLUSTER_CONFIG"] except KeyError: log_info("Make sure CLUSTER_CONFIG is defined and pointing to the configuration you would like to provision") raise KeyError("CLUSTER_CONFIG not defined. Unable to provision cluster.") should_reinstall = True apk_path = os.environ["P2P_APP"] activity = "com.couchbase.ui.maven/com.couchbase.ui.MainActivity" db_name = "db" device_defs = [ {"target_device": "emulator-5554", "local_port": 10000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5556", "local_port": 11000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5558", "local_port": 12000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5560", "local_port": 13000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5562", "local_port": 14000, "apk_path": apk_path, "activity": activity}, ] listeners = parallel_install(cluster_conf, device_defs, should_reinstall) time.sleep(2) emu_1 = listeners["emulator-5554"] emu_2 = listeners["emulator-5556"] emu_3 = listeners["emulator-5558"] emu_4 = listeners["emulator-5560"] emu_5 = listeners["emulator-5562"] all_emus = [emu_1, emu_2, emu_3, emu_4, emu_5] for emu in all_emus: emu.verify_launched() # Add docs on master device emu_1_pusher = User(target=emu_1, db=db_name, name="emu1_doc_pusher", password="******", channels=["ABC"]) emu_1_pusher.add_docs(600, bulk=True) emu_1_pusher.add_design_doc( doc_id="view1", content={ "views": { "dt1": { "map": "function(doc) {if (doc.type == \'dt1\') {emit(doc._id, doc);}}" }, "filters": { "dt1": "function(doc) {if (doc.type == \'dt1\') {return true} return false}" } } } ) # Start all replication targets = [emu_2, emu_3, emu_4, emu_5] start_pull_replications(db_name, emu_1, targets) start_push_replications(db_name, emu_1, targets) # Wait for all docs to push to emulators time.sleep(30) # Assert each endpoint has 601 docs for emu in all_emus: assert emu.get_num_docs(db_name) == 601 # Stop all replication stop_pull_replications(db_name, emu_1, targets) stop_push_replications(db_name, emu_1, targets) # Wait for all replications to stop time.sleep(30) # Delete dbs on master and first slave emu_1.delete_db(db_name) emu_2.delete_db(db_name) # TODO Verify db is deleted time.sleep(2) # Add docs to the reset of the slaves emu_3_pusher = User(target=emu_3, db=db_name, name="emu3_doc_pusher", password="******", channels=["ABC"]) emu_4_pusher = User(target=emu_4, db=db_name, name="emu4_doc_pusher", password="******", channels=["ABC"]) emu_5_pusher = User(target=emu_5, db=db_name, name="emu5_doc_pusher", password="******", channels=["ABC"]) emu_3_pusher.add_docs(20) emu_4_pusher.add_docs(20) emu_5_pusher.add_docs(20) time.sleep(2) # TODO Verify 3,4,5 have 621 for emu in [emu_3, emu_4, emu_5]: assert emu.get_num_docs(db_name) == 621 # Create dbs on master and first slave emu_1.create_db(db_name) emu_2.create_db(db_name) # TODO Verify db has been created, and doc count should be 0 time.sleep(5) # Start all replication start_pull_replications(db_name, emu_1, targets) start_push_replications(db_name, emu_1, targets) time.sleep(45) emus = [emu_1, emu_2, emu_3, emu_4, emu_5] for emu in emus: doc_num = emu.get_num_docs(db_name) log.info("emu: {} doc_num: {}".format(emu.target_device, doc_num)) assert doc_num == 661
def test_replication_unstable_network(): try: cluster_conf = os.environ["CLUSTER_CONFIG"] except KeyError: log_info("Make sure CLUSTER_CONFIG is defined and pointing to the configuration you would like to provision") raise KeyError("CLUSTER_CONFIG not defined. Unable to provision cluster.") should_reinstall = False apk_path = os.environ["P2P_APP"] activity = "com.couchbase.ui.maven/com.couchbase.ui.MainActivity" db_name = os.environ["P2P_APP_DB"] device_defs = [ {"target": "06c455850b3fa11e", "local_port": None, "apk_path": apk_path, "activity": activity}, {"target": "emulator-5554", "local_port": 10000, "apk_path": apk_path, "activity": activity}, {"target": "emulator-5556", "local_port": 11000, "apk_path": apk_path, "activity": activity}, {"target": "emulator-5558", "local_port": 12000, "apk_path": apk_path, "activity": activity}, {"target": "emulator-5560", "local_port": 13000, "apk_path": apk_path, "activity": activity}, {"target": "emulator-5562", "local_port": 14000, "apk_path": apk_path, "activity": activity}, {"target": "emulator-5564", "local_port": 15000, "apk_path": apk_path, "activity": activity}, ] listeners = parallel_install(cluster_conf, device_defs, should_reinstall) dev = listeners["06c455850b3fa11e"] emu_1 = listeners["emulator-5554"] emu_2 = listeners["emulator-5556"] emu_3 = listeners["emulator-5558"] emu_4 = listeners["emulator-5560"] emu_5 = listeners["emulator-5562"] emu_6 = listeners["emulator-5564"] emus = [emu_1, emu_2, emu_3, emu_4, emu_5, emu_6] dev.verify_launched() for emu in emus: emu.verify_launched() log.info("Starting push replications ...") start_push_replications(db_name, dev, emus) log.info("Starting pull replications ...") start_pull_replications(db_name, dev, emus) log.info("Adding 100 docs to dev") dev_pusher = User(target=dev, db=db_name, name="emu1_doc_pusher", password="******", channels=["ABC"]) dev_pusher.add_docs(100, bulk=True) time.sleep(10) # Assert each endpoint has 100 docs assert dev.get_num_docs(db_name) == 100 for emu in emus: assert emu.get_num_docs(db_name) == 100 # Create docs on targets emu_1_pusher = User(target=emu_1, db=db_name, name="emu1_doc_pusher", password="******", channels=["ABC"]) emu_2_pusher = User(target=emu_2, db=db_name, name="emu2_doc_pusher", password="******", channels=["ABC"]) emu_3_pusher = User(target=emu_3, db=db_name, name="emu3_doc_pusher", password="******", channels=["ABC"]) emu_4_pusher = User(target=emu_4, db=db_name, name="emu4_doc_pusher", password="******", channels=["ABC"]) emu_5_pusher = User(target=emu_5, db=db_name, name="emu5_doc_pusher", password="******", channels=["ABC"]) emu_6_pusher = User(target=emu_6, db=db_name, name="emu6_doc_pusher", password="******", channels=["ABC"]) emu_1_pusher.add_docs(10) emu_2_pusher.add_docs(10) emu_3_pusher.add_docs(10) emu_4_pusher.add_docs(10) emu_5_pusher.add_docs(10) emu_6_pusher.add_docs(10) time.sleep(5) # Assert each endpoint has 140 docs assert dev.get_num_docs(db_name) == 160 for emu in emus: assert emu.get_num_docs(db_name) == 160
def test_selective_db_delete_and_replication_lifecycle(): try: cluster_conf = os.environ["CLUSTER_CONFIG"] except KeyError: log_info("Make sure CLUSTER_CONFIG is defined and pointing to the configuration you would like to provision") raise KeyError("CLUSTER_CONFIG not defined. Unable to provision cluster.") should_reinstall = True apk_path = os.environ["P2P_APP"] activity = "com.couchbase.ui.maven/com.couchbase.ui.MainActivity" db_name = "db" device_defs = [ {"target_device": "emulator-5554", "local_port": 10000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5556", "local_port": 11000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5558", "local_port": 12000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5560", "local_port": 13000, "apk_path": apk_path, "activity": activity}, {"target_device": "emulator-5562", "local_port": 14000, "apk_path": apk_path, "activity": activity}, ] listeners = parallel_install(cluster_conf, device_defs, should_reinstall) time.sleep(2) emu_1 = listeners["emulator-5554"] emu_2 = listeners["emulator-5556"] emu_3 = listeners["emulator-5558"] emu_4 = listeners["emulator-5560"] emu_5 = listeners["emulator-5562"] all_emus = [emu_1, emu_2, emu_3, emu_4, emu_5] for emu in all_emus: emu.verify_launched() # Add docs on master device emu_1_pusher = User(target=emu_1, db=db_name, name="emu1_doc_pusher", password="******", channels=["ABC"]) emu_1_pusher.add_docs(600, bulk=True) emu_1_pusher.add_design_doc( doc_id="view1", content={ "views": { "dt1": {"map": "function(doc) {if (doc.type == 'dt1') {emit(doc._id, doc);}}"}, "filters": {"dt1": "function(doc) {if (doc.type == 'dt1') {return true} return false}"}, } }, ) # Start all replication targets = [emu_2, emu_3, emu_4, emu_5] start_pull_replications(db_name, emu_1, targets) start_push_replications(db_name, emu_1, targets) # Wait for all docs to push to emulators time.sleep(30) # Assert each endpoint has 601 docs for emu in all_emus: assert emu.get_num_docs(db_name) == 601 # Stop all replication stop_pull_replications(db_name, emu_1, targets) stop_push_replications(db_name, emu_1, targets) # Wait for all replications to stop time.sleep(30) # Delete dbs on master and first slave emu_1.delete_db(db_name) emu_2.delete_db(db_name) # TODO Verify db is deleted time.sleep(2) # Add docs to the reset of the slaves emu_3_pusher = User(target=emu_3, db=db_name, name="emu3_doc_pusher", password="******", channels=["ABC"]) emu_4_pusher = User(target=emu_4, db=db_name, name="emu4_doc_pusher", password="******", channels=["ABC"]) emu_5_pusher = User(target=emu_5, db=db_name, name="emu5_doc_pusher", password="******", channels=["ABC"]) emu_3_pusher.add_docs(20) emu_4_pusher.add_docs(20) emu_5_pusher.add_docs(20) time.sleep(2) # TODO Verify 3,4,5 have 621 for emu in [emu_3, emu_4, emu_5]: assert emu.get_num_docs(db_name) == 621 # Create dbs on master and first slave emu_1.create_db(db_name) emu_2.create_db(db_name) # TODO Verify db has been created, and doc count should be 0 time.sleep(5) # Start all replication start_pull_replications(db_name, emu_1, targets) start_push_replications(db_name, emu_1, targets) time.sleep(45) emus = [emu_1, emu_2, emu_3, emu_4, emu_5] for emu in emus: doc_num = emu.get_num_docs(db_name) log.info("emu: {} doc_num: {}".format(emu.target_device, doc_num)) assert doc_num == 661