Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Python 3.6 or above should be installed in the machine
  2. Jpype should be added as the dependency in Python
    1. Command to add Jpype dependency 

      Code Block
      languagebash
      titleInstall JpyPe
      pip install jpype1

...

  1. When creating the python test case multiple test cases can be written in a single python script under different functions with parameter as DUT.
  2. All these function should be annotated with "@TestCaseId('AUTOMATION_ID')" with automation id as the parameter.
  3. Python Wrapper scripts and other Automatics Python dependency scripts like Annotation Python scripts should be available.
  4. Automatics Utils and Core API's can be invoked from the Python test scripts as follows.mentioned in the below section link.


Adding Test Case to Automatics Orchestration

For identifying a particular Test case is python a new field will be introduced in managed scripts. This new field will be used to mark particular test case as Python or not. The below are the stepwise information on how a particular test case will executed

  1. The below alter scripts needs to be executed in Automatics Orchestration DB
    1. Code Block
      languagesql
      titleAlter scripts
      linenumberstrue
      alter table script_details add TEST_SCRIPT_TYPE varchar(25) default 'JAVA';
      
      alter table script_details add IS_PYTEST_TC boolean default false;
      
      insert into available_job_params (PARAMETER_NAME,UPDATED_BY) values ('Test_Script_Type','admin');
      insert into available_job_params (PARAMETER_NAME,UPDATED_BY) values ('is_pytest_testCase','admin');
      
      
  2. At first Python test case should be added in automatics, for that when adding a new test case the new test case identification field should be selected as Python, the the second field  to identify the particular test case is using Pytest framework or not .
    1. Image Added
  3. Once a Test case is triggered, Automatics will first check whether the test case is Python or not, by using the value in the new identification field.
  4. Once Jenkins job for Python script execution is triggered from automatics, it will pull the latest Python code from the configured repo along with the RDK B or V tests and it will invoke the corresponding Python script using Java Process builder based on the Automation ID of the test case.

Enable Python scripts to use Automatics Core, Test Utils & Props

