def test_validate(stc):
    stc_sys = CStcSystem.Instance()
    sequencer = stc_sys.GetObject('Sequencer')
    ctor = CScriptableCreator()
    command1 = ctor.Create(TTPKG + '.LoadAclTrafficRulesCommand', sequencer)
    command2 = ctor.Create(TPKG + '.LoadTrafficTemplateCommand', sequencer)
    Cmd1.get_this_cmd = MagicMock(return_value=command1)
    Cmd2.get_this_cmd = MagicMock(return_value=command2)

    rules = 'test_CsvTraffic.rules'
    try:
        with open(rules, "w") as f:
            f.write(rules_file_content1())
        msg = Cmd1.validate(1, [], template1(), '', '', False, False, None, 0.0, rules, 1, True)
        assert msg == ''
        msg = Cmd1.validate(1, [], template1(), '', '', False, False, None, 0.0, rules, 0, True)
        assert msg != ''
    finally:
        os.remove(rules)
    return
def test_run(stc):
    stc_sys = CStcSystem.Instance()
    proj = stc_sys.GetObject('Project')
    sequencer = stc_sys.GetObject('Sequencer')
    ctor = CScriptableCreator()
    command1 = ctor.Create(TTPKG + '.LoadAclTrafficRulesCommand', sequencer)
    command2 = ctor.Create(TPKG + '.LoadTrafficTemplateCommand', sequencer)
    command3 = ctor.Create(PKG + '.ModifyTemplatePropertyCommand', sequencer)
    command4 = ctor.Create(TPKG + '.ExpandTrafficMix1Command', sequencer)
    Cmd1.get_this_cmd = MagicMock(return_value=command1)
    Cmd2.get_this_cmd = MagicMock(return_value=command2)
    Cmd3.get_this_cmd = MagicMock(return_value=command3)
    Cmd4.get_this_cmd = MagicMock(return_value=command4)

    traf_mix = ctor.Create('StmTrafficMix', proj)
    assert traf_mix is not None
    mix_info = {'Load': str(100.0), 'LoadUnit': 'PERCENT_LINE_RATE', 'WeightList': ''}
    mix_elem = etree.Element('MixInfo', mix_info)
    assert mix_elem is not None
    xml = etree.tostring(mix_elem)
    traf_mix.Set('MixInfo', xml)
    assert xml == traf_mix.Get('MixInfo')
    mix = traf_mix.GetObjectHandle()
    assert mix is not None

    rules = 'test_CsvTraffic.rules'
    try:
        with open(rules, "w") as f:
            f.write(rules_file_content1())

        msg = Cmd1.validate(1, [], template1(), '', '', False, False, mix, 0.0, rules, 1, True)
        assert msg == ''
        assert Cmd1.run(1, [], template1(), '', 'bad', False, False, mix, 40.0, rules, 2, True)

        Cmd4.run(mix)

        e = etree.fromstring(traf_mix.Get('MixInfo'))
        assert e is not None
        for w in e.get('WeightList').split(' '):
            assert float(w) == 4.0

        sb_list = tag_utils.get_tagged_objects_from_string_names(['badttStreamBlock'])
        assert len(sb_list) == 10
        for sb in sb_list:
            load_prof = sb.GetObject('StreamBlockLoadProfile',
                                     RelationType('AffiliationStreamBlockLoadProfile'))
            assert load_prof.Get('Load') == 4.0
        rm_list = tag_utils.get_tagged_objects_from_string_names(['badttDstMac'])
        assert len(rm_list) == 10
        for rm, v in zip(rm_list, ['00:01:02:03:04:05',
                                   '00:01:02:03:FE:FF',
                                   '00:01:02:03:FF:FF',
                                   '00:00:01:00:00:01',
                                   '00:00:01:00:00:01',
                                   '00:01:02:03:04:05',
                                   '00:01:02:03:FE:FE',
                                   '00:01:02:03:FF:FE',
                                   '00:00:01:00:00:01',
                                   '00:00:01:00:00:01']):
            assert rm.Get('Data') == v
        rm_list = tag_utils.get_tagged_objects_from_string_names(['badttSrcMac'])
        assert len(rm_list) == 10
        for rm, v in zip(rm_list, ['00:01:02:03:33:FF',
                                   '00:01:02:03:FF:00',
                                   '00:01:02:04:00:00',
                                   '00:10:94:00:00:01',
                                   '00:10:94:00:00:01',
                                   '00:01:02:03:33:FF',
                                   '00:01:02:03:FF:01',
                                   '00:01:02:04:00:01',
                                   '00:10:94:00:00:01',
                                   '00:10:94:00:00:01']):
            assert rm.Get('Data') == v
        rm_list = tag_utils.get_tagged_objects_from_string_names(['badttDstIpAddr'])
        assert len(rm_list) == 10
        for rm, v in zip(rm_list, ['1.1.1.1',
                                   '3.1.2.0',
                                   '4.1.2.0',
                                   '193.85.1.3',
                                   '193.85.1.3',
                                   '1.1.1.1',
                                   '3.1.2.1',
                                   '4.1.2.1',
                                   '193.85.1.3',
                                   '193.85.1.3']):
            assert rm.Get('Data') == v
        rm_list = tag_utils.get_tagged_objects_from_string_names(['badttSrcIpAddr'])
        assert len(rm_list) == 10
        for rm, v in zip(rm_list, ['4.1.1.1',
                                   '6.1.0.255',
                                   '5.1.2.0',
                                   '41.1.1.1',
                                   '12.1.1.1',
                                   '4.1.1.1',
                                   '6.1.0.254',
                                   '5.1.2.1',
                                   '41.1.1.1',
                                   '12.1.1.1']):
            assert rm.Get('Data') == v
    finally:
        os.remove(rules)
    return