|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import click |
| 16 | +import contextlib |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import pathlib |
| 20 | + |
| 21 | +from fireci import ci_command |
| 22 | +from fireci import gradle |
| 23 | + |
| 24 | +_logger = logging.getLogger('fireci.fireperf') |
| 25 | + |
| 26 | + |
| 27 | +@click.option( |
| 28 | + '--plugin_repo_dir', |
| 29 | + help='The location of the fireperf plugin repository.', |
| 30 | + required=True, |
| 31 | +) |
| 32 | +@click.option( |
| 33 | + '--target_environment', |
| 34 | + type=click.Choice(['prod', 'autopush'], case_sensitive=False), |
| 35 | + help='The target environment fireperf is built for.', |
| 36 | + required=True, |
| 37 | +) |
| 38 | +@ci_command() |
| 39 | +def fireperf_e2e_test(target_environment, plugin_repo_dir): |
| 40 | + """Run Firebase Performance end to end test.""" |
| 41 | + |
| 42 | + _logger.info('Building fireperf plugin ...') |
| 43 | + with chdir(plugin_repo_dir): |
| 44 | + build_plugin_task = ':firebase-performance:performance-gradle:publishToMavenLocal' |
| 45 | + gradle.run(build_plugin_task, gradle.P('publishMode', 'SNAPSHOT')) |
| 46 | + |
| 47 | + version = _find_fireperf_plugin_version() |
| 48 | + _logger.info(f'Setting environment variable: FIREBASE_PERF_PLUGIN_VERSION={version} ...') |
| 49 | + os.environ['FIREBASE_PERF_PLUGIN_VERSION'] = version |
| 50 | + |
| 51 | + fireperf_e2e_test_gradle_command = [ |
| 52 | + '--build-cache', |
| 53 | + '--parallel', |
| 54 | + '--continue', |
| 55 | + ':firebase-perf:e2e-app:deviceCheck' |
| 56 | + ] |
| 57 | + if target_environment == 'autopush': |
| 58 | + fireperf_e2e_test_gradle_command += [gradle.P('fireperfBuildForAutopush', 'true')] |
| 59 | + _logger.info(f'Running fireperf e2e test with target environment: {target_environment} ...') |
| 60 | + gradle.run(*fireperf_e2e_test_gradle_command) |
| 61 | + |
| 62 | + |
| 63 | +@contextlib.contextmanager |
| 64 | +def chdir(directory): |
| 65 | + """Change working dir to `directory` and restore to original afterwards.""" |
| 66 | + _logger.debug(f'Changing directory to: {directory} ...') |
| 67 | + original_dir = os.getcwd() |
| 68 | + os.chdir(directory) |
| 69 | + try: |
| 70 | + yield |
| 71 | + finally: |
| 72 | + _logger.debug(f'Restoring directory to: {original_dir} ...') |
| 73 | + os.chdir(original_dir) |
| 74 | + |
| 75 | + |
| 76 | +def _find_fireperf_plugin_version(): |
| 77 | + local_maven_repo_dir = pathlib.Path.home().joinpath('.m2', 'repository') |
| 78 | + artifacts_path = local_maven_repo_dir.joinpath('com', 'google', 'firebase', 'perf-plugin') |
| 79 | + versions = [pom.parent.name for pom in artifacts_path.rglob('*.pom')] |
| 80 | + snapshots = list(filter(lambda v: v.endswith('-SNAPSHOT'), versions)) |
| 81 | + |
| 82 | + if snapshots: |
| 83 | + return snapshots[0] |
| 84 | + else: |
| 85 | + raise click.ClickException('Cannot find a snapshot version in local maven repo.') |
0 commit comments