def test_cmd_with_res_fmwk(stc):
    ctor = CScriptableCreator()
    sequencer = CStcSystem.Instance().GetObject("Sequencer")

    # Set up an iteration group
    group_cmd = ctor.Create(PKG + ".IterationGroupCommand", sequencer)
    assert group_cmd
    while_cmd = ctor.Create("SequencerWhileCommand", group_cmd)
    assert while_cmd
    iter_cmd = ctor.Create(PKG + ".ObjectIteratorCommand", while_cmd)
    valid_cmd = ctor.Create(PKG + ".IteratorValidateCommand", while_cmd)

    while_cmd.Set("ExpressionCommand", iter_cmd.GetObjectHandle())
    while_cmd.SetCollection("CommandList", [valid_cmd.GetObjectHandle()])
    group_cmd.SetCollection("CommandList", [while_cmd.GetObjectHandle()])
    sequencer.SetCollection("CommandList", [group_cmd.GetObjectHandle()])

    # Setup the methodology manager and the active test case
    meth_mgr = meth_mgr_utils.get_meth_manager()
    assert meth_mgr is not None
    methodology = ctor.Create("StmMethodology", meth_mgr)
    test_case = ctor.Create("StmTestCase", methodology)
    meth_mgr_utils.set_active_test_case(test_case)
    test_case_res = meth_mgr_utils.get_stm_test_result()
    assert test_case_res is not None

    # Mock get_this_cmd
    IteratorValidateCommand.get_this_cmd = MagicMock(return_value=valid_cmd)

    # Set up the results framework (passing iteration)
    RI.create_test()
    RI.start_test()
    fs = 128
    fs_iter_id = 1
    fs_iter_hnd = iter_cmd.GetObjectHandle()
    RI.set_iterator_current_value(fs_iter_hnd, "FrameSize", fs, fs_iter_id)
    RI.add_provider_result(test_utils.dummy_verify_result_passed)
    RI.complete_iteration()

    # Call the different parts of the command and verify the results
    res = IteratorValidateCommand.run(fs_iter_id)
    assert res is True
    assert valid_cmd.Get("Verdict") is True

    # Failed iteration
    RI.set_iterator_current_value(fs_iter_hnd, "FrameSize",
                                  fs + 128, fs_iter_id + 1)
    RI.add_provider_result(test_utils.dummy_verify_result_failed)
    RI.complete_iteration()

    res = IteratorValidateCommand.run(fs_iter_id + 1)
    assert res is True
    assert valid_cmd.Get("Verdict") is False
def get_stm_test_result():
    """
    Return StmTestResult object if exist else return None.
    """
    logger.debug('get_stm_test_result started.')
    result_parent = meth_man_utils.get_meth_manager()
    active_test = meth_man_utils.get_active_test_case()
    if active_test is not None:
        result_parent = active_test
    test_result = result_parent.GetObject('StmTestResult')
    logger.debug('get_stm_test_result completed.')
    return test_result
def test_param_a_to_script(stc):
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject('Project')
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    pkg = "spirent.methodology"

    tags = project.GetObject('Tags')
    tagA = ctor.Create("tag", tags)
    tagA.Set("Name", "tagA")

    cmd = ctor.Create(pkg + ".RunPyScriptCommand", sequencer)
    RunPyScriptCommand.get_this_cmd = MagicMock(return_value=cmd)

    cmd2 = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)
    cmd2.AddObject(tagA, RelationType("UserTag"))

    script_folder = mmutils.get_scripts_home_dir()
    filename = os.path.join(script_folder, "unit_test_file_param_a_to_script.py")
    try:
        if not os.path.exists(script_folder):
            os.makedirs(script_folder)
        with open(filename, "w") as f:
            f.write(unit_test_file_param_a_to_script())
        RunPyScriptCommand.run("unit_test_file_param_a_to_script", "run", "tagA", {'a': 4})
        assert cmd2.Get("CopiesPerParent") == 4
    finally:
        remove_files(filename)
    return
def hold_test_error_returned(stc):
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject('Project')
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    pkg = "spirent.methodology"

    tags = project.GetObject('Tags')
    tagA = ctor.Create("tag", tags)
    tagA.Set("Name", "tagA")

    cmd = ctor.Create(pkg + ".RunPyScriptCommand", sequencer)
    RunPyScriptCommand.get_this_cmd = MagicMock(return_value=cmd)

    script_folder = mmutils.get_scripts_home_dir()
    filename = os.path.join(script_folder, "unit_test_file_error_returned.py")
    try:
        if not os.path.exists(script_folder):
            os.makedirs(script_folder)
        with open(filename, "w") as f:
            f.write(unit_test_file_error_returned())
        rc = RunPyScriptCommand.run("unit_test_file_error_returned", "errorout", "tagA", {})
        assert rc is False
        assert cmd.Get("ErrorMsg") != ""
    finally:
        remove_files(filename)
    return
