def test_run_single_tag(stc):
    # Test XML
    test_xml = get_test_xml()
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    ctor = CScriptableCreator()

    pkg = "spirent.methodology"
    template = ctor.Create("StmTemplateConfig", project)
    template.Set("TemplateXml", test_xml)

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    ipv4_ele = xml_utils.get_element(root, "Ipv4If")
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "1500"
    assert inner_vlan_ele.get("VlanId") == "2000"
    assert ipv4_ele is not None
    assert ipv4_ele.get("Address") == "192.85.1.3"

    # Modify the OuterVlan
    cmd = ctor.CreateCommand(pkg + ".ModifyTemplatePropertyCommand")
    cmd.Set("StmTemplateConfig", template.GetObjectHandle())
    cmd.SetCollection("PropertyList", ["VlanIf.VlanId"])
    cmd.SetCollection("ValueList", ["999"])
    cmd.SetCollection("TagNameList", ["OuterVlan"])
    cmd.Execute()
    cmd.MarkDelete()

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "999"
    assert inner_vlan_ele.get("VlanId") == "2000"

    # Modify the InnerVlan
    cmd = ctor.CreateCommand(pkg + ".ModifyTemplatePropertyCommand")
    cmd.Set("StmTemplateConfig", template.GetObjectHandle())
    cmd.SetCollection("PropertyList", ["VlanIf.VlanId",
                                       "VlanIf.IdStep",
                                       "VlanIf.Priority"])
    cmd.SetCollection("ValueList", ["123", "321", "5"])
    cmd.SetCollection("TagNameList", ["InnerVlan"])
    cmd.Execute()
    cmd.MarkDelete()

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "999"
    assert outer_vlan_ele.get("IdStep") == "1"
    assert outer_vlan_ele.get("Priority") == "7"
    assert inner_vlan_ele.get("VlanId") == "123"
    assert inner_vlan_ele.get("IdStep") == "321"
    assert inner_vlan_ele.get("Priority") == "5"

    failed = False
    fail_message = ""
    # Modify a property on an untagged object (should fail)
    with AutoCommand(PKG + '.ModifyTemplatePropertyCommand') as cmd:
        try:
            cmd.Set("StmTemplateConfig", template.GetObjectHandle())
            cmd.SetCollection("PropertyList", ["VlanIf.VlanId",
                                               "Ipv4If.Address"])
            cmd.SetCollection("ValueList", ["1501", "65.65.65.1"])
            cmd.SetCollection("TagNameList", ["InnerVlan"])
            cmd.Execute()
        except RuntimeError as e:
            fail_message = str(e)
            if 'Did not find object Ipv4If in list of tagged objects' in fail_message:
                failed = True
    if not failed:
        raise AssertionError('ModifyTemplatePropertyCommand failed with ' +
                             'unexpected error: "' + fail_message + '"')

    failed = False
    fail_message = ""
    # Modify a property using invalid tag list (objects don't match) - should fail
    with AutoCommand(PKG + '.ModifyTemplatePropertyCommand') as cmd:
        try:
            cmd.Set("StmTemplateConfig", template.GetObjectHandle())
            cmd.SetCollection("PropertyList", ["VlanIf.VlanId"])
            cmd.SetCollection("ValueList", ["1502"])
            cmd.SetCollection("TagNameList", ["TopLevelIf"])
            cmd.Execute()
        except RuntimeError as e:
            fail_message = str(e)
            if 'Did not find object VlanIf in list of tagged objects' in fail_message:
                failed = True
    if not failed:
        raise AssertionError('ModifyTemplatePropertyCommand failed with ' +
                             'unexpected error: "' + fail_message + '"')