For unlocking/enabling the access of Automatics Java API's to python scripts, JPype framework is used. For accessing the Java API's from python test scripts, a new set of Python scripts which contains all the basic API's should be developed which will make use of the JPype framework to instantiate and invoke the Java classes and method in Automatics Core and Test Utils. For example, A python script similar to AutomaticsTapApi class can be created to get the Automatics properties configuration and other API's of Automatics core. This python script will in-turn call the AutomaticsTapApi Java class present in the Automatics core project using JPype to enable all the required functions. 

  1. The below python script will invoke AutomaticsTapApi java class in Automatics Core to get the current execution mode if available AutomaticsTapAPI.py


    Code Block
    languagepy
    titleAutomaticsTapApi.py
    linenumberstrue
    # Boiler plate stuff to start the module
    import json
    import os
    import jpype
    import jpype.imports
    from jpype.types import *
    
    import logging
    
    logger = logging.getLogger()
    
    
    # Launch the JVM
    def launchJVM(automaticsPropsUrl,pathToExecJar):
        if jpype.isJVMStarted():
            logger.info('JVM is already running. Do not init twice!')
        else:
            logger.info('Inside else')
            logger.info(jpype.getDefaultJVMPath())
            javaClassPath = "-Djava.class.path="+pathToExecJar
            propsJavaOpts = "-Dautomatics.properties.file="+automaticsPropsUrl
            jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", javaClassPath, propsJavaOpts)
    
    
    
    
    # tapApi = AutomaticsTapApi.getInstance()
    
            # import the Java modules
    from com.automatics.tap import AutomaticsTapApi
    from com.automatics.executor import PythonScriptExecutor
    from com.automatics.utils import BeanUtils
    
    
    def createDeviceObj(deviceJson):
        pyexecutor = PythonScriptExecutor()
        deviceObj = pyexecutor.convertJsonToDevice(deviceJson)
        return deviceObj
    
    
    def executeCommandUsingSsh(dut, command):
        pyexecutor = PythonScriptExecutor()
        return pyexecutor.executeCommandUsingSsh(dut, command)
    
    
    def getInstance():
        jpype.JClass("com.automatics.core.SupportedModelHandler").main([])
        BeanUtils.startContext()
        return AutomaticsTapApi.getInstance()
      
  2. Sample code for an Automatics Test Case Written in Python and which use Utils and Core APIs for different

    operations

    operations 

    Code Block
    languagepy
    titleSample Automatics Python Script
    linenumberstrue
    import logging
    
    from jpype.types import *
    # Adding BroadBandCommonUtils from RDKB Utils projects
    
    from com.automatics.rdkb.utils import BroadBandCommonUtils
    
    from 
    src
    automaticsPythonLib.annotation.PythonScriptAnnotation import TestCaseId
    from 
    src
    automaticsPythonLib.
    com.
    automatics import AutomaticsTapApi
    
    from src.logging import AutomaticsPyLogger
    
    
    logger = 
    AutomaticsPyLogger
    logging.
    setup_logging(__name__)
    getLogger()
    
    
    @TestCaseId('TC-RDKB-
    FS
    PYTHON-
    LAYOUT
    TEST-001')
    def 
    nvramFileSystemLayout
    checkImageVersionInDevice(dut):
        testCaseId = "TC-RDKB-
    FS
    PYTHON-
    LAYOUT
    TEST-001";
        tapEnv = AutomaticsTapApi.getInstance()
        stepNum = "
    s1
    S1";
        errorMessage = "
    nvram
    command 
    is
    execution 
    not
    in 
    properly mounted after code download
    device failed";
        status = JBoolean(False);
        logger.info("#######################################################################################");
        logger.info("STARTING TEST CASE: TC-RDKB-
    FS
    PYTHON-
    LAYOUT
    TEST-
    1001
    001");
        logger.info("TEST DESCRIPTION: 
    nvram
    Validation 
    file
    of 
    system
    device 
    layout validation
    version");
    
        logger.info("TEST STEPS : ");
        logger.info("1. 
    Check
    Run 
    whether
    a 
    nvram
    Command 
    is
    in 
    properly
    the 
    mounted
    device 
    and
    VIA 
    it
    SSH 
    has
    to 
    read-
    parse 
    write permission after code download"); logger.info("2. Verify the file access by creating a dummy file in nvram
    the version.txt file");
    
        logger.info("#######################################################################################");
    
        try:
            logger.info("**********************************************************************************");
    
            logger.info("**********************************************************************************");
            logger.info(
                "STEP 1: DESCRIPTION : Check 
    whether nvram is properly mounted
    and 
    it
    Validate 
    has
    the 
    read-
    version 
    write
    of 
    permission
    the 
    after
    device 
    code download
    ");
            logger.info(
                "STEP 1: ACTION : \"
    check
    Run 
    mounted
    a 
    status
    command 
    using
    in 
    mount
    the 
    command(mount
    device 
    |
    to 
    grep
    view 
    -w \"nvram\") and verify read-write permission
    the current RDK version in 
    nvram
    device\"");
            logger.info(
                "STEP 1: EXPECTED : 
    nvram
    The 
    should
    current 
    be
    version 
    properly
    in 
    mounted
    the 
    and
    device should 
    have
    be 
    read-write permission(mtd:data on /nvram type jffs2 (rw,relatime)
    received as response");
            logger.info("**********************************************************************************");
    
            response = tapEnv.executeCommandUsingSsh(dut, JString("
    mount | grep -w nvram
    cat /version.txt"));
            logger.info(response)
        except Exception as e:
            logger.info(str(e))
        finally:
            logger.info("################### STARTING POST-CONFIGURATIONS ###################");
            logger.info("POST-CONDITION STEPS");
            logger.info("POST-CONDITION : DESCRIPTION : Remove dummy file");
            logger.info("POST-CONDITION : ACTION : Remove the dummy file permanently (rm -rf  /nvram/dummytest.sh)");
            logger.info("POST-CONDITION : EXPECTED : Created dummy file should be removed");
    
            status = BroadBandCommonUtils.removeFileAndVerifyStatus(tapEnv, dut, JString("/nvram/dummytest.sh"));
    
            if status:
                logger.info("POST-CONDITION : ACTUAL : Post condition executed successfully");
            else:
                logger.info("POST-CONDITION : ACTUAL : Post condition failed");
    
            logger.info("POST-CONFIGURATIONS : FINAL STATUS - " + str(status));
            tapEnv.updateExecutionStatus(dut, testCaseId, stepNum, JBoolean(status), errorMessage, JBoolean(False));
            logger.info("################### COMPLETED POST-CONFIGURATIONS ###################");

For identifying a particular Test case is python a new field will be introduced in managed scripts. This new field will be used to mark particular test case as Python or not. The below are the stepwise information on how a particular test case will executed

  1. At first Python test case should be added in automatics, for that when adding a new test case the new test case identification field should be selected as Python and saved in Automatics.
  2. Once a Test case is triggered, Automatics will first check whether the test case is Python or not using the value in the new identification field.
  3. Once Jenkins job for Python script execution is triggered from automatics, it will pull the latest Python code from the configured repo along with the latest automatics core and it will invoke the corresponding Python script using JEP based on the Automation ID of the test case.

Image Removed

 Enable Python scripts to use Automatics Core, Test Utils & Props

For unlocking/enabling the access of Automatics Java API's to python scripts, JPype framework can be used. For accessing the Java API's from python test scripts, a new set of Python scripts which contains all the required API's should be developed which will make use of the JPype framework to instantiate and invoke the Java classes and method in Automatics Core and Test Utils. For example, A python script similar to AutomaticsTapApi class can be created to get the Automatics properties configuration and other API's of Automatics core. This python script will in-turn call the AutomaticsTapApi Java class present in the Automatics core project using JPype to enable all the required functions. The below python script will invoke AutomaticsTapApi java class inside automatics-core-2.9.0.jar to get the current execution mode if available
AutomaticsTapAPI.py

...

Code Block
languagepy
titleSample PYTEST Test case
linenumberstrue
from unittest import result

import pytest
import logging
from jpype.types import *

from automaticsPythonLib.annotation.PythonScriptAnnotation import TestCaseId
from automaticsPythonLib.automatics import AutomaticsTapApi

logger = logging.getLogger()

@TestCaseId('TC-RDKB-PYTHON-PY-TEST-001')
def test_command_execution_in_device():
    dut = pytest.Config.dut
    tapEnv = pytest.Config.tapEnv
    testCaseId = "TC-RDKB-PYTHON-PY-TEST-001";
    stepNum = "S1";
    errorMessage = "command execution in device failed";
    status = JBoolean(False);
    expected_result = False
    print(dut)
    response = tapEnv.executeCommandUsingSsh(dut, JString("pwd"));
    if not response:
        status = JBoolean(False);
    else:
        expected_result=True
        status = JBoolean(True);
    tapEnv.updateExecutionStatus(dut, testCaseId, stepNum, JBoolean(status), errorMessage, JBoolean(False));
    assert expected_result == True, f"Expected {expected_result}, but got {response}"