def find_template_file(file_path):
    if not os.path.isabs(file_path):
        abs_file_name = mmutils.find_template_across_common_paths(file_path)
        if abs_file_name != '':
            file_path = abs_file_name
    if os.path.isfile(file_path):
        return file_path, True
    else:
        return file_path, False
Example #6
0
 def import_rules_file(self, rules_file):
     if not os.path.isfile(rules_file):
         found_file = mmutils.find_file_across_common_paths(rules_file)
         if found_file == '':
             return 'Unable to find rules file:' + rules_file
         rules_file = found_file
     with open(rules_file, 'r') as f:
         file_content = f.read()
     return self.import_content(file_content)
def reset():
    """Create StmTestResult object if does not exist.
    reset object if exist.
    StmTestResult object should be created under active test.
    If active test does not exist then under methodology manager.
    """
    logger.debug('Reset result objects.')
    result_parent = meth_man_utils.get_meth_manager()
    active_test = meth_man_utils.get_active_test_case()
    if active_test is not None:
        logger.debug('Active test is result parent.')
        result_parent = active_test
    test_result = result_parent.GetObject('StmTestResult')
    if test_result is None:
        ctor = CScriptableCreator()
        test_result = ctor.Create("StmTestResult", result_parent)
    else:
        reset_stm_test_result(test_result)
    logger.debug('Reset result objects done.')
    return test_result
def run():
    plLogger = get_logger()
    plLogger.LogDebug("begin.run")

    meth_mgr = mm_utils.get_meth_manager()
    test_meth_list = meth_mgr.GetObjects("StmMethodology")
    meta_jsons = get_meta_json(test_meth_list)

    this_cmd = get_this_cmd()
    this_cmd.Set("MethodologyList", meta_jsons)

    plLogger.LogDebug("end.run")
    return True
Example #9
0
def load_xml_from_file(file_path):
    plLogger = PLLogger.GetLogger('methodology')
    xml_str = None
    if not os.path.isabs(file_path):
        full_path = mm_utils.find_template_across_common_paths(file_path)
        if full_path != '':
            file_path = full_path
    if os.path.isfile(file_path):
        with open(file_path, "r") as f:
            xml_str = f.read()
    else:
        plLogger.LogError("ERROR: Failed to find file " +
                          file_path)
        return None
    return xml_str
def load_xmlroot_from_file(filename):
    # FIXME: This needs to move into an XML util file...
    plLogger = PLLogger.GetLogger("methodology")
    plLogger.LogDebug("begin.load_xmlroot_from_file.MethodologyGroupCommand")
    file_path = filename
    if not os.path.isabs(file_path):
        abs_file_name = mmutils.find_template_across_common_paths(file_path)
        if abs_file_name != '':
            file_path = abs_file_name

    if not os.path.isfile(file_path):
        plLogger.LogError("ERROR: Failed to find file '" + filename + "'")
        return None

    # return etree.parse(file_path)
    with open(file_path) as f:
        return etree.fromstring(f.read())
def test_create_stm_get_stm(stc, resource_cleanup):
    mm = meth_man_utils.get_meth_manager()
    assert mm is not None
    assert result_obj.get_stm_test_result() is None
    stm_result = mm.GetObject('StmTestResult')
    assert stm_result is None
    stm_result = result_obj.create_stm_test_result_under_mm()
    assert stm_result is not None
    stm_mm = mm.GetObject('StmTestResult')
    assert stm_result.GetObjectHandle() == stm_mm.GetObjectHandle()
    stm3 = result_obj.get_stm_test_result()
    assert stm3 is not None
    assert stm_result.GetObjectHandle() == stm3.GetObjectHandle()
    # check leaf iterator is none
    assert iterator_utils.get_leaf_iterator() is None
    # check StmTestResult is active result
    stm4 = iterator_utils.get_active_result_object()
    assert stm4 is not None
    assert stm_result.GetObjectHandle() == stm4.GetObjectHandle()
