|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (C) 2024 Intel Corporation |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +set -x |
| 6 | + |
| 7 | +WORKPATH=$(dirname "$PWD") |
| 8 | +ip_address=$(hostname -I | awk '{print $1}') |
| 9 | + |
| 10 | +function build_docker_images() { |
| 11 | + echo "Start building docker images for microservice" |
| 12 | + cd $WORKPATH |
| 13 | + docker build --no-cache -t opea/guardrails-bias-detection:comps --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/guardrails/bias_detection/Dockerfile . |
| 14 | + if [ $? -ne 0 ]; then |
| 15 | + echo "opea/guardrails-bias-detection built fail" |
| 16 | + exit 1 |
| 17 | + else |
| 18 | + echo "opea/guardrails-bias-detection built successful" |
| 19 | + fi |
| 20 | +} |
| 21 | + |
| 22 | +function start_service() { |
| 23 | + echo "Starting microservice" |
| 24 | + docker run -d --runtime=runc --name="test-comps-guardrails-bias-detection-endpoint" -p 9092:9092 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy opea/guardrails-bias-detection:comps |
| 25 | + sleep 5 |
| 26 | + echo "Microservice started" |
| 27 | +} |
| 28 | + |
| 29 | +function validate_microservice() { |
| 30 | + echo "Validate microservice started" |
| 31 | + echo "test 1 - biased" |
| 32 | + result=$(curl localhost:9092/v1/bias -X POST -d '{"text":"John McCain exposed as an unprincipled politician."}' -H 'Content-Type: application/json') |
| 33 | + if [[ $result == *"Violated"* ]]; then |
| 34 | + echo "Result correct." |
| 35 | + else |
| 36 | + docker logs test-comps-guardrails-bias-detection-endpoint |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + echo "test 2 - non-biased" |
| 40 | + result=$(curl localhost:9092/v1/bias -X POST -d '{"text":"John McCain described as an unprincipled politician."}' -H 'Content-Type: application/json') |
| 41 | + if [[ $result == *"described"* ]]; then |
| 42 | + echo "Result correct." |
| 43 | + else |
| 44 | + echo "Result wrong." |
| 45 | + docker logs test-comps-guardrails-bias-detection-endpoint |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + echo "Validate microservice completed" |
| 49 | +} |
| 50 | + |
| 51 | +function stop_docker() { |
| 52 | + cid=$(docker ps -aq --filter "name=test-comps-guardrails-bias-detection-endpoint") |
| 53 | + echo "Shutdown legacy containers "$cid |
| 54 | + if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi |
| 55 | +} |
| 56 | + |
| 57 | +function main() { |
| 58 | + |
| 59 | + stop_docker |
| 60 | + |
| 61 | + build_docker_images |
| 62 | + start_service |
| 63 | + |
| 64 | + validate_microservice |
| 65 | + |
| 66 | + stop_docker |
| 67 | + echo "cleanup container images and volumes" |
| 68 | + echo y | docker system prune 2>&1 > /dev/null |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +main |
0 commit comments