|
| 1 | +# Copyright © 2022 Cask Data, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | +# use this file except in compliance with the License. You may obtain a copy of |
| 5 | +# 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, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations under |
| 13 | +# the License. |
| 14 | + |
| 15 | +import io |
| 16 | +import json |
| 17 | +import os |
| 18 | +import requests |
| 19 | +import subprocess |
| 20 | +import xml.etree.ElementTree as ET |
| 21 | +import zipfile |
| 22 | +import shutil |
| 23 | +import sys |
| 24 | +import argparse |
| 25 | +import urllib.request |
| 26 | + |
| 27 | +def run_shell_command(cmd): |
| 28 | + process = subprocess.run(cmd.split(" "), stderr=subprocess.PIPE) |
| 29 | + if process.returncode != 0: |
| 30 | + print("Process completed with error: ", process.stderr) |
| 31 | + assert process.returncode == 0 |
| 32 | + |
| 33 | +# Parse command line optional arguments |
| 34 | +parser=argparse.ArgumentParser() |
| 35 | +parser.add_argument('--testRunner', help='TestRunner class to execute tests') |
| 36 | +parser.add_argument('--dependency', help='Path for additional dependencies to install', action='append', required=True) |
| 37 | +parser.add_argument('--module', help='Module for which tests need to be run', required=True) |
| 38 | +parser.add_argument('--framework', help='Pass this param if workflow is triggered from e2e framework repo') |
| 39 | +args=parser.parse_args() |
| 40 | + |
| 41 | +# Start CDAP sandbox |
| 42 | +print("Downloading CDAP sandbox") |
| 43 | +sandbox_url = "https://github.com/cdapio/cdap-build/releases/download/latest/cdap-sandbox-6.8.0-SNAPSHOT.zip" |
| 44 | +sandbox_dir = sandbox_url.split("/")[-1].split(".zip")[0] |
| 45 | +r = requests.get(sandbox_url) |
| 46 | +z = zipfile.ZipFile(io.BytesIO(r.content)) |
| 47 | +z.extractall("./sandbox") |
| 48 | + |
| 49 | +print("Installing gcs connector jar") |
| 50 | +gcs_jar_url = "https://storage.googleapis.com/hadoop-lib/gcs/gcs-connector-hadoop2-2.2.5.jar" |
| 51 | +gcs_jar_fname = f"sandbox/{sandbox_dir}/lib/gcs-connector-hadoop2-2.2.5.jar" |
| 52 | +urllib.request.urlretrieve(gcs_jar_url, gcs_jar_fname) |
| 53 | + |
| 54 | +print("Start the sandbox") |
| 55 | +run_shell_command(f"chmod +x sandbox/{sandbox_dir}/bin/cdap") |
| 56 | +my_env = os.environ.copy() |
| 57 | +my_env["_JAVA_OPTIONS"] = "-Xmx24G" |
| 58 | +sandbox_start_cmd = "sandbox/" + sandbox_dir + "/bin/cdap sandbox restart" |
| 59 | +process = subprocess.Popen(sandbox_start_cmd, shell=True, env=my_env) |
| 60 | +process.communicate() |
| 61 | +assert process.returncode == 0 |
| 62 | + |
| 63 | +e2e_test_module = args.module |
| 64 | + |
| 65 | +# Changedir into the plugin |
| 66 | +os.chdir("plugin") |
| 67 | + |
| 68 | +# Dependencies must be installed locally beforehand |
| 69 | +for dependency in args.dependency: |
| 70 | + print(f"Installing dependency: {dependency}") |
| 71 | + os.chdir(dependency) |
| 72 | + |
| 73 | + # Get plugin artifact name and version from pom.xml. |
| 74 | + root = ET.parse('pom.xml').getroot() |
| 75 | + plugin_name = root.find('{http://maven.apache.org/POM/4.0.0}artifactId').text |
| 76 | + plugin_version = root.find('{http://maven.apache.org/POM/4.0.0}version').text |
| 77 | + |
| 78 | + os.chdir('target') |
| 79 | + plugin_properties = {} |
| 80 | + plugin_parents = [] |
| 81 | + |
| 82 | + # Get plugin properties and parent from plugin json. |
| 83 | + with open(f'{plugin_name}-{plugin_version}.json') as f: |
| 84 | + obj = json.loads(f.read()) |
| 85 | + plugin_properties = obj['properties'] |
| 86 | + plugin_parents = obj['parents'] |
| 87 | + |
| 88 | + data = None |
| 89 | + with open(f'{plugin_name}-{plugin_version}.jar', 'rb') as f: |
| 90 | + data = f.read() |
| 91 | + |
| 92 | + # Install the plugin on the sandbox. |
| 93 | + print("Installing plugin") |
| 94 | + res=requests.post(f"http://localhost:11015/v3/namespaces/default/artifacts/{plugin_name}", headers={"Content-Type": "application/octet-stream", "Artifact-Extends": '/'.join(plugin_parents), "Artifact-Version": plugin_version}, data=data) |
| 95 | + assert res.ok or print(res.text) |
| 96 | + res=requests.put(f"http://localhost:11015/v3/namespaces/default/artifacts/{plugin_name}/versions/{plugin_version}/properties", json=plugin_properties) |
| 97 | + assert res.ok or print(res.text) |
| 98 | + os.chdir("../..") |
| 99 | + |
| 100 | +print(f"Using E2E test module: {e2e_test_module}") |
| 101 | + |
| 102 | +os.chdir("..") |
| 103 | + |
| 104 | +# Run e2e tests |
| 105 | +if args.framework: |
| 106 | + print("Preparing e2e framework") |
| 107 | + os.chdir("e2e") |
| 108 | + run_shell_command("mvn clean install") |
| 109 | + os.chdir("../plugin") |
| 110 | +else: |
| 111 | + os.chdir("plugin") |
| 112 | + run_shell_command("mvn dependency:purge-local-repository -DmanualInclude=io.cdap.tests.e2e:cdap-e2e-framework") |
| 113 | + |
| 114 | +print("Running e2e integration tests") |
| 115 | + |
| 116 | +testrunner_to_run = "" |
| 117 | +if args.testRunner: |
| 118 | + testrunner_to_run = args.testRunner |
| 119 | + |
| 120 | +try: |
| 121 | + os.chdir(e2e_test_module) |
| 122 | + if testrunner_to_run: |
| 123 | + print("TestRunner to run : " + testrunner_to_run) |
| 124 | + run_shell_command(f"mvn verify -P e2e-tests -DTEST_RUNNER={testrunner_to_run}") |
| 125 | + else: |
| 126 | + run_shell_command("mvn verify -P e2e-tests") |
| 127 | +except AssertionError as e: |
| 128 | + raise e |
| 129 | +finally: |
| 130 | + os.chdir("../..") |
| 131 | + cwd = os.getcwd() |
| 132 | + print("Copying sandbox logs to e2e-debug") |
| 133 | + shutil.copytree(cwd+"/sandbox/"+sandbox_dir+"/data/logs", cwd+"/plugin/"+e2e_test_module+"/target/e2e-debug/sandbox/data/logs") |
| 134 | + shutil.copytree(cwd+"/sandbox/"+sandbox_dir+"/logs", cwd+"/plugin/"+e2e_test_module+"/target/e2e-debug/sandbox/logs") |
0 commit comments