def test_run_multiple_tags_multiple_properties(stc):
    # Test XML
    test_xml = get_test_xml()
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    ctor = CScriptableCreator()

    pkg = "spirent.methodology"
    template = ctor.Create("StmTemplateConfig", project)
    template.Set("TemplateXml", test_xml)

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    ipv4_ele = xml_utils.get_element(root, "Ipv4If")
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "1500"
    assert outer_vlan_ele.get("Priority") == "7"
    assert inner_vlan_ele.get("VlanId") == "2000"
    assert inner_vlan_ele.get("Priority") == "7"
    assert ipv4_ele is not None
    assert ipv4_ele.get("Address") == "192.85.1.3"

    prop_val_list = [("VlanIf.VlanId", "999"),
                     ("VlanIf.Priority", "1"),
                     ("Ipv4If.Address", "57.57.57.57")]

    # Modify both vlans
    cmd = ctor.CreateCommand(pkg + ".ModifyTemplatePropertyCommand")
    cmd.Set("StmTemplateConfig", template.GetObjectHandle())
    cmd.SetCollection("PropertyList", [pv[0] for pv in prop_val_list])
    cmd.SetCollection("ValueList", [pv[1] for pv in prop_val_list])
    cmd.SetCollection("TagNameList", ["OuterVlan", "InnerVlan",
                                      "TopLevelIf"])
    cmd.Execute()
    cmd.MarkDelete()

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    ipv4_ele = xml_utils.get_element(root, "Ipv4If")
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "999"
    assert outer_vlan_ele.get("Priority") == "1"
    assert inner_vlan_ele.get("VlanId") == "999"
    assert inner_vlan_ele.get("Priority") == "1"
    assert ipv4_ele is not None
    assert ipv4_ele.get("Address") == "57.57.57.57"

    prop_val_list = [("VlanIf.VlanId", "333"),
                     ("Ipv4If.Address", "11.11.11.11"),
                     ("VlanIf.Priority", "3")]

    # Use only the InnerVlan tag and verify the outer vlan values are not modified
    cmd = ctor.CreateCommand(pkg + ".ModifyTemplatePropertyCommand")
    cmd.Set("StmTemplateConfig", template.GetObjectHandle())
    cmd.SetCollection("PropertyList", [pv[0] for pv in prop_val_list])
    cmd.SetCollection("ValueList", [pv[1] for pv in prop_val_list])
    cmd.SetCollection("TagNameList", ["InnerVlan", "TopLevelIf"])
    cmd.Execute()
    cmd.MarkDelete()

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    ipv4_ele = xml_utils.get_element(root, "Ipv4If")
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "999"
    assert outer_vlan_ele.get("Priority") == "1"
    assert inner_vlan_ele.get("VlanId") == "333"
    assert inner_vlan_ele.get("Priority") == "3"
    assert ipv4_ele is not None
    assert ipv4_ele.get("Address") == "11.11.11.11"
def test_run_invalid_tags_multiple_properties(stc):
    # Test XML
    test_xml = get_test_xml()
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    ctor = CScriptableCreator()

    pkg = "spirent.methodology"
    template = ctor.Create("StmTemplateConfig", project)
    template.Set("TemplateXml", test_xml)

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    ipv4_ele = xml_utils.get_element(root, "Ipv4If")
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "1500"
    assert outer_vlan_ele.get("Priority") == "7"
    assert inner_vlan_ele.get("VlanId") == "2000"
    assert inner_vlan_ele.get("Priority") == "7"
    assert ipv4_ele is not None
    assert ipv4_ele.get("Address") == "192.85.1.3"

    prop_val_list = [("VlanIf.VlanId", "555"),
                     ("Ipv4If.Address", "32.32.32.32"),
                     ("VlanIf.Priority", "4")]

    # In TagNameList have a duplicate tag and two invalid tags
    cmd = ctor.CreateCommand(pkg + ".ModifyTemplatePropertyCommand")
    cmd.Set("StmTemplateConfig", template.GetObjectHandle())
    cmd.SetCollection("PropertyList", [pv[0] for pv in prop_val_list])
    cmd.SetCollection("ValueList", [pv[1] for pv in prop_val_list])
    cmd.SetCollection("TagNameList", ["OuterVlan", "InnerVlan",
                                      "TopLevelIf", "OuterVlan", "asdf", ""])
    cmd.Execute()
    cmd.MarkDelete()

    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    outer_vlan_ele, inner_vlan_ele = get_vlan_elements(root)
    ipv4_ele = xml_utils.get_element(root, "Ipv4If")
    assert outer_vlan_ele is not None
    assert inner_vlan_ele is not None
    assert outer_vlan_ele.get("VlanId") == "555"
    assert outer_vlan_ele.get("Priority") == "4"
    assert inner_vlan_ele.get("VlanId") == "555"
    assert inner_vlan_ele.get("Priority") == "4"
    assert ipv4_ele is not None
    assert ipv4_ele.get("Address") == "32.32.32.32"

    prop_val_list = [("VlanIf.VlanId", "888"),
                     ("Ipv4If.Address", "45.45.45.45"),
                     ("VlanIf.Priority", "2")]

    failed = False
    fail_message = ""
    # Set TagNameList to an empty string
    with AutoCommand(PKG + '.ModifyTemplatePropertyCommand') as cmd:
        try:
            cmd.Set("StmTemplateConfig", template.GetObjectHandle())
            cmd.SetCollection("PropertyList", [pv[0] for pv in prop_val_list])
            cmd.SetCollection("ValueList", [pv[1] for pv in prop_val_list])
            cmd.SetCollection("TagNameList", [""])
            cmd.Execute()
        except RuntimeError as e:
            fail_message = str(e)
            if 'Did not find object VlanIf in list of tagged objects' in fail_message:
                failed = True
    if not failed:
        raise AssertionError('ModifyTemplatePropertyCommand failed with ' +
                             'unexpected error: "' + fail_message + '"')

    prop_val_list = [("Ipv4If.Address", "45.45.45.45"),
                     ("VlanIf.VlanId", "888"),
                     ("VlanIf.Priority", "2")]

    failed = False
    fail_message = ""
    # Set TagNameList to an invalid string
    with AutoCommand(PKG + '.ModifyTemplatePropertyCommand') as cmd:
        try:
            cmd.Set("StmTemplateConfig", template.GetObjectHandle())
            cmd.SetCollection("PropertyList", [pv[0] for pv in prop_val_list])
            cmd.SetCollection("ValueList", [pv[1] for pv in prop_val_list])
            cmd.SetCollection("TagNameList", ["blah"])
            cmd.Execute()
        except RuntimeError as e:
            fail_message = str(e)
            if 'Did not find object Ipv4If in list of tagged objects' in fail_message:
                failed = True
    if not failed:
        raise AssertionError('ModifyTemplatePropertyCommand failed with ' +
                             'unexpected error: "' + fail_message + '"')

    prop_val_list = [("Ipv4If.Address", "45.45.45.45"),
                     ("VlanIf.VlanId", "888"),
                     ("VlanIf.Priority", "2")]

    failed = False
    fail_message = ""
    # Set TagNameList to a valid tag but doesn't correspond to any of the objects to modify
    with AutoCommand(PKG + '.ModifyTemplatePropertyCommand') as cmd:
        try:
            cmd.Set("StmTemplateConfig", template.GetObjectHandle())
            cmd.SetCollection("PropertyList", [pv[0] for pv in prop_val_list])
            cmd.SetCollection("ValueList", [pv[1] for pv in prop_val_list])
            cmd.SetCollection("TagNameList", ["Dhcpv4"])
            cmd.Execute()
        except RuntimeError as e:
            fail_message = str(e)
            if 'Did not find object Ipv4If in list of tagged objects' in fail_message:
                failed = True
    if not failed:
        raise AssertionError('ModifyTemplatePropertyCommand failed with ' +
                             'unexpected error: "' + fail_message + '"')
