Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5ac1346f authored by Jack He's avatar Jack He
Browse files

Cert: Add virtualenv setup for cert tests

* Add cert/set_up_virtualenv.sh for cert tests so that:
  1) We can use common IDEs that work with virtualenv to edit GD cert
     tests and libraries
  2) We no longer have to use PYTHONPATH when running tests
  3) Explicitly list dependencies in virtualenv setup to make the setup
     more Android independent as it really just need that two ZIP files
* Fixed some import directives as gd/ should always be the import root
  event when importing from the same directory

Bug: 148147248
Test: set up virtual env and run tests
Change-Id: Ie5b42dc87f2ff67910de085e96cf616b257b0b7f
parent c02f5595
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
**/default.profraw
**/__pycache__/
gd_cert_venv
+2 −4
Original line number Diff line number Diff line
@@ -14,10 +14,8 @@
#   See the License for the specific language governing permissions and
#   limitations under the License.

from gd_device_base import GdDeviceBase
from gd_device_base import replace_vars

from cert import rootservice_pb2_grpc as cert_rootservice_pb2_grpc
from cert.gd_device_base import GdDeviceBase, GdDeviceConfigError, replace_vars
import cert.rootservice_pb2_grpc as cert_rootservice_pb2_grpc
from l2cap.classic.cert import api_pb2_grpc as l2cap_cert_pb2_grpc

ACTS_CONTROLLER_CONFIG_NAME = "GdCertDevice"
+1 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
import logging

from facade import rootservice_pb2_grpc as facade_rootservice_pb2_grpc
from gd_device_base import GdDeviceBase, GdDeviceConfigError, replace_vars
from cert.gd_device_base import GdDeviceBase, GdDeviceConfigError, replace_vars
from hal import facade_pb2_grpc as hal_facade_pb2_grpc
from hci.facade import facade_pb2_grpc as hci_facade_pb2_grpc
from hci.facade import acl_manager_facade_pb2_grpc
+2 −0
Original line number Diff line number Diff line
#! /bin/bash
PYTHONPATH=$PYTHONPATH:$ANDROID_BUILD_TOP/out/host/linux-x86/lib64:$ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd:$ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py python3.8 "$@"
+221 −0
Original line number Diff line number Diff line
#! /bin/bash
#
# Script to setup virtual environment to run GD cert tests and devlop using IDE
#
# Usage
#  1. cd packages/modules/Bluetooth/system/gd
#  2. source cert/set_up_virtualenv.sh
#  3. source gd_cert_venv/bin/activate
#  4. [run tests, do development, hack]
#  5. deactivate (or just close the terminal window)

## Android build main build setup script relative to top level android source root
BUILD_SETUP=./build/envsetup.sh

function UsageAndroidTree {
    cat<<EOF
Ensure invoked from within the android source tree
EOF
}

function UsageSourcedNotExecuted {
    cat<<EOF
Ensure script is SOURCED and not executed to persist the build setup
e.g.
source $0
EOF
}

function UpFind {
    while [[ $PWD != / ]] ; do
        rc=$(find "$PWD" -maxdepth 1 "$@")
        if [ -n "$rc" ]; then
            echo $(dirname "$rc")
            return
        fi
        cd ..
    done
}

function SetUpAndroidBuild {
    pushd .
    android_root=$(UpFind -name out -type d)
    if [[ -z $android_root ]] ; then
        UsageAndroidTree
        return
    fi
    echo "Found android root $android_root"
    cd $android_root && . $BUILD_SETUP
    echo "Sourced build setup rules"
    cd $android_root && lunch
    popd
}

function SetupPython38 {
    echo "Setting up python3.8"
    sudo apt-get install python3.8-dev
}

function SetupPip3 {
    echo "Setting up pip3"
    sudo apt-get install python3-pip
}

function CompileBluetoothPacketsPython3 {
    echo "bluetooth_packets_python3 is not found, compiling"
    croot
    make -j bluetooth_packets_python3
}

# Deactivate existing virtual environment, if any, ignore errors
deactivate > /dev/null 2>&1

if [[ "${BASH_SOURCE[0]}" == "${0}" ]] ; then
    UsageSourcedNotExecuted
    return 1
fi

## Check python3.8 is installed properly
## Need Python 3.8 because bluetooth_packets_python3 is compiled against
## Python 3.8 headers
dpkg -l python3.8-dev > /dev/null 2>&1
if [[ $? -ne 0 ]] ; then
    SetupPython38
fi

## Check pip3 is installed properly
## Need pip3 for Python 3 support
dpkg -l python3-pip > /dev/null 2>&1
if [[ $? -ne 0 ]] ; then
    SetupPip3
