def test_on_complete(stc):
    sequencer = CStcSystem.Instance().GetObject('Sequencer')
    project = CStcSystem.Instance().GetObject('Project')
    ctor = CScriptableCreator()
    mix = ctor.Create('StmProtocolMix', project)
    mix.Set("MixInfo", mix_info())
    cmd = ctor.Create(TPKG + '.CreateTrafficMixCommand', sequencer)
    cmd.Set('MixTagName', 'TheMix')
    cmd.Set('StmTemplateMix', mix.GetObjectHandle())
    cmd.Set('AutoExpandTemplateMix', False)
    cmd.Set("GroupCommandTagInfo", "{\"fakeJson\": 3}")
    CreateTMix2Cmd.get_this_cmd = MagicMock(return_value=cmd)
    # Verify that no failed children will pass...
    res = CreateTMix2Cmd.on_complete([])
    assert cmd.Get("Status") == ""
    assert res
    # Verify that any element in the list (representing a child command)
    # is interpreted as a failed command and will fail...
    res = CreateTMix2Cmd.on_complete([1])
    assert not res
def test_autoexpand(stc):
    stc_sys = CStcSystem.Instance()
    sequencer = CStcSystem.Instance().GetObject('Sequencer')
    project = stc_sys.GetObject("Project")
    ctor = CScriptableCreator()

    port1 = ctor.Create("Port", project)
    port1.Set("Location", "//10.14.16.27/2/1")
    port2 = ctor.Create("Port", project)
    port2.Set("Location", "//10.14.16.27/2/2")

    etcu1 = ctor.Create("EthernetCopper", port1)
    port1.AddObject(etcu1, RelationType("ActivePhy"))
    etcu2 = ctor.Create("EthernetCopper", port2)
    port2.AddObject(etcu2, RelationType("ActivePhy"))

    tags = project.GetObject("Tags")
    if tags is None:
        tags = ctor.Create("Tags", project)
    assert tags is not None

    east_port_group_tag = ctor.Create("Tag", tags)
    east_port_group_tag.Set("Name", "East Port Group")
    west_port_group_tag = ctor.Create("Tag", tags)
    west_port_group_tag.Set("Name", "West Port Group")
    port1.AddObject(east_port_group_tag, RelationType("UserTag"))
    port2.AddObject(west_port_group_tag, RelationType("UserTag"))
    tags.AddObject(east_port_group_tag, RelationType("UserTag"))
    tags.AddObject(west_port_group_tag, RelationType("UserTag"))

    # East Device
    dev1 = ctor.Create("EmulatedDevice", project)
    ipv4if1 = ctor.Create("Ipv4If", dev1)
    ethiiif1 = ctor.Create("EthIIIf", dev1)
    ipv4if1.AddObject(ethiiif1, RelationType("StackedOnEndpoint"))
    dev1.AddObject(ipv4if1, RelationType("PrimaryIf"))
    dev1.AddObject(ethiiif1, RelationType("TopLevelIf"))
    dev1.AddObject(port1, RelationType("AffiliationPort"))
    east_ip_tag = ctor.Create("Tag", tags)
    east_ip_tag.Set("Name", "East Ipv4If")
    east_dev_tag = ctor.Create("Tag", tags)
    east_dev_tag.Set("Name", "East Dev")
    dev1.AddObject(east_dev_tag, RelationType("UserTag"))
    ipv4if1.AddObject(east_ip_tag, RelationType("UserTag"))

    # West Device
    dev2 = ctor.Create("EmulatedDevice", project)
    ipv4if2 = ctor.Create("Ipv4If", dev2)
    ethiiif2 = ctor.Create("EthIIIf", dev2)
    ipv4if2.AddObject(ethiiif2, RelationType("StackedOnEndpoint"))
    dev2.AddObject(ipv4if2, RelationType("PrimaryIf"))
    dev2.AddObject(ethiiif2, RelationType("TopLevelIf"))
    dev2.AddObject(port2, RelationType("AffiliationPort"))
    west_ip_tag = ctor.Create("Tag", tags)
    west_ip_tag.Set("Name", "West Ipv4If")
    west_dev_tag = ctor.Create("Tag", tags)
    west_dev_tag.Set("Name", "West Dev")
    dev2.AddObject(west_dev_tag, RelationType("UserTag"))
    ipv4if2.AddObject(west_ip_tag, RelationType("UserTag"))

    # Create an StmTrafficMix object as if the endpoints have
    # already been added and the topology templates have already
    # been expanded.
    trf_mix = ctor.Create("StmTrafficMix", project)

    # Create a child StmTemplateConfig
    container = ctor.Create("StmTemplateConfig", trf_mix)
    container.Set("TemplateXml", get_trf_template())

    # Build the InputJson
    json_dict = {
        "load": 250,
        "loadUnits": "FRAMES_PER_SECOND",
        "components": [
            {
                "baseTemplateFile": "Ipv4_Streams.xml",
                "weight": "75%",
                "postExpandModify": [
                    {
                        "streamBlockExpand": {
                            "endpointMapping": {
                                "srcBindingTagList": [east_ip_tag.Get("Name")],
                                "dstBindingTagList": [west_ip_tag.Get("Name")]
                            }
                        }
                    }
                ]
            }
        ]
    }
    trf_mix.Set("MixInfo", json.dumps(json_dict))

    # Check the lack of any project-level streamblocks
    sb_list = project.GetObjects("StreamBlock")
    assert len(sb_list) == 0

    # Create CreateTrafficMixCommand and execute on_complete with auto expand
    cmd = ctor.Create(TPKG + '.CreateTrafficMixCommand', sequencer)
    cmd.Set('MixTagName', 'TheMix')
    cmd.Set('StmTemplateMix', trf_mix.GetObjectHandle())
    cmd.Set('AutoExpandTemplateMix', True)
    cmd.Set("GroupCommandTagInfo", "{\"fakeJson\": 3}")
    CreateTMix2Cmd.get_this_cmd = MagicMock(return_value=cmd)
    res = CreateTMix2Cmd.on_complete([])
    assert cmd.Get("Status") == ""
    assert res

    # Check the lack of any project-level streamblocks
    sb_list = project.GetObjects("StreamBlock")
    assert len(sb_list) == 0

    # Check the generated objects
    sb_list = port1.GetObjects("StreamBlock")
    assert len(sb_list) == 1
    sb = sb_list[0]
    assert sb is not None
    src_binding = sb.GetObject("NetworkInterface", RelationType("SrcBinding"))
    dst_binding = sb.GetObject("NetworkInterface", RelationType("DstBinding"))
    assert src_binding is not None
    assert dst_binding is not None
    assert src_binding.GetObjectHandle() == ipv4if1.GetObjectHandle()
    assert dst_binding.GetObjectHandle() == ipv4if2.GetObjectHandle()
    exp_dst_port = sb.GetObject('Port', RelationType('ExpectedRx'))
    assert exp_dst_port is not None
    assert exp_dst_port.GetObjectHandle() == port2.GetObjectHandle()
    tag_list = sb.GetObjects('Tag', RelationType('UserTag'))
    assert 1 == len(tag_list)
    assert 'ttStreamBlock' == tag_list[0].Get('Name')

    # port2 should have nothing
    sb_list = port2.GetObjects("StreamBlock")
    assert not len(sb_list)