Skip to content

Tips and Tricks for setting up new projects

TB-1993 edited this page Jul 22, 2024 · 1 revision

Tips and Tricks for setting up new projects

This guide is intended as a collection of recommendations to setup a new project for testing

Contents

Directory structure

└── tests
    ├── common
    └── configs

The above directory structure is recommended for new project with all tests being added to the test folder. The RAFT framework should also be cloned into the tests folder
The common directory is used to store code that can be shared between tests. For an example of this see the common test controller
The configs directory is used to store the common configuration for yours tests. It is recommended that the example rack and device configs from the raft examples folder are copied here, so they can be referred to later.

Install script

Below is a bash script that can be copied and modified install all the requirements for your test project.

It is intended to be used with the above directory structure, it should be run in the directory above tests.

To modify an extend this script follow the comments at the bottom of the script. It has premade functions to allow for cloning extra repos, checking packages requirements and installing pip packages.

Install script

install.sh

#!/usr/bin/env bash

MY_PATH="$(realpath ${BASH_SOURCE[0]})"
MY_DIR="$(dirname ${MY_PATH})"
RAFT_DIR="${MY_DIR}/tests/raft"

NO_COLOR="\e[0m"
RED="\e[0;31m"
CYAN="\e[0;36m"
YELLOW="\e[1;33m"
GREEN="\e[0;32m"
RED_BOLD="\e[1;31m"
BLUE_BOLD="\e[1;34m"
YELLOW_BOLD="\e[1;33m"

function ECHO()
{
    echo -e "$*"
}

function DEBUG()
{
    # if set -x is in use debug messages are useless as whole stript will be shown
    if [[ "$-" =~ "x" ]]; then
        return
    fi
    if [[ "${DEBUG_FLAG}" == "1" ]];then
        ECHO "${BLUE_BOLD}DEBUG: ${CYAN}$*${NO_COLOR}" > /dev/stderr
    fi
}

function INFO()
{
    ECHO "${GREEN}$*${NO_COLOR}"
}

function WARNING()
{
    ECHO "${YELLOW_BOLD}Warning: ${YELLOW}$*${NO_COLOR}" > /dev/stderr
}

function ERROR()
{
    ECHO "${RED_BOLD}ERROR: ${RED}$*${NO_COLOR}" 
    exit 1
}


function check_installed()
{
    DEBUG "BEGIN: $FUNCNAME $*"
    pkg=$1
    version=$2
    check="$(command -v ${pkg})"
    if [[ -n "${check}" ]];then
        DEBUG "Package is installed: [${pkg}]"
        if [[ -n "${version}" ]];then
            DEBUG "Performing version check: [${version}]"
            ver_check="$(${pkg} --version | grep ${version})"
            if [[ -z "${ver_check}" ]];then
                ERROR "${pkg} is installed but version is not [${version}]"
            fi
            DEBUG "${pkg} version is correct"
        fi
        return
    fi
    ERROR "Required package is not installed: [${pkg}]"
    DEBUG "END: $FUNCNAME"
}

function install_pip_requirements()
{
    DEBUG "BEGIN: $FUNCNAME $*"
    local requirements_file="$1"
    if [[ ! -e "${requirements_file}" ]];then
        ERROR "Could not install pip requirements.\nFile not found: [${requirements_file}]"
    fi
    pip install -qr "${requirements_file}"
    if [[ "$?" != "0" ]];then
        ERROR "Pip install failed.\nPlease try manually with:\ncd ${RAFT_DIR}; pip install -r requirements.txt; cd -"
    fi
    DEBUG "END: $FUNCNAME"
}

function clone_repo()
{
    DEBUG "BEGIN: $FUNCNAME $*"
    local repo_url="$1"
    if [[ -z "${repo_url}" ]];then
        ERROR "A url for a repository must be passed to the clone repo function"
    fi
    local path="$2"
    if [[ -z "${path}" ]];then
        path="${MY_DIR}/$(echo "${repo_url}"|grep -Po '\/\K.*?(?=\.git)')"
    fi
    if [[ -e "${path}" ]];then
        WARNING "[$path] appears to already be installed."
        valid_resp=0
        while [[ "${valid_resp}" == "0" ]]
        do
            read -p "Would you like to reinstall it? y/n"$'\n' result
            case "${result}" in
                "y"|"Y")
                    valid_resp=1
                    rm -rf "${path}"
                    ;;
                "n"|"N")
                    valid_resp=1
                    exit 0
                    ;;
                *)
                    continue
                    ;;
            esac
        done
    fi
    git clone "${repo_url}" "${path}"
    DEBUG "END: $FUNCNAME"
}

function clone_python_raft()
{
    DEBUG "BEGIN: $FUNCNAME $*"
    clone_repo [email protected]:rdkcentral/python_raft.git "${RAFT_DIR}"
    install_pip_requirements "${RAFT_DIR}"/requirements.txt
    DEBUG "END: $FUNCNAME"
}

### Check packages are installed ###
# check_installed package version
check_installed python3 "3.11.8"

### Clone required repos ###
# clone_repo url path
clone_python_raft

### Install required python packages ###
# install_pip_requirements 

Example of common test controller

#!/usr/bin/env python3

import sys
from os import path

# Since this test is in a sub-directory we need to add the directory above
# so we can import the framework correctly
MY_PATH = path.abspath(__file__)
MY_DIR = path.dirname(MY_PATH)
sys.path.append(path.join(MY_DIR,'../raft/'))
from framework.core import testController,logModule

class RAFTController(testController):

    def __init__(self, testName="", qcId="", maxRunTime=testController.TEST_MAX_RUN_TIME, level=logModule.STEP, loop=1, log=None):
        super().__init__(testName=testName, qcId=qcId, maxRunTime=maxRunTime, level=level, loop=loop, log=log)

The above code snippet shows how the testController can be subclassed. This allows users to implement their own custom methods that can be shared among all their tests.

Note: If the above directory structure isn't used, the lines at the top, appending to the path, will need correcting. The path to the framework directory must be appended to the sys.path variable for the import to work.