def test_validate_missing_method():
    stc_sys = CStcSystem.Instance()
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    pkg = "spirent.methodology"

    cmd = ctor.Create(pkg + ".RunPyScriptCommand", sequencer)
    RunPyScriptCommand.get_this_cmd = MagicMock(return_value=cmd)

    script_folder = mmutils.get_scripts_home_dir()
    filename = os.path.join(script_folder, "unit_test_file_valid_script.py")
    try:
        if not os.path.exists(script_folder):
            os.makedirs(script_folder)
        with open(filename, "w") as f:
            f.write(unit_test_file_valid_script())
        err = RunPyScriptCommand.validate("unit_test_file_valid_script", "", "tagA", "")
        assert err != ""
    finally:
        remove_files(filename)
    return
def find_config_path(file_name):
    stc_sys = CStcSystem.Instance()
    if os.path.isabs(file_name):
        if os.path.isfile(file_name):
            return file_name
        else:
            return None
    path_list = []
    methodology = None
    test_case = meth_man_utils.get_active_test_case()
    if test_case:
        methodology = test_case.GetParent()
        path_list.append(test_case.Get('Path'))
    if methodology:
        path_list.append(methodology.Get('Path'))
    path_list.append(stc_sys.GetApplicationCommonDataPath())
    for path in path_list:
        path = os.path.normpath(path)
        abs_path = os.path.join(path, file_name)
        if os.path.isfile(abs_path):
            return abs_path
    return None
def validate(TestCaseKey, StmTestCase, MethodologyKey, MethodologyJson, EnableResourceCheck):
    plLogger = PLLogger.GetLogger("methodology")
    plLogger.LogDebug("begin.validate.RunMethodologyTestCommand.")

    if TestCaseKey:
        # Check if test case key exists in installed methodologies
        test_case_handle, err_str = mm_utils.get_test_case_from_key(TestCaseKey)
        return err_str
    elif StmTestCase:
        hnd_reg = CHandleRegistry.Instance()
        test_case = hnd_reg.Find(StmTestCase)
        if test_case is None or not test_case.IsTypeOf("StmTestCase"):
            plLogger.LogError("Was unable to find StmTestCase with handle " + str(StmTestCase) + " in the system.")
            return "Could not find StmTestCase"
    else:
        # Must specify a key and json
        if not MethodologyKey or not MethodologyJson:
            return "Must specify a TestCaseKey, StmTestCase or MethodologyKey and MethodologyJson"

        # Validate against the schema
        this_cmd = get_this_cmd()
        res = json_utils.validate_json(MethodologyJson, this_cmd.Get("InputJsonSchema"))
        if res != "":
            return "Methodology JSON is invalid or does not conform to the schema: " + res

        # Load the json if it passes schema validation
        err_str, meth_json = json_utils.load_json(MethodologyJson)
        if err_str != "":
            return err_str

        # Check the MethodologyKey matches the meth key in the json
        if MethodologyKey != meth_json["methodology_key"]:
            return "Methodology Key does not match the methodology_key in the JSON"

    plLogger.LogDebug("end.validate.RunMethodologyTestCommand")
    return ""
def run(RouterTagList, BgpImportParamsTagName, CreatedRoutesTagName):
    plLogger = PLLogger.GetLogger('methodology')
    plLogger.LogInfo(" Run BgpImportRoutesCommand")
    this_cmd = get_this_cmd()

    if not BgpImportParamsTagName:
        return "Invalid BgpImportParamsTagName passed in"

    router_obj_list = tag_utils.get_tagged_objects_from_string_names(RouterTagList)
    # Turn into handles
    router_list = []
    for router_obj in router_obj_list:
        if router_obj.IsTypeOf('EmulatedDevice'):
            router_list.append(router_obj)
        elif routerObj.IsTypeOf('RouterConfig'):
            router_list.append(router_obj.GetParent().GetObjectHandle())

    if not router_list:
        this_cmd.Set('Status', "RouterTagList does not point to any routers")
        return False

    param_obj_list = \
        tag_utils.get_tagged_objects_from_string_names([BgpImportParamsTagName])

    route_list = []
    for param_obj in param_obj_list:
        # Remove any existing parameter list relations
        exist_list = param_obj.GetObjects('Scriptable',
                                          RelationType('SelectedRouterRelation'))
        for exist in exist_list:
            param_obj.RemoveObject(exist,
                                   RelationType('SelectedRouterRelation'))
        for router in router_list:
            param_obj.AddObject(router,
                                RelationType('SelectedRouterRelation'))
        router_hdl_list = [r.GetObjectHandle() for r in router_list]
        # Create the BgpImportRouteTableCommand
        with AutoCommand('BgpImportRouteTableCommand') as cmd:
            cmd.Set("ImportParams", param_obj.GetObjectHandle())
            # Ensure BLL command gets an absolute path
            abs_path = \
                mm_utils.find_file_across_common_paths(param_obj.Get("FileName"),
                                                       search_templates=True)
            if len(abs_path):
                param_obj.Set("FileName", abs_path)
            cmd.SetCollection("RouterList", router_hdl_list)
            cmd.Set("DeleteRoutes", "NO")
            cmd.Execute()
            cmd_state = cmd.Get('State')
            cmd_status = cmd.Get('Status')
            if cmd_state != 'COMPLETED':
                this_cmd.Set('Status', cmd_status)
                return False

            route_list = param_obj.GetObjects('BgpRouteConfig',
                                              RelationType('WizardGenerated'))
            if not route_list:
                this_cmd.Set('Status', 'Failed to generate any '
                             'route objects for {}'.format(param_obj.Get('Name')))
                return False
            tag_wizgenerated_as_created(param_obj, CreatedRoutesTagName)

    return True