fi

# Install and upgrade virtualenv to latest version
pip3 install --user --upgrade virtualenv > /dev/null 2>&1
if [[ $? -ne 0 ]] ; then
    echo "Error install and upgrade virtualenv"
    return 1
fi

# Set-up Android environment variables
if [[ -z "$ANDROID_BUILD_TOP" ]] ; then
    SetUpAndroidBuild
fi

## Check bluetooth_packets_python3 is compiled succssfully
$ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/cert/python3.8-gd -c "
import bluetooth_packets_python3 as bp3
bp3.BaseStruct
"
if [[ $? -ne 0 ]] ; then
  pushd .
  CompileBluetoothPacketsPython3
  popd
  $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/cert/python3.8-gd -c "
import bluetooth_packets_python3 as bp3
bp3.BaseStruct
"
  if [[ $? -ne 0 ]] ; then
    echo "Setup failed as bluetooth_packets_python3 cannot be found"
    return 1
  else
    echo "Found bluetooth_packets_python3 after compilation"
  fi
else
  echo "Found bluetooth_packets_python3"
fi

## Compile and unzip test artifacts
if [[ ! -f "$ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py.zip" || ! -f "$ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test.zip" ]]; then
    echo "bluetooth_cert_generated_py.zip OR bluetooth_cert_test.zip is not found, compiling"
    m -j dist bluetooth_stack_with_facade
    if [[ ! -f "$ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py.zip" || ! -f "$ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test.zip" ]]; then
        echo "Failed to compile bluetooth_stack_with_facade"
        return 1
    fi
fi
unzip -u $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py.zip -d $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py
if [[ $? -ne 0 ]] ; then
    echo "Failed to unzip bluetooth_cert_generated_py.zip"
    return 1
fi
unzip -u $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test.zip -d $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test
if [[ $? -ne 0 ]] ; then
    echo "Failed to unzip bluetooth_cert_test.zip"
    return 1
fi

# Set-up virtualenv
pushd .
cd $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd
virtualenv -p python3.8 gd_cert_venv
popd
if [[ $? -ne 0 ]] ; then
    echo "Error setting up virtualenv"
    return 1
fi

# Set up artifacts
pushd .
cd $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/gd_cert_venv/lib/python3.8
# Python generated code
ln -sfT $ANDROID_BUILD_TOP/tools/test/connectivity/acts/framework/acts acts
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py/cert cert
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py/facade facade
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py/hal hal
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py/hci hci
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py/l2cap l2cap
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py/neighbor neighbor
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_generated_py/security security
# Native libraries
# Symbolic link does not work for dynamic libraries
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test/out/host/linux-x86/lib64/bluetooth_packets_python3.so bluetooth_packets_python3.so
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test/out/host/linux-x86/lib64/libbluetooth_gd.so libbluetooth_gd.so
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test/out/host/linux-x86/lib64/libgrpc++_unsecure.so libgrpc++_unsecure.so
# Per systrace, Python only load from python3.8/lib64 directory for libc++.so
mkdir -p $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/gd_cert_venv/lib/python3.8/lib64
cd $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/gd_cert_venv/lib/python3.8/lib64
ln -sfT $ANDROID_BUILD_TOP/out/dist/bluetooth_cert_test/out/host/linux-x86/lib64/libc++.so libc++.so
# Binaries
cd $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/gd_cert_venv/bin
ln -sfT $ANDROID_BUILD_TOP/out/host/linux-x86/bin/bluetooth_stack_with_facade bluetooth_stack_with_facade
ln -sfT $ANDROID_BUILD_TOP/out/host/linux-x86/nativetest64/root-canal/root-canal root-canal
popd

# Activate virtualenv
pushd .
source $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/gd_cert_venv/bin/activate
popd
if [[ $? -ne 0 ]] ; then
    echo "Failed to activate virtualenv"
    deactivate
    return 1
fi
if [[ -z "$ANDROID_BUILD_TOP" ]] ; then
    echo "Failed to inherit Android build environment"
    deactivate
    return 1
fi

## Set up ACTS
# sudo is no longer needed since we are in a virtual environment
python3.8 $ANDROID_BUILD_TOP/tools/test/connectivity/acts/framework/setup.py develop
if [[ $? -ne 0 ]] ; then
    echo "ACTS setup failed"
    deactivate
    return 1
fi

pip3 install protobuf
if [[ $? -ne 0 ]] ; then
    echo "Failed to install protobuf"
    deactivate
    return 1
fi

deactivate

echo ""
echo "Please mark GD root directory as \"Project Sources and Headers\" in IDE"
echo "If still seeing errors, invalidate cached and restart"
echo "virtualenv setup complete"