def test_cannot_push_application_twice(self, context, test_sample_apps, sample_app, api_service_admin_client): """ <b>Description:</b> Tries to push the same application twice <b>Input data:</b> - Sample application that will be pushed twice <b>Expected results:</b> - The second push is not possible <b>Steps:</b> - Application is pushed - Application is pushed again in the test itself - The second push is blocked """ step("Check that pushing the same application again causes an error") log_fixture("sample_application: update manifest") p_a = PrepApp(test_sample_apps.tapng_python_app.filepath) manifest_params = { "type": TapApplicationType.PYTHON27, "name": sample_app.name } manifest_path = p_a.update_manifest(params=manifest_params) with pytest.raises(UnexpectedResponseError) as e: Application.push( context, app_path=test_sample_apps.tapng_python_app.filepath, name=sample_app.name, manifest_path=manifest_path, client=api_service_admin_client) assert self.EXPECTED_MESSAGE_WHEN_APP_PUSHED_TWICE in str(e)
def test_step_0_push_ws2kafka2hdfs(self, test_space, class_context): step("Clone and compile ingestion app sources") github_auth = config.github_credentials() ingestion_repo = AppSources.from_github(repo_name=TapGitHub.ws_kafka_hdfs, repo_owner=self.REPO_OWNER, gh_auth=github_auth) ws2kafka_path = os.path.join(ingestion_repo.path, TapGitHub.ws2kafka) kafka2hdfs_path = os.path.join(ingestion_repo.path, TapGitHub.kafka2hdfs) ingestion_repo.compile_gradle(working_directory=kafka2hdfs_path) postfix = str(int(time.time())) self.__class__.topic_name = "topic-{}".format(postfix) step("Push application ws2kafka") self.__class__.app_ws2kafka = Application.push(class_context, space_guid=test_space.guid, source_directory=ws2kafka_path, name="ws2kafka-{}".format(postfix), bound_services=(self.KAFKA_INSTANCE_NAME,)) step("Push application kafka2hdfs") self.__class__.app_kafka2hdfs = Application.push(class_context, space_guid=test_space.guid, source_directory=kafka2hdfs_path, name="kafka2hdfs-{}".format(postfix), bound_services=(self.KAFKA_INSTANCE_NAME, self.ZOOKEEPER_INSTANCE_NAME, self.HDFS_INSTANCE_NAME, self.KERBEROS_INSTANCE_NAME), env={"TOPICS": self.topic_name, "CONSUMER_GROUP": "group-{}".format(postfix)}) assert self.app_ws2kafka.is_started is True, "ws2kafka app is not started" assert self.app_kafka2hdfs.is_started is True, "kafka2hdfs app is not started"
def test_non_developer_cannot_manage_app(self, context, test_org, test_space, test_org_manager, test_org_manager_client): step("Push example app as admin") cf.cf_login(test_org.name, test_space.name) example_app_path = ApplicationPath.SAMPLE_PYTHON_APP test_app = Application.push(context, space_guid=test_space.guid, source_directory=example_app_path) apps = Application.cf_api_get_list_by_space(test_space.guid) assert test_app in apps step("Add user to space as manager") space_manager = test_org_manager space_manager_client = test_org_manager_client space_manager.api_add_to_space(space_guid=test_space.guid, org_guid=test_org.guid, roles=User.SPACE_ROLES["manager"]) step("Check that manager cannot stop app") assertions.assert_raises_http_exception(HttpStatus.CODE_FORBIDDEN, HttpStatus.MSG_FORBIDDEN, test_app.api_stop, client=space_manager_client) step("Check that manager cannot start app") assertions.assert_raises_http_exception(HttpStatus.CODE_FORBIDDEN, HttpStatus.MSG_FORBIDDEN, test_app.api_start, client=space_manager_client) step("Check that manager cannot delete app") assertions.assert_raises_http_exception(HttpStatus.CODE_FORBIDDEN, HttpStatus.MSG_FORBIDDEN, test_app.api_delete, client=space_manager_client) apps = Application.cf_api_get_list_by_space(test_space.guid) assert test_app in apps
def app_bound_psql(module_context, psql_instance, api_service_admin_client): log_fixture("psql_app: Download libraries") app_src = AppSources.from_local_path(ApplicationPath.SQL_API_EXAMPLE) p_a = PrepApp(app_src.path) log_fixture("psql_app: package sample application") if os.path.exists(ApplicationPath.SQL_API_EXAMPLE_TAR): gzipped_app_path = ApplicationPath.SQL_API_EXAMPLE_TAR else: gzipped_app_path = p_a.package_app(module_context) log_fixture("psql_app: update manifest") manifest_params = {"type": TapApplicationType.PYTHON27, "bindings": [psql_instance.name]} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("psql_app: push sample application") db_app = Application.push(module_context, app_path=gzipped_app_path, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) log_fixture("psql_app: Check the application is running") db_app.ensure_running() log_fixture("psq_app: Check the application is responding") db_app.ensure_responding() return db_app
def kafka2hdfs_app(class_context, kafka_instance, hdfs_instance, kerberos_instance, api_service_admin_client): log_fixture("kafka2hdfs: download libraries") ingestion_repo = AppSources.get_repository(repo_name=TapGitHub.ws_kafka_hdfs, repo_owner=TapGitHub.intel_data) kafka2hdfs_path = os.path.join(ingestion_repo.path, TapGitHub.kafka2hdfs) log_fixture("Package kafka2hdfs app") ingestion_repo.compile_gradle(working_directory=kafka2hdfs_path) build_path = os.path.join(kafka2hdfs_path, "deploy") ingestion_repo.run_build_sh(cwd=build_path, command="./pack.sh") app_path = os.path.join(build_path, "kafka2hdfs.tar") log_fixture("kafka2hdfs: update manifest") p_a = PrepApp(build_path) manifest_params = {"bindings": [kafka_instance.name, hdfs_instance.name, kerberos_instance.name]} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("kafka2hdfs: push application") app = Application.push(class_context, app_path=app_path, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) log_fixture("kafka2hdfs: Check the application is running") app.ensure_running() return app
def test_0_push_hdfs_hive_demo(self, dataset_target_uri, hdfs_instance, kerberos_instance, hive_instance, cups_sso_instance, login_to_cf, class_context): """ <b>Description:</b> Check that hdfs hive demo can run on platform. <b>Input data:</b> 1. hdfs hive demo sources 2. hdfs instance 3. kerberos instance 4. hive instance 5. cups sso instance <b>Expected results:</b> Test passes when hdfs hive demo is successfully pushed to platform and app has an url. <b>Steps:</b> 1. Get hdfs hive demo sources from repository. 2. Push hdfs hive demo to platform. 3. Check that pushed app has an url address. """ step("Get app sources") repo = AppSources.get_repository(repo_name=TapGitHub.hdfs_hive_demo, repo_owner=TapGitHub.intel_data) repo.compile_mvn() step("Push hdfs-hive-demo app to cf") self.__class__.hdfs_reader_app = Application.push(class_context, source_directory=repo.path, bound_services=[hdfs_instance.name, kerberos_instance.name, hive_instance.name, cups_sso_instance.name]) step("Check hdfs-hive-demo app has url") assert len(self.hdfs_reader_app.urls) == 1
def test_data_urls(session_context): """ Fixture with files from tests/fixtures/data_repo.py::DATA_FILES. They are accessible by url or filepath. Eg.: test_data_urls.test_transfer.url test_data_urls['test_transfer'].url test_data_urls.test_transfer.filepath """ if config.data_repo_url: return Urls(config.data_repo_url) else: log_fixture("data_repo: package sample application") p_a = PrepApp(DATA_REPO_PATH) if os.path.exists(DATA_REPO_TAR_PATH): gzipped_app_path = DATA_REPO_TAR_PATH else: gzipped_app_path = p_a.package_app(session_context) log_fixture("data_repo: update manifest") manifest_path = p_a.update_manifest(params={}) log_fixture("data_repo: Push data_repo application") app = Application.push(context=session_context, app_path=gzipped_app_path, name=p_a.app_name, manifest_path=manifest_path) log_fixture("data_repo: Check application is running") app.ensure_running() return Urls(app.urls[0])
def orientdb_app(cls, test_space, login_to_cf, orientdb_service, class_context): """SetUp: OrientDB API application creation.""" step("Push OrientDB Api application to cf.") app = Application.push(class_context, space_guid=test_space.guid, source_directory=ApplicationPath.ORIENTDB_API, bound_services=(orientdb_service.name, )) cls._API = OrientDbApi(app)
def test_non_developer_cannot_push_app(self, context, test_org, test_space, test_org_manager): step("Add user to space as manager") space_manager = test_org_manager space_manager.api_add_to_space(space_guid=test_space.guid, org_guid=test_org.guid, roles=User.SPACE_ROLES["manager"]) step("Login as this user") cf.cf_login(test_org.name, test_space.name, credentials=(space_manager.username, space_manager.password)) step("Check that manager cannot push app") example_app_path = ApplicationPath.SAMPLE_PYTHON_APP with pytest.raises(CommandExecutionException): Application.push(context, space_guid=test_space.guid, source_directory=example_app_path)
def org_space_app(self, context): step("Create test organization and space") test_org = Organization.api_create(context) test_space = Space.api_create(test_org) step("Login to cf targeting test org and test space") cf.cf_login(test_org.name, test_space.name) step("Push example app") example_app_path = ApplicationPath.SAMPLE_PYTHON_APP test_app = Application.push(context, space_guid=test_space.guid, source_directory=example_app_path) return test_org, test_space, test_app
def test_change_app_state_in_catalog_and_delete_it( self, context, test_sample_apps, api_service_admin_client): """ <b>Description:</b> Change the application state in catalog and later delete it <b>Input data:</b> - Path to application - Admin credentials <b>Expected results:</b> - Application state can be changed in catalog - Application state can be set back via api service <b>Steps:</b> - Prepare the application and push it - Make sure the application is running - Change the state of the application via catalog - Make sure the state has changed - Stop the application via api service client and remove it - Verify the application was removed """ log_fixture("sample_application: update manifest") p_a = PrepApp(test_sample_apps.tapng_python_app.filepath) manifest_params = {"type": TapApplicationType.PYTHON27} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("Push sample application and check it's running") application = Application.push( context, app_path=test_sample_apps.tapng_python_app.filepath, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) application.ensure_running() step("Check that the application has only one instance") instances = CatalogApplicationInstance.get_list_for_application( application_id=application.id) assert len(instances) == 1 updated_state = TapEntityState.FAILURE step("Update app state to {} using catalog api".format(updated_state)) catalog_api.update_instance(instance_id=instances[0].id, field_name="state", value=updated_state) step("Check that the app state was updated") app = Application.get(app_inst_id=application.id, client=api_service_admin_client) assert app.state == updated_state, "Application is not in the expected state. App state: {}".format( app.state) step("Check that the application can be deleted") application.delete() step("Check that application has been removed") apps = Application.get_list(client=api_service_admin_client) assert application not in apps
def psql_app(psql_instance): sql_api_sources = AppSources.from_github( repo_name=TapGitHub.sql_api_example, repo_owner=TapGitHub.intel_data, gh_auth=config.github_credentials()) context = Context() TestData.psql_app = Application.push(context=context, space_guid=TestData.test_space.guid, source_directory=sql_api_sources.path, bound_services=(psql_instance.name, )) return TestData.psql_app
def sample_application(self, class_context, test_sample_apps, api_service_admin_client): log_fixture("sample_application: update manifest") p_a = PrepApp(test_sample_apps[self.SAMPLE_APP].filepath) manifest_params = {"type" : self.APP_TYPE} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("Push sample application and check it's running") application = Application.push(class_context, app_path=test_sample_apps[self.SAMPLE_APP].filepath, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) application.ensure_running() return application
def setup_psql_api_app(cls, test_space, login_to_cf, postgres_instance, class_context): step("Get sql api app sources") sql_api_sources = AppSources.from_github( repo_name=TapGitHub.sql_api_example, repo_owner=TapGitHub.intel_data, gh_auth=config.github_credentials()) step("Push psql api app to cf") cls.psql_app = Application.push( class_context, space_guid=test_space.guid, source_directory=sql_api_sources.path, bound_services=(postgres_instance.name, ))
def test_0_push_dataset_reader_app(self, test_space, dataset_target_uri, hdfs_instance, kerberos_instance, login_to_cf, class_context): step("Get app sources") repo = AppSources.from_github(repo_name=TapGitHub.dataset_reader_sample, repo_owner=TapGitHub.trustedanalytics) repo.compile_mvn() step("Push dataset-reader app to cf") self.__class__.hdfs_reader_app = Application.push(class_context, space_guid=test_space.guid, source_directory=repo.path, bound_services=(hdfs_instance.name, kerberos_instance.name), env={"FILE": dataset_target_uri}) step("Check dataset reader app has url") assert len(self.hdfs_reader_app.urls) == 1
def test_app(self, context, test_space, instance): test_app_sources = AppSources.from_local_path( sources_directory=ApplicationPath.SAMPLE_JAVA_APP) step("Compile the sources") test_app_sources.compile_mvn() step("Push application to cf") application = Application.push( context, source_directory=ApplicationPath.SAMPLE_JAVA_APP, space_guid=test_space.guid, bound_services=(instance.name, )) step("Check the application is running") application.ensure_started() return application
def push_app_from_tar(context, app_path): archive_path = os.path.join(app_path, 'app.tar.gz') log_fixture('using existing archive to push app: {}'.format(archive_path)) app_name = generate_test_object_name(separator='') manifest_path = os.path.join(app_path, 'manifest.json') with open(manifest_path) as manifest_file: manifest = json.load(manifest_file) manifest['name'] = app_name return Application.push(context, app_path=archive_path, name=app_name, manifest=manifest, client=api_service_admin_client())
def sample_python_app(request, test_org, test_space): context = Context() log_fixture("sample_python_app: Push app to cf") cf.cf_login(test_org.name, test_space.name) app = Application.push(context=context, space_guid=test_space.guid, source_directory=ApplicationPath.SAMPLE_PYTHON_APP) def fin(): log_fixture("sample_python_app: Delete sample app") context.cleanup() request.addfinalizer(fin) return app
def sample_app(self, class_context, test_sample_apps, api_service_admin_client): log_fixture("sample_application: update manifest") p_a = PrepApp(test_sample_apps.tapng_python_app.filepath) manifest_params = {"type": TapApplicationType.PYTHON27} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("Push sample application") application = Application.push( class_context, app_path=test_sample_apps.tapng_python_app.filepath, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) application.ensure_running() return application
def test_1_push_app_to_cf(self, class_context, test_space, psql_instance, login_to_cf): step("Push application to cf") sql_api_sources = AppSources.get_repository( repo_name=TapGitHub.sql_api_example, repo_owner=TapGitHub.intel_data) self.__class__.psql_app = Application.push( context=class_context, source_directory=sql_api_sources.path, space_guid=test_space.guid, bound_services=(psql_instance.name, self.kerberos_instance.name)) step("Check the application is running") assertions.assert_equal_with_retry(True, self.psql_app.cf_api_app_is_running) step("Check that application is on the list") apps = Application.cf_api_get_list_by_space(test_space.guid) assert self.psql_app in apps
def test_cascade_app_delete(self, context, instance, admin_client): """ <b>Description:</b> Checks if cascade removal of an application removes bound service instance too. <b>Input data:</b> 1. Sample service instance. 2. Sample java application. 3. Admin client <b>Expected results:</b> The application and the service instance are deleted. <b>Steps:</b> 1. Push an application with bound service instance. 2. Verify the application is running. 3. Delete the application with cascade flag. 4. Verify the application is deleted. 5. Verify the service instance is deleted. """ step("Compile the app") test_app_sources = AppSources.from_local_path(sources_directory=ApplicationPath.SAMPLE_JAVA_APP) test_app_sources.compile_mvn() step("Package the app") p_a = PrepApp(ApplicationPath.SAMPLE_JAVA_APP) gzipped_app_path = p_a.package_app(context) step("Update manifest") manifest_params = {"type" : TapApplicationType.JAVA, "bindings" : instance.id} manifest_path = p_a.update_manifest(params=manifest_params) step("Push app to tap") app = Application.push(context, app_path=gzipped_app_path, name=p_a.app_name, manifest_path=manifest_path, client=admin_client) step("Check the application is running") app.ensure_running() app.api_delete(cascade=True) assertions.assert_not_in_by_id_with_retry(app.id, Application.get_list) assertions.assert_not_in_by_id_with_retry(instance.id, ServiceInstance.get_list)
def test_4_push_app(self, class_context, test_space, login_to_cf): """ <b>Description:</b> Push mongodb api application to the platform. <b>Input data:</b> 1. Mongodb api application <b>Expected results:</b> Test passes if mongodb api can be successfully pushed to the platform. <b>Steps:</b> 1. Push mongodb api application to the platform. """ step("Push application to cf") self.__class__.mongodb_app = Application.push(context=class_context, source_directory=ApplicationPath.MONGODB_API, space_guid=test_space.guid, bound_services=(self.mongodb_instance.name,))
def test_push_and_delete_application(self, context, test_sample_apps, api_service_admin_client): """ <b>Description:</b> Attempts to push application, stop it and then delete it. <b>Input data:</b> - Sample application - Admin credentials <b>Expected results:</b> - Application is successfully pushed to platform - Apllication is in running state - It's possible to stop the application - it's possible to remove the application - After removal application is no longer available <b>Steps:</b> - Sample application is downloaded - Manifest is updated with unique name - Application is pushed to platform using admin - It's verified that the application is in running state - Application is stopped and it's verified that the application has stopped - Remove the application and verify it's no longer available """ log_fixture("sample_application: update manifest") p_a = PrepApp(test_sample_apps[self.SAMPLE_APP].filepath) manifest_params = {"type" : self.APP_TYPE} manifest_path = p_a.update_manifest(params=manifest_params) step("Push sample application") application = Application.push(context, app_path=test_sample_apps[self.SAMPLE_APP].filepath, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) step("Check application is running") application.ensure_running() step("Stop application") application.stop() application.ensure_stopped() step("Delete application") application.delete() step("Check that application is not on the list") assertions.assert_not_in_with_retry(application, Application.get_list, client=api_service_admin_client)
def ws2kafka_app(class_context, kafka_instance, api_service_admin_client): log_fixture("ws2kafka: download libraries") ingestion_repo = AppSources.get_repository(repo_name=TapGitHub.ws_kafka_hdfs, repo_owner=TapGitHub.intel_data) ws2kafka_path = os.path.join(ingestion_repo.path, TapGitHub.ws2kafka) build_path = os.path.join(ws2kafka_path, "deploy") ingestion_repo.run_build_sh(cwd=build_path, command="./pack.sh") app_path = os.path.join(build_path, "ws2kafka.tar.gz") log_fixture("ws2kafka: update manifest") p_a = PrepApp(build_path) manifest_params = {"bindings": [kafka_instance.name]} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("ws2kafka: push application") app = Application.push(class_context, app_path=app_path, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) log_fixture("ws2kafka: Check the application is running") app.ensure_running() return app
def _check_user_can_do_app_flow(self, test_space, client, context): step("Push example application") example_app_path = ApplicationPath.SAMPLE_PYTHON_APP test_app = Application.push(context, space_guid=test_space.guid, source_directory=example_app_path) step("Check the application is running") assertions.assert_equal_with_retry(True, test_app.cf_api_app_is_running) step("Stop the application and check that it is stopped") test_app.api_stop(client=client) assertions.assert_equal_with_retry(False, test_app.cf_api_app_is_running) step("Start the application and check that it has started") test_app.api_start(client=client) assertions.assert_equal_with_retry(True, test_app.cf_api_app_is_running) step("Delete the application and check that it doesn't exist") test_app.api_delete(client=client) apps = Application.cf_api_get_list_by_space(test_space.guid) assert test_app not in apps
def sample_java_app(request, test_org, test_space): context = Context() test_app_sources = AppSources.from_local_path( sources_directory=ApplicationPath.SAMPLE_JAVA_APP) log_fixture("sample_java_app: Compile the sources") test_app_sources.compile_mvn() log_fixture("sample_java_app: Push app to cf") cf.cf_login(test_org.name, test_space.name) app = Application.push(context=context, space_guid=test_space.guid, source_directory=ApplicationPath.SAMPLE_JAVA_APP) app.ensure_started() log_fixture("Check the application is running") def fin(): log_fixture("sample_java_app: Delete sample app") context.cleanup() request.addfinalizer(fin) return app
def hbase_reader_app(class_context, kerberos_instance, api_service_admin_client): log_fixture("hbase_reader: download libraries") hbase_reader_repo = AppSources.get_repository(repo_name=TapGitHub.hbase_api_example, repo_owner=TapGitHub.intel_data) log_fixture("Package hbase_reader app") build_path = os.path.join(hbase_reader_repo.path, "deploy") hbase_reader_repo.run_build_sh(cwd=build_path, command="./pack.sh") app_path = os.path.join(build_path, "hbase-java-api-example-0.1.1.tar") log_fixture("hbase_reader: update manifest") p_a = PrepApp(build_path) manifest_params = {"bindings": [kerberos_instance.name]} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("hbase_reader: push application") app = Application.push(class_context, app_path=app_path, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) log_fixture("hbase_reader: Check the application is running") app.ensure_running() return app
def app_bound_orientdb(module_context, orientdb_instance, api_service_admin_client): log_fixture("orientdb_app: download libraries") app_src = AppSources.from_local_path(ApplicationPath.ORIENTDB_API) app_src.run_build_sh() log_fixture("orientdb_app: package sample application") p_a = PrepApp(app_src.path) gzipped_app_path = p_a.package_app(module_context) log_fixture("orientdb_app: update manifest") manifest_params = {"type": TapApplicationType.PYTHON27, "bindings": [orientdb_instance.name]} manifest_path = p_a.update_manifest(params=manifest_params) log_fixture("orientdb_app: push orientdb-api application") application = Application.push(module_context, app_path=gzipped_app_path, name=p_a.app_name, manifest_path=manifest_path, client=api_service_admin_client) log_fixture("orientdb_app: Ensure app is running") application.ensure_running() log_fixture("orientdb_app: Ensure app is responding") application.ensure_responding() return application
def test_user_can_do_app_flow(self, test_user_clients, role, context): """ <b>Description:</b> Checks if different type of users can do the application flow. <b>Input data:</b> 1. Username. 2. User password. 3. Sample java application. <b>Expected results:</b> Different users can do an application flow. <b>Steps:</b> 1. Push an application. 2. Verify is running. 3. Verify can be stopped. 4. Verify can be started. 5. Verify can be restarted. 6. Verify can be deleted. 7. Verify is doesn't exist. """ client = test_user_clients[role] step("Compile the app") test_app_sources = AppSources.from_local_path( sources_directory=ApplicationPath.SAMPLE_JAVA_APP) test_app_sources.compile_mvn() step("Package the app") p_a = PrepApp(ApplicationPath.SAMPLE_JAVA_APP) gzipped_app_path = p_a.package_app(context) step("Update manifest") manifest_params = {"type": TapApplicationType.JAVA} manifest_path = p_a.update_manifest(params=manifest_params) step("Push app to tap") app = Application.push(context, app_path=gzipped_app_path, name=p_a.app_name, manifest_path=manifest_path, client=client) step("Check the application is running") app.ensure_running() step("Stop the application and check that it is stopped") app.stop() app.ensure_stopped() step("Start the application and check that it has started") app.start() app.ensure_running() step("Restart the application and check that it's running") app.restart() app.ensure_running() step("Stop application before delete") app.stop() app.ensure_stopped() step("Delete the application and check that it doesn't exist") app.delete() assertions.assert_not_in_by_id_with_retry(app.id, Application.get_list)
def test_mqtt_demo(self, test_org, test_space, login_to_cf, class_context): step("Clone repository") mqtt_demo_sources = AppSources.from_github( repo_name=self.REPO_NAME, repo_owner=self.SOURCES_OWNER, gh_auth=config.github_credentials()) step("Compile the sources") mqtt_demo_sources.compile_mvn() step("Create required service instances.") ServiceInstance.api_create_with_plan_name( org_guid=test_org.guid, space_guid=test_space.guid, service_label=ServiceLabels.INFLUX_DB, name=self.INFLUX_INSTANCE_NAME, service_plan_name=ServicePlan.FREE) ServiceInstance.api_create_with_plan_name( org_guid=test_org.guid, space_guid=test_space.guid, service_label=ServiceLabels.MOSQUITTO, name=self.MQTT_INSTANCE_NAME, service_plan_name=ServicePlan.FREE) step("Push mqtt app to cf") mqtt_demo_app = Application.push( class_context, source_directory=mqtt_demo_sources.path, space_guid=test_space.guid) step("Retrieve credentials for mqtt service instance") self.credentials = mqtt_demo_app.get_credentials( service_name=ServiceLabels.MOSQUITTO) mqtt_port = self.credentials.get("port") assert mqtt_port is not None mqtt_username = self.credentials.get("username") assert mqtt_username is not None mqtt_pwd = self.credentials.get("password") assert mqtt_pwd is not None step("Connect to mqtt app with mqtt client") mqtt_client = mqtt.Client() mqtt_client.username_pw_set(mqtt_username, mqtt_pwd) mqtt_client.tls_set(self.SERVER_CERTIFICATE, tls_version=ssl.PROTOCOL_TLSv1_2) mqtt_server_address = mqtt_demo_app.urls[0] mqtt_client.connect(mqtt_server_address, int(mqtt_port), 20) with open(self.TEST_DATA_FILE) as f: expected_data = f.read().split("\n") step("Start reading logs") logs = subprocess.Popen(["cf", "logs", "mqtt-demo"], stdout=subprocess.PIPE) time.sleep(5) step("Send {0} data vectors to {1}:{2} on topic {3}".format( len(expected_data), mqtt_server_address, mqtt_port, self.MQTT_TOPIC_NAME)) for line in expected_data: mqtt_client.publish(self.MQTT_TOPIC_NAME, line) step("Stop reading logs. Retrieve vectors from log content.") grep = subprocess.Popen(["grep", "message:"], stdin=logs.stdout, stdout=subprocess.PIPE) logs.stdout.close() time.sleep(50) os.kill(logs.pid, signal.SIGTERM) cut = subprocess.Popen("cut -d ':' -f7 ", stdin=grep.stdout, stdout=subprocess.PIPE, shell=True) grep.stdout.close() step("Check that logs display all the vectors sent") log_result = cut.communicate()[0].decode().split("\n") log_result = [ item.strip() for item in log_result if item not in (" ", "") ] self.maxDiff = None # allows for full diff to be displayed assert log_result == expected_data, "Data in logs do not match sent data"