def test_load_nonfile(stc):
    pkg = "spirent.methodology"

    test_xml = get_test_xml()
    neg_test_xml = "<xxx/>"
    with open("TemplateXmlTestFile.xml", "w") as f:
        f.write(neg_test_xml)
    assert os.path.isfile("TemplateXmlTestFile.xml")

    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()
    hnd_reg = CHandleRegistry.Instance()

    cmd = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)
    cmd.Set("AutoExpandTemplate", False)
    LoadCmd.get_this_cmd = MagicMock(return_value=cmd)

    res = LoadCmd.run(
        1, "", test_xml,
        os.path.join(os.getcwd(), "IncorrectFileName.xml"),
        "", False, True, 0)
    # Note carefully the name of the file above is incorrect...
    assert res is False
    StmTemplateConfig = hnd_reg.Find(cmd.Get("StmTemplateConfig"))
    assert StmTemplateConfig is None
    os.remove("TemplateXmlTestFile.xml")
    assert not os.path.isfile("TemplateXmlTestFile.xml")
    return
def test_load_run(stc):
    pkg = "spirent.methodology"
    test_xml = get_test_xml()

    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()
    hnd_reg = CHandleRegistry.Instance()
    cmd = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)
    cmd.SetCollection('TargetTagList', ['meports'])
    LoadCmd.get_this_cmd = MagicMock(return_value=cmd)

    # Create the tag for the ports...
    tags = project.GetObject("Tags")
    tagMePorts = ctor.Create("Tag", tags)
    tagMePorts.Set("Name", "meports")
    tags.AddObject(tagMePorts, RelationType("UserTag"))

    port1 = ctor.Create("Port", project)
    port1.Set("Location", "//10.14.16.27/2/1")
    port1.AddObject(tagMePorts, RelationType("UserTag"))
    res = LoadCmd.run(1, ["meports"], test_xml, "", "", True, False, 0)
    assert res is True

    # Verify the StmTemplateConfig object was correctly created...
    container_hnd = cmd.Get("StmTemplateConfig")
    assert container_hnd is not 0
    container = hnd_reg.Find(container_hnd)
    assert container is not None
    assert container.IsTypeOf("StmTemplateConfig")
    assert container.Get("TemplateXml") == test_xml
    return
def test_load_file(stc):
    pkg = "spirent.methodology"

    test_xml = get_test_xml()
    with open("TemplateXmlTestFile.xml", "w") as f:
        f.write(test_xml)
    assert os.path.isfile("TemplateXmlTestFile.xml")

    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()
    hnd_reg = CHandleRegistry.Instance()

    cmd = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)
    cmd.Set("AutoExpandTemplate", False)
    LoadCmd.get_this_cmd = MagicMock(return_value=cmd)

    res = LoadCmd.run(
        1, "", "<xxx/>",
        os.path.join(os.getcwd(), "TemplateXmlTestFile.xml"),
        "", False, True, 0)
    assert res is True
    StmTemplateConfig = hnd_reg.Find(cmd.Get("StmTemplateConfig"))
    assert StmTemplateConfig is not None
    xml = StmTemplateConfig.Get("TemplateXml")
    assert xml == test_xml
    os.remove("TemplateXmlTestFile.xml")
    assert not os.path.isfile("TemplateXmlTestFile.xml")
    return
def test_validate_contained_cmds(stc):
    pkg = "spirent.methodology"
    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()

    cmd = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)

    # Test the situation where there is an ExpandTemplateCommand
    # in the group
    exp_cmd = ctor.Create(pkg + ".ExpandTemplateCommand", cmd)
    cmd.SetCollection("CommandList", [exp_cmd.GetObjectHandle()])
    res = LoadCmd.validate_contained_cmds(cmd)
    assert "spirent.methodology.ExpandTemplateCommand is " + \
        "not allowed " in res

    # Test the situation where there is an invalid command
    # in the group.
    other_cmd = ctor.Create("ArpNdStartCommand", cmd)
    cmd.SetCollection("CommandList", [other_cmd.GetObjectHandle()])
    res = LoadCmd.validate_contained_cmds(cmd)
    assert "Command arpndstartcommand not in the set of " in res
def test_validate(stc):
    pkg = "spirent.methodology"
    test_xml = get_test_xml()

    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()

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

    res = LoadCmd.validate(1, [], test_xml, "", "", True, False, 0)
    assert res == ""
