Skip to content

Commit

Permalink
chore: bazelified python services are tested for missing modules
Browse files Browse the repository at this point in the history
Signed-off-by: Krisztián Varga <krisztian.varga@tngtech.com>
  • Loading branch information
vktng committed Apr 20, 2022
1 parent 2c297fd commit 301e1ce
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions bazel/scripts/test_bazel_python_import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env bash

################################################################################
# Copyright 2022 The Magma Authors.

# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################

set -euo pipefail

###############################################################################
# FUNCTION DECLARATIONS
###############################################################################

help() {
echo "Tests all python services for missing imports or the ones found in"
echo "the specified directory (recursively), or one single service if a"
echo "service name is provided."
echo "Usage:"
echo " $(basename "$0") # test all python services in the magma repository"
echo " $(basename "$0") path_to_services_directory/"
echo " $(basename "$0") path_to_services_directory:service_name"
exit 1
}

collect_services() {
if [[ "${SERVICE_PATH}" == *":"* ]];
then
echo "Single service specified:"
SERVICES=( "${SERVICE_PATH}" )
else
echo "Multiple services specified:"
mapfile -t SERVICES < <(bazel query "attr(main, main.py, kind(py_binary, //${SERVICE_PATH}...))")
fi
if [[ "${#SERVICES[@]}" -eq 0 ]];
then
echo "ERROR: No services found."
help
exit 1
fi
for SERVICE in "${SERVICES[@]}"
do
echo "${SERVICE}"
done
}

###############################################################################
# SCRIPT SECTION
###############################################################################

SERVICE_PATH="${1:-}"

echo "Collecting services"
declare -a SERVICES
LOGGING_FILE="/tmp/test_import_logging.log"
NUM_SUCCESS=0

collect_services

for SERVICE in "${SERVICES[@]}"
do
echo "Building service: ${SERVICE}"
bazel build "${SERVICE}"
echo "Testing service: ${SERVICE}"
if timeout --preserve-status 5 bazel run "${SERVICE}" 2>&1 | tee "${LOGGING_FILE}";
then
echo "Service successfully started."
NUM_SUCCESS=$((NUM_SUCCESS + 1))
else
echo "Checking if ModuleNotFoundError is present in logs."
RESULT=$(grep -m 1 "ModuleNotFoundError" "${LOGGING_FILE}" || [[ $? == 1 ]])
if [[ "${RESULT}" != "" ]];
then
echo "ModuleNotFoundError found in logs from ${SERVICE}:"
echo "${RESULT}"
else
echo "ModuleNotFoundError not found in logs from ${SERVICE}:"
NUM_SUCCESS=$((NUM_SUCCESS + 1))
fi
fi
echo -e "\n################################################################################\n"
done

echo "$NUM_SUCCESS/${#SERVICES[@]} succeeded."

[[ "${NUM_SUCCESS}" == "${#SERVICES[@]}" ]]

0 comments on commit 301e1ce

Please sign in to comment.