def test_run(stc):
    # Test XML
    test_xml = get_test_xml()

    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject('Project')
    ctor = CScriptableCreator()

    pkg = 'spirent.methodology'
    template = ctor.Create('StmTemplateConfig', project)
    template.Set('TemplateXml', test_xml)

    # Add a valid (but strange) relation between Dhcp and Ethernet
    passFailState = ''
    with AutoCommand(pkg + '.ConfigTemplateRelationCommand') as cmd:
        cmd.Set('StmTemplateConfig', template.GetObjectHandle())
        cmd.Set('SrcTagName', 'Dhcpv4')
        cmd.Set('TargetTagName', 'EthIIIf')
        cmd.Set('RelationName', 'UsesIf')
        cmd.Set('RemoveRelation', False)
        cmd.Execute()
        passFailState = cmd.Get('PassFailState')

    assert passFailState == 'PASSED'
    # Check the XML
    root = etree.fromstring(template.Get('TemplateXml'))
    assert root is not None
    dhcpv4 = xml_utils.get_element(root, 'Dhcpv4BlockConfig')
    # There will be an already existing UsesIf relation as well as the new one
    rel_list = []
    for child_ele in dhcpv4:
        if child_ele.tag == 'Relation' and \
           child_ele.get('type') == 'UsesIf':
            rel_list.append(child_ele.get('target'))
    assert '2204' in rel_list

    # Remove a relation
    passFailState = ''
    with AutoCommand(pkg + '.ConfigTemplateRelationCommand') as cmd:
        cmd.Set('StmTemplateConfig', template.GetObjectHandle())
        cmd.Set('SrcTagName', 'Dhcpv4')
        cmd.Set('TargetTagName', 'TopLevelIf')
        cmd.Set('RelationName', 'UsesIf')
        cmd.Set('RemoveRelation', True)
        cmd.Execute()
        passFailState = cmd.Get('PassFailState')

    assert passFailState == 'PASSED'
    # Check the XML
    root = etree.fromstring(template.Get("TemplateXml"))
    assert root is not None
    ipv4_ele = xml_utils.get_element(root, 'Ipv4If')
    dhcp_ele = xml_utils.get_element(root, 'Dhcpv4BlockConfig')
    assert ipv4_ele is not None
    assert dhcp_ele is not None
    rel_list = []
    # need to add condition to check target handle since we added ethiiif
    for child_ele in dhcp_ele:
        if child_ele.tag == 'Relation' and \
           child_ele.get('type') == 'UsesIf':
            rel_list.append(child_ele.get('target'))
    assert '2203' not in rel_list