def test_command_verify_arp(stc):
    ctor = CScriptableCreator()
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    port = ctor.Create("Port", project)
    handle_list = [port.GetObjectHandle()]

    cmd = ctor.CreateCommand("spirent.methodology.L2L3LearningCommand")
    gtc_p = patch("spirent.methodology.L2L3LearningCommand.get_this_cmd",
                  new=MagicMock(return_value=cmd))
    gtc_p.start()
    verify_cmd = ctor.CreateCommand("ArpNdVerifyResolvedCommand")
    verify_cmd.Set('PassFailState', 'FAILED')
    verify_cmd.Set('State', 'COMPLETED')
    verify_p = patch("spirent.methodology.L2L3LearningCommand.verify_arp",
                     new=MagicMock(return_value=verify_cmd))
    verify_p.start()

    # VerifyArp is True
    assert not learningCmd.run(handle_list, [], False, True,
                               "TX_RX", True, False, True)
    assert "ArpNdVerifyResolvedCommand did not pass" in cmd.Get("Status")

    # VerifyArp is False
    assert learningCmd.run(handle_list, [], False, True,
                           "TX_RX", True, False, False)
    gtc_p.stop()
    verify_p.stop()
    return
def test_command_fail(stc):
    ctor = CScriptableCreator()
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    port = ctor.Create("Port", project)
    handle_list = [port.GetObjectHandle()]

    cmd = ctor.CreateCommand("spirent.methodology.L2L3LearningCommand")
    gtc_p = patch("spirent.methodology.L2L3LearningCommand.get_this_cmd",
                  new=MagicMock(return_value=cmd))
    gtc_p.start()

    # Mock the function to return the command that's in a failed state
    l2StartCmd = ctor.CreateCommand("L2LearningStartCommand")
    l2StartCmd.Set('State', 'FAILED')
    l2start_p = patch("spirent.methodology.L2L3LearningCommand.l2_learning_start",
                      new=MagicMock(return_value=l2StartCmd))
    l2start_p.start()

    # Ensure the STAK command fails when the BLL command fails
    assert not learningCmd.run(handle_list, [], True, False,
                               "TX_RX", True, False, False)
    assert "L2LearningStartCommand did not pass" in cmd.Get('Status')
    gtc_p.stop()
    l2start_p.stop()
    return
def test_run(stc):
    sequencer = CStcSystem.Instance().GetObject("Sequencer")
    ctor = CScriptableCreator()
    cmd = ctor.Create("spirent.methodology.L2L3LearningCommand", sequencer)
    gtc_p = patch("spirent.methodology.L2L3LearningCommand.get_this_cmd",
                  new=MagicMock(return_value=cmd))
    gtc_p.start()

    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject("Project")
    port = ctor.Create("Port", project)
    handle_list = [port.GetObjectHandle()]

    rx_port = ctor.Create("Port", project)
    stream_block = ctor.Create("StreamBlock", port)
    stream_block.AddObject(rx_port, RelationType("ExpectedRx"))
    tag_utils.add_tag_to_object(stream_block, "ttStreamBlock")
    em_device = ctor.Create("EmulatedDevice", project)
    em_device.AddObject(port, RelationType("AffiliationPort"))
    tag_utils.add_tag_to_object(em_device, "ttEmulatedDevice")
    invalid = ctor.Create("BgpRouterConfig", em_device)

    # L2Learning
    assert learningCmd.run(handle_list, [], True, False,
                           "TX_RX", True, True, False)

    # L3Learning
    assert learningCmd.run([], ["ttStreamBlock"], False, True,
                           "TX_RX", False, False, False)

    # L2L3Learning
    assert learningCmd.run(handle_list, ["ttStreamBlock"], True, True,
                           "TX_RX", True, True, False)

    # L2Learning with invalid handle type and valid tag
    assert not learningCmd.run([em_device.GetObjectHandle()], ["ttStreamBlock"], True, False,
                               "TX_RX", True, False, False)
    assert "Invalid handle type: emulateddevice. L2 learning command " + \
        "only allows handles of type Port and StreamBlock" in cmd.Get('Status')

    # L2Learning with invalid handle type and valid handle
    assert not learningCmd.run([em_device.GetObjectHandle(), port.GetObjectHandle()],
                               ["ttStreamBlock"], True, False, "TX_RX", True, False, False)
    assert "Invalid handle type: emulateddevice. L2 learning command " + \
        "only allows handles of type Port and StreamBlock" in cmd.Get('Status')

    # L2L3Learning with invalid handle type and valid handle
    assert not learningCmd.run([invalid.GetObjectHandle(), port.GetObjectHandle()],
                               ["ttStreamBlock"], True, True, "TX_RX", True, False, False)
    assert "Invalid handle type: bgprouterconfig. Learning command " + \
        "only allows handles of type Port, StreamBlock, Host, Router, " + \
        "and EmulatedDevice" in cmd.Get('Status')

    # Empty ObjectList and TagNameList
    assert learningCmd.run([], [], True, True,
                           "TX_RX", True, True, False)

    # L2L3Learning with only valid handle for L3
    assert learningCmd.run([em_device.GetObjectHandle()], [], True, True,
                           "TX_RX", True, False, False)

    # L2L3Learning with only valid tag for L3
    assert learningCmd.run([], ["ttEmulatedDevice"], True, True,
                           "TX_RX", True, True, False)

    # L2Learning with only valid tag for L3
    assert learningCmd.run([], ["ttEmulatedDevice"], True, False,
                           "TX_RX", True, True, False)

    gtc_p.stop()
    return