def create_stm_test_result_under_mm():
    logger.debug('Create StmTestResult object')
    result_parent = meth_man_utils.get_meth_manager()
    ctor = CScriptableCreator()
    test_result = ctor.Create("StmTestResult", result_parent)
    return test_result
def get_script_path(script_name):
    abs_path = mm_utils.find_script_across_common_paths(script_name + ".py")
    if abs_path == "":
        return ""
    return os.path.split(abs_path)[0]
def set_active_test_case(test_case):
    mm_utils.set_active_test_case(test_case)
    return
        except Exception, e:
            err_str = str(e)
        result = cmd.Get("PassFailState")
        TestCaseKey = cmd.Get("TestCaseKey")
        cmd.MarkDelete()

        # Verify create command passed
        if err_str or result is None or result != "PASSED":
            err_str = "Failed to create methodology test case. " + err_str
            plLogger.LogError(err_str)
            this_cmd.Set("Status", err_str)
            return False

    if TestCaseKey:
        # Get test case from installed methodologies
        StmTestCase, err_str = mm_utils.get_test_case_from_key(TestCaseKey)
        if err_str:
            plLogger.LogError(err_str)
            this_cmd.Set("Status", err_str)
            return False

    # Get the StmTestCase object
    test_case = hnd_reg.Find(StmTestCase)
    if test_case is None or not test_case.IsTypeOf("StmTestCase"):
        err_str = "Was unable to find StmTestCase with handle " + str(StmTestCase) + " in the system."
        plLogger.LogError(err_str)
        this_cmd.Set("Status", err_str)
        return False

    # Get the StmMethodology so we can get the meth key
    stm_meth = test_case.GetObject("StmMethodology", RelationType("ParentChild", 1))
