def test_expand_run_validate(stc):
    plLogger = PLLogger.GetLogger("methodology")
    plLogger.LogInfo("start.test_ExpandProtocolMixCommand2.test_expand_run_validate")
    ctor = CScriptableCreator()
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")

    test_xml = get_basic_template()
    north_test_xml = xml_utils.add_prefix_to_tags("North_", test_xml)
    south_test_xml = xml_utils.add_prefix_to_tags("South_", test_xml)

    cmd = ctor.CreateCommand(PKG + ".ExpandProtocolMixCommand2")
    ExpProtoMixCmd.get_this_cmd = MagicMock(return_value=cmd)

    # Test no StmTemplateMix
    res = ExpProtoMixCmd.run(0, "", 100)
    assert res is False

    # Create the StmTemplateMix
    proto_mix = ctor.Create("StmProtocolMix", project)

    # Test empty MixInfo
    res = ExpProtoMixCmd.run(proto_mix.GetObjectHandle(), "", 100)
    assert res is False

    mi = {}
    n_ti_dict = {}
    n_ti_dict["weight"] = 40.0
    n_ti_dict["devicesPerBlock"] = 10
    n_ti_dict["portGroupTag"] = "North Port Group"
    # n_ti_dict["deviceTag"] = "North_ttEmulatedClient"
    s_ti_dict = {}
    s_ti_dict["weight"] = 60.0
    s_ti_dict["staticDeviceCount"] = 10
    s_ti_dict["useStaticDeviceCount"] = False
    s_ti_dict["useBlock"] = False
    s_ti_dict["portGroupTag"] = "South Port Group"
    # s_ti_dict["deviceTag"] = "South_ttEmulatedClient"
    mi["templateInfo"] = [n_ti_dict, s_ti_dict]

    proto_mix.Set("MixInfo", json.dumps(mi))

    # Test mismatched TemplateInfo against StmTemplateConfigs
    res = ExpProtoMixCmd.run(proto_mix.GetObjectHandle(), "", 100)
    assert res is False

    # Create a child StmTemplateConfigs
    north_template = ctor.Create("StmTemplateConfig", proto_mix)
    north_template.Set("TemplateXml", north_test_xml)
    south_template = ctor.Create("StmTemplateConfig", proto_mix)
    south_template.Set("TemplateXml", south_test_xml)

    # Test not enough devices to distribute
    res = ExpProtoMixCmd.run(proto_mix.GetObjectHandle(), "", 1)
    assert res is False
def test_expand_proto_mix_invalid_device_count(stc):
    test_xml = get_basic_template()
    north_test_xml = xml_utils.add_prefix_to_tags("North_", test_xml)
    south_test_xml = xml_utils.add_prefix_to_tags("South_", test_xml)

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

    plLogger = PLLogger.GetLogger("test_expand_proto_mix")
    plLogger.LogDebug("start")

    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")

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

    north_port_group_tag = ctor.Create("Tag", tags)
    north_port_group_tag.Set("Name", "North Port Group")
    south_port_group_tag = ctor.Create("Tag", tags)
    south_port_group_tag.Set("Name", "South Port Group")
    port1.AddObject(north_port_group_tag, RelationType("UserTag"))
    port2.AddObject(south_port_group_tag, RelationType("UserTag"))
    tags.AddObject(north_port_group_tag, RelationType("UserTag"))
    tags.AddObject(south_port_group_tag, RelationType("UserTag"))

    # Create the StmTemplateMix
    proto_mix = ctor.Create("StmProtocolMix", project)
    mi = {}
    n_ti_dict = {}
    n_ti_dict["weight"] = 50.0
    n_ti_dict["staticDeviceCount"] = 30
    n_ti_dict["useStaticDeviceCount"] = True
    n_ti_dict["useBlock"] = False
    n_ti_dict["portGroupTag"] = "North Port Group"
    n_ti_dict["deviceTag"] = "North_ttEmulatedClient"
    s_ti_dict = {}
    s_ti_dict["weight"] = 50.0
    s_ti_dict["staticDeviceCount"] = 20
    s_ti_dict["useStaticDeviceCount"] = True
    s_ti_dict["useBlock"] = False
    s_ti_dict["portGroupTag"] = "South Port Group"
    s_ti_dict["deviceTag"] = "South_ttEmulatedClient"
    mi["templateInfo"] = [n_ti_dict, s_ti_dict]

    proto_mix.Set("MixInfo", json.dumps(mi))

    # Create a child StmTemplateConfig for North
    north_template = ctor.Create("StmTemplateConfig", proto_mix)
    north_template.Set("TemplateXml", north_test_xml)

    # Create a child StmTemplateConfig for South
    south_template = ctor.Create("StmTemplateConfig", proto_mix)
    south_template.Set("TemplateXml", south_test_xml)

    # Call Expand
    res = ExpProtoMixCmd.run(proto_mix.GetObjectHandle(), "", 10)
    assert res is False
    assert cmd.GetStatus("")