def test_generate_tagged_ports_no_exposed(stc):
    # The sample didn't have everything, so this unit test will take the
    # minimum needed
    input_dict = {
        "port_groups": [
            {
                "prop_id": "LeftPortGroup",
                "name": "Left",
                "ports": [
                    {
                        "location": "10.10.10.1/1/1"
                    }
                ]
            },
            {
                "prop_id": "RightPortGroup",
                "name": "Right",
                "ports": [
                    {
                        "location": "10.10.10.2/1/1"
                    }
                ]
            }
        ],
    }

    port_hnd_list, offline, err_str = RunCmd.generate_tagged_ports(input_dict)
    assert port_hnd_list == []
    assert err_str == "No exposed config object found"
def test_generate_tagged_ports_empty_port_list(stc):
    project = CStcSystem.Instance().GetObject('Project')
    ctor = CScriptableCreator()
    left_tag = tag_utils.get_tag_object('Left_Port_Group')
    right_tag = tag_utils.get_tag_object('Right_Port_Group')
    ep_cfg = ctor.Create('ExposedConfig', project)
    ep = ctor.Create('ExposedProperty', ep_cfg)
    ep.Set('EPNameId', 'LeftPortGroup')
    ep.Set('EPClassId', 'tag')
    ep.Set('EPPropertyId', 'scriptable.name')
    ep.AddObject(left_tag, RelationType('ScriptableExposedProperty'))
    ep = ctor.Create('ExposedProperty', ep_cfg)
    ep.Set('EPNameId', 'RightPortGroup')
    ep.Set('EPClassId', 'tag')
    ep.Set('EPPropertyId', 'scriptable.name')
    ep.AddObject(right_tag, RelationType('ScriptableExposedProperty'))

    # The sample didn't have everything, so this unit test will take the
    # minimum needed
    input_dict = {
        "port_groups": [
            {
                "prop_id": "LeftPortGroup",
                "name": "Left"
            },
            {
                "prop_id": "RightPortGroup",
                "name": "Right"
            }
        ],
    }

    port_hnd_list, offline, err_str = RunCmd.generate_tagged_ports(input_dict)
    assert port_hnd_list == []
    assert err_str == "Port group {} has empty port list"
def test_generate_tagged_ports(stc):
    project = CStcSystem.Instance().GetObject('Project')
    ctor = CScriptableCreator()
    left_tag = tag_utils.get_tag_object('Left_Port_Group')
    right_tag = tag_utils.get_tag_object('Right_Port_Group')
    ep_cfg = ctor.Create('ExposedConfig', project)
    ep = ctor.Create('ExposedProperty', ep_cfg)
    ep.Set('EPNameId', 'LeftPortGroup')
    ep.Set('EPClassId', 'tag')
    ep.Set('EPPropertyId', 'scriptable.name')
    ep.AddObject(left_tag, RelationType('ScriptableExposedProperty'))
    ep = ctor.Create('ExposedProperty', ep_cfg)
    ep.Set('EPNameId', 'RightPortGroup')
    ep.Set('EPClassId', 'tag')
    ep.Set('EPPropertyId', 'scriptable.name')
    ep.AddObject(right_tag, RelationType('ScriptableExposedProperty'))

    # The sample didn't have everything, so this unit test will take the
    # minimum needed
    input_dict_string = '''{
        "port_groups": [
            {
                "prop_id": "LeftPortGroup",
                "name": "Left",
                "bring_online": true,
                "ports": [
                    {
                        "location": "10.10.10.1/1/1"
                    }
                ]
            },
            {
                "prop_id": "RightPortGroup",
                "name": "Right",
                "ports": [
                    {
                        "location": "10.10.10.2/1/1"
                    }
                ]
            }
        ]
    }'''
    err_str, input_dict = json_utils.load_json(input_dict_string)
    assert err_str == ""

    port_hnd_list, offline, err_str = RunCmd.generate_tagged_ports(input_dict)

    assert 2 == len(port_hnd_list)
    assert False is offline
    assert err_str == ""
    hnd_reg = CHandleRegistry.Instance()
    port_list = [hnd_reg.Find(hnd) for hnd in port_hnd_list]
    tag0 = port_list[0].GetObject('Tag', RelationType('UserTag'))
    tag1 = port_list[1].GetObject('Tag', RelationType('UserTag'))
    assert left_tag.GetObjectHandle() == tag0.GetObjectHandle()
    assert port_list[0].Get('Name').startswith('Left 1')
    assert port_list[0].Get('Location') == '//10.10.10.1/1/1'
    assert right_tag.GetObjectHandle() == tag1.GetObjectHandle()
    assert port_list[1].Get('Name').startswith('Right 1')
    assert port_list[1].Get('Location') == '//10.10.10.2/1/1'

    # Test the bring_online parameter
    input_dict_string = '''{
        "port_groups": [
            {
                "prop_id": "LeftPortGroup",
                "name": "Left",
                "bring_online": false,
                "ports": [
                    {
                        "location": "10.10.10.1/1/1"
                    }
                ]
            },
            {
                "prop_id": "RightPortGroup",
                "name": "Right",
                "bring_online": true,
                "ports": [
                    {
                        "location": "10.10.10.2/1/1"
                    }
                ]
            }
        ]
    }'''
    err_str, input_dict = json_utils.load_json(input_dict_string)
    assert err_str == ""

    port_hnd_list, offline, err_str = RunCmd.generate_tagged_ports(input_dict)

    assert 2 == len(port_hnd_list)
    assert True is offline
    assert err_str == ""