def test_config_contained_cmds(stc):
    pkg = "spirent.methodology"
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()
    cmd = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)
    add_cmd = ctor.Create(pkg + ".AddTemplateObjectCommand", cmd)
    conf_cmd = ctor.Create(pkg + ".ConfigTemplateRelationCommand", cmd)
    mod_cmd = ctor.Create(pkg + ".ModifyTemplatePropertyCommand", cmd)
    del_cmd = ctor.Create(pkg + ".DeleteTemplateObjectCommand", cmd)
    exp_cmd = ctor.Create(pkg + ".ExpandTemplateCommand", cmd)
    add_cmd2 = ctor.Create(pkg + ".AddTemplateObjectCommand", cmd)
    iter_cmd = ctor.Create(pkg + ".ObjectIteratorCommand", cmd)
    mrg_cmd = ctor.Create(pkg + ".MergeTemplateCommand", cmd)
    add_cmd3 = ctor.Create(pkg + ".AddTemplateObjectCommand", sequencer)
    modifier_cmd = ctor.Create(pkg + ".ConfigTemplateStmPropertyModifierCommand",
                               cmd)
    cmd.SetCollection("CommandList", [add_cmd.GetObjectHandle(),
                                      conf_cmd.GetObjectHandle(),
                                      mod_cmd.GetObjectHandle(),
                                      del_cmd.GetObjectHandle(),
                                      exp_cmd.GetObjectHandle(),
                                      add_cmd2.GetObjectHandle(),
                                      iter_cmd.GetObjectHandle(),
                                      mrg_cmd.GetObjectHandle(),
                                      modifier_cmd.GetObjectHandle()])
    sequencer.SetCollection("CommandList", [cmd.GetObjectHandle(),
                                            add_cmd3.GetObjectHandle()])

    container = ctor.Create("StmTemplateConfig", project)
    res = LoadCmd.config_contained_cmds(cmd, container)

    # Note that the ExpandTemplateCommand is skipped.  It would have
    # errored on validate if validate was called first.
    assert res == 7

    # Check the normal commands
    for contained_cmd in [add_cmd, conf_cmd, mod_cmd,
                          del_cmd, add_cmd2, mrg_cmd,
                          modifier_cmd]:
        assert contained_cmd.Get("StmTemplateConfig") == \
            container.GetObjectHandle()

    # Check the ExpandTemplateCommand
    # Nothing is configured for this since it isn't a valid command
    # in this group.
    assert len(exp_cmd.GetCollection("StmTemplateConfigList")) == 0

    # Check the negative test case
    assert add_cmd3.Get("StmTemplateConfig") == 0
    return
def test_no_template_source(stc):
    pkg = "spirent.methodology"
    test_xml = get_test_xml()

    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()
    cmd = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)
    cmd.SetCollection('TargetTagList', ['meports'])
    LoadCmd.get_this_cmd = MagicMock(return_value=cmd)

    # Create the tag for the ports...
    tags = project.GetObject("Tags")
    tagMePorts = ctor.Create("Tag", tags)
    tagMePorts.Set("Name", "meports")
    tags.AddObject(tagMePorts, RelationType("UserTag"))

    port1 = ctor.Create("Port", project)
    port1.Set("Location", "//10.14.16.27/2/1")
    port1.AddObject(tagMePorts, RelationType("UserTag"))
    res = LoadCmd.run(1, ["meports"], test_xml, "", "", True, True, 0)
    assert res is False
    return
def test_load_on_complete_auto_expand(stc):
    pkg = "spirent.methodology"
    test_xml = get_test_xml()

    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")

    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()

    # Create the tag for the ports...
    tags = project.GetObject("Tags")
    tagMePorts = ctor.Create("Tag", tags)
    tagMePorts.Set("Name", "meports")
    tags.AddObject(tagMePorts, RelationType("UserTag"))

    port1 = ctor.Create("Port", project)
    port1.Set("Location", "//10.14.16.27/2/1")
    port1.AddObject(tagMePorts, RelationType("UserTag"))

    cmd = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)

    container = ctor.Create("StmTemplateConfig", project)
    container.Set("TemplateXml", test_xml)
    cmd.Set("StmTemplateConfig", container.GetObjectHandle())

    LoadCmd.get_this_cmd = MagicMock(return_value=cmd)

    # Test AutoExpandTemplate when disabled
    cmd.Set("AutoExpandTemplate", False)
    cmd.Set("EnableLoadFromFilename", False)
    cmd.SetCollection("TargetTagList", ["meports"])
    res = LoadCmd.on_complete([])
    assert res is True

    # Verify that nothing was created
    created_stuff = container.GetObjects("Scriptable",
                                         RelationType("GeneratedObject"))
    assert len(created_stuff) == 0

    # Test AutoExpandTemplate when enabled
    cmd.Set("AutoExpandTemplate", True)
    cmd.SetCollection("TargetTagList", ["meports"])
    res = LoadCmd.on_complete([])
    assert res is True

    # Done with the command object...
    cmd.MarkDelete()

    # Verify that something was created
    created_stuff = container.GetObjects("Scriptable",
                                         RelationType("GeneratedObject"))
    assert len(created_stuff) == 1
    created_stuff_hnd_list = []
    for created_thing in created_stuff:
        created_stuff_hnd_list.append(created_thing.GetObjectHandle())

    dev = project.GetObject("EmulatedDevice")

    assert tags.GetObjectHandle() not in created_stuff_hnd_list
    assert dev.GetObjectHandle() in created_stuff_hnd_list

    # Note that the created_stuff is tested in the expand command
    # and does not need to be repeated here.
    return