diff --git a/docker/run_full_export.py b/docker/run_full_export.py --- a/docker/run_full_export.py +++ b/docker/run_full_export.py @@ -25,14 +25,18 @@ MAVEN_INDEX_ARCHIVE = f"{MAVEN_INDEX_NAME}.gz" -def _download_indexes(work_dir: str, instance_url: str) -> None: +def _download_indexes(base_url: str, work_dir: str) -> None: """Download all required indexes from the .index/ directory of the specified instance. """ - logger.info("Downloading all required indexes") + if base_url.startswith('test://'): + logger.info("(Testing) Fake downloading required indexes") + return None - index_url = urljoin(instance_url, ".index/") + logger.info("Downloading required indexes") + + index_url = urljoin(base_url, ".index/") properties_name = f"{MAVEN_INDEX_NAME}.properties" properties_file = join(work_dir, properties_name) @@ -122,7 +126,7 @@ # Grab all the indexes # Only fetch the new ones, existing files won't be re-downloaded. - _download_indexes(work_dir, base_url) + _download_indexes(base_url, work_dir) # Extract indexes into a .fld file check_call(["/opt/extract_indexes.sh", work_dir]) diff --git a/scripts/requirements.txt b/scripts/requirements.txt deleted file mode 100644 --- a/scripts/requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -certifi==2021.5.30 -charset-normalizer==2.0.4 -docker==5.0.0 -idna==3.2 -pkg-resources==0.0.0 -requests==2.26.0 -six==1.16.0 -urllib3==1.26.6 -websocket-client==1.2.1 -click diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,2 @@ +publish/ +export/ diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,5 @@ +build-and-test: + ./build_and_test_image.sh + +test: + ./test_image.sh diff --git a/tests/build_and_test_image.sh b/tests/build_and_test_image.sh new file mode 100755 --- /dev/null +++ b/tests/build_and_test_image.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Copyright (C) 2021-2022 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +DOCKER_IMAGE="softwareheritage/maven-index-exporter" +LOG=test_docker_image.log + +# This script builds the docker image for maven-index-exporter, and +# executes it on a known set of indexes and checks the results in order +# to test the full tool chain. + +echo "Script started on `date +%Y%m%d_%H%M%S`." +echo "* Writing log to $LOG." + +# Find location of script directory +OLD_DIR=$(pwd) +REPO_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd $OLD_DIR + +# First clean up and remove any docker image with our own name +docker rmi $DOCKER_IMAGE >>$LOG 2>&1 +RET=$? +if [[ $RET -eq 0 ]]; then + echo "* Docker image [$DOCKER_IMAGE] deleted." +elif [[ $RET -eq 1 ]]; then + echo "* Docker image [$DOCKER_IMAGE] doesn't exist." +else + echo "Error when deleting docker image [$DOCKER_IMAGE]." +fi + +# Build the image and tag it as $DOCKER_IMAGE +cd $REPO_DIR/docker +echo "* Building docker image." +docker build . -t $DOCKER_IMAGE >>$LOG +RET=$? +if [[ $RET -eq 0 ]]; then + echo "PASS: docker build returned 0." +else + echo "FAIL: docker build returned $RET." + exit 20 +fi + +# Assert docker image has been created. +COUNT=$(docker images | grep -E "^$DOCKER_IMAGE\s" | wc -l) +if [[ $COUNT -eq 0 ]]; then + echo "FAIL: Docker image cannot be listed." + exit 10 +else + echo "PASS: Docker image is listed." +fi + +cd $OLD_DIR +./test_image.sh diff --git a/scripts/test_docker_image.sh b/tests/test_image.sh rename from scripts/test_docker_image.sh rename to tests/test_image.sh --- a/scripts/test_docker_image.sh +++ b/tests/test_image.sh @@ -1,63 +1,29 @@ #!/bin/bash -# Copyright (C) 2021 The Software Heritage developers +# Copyright (C) 2021-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -DOCKER_IMAGE="maven-index-exporter" +DOCKER_IMAGE="softwareheritage/maven-index-exporter" LOG=test_docker_image.log -# This script builds the docker image for maven-index-exporter, and -# executes it on a known set of indexes and checks the results in order -# to test the full tool chain. - -echo "Script started on `date +%Y%m%d_%H%M%S`." -echo "* Writing log to $LOG." - -# Find location of script directory -OLD_DIR=$(pwd) REPO_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) -cd $OLD_DIR - WORK_DIR=$REPO_DIR/tests/repository_test EXPORT_DIR=$WORK_DIR/export +PUBLISH_DIR=$REPO_DIR/tests/publish -# First clean up and remove any docker image with our own name -docker rmi $DOCKER_IMAGE >>$LOG 2>&1 -RET=$? -if [[ $RET -eq 0 ]]; then - echo "* Docker image [$DOCKER_IMAGE] deleted." -elif [[ $RET -eq 1 ]]; then - echo "* Docker image [$DOCKER_IMAGE] doesn't exist." -else - echo "Error when deleting docker image [$DOCKER_IMAGE]." -fi - - -# Build the image and tag it as $DOCKER_IMAGE -cd $REPO_DIR/docker -echo "* Building docker image." -docker build . -t $DOCKER_IMAGE --no-cache >>$LOG -RET=$? -if [[ $RET -eq 0 ]]; then - echo "PASS: docker build returned 0." -else - echo "FAIL: docker build returned $RET." - exit 20 -fi - -# Assert docker image has been created. -COUNT=$(docker images | grep -E "^$DOCKER_IMAGE\s" | wc -l) -if [[ $COUNT -eq 0 ]]; then - echo "FAIL: Docker image cannot be listed." - exit 10 -else - echo "PASS: Docker image is listed." -fi +# clean up publish directory +rm -rf $PUBLISH_DIR/* +mkdir -p $PUBLISH_DIR +# This will mock out the download part of the python code +export MVN_IDX_EXPORTER_BASE_URL='test://example.org' # Run the image on the maven indexes. -docker run -v $WORK_DIR:/work $DOCKER_IMAGE >>$LOG 2>&1 +docker run -v $WORK_DIR:/work \ + -v $PUBLISH_DIR:/publish \ + -e MVN_IDX_EXPORTER_BASE_URL \ + $DOCKER_IMAGE >>$LOG 2>&1 # Assert exported text files are there, with the correct content. EXPORT_FILE=$(ls $EXPORT_DIR/*.fld) @@ -117,5 +83,6 @@ fi # Cleanup -rm -rf $EXPORT_DIR +# clean up publish directory +rm -rf $PUBLISH_DIR/* cd $OLD_DIR