def run(StmTestCase, EnableTieCheck, EnableLoadConfig):
    plLogger = PLLogger.GetLogger('methodology')
    plLogger.LogDebug('begin.run.RunStmTestCaseCommand')

    stc_sys = CStcSystem.Instance()
    hnd_reg = CHandleRegistry.Instance()
    test_case = hnd_reg.Find(StmTestCase)
    if test_case is None or not test_case.IsTypeOf('StmTestCase'):
        plLogger.LogError('Was unable to find StmTestCase with handle ' +
                          str(StmTestCase) + ' in the system.')
        return False

    txml_path = os.path.join(test_case.Get('Path'), mgr_const.MM_META_FILE_NAME)
    plLogger.LogDebug('txml_path: ' + str(txml_path))
    plLogger.LogDebug('stm_test_case: ' + test_case.Get('Name'))

    txml_root = get_txml_file_root(txml_path)
    if txml_root is None:
        plLogger.LogError('Could not parse TXML')
        return False

    # Parse the TXML for the keys and values
    key_val_dict, gui_key_val_dict = parse_txml_keys_values(txml_root)

    # Parse the TXML for the processing functions
    proc_func_dict = parse_txml_proc_funcs(txml_root)

    # Run the TXML processing functions
    res = run_txml_proc_funcs(proc_func_dict,
                              key_val_dict,
                              gui_key_val_dict)
    if not res:
        plLogger.LogError('Failed to run TXML processing functions!')
        return False

    # Parse the TXML processing dictionaries
    input_dict_list = get_txml_proc_dicts(txml_root)

    # Parse json dictionaries and run processing functions
    res = run_txml_proc_util(input_dict_list, key_val_dict, gui_key_val_dict)

    if not res:
        plLogger.LogError('Failed to run TXML processing function utilities!')
        return False

    port_group_dict = parse_txml_port_groups(txml_root)
    port_group_list = build_port_group_list(port_group_dict)

    plLogger.LogDebug('key_val_dict (json): ' + json.dumps(key_val_dict))
    plLogger.LogDebug('port_group_list: ' + str(port_group_list))

    # Remove the gui-only keys from key_val_dict
    for key in gui_key_val_dict.keys():
        if key in key_val_dict.keys():
            key_val_dict.pop(key)
    plLogger.LogDebug("key_val_dict without gui only keys (json): " +
                      json.dumps(key_val_dict))

    # Create the ports (they will be tagged appropriately)...
    ports, offline_detected = create_and_tag_ports(port_group_list)

    if EnableTieCheck and not offline_detected:
        estimationUtils = EstimationUtils(test_case)
        output_json = estimationUtils.get_estimates_json()
        plLogger.LogDebug('output_json: ' + output_json)
        tie_pkg = 'spirent.testintel'

        result = None
        verdict = []
        with AutoCommand(tie_pkg + '.ScalingValidatePortsCommand') as tie_cmd:
            tie_cmd.Set('Profile', output_json)
            try:
                tie_cmd.Execute()
            except:
                pass
            result = tie_cmd.Get('PassFailState')
            try:
                verdict = json.loads(tie_cmd.Get('Verdict'))
            except:
                pass
        plLogger.LogInfo('Validation Verdict: {}'.format(verdict))
        if result is None or result != 'PASSED':
            if result is None:
                plLogger.LogError('ERROR: Unable to create an instance of ' +
                                  tiepkg + '.ScalingValidatePortsCommand.')
            else:
                fail_list = []
                for ent in verdict:
                    if 'portLocations' not in ent:
                        continue
                    for loc in ent['portLocations']:
                        if not loc['confidence']:
                            out_fmt = 'Port {} can not run test: {}'
                            out = out_fmt.format(loc['location'],
                                                 loc['reason'])
                            fail_list.append(out)
                        elif loc['confidence'] < 100.0:
                            out_fmt = 'Port {} may run with {}% confidence, ' + \
                                'disable pre-run resource validation check ' + \
                                'to proceed: {}'
                            out = out_fmt.format(loc['location'],
                                                 loc['confidence'], loc['reason'])
                            fail_list.append(out)
                this_cmd = hnd_reg.Find(__commandHandle__)
                this_cmd.Set('Status', '\n'.join(fail_list))
            # Common exit point after setting failure
            return False

    # If true, the caller hasn't already loaded the config, so do it on their behalf
    if EnableLoadConfig:
        with AutoCommand('LoadFromXml') as load_cmd:
            load_cmd.Set('FileName',
                         os.path.join(test_case.Get('Path'),
                                      mgr_const.MM_SEQUENCER_FILE_NAME))
            load_cmd.Execute()

    # Set the active test case
    mm_utils.set_active_test_case(test_case)

    # Configure the MethodologyGroupCommand
    sequencer = stc_sys.GetObject('Sequencer')
    cmd_list = sequencer.GetCollection('CommandList')
    for cmd_hnd in cmd_list:
        cmd = hnd_reg.Find(cmd_hnd)
        if cmd is None:
            continue

        if cmd.IsTypeOf("spirent.methodology.manager.MethodologyGroupCommand"):
            cmd.Set("KeyValueJson", json.dumps(key_val_dict))
            break

    # If any of the ports were offline ports, then assume a test config
    # that isn't to connect to chassis (e.g., a unit test).
    if not offline_detected:
        attach_ports(ports)

    plLogger.LogDebug('end.run.RunStmTestCaseCommand')
    return True
Example #21
0
def script_filename_path(ScriptFilename):
    file_path = assumed_filename(ScriptFilename) + ".py"
    abs_file_name = mmutils.find_script_across_common_paths(file_path)
    if abs_file_name != '':
        return os.path.split(abs_file_name)[0]
    return ""
def get_script_path(script_name):
    abs_path = mm_utils.find_script_across_common_paths(script_name + '.py')
    if abs_path == '':
        return ''
    return os.path.split(abs_path)[0]