def test_change_endpoint_nextstate(): uss = Update_Switch_State() uss.first_time = False endpoint_data = dict({ 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None }) hash_value = '3da53a95ae5d034ae37b539a24370260a36f8bb2' state = 'KNOWN' make_endpoint_dict(uss.endpoints.state, hash_value, state, endpoint_data) answer = dict({ '3da53a95ae5d034ae37b539a24370260a36f8bb2': { 'state': 'KNOWN', 'next-state': 'TEST_STATE', 'endpoint': { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None } } }) uss.endpoints.change_endpoint_nextstate( '3da53a95ae5d034ae37b539a24370260a36f8bb2', next_state='TEST_STATE') #assert str(answer) == str(dict(uss.endpoint_states)) for key in answer: assert answer[key]['state'] == uss.endpoints.state[key].state assert answer[key]['next-state'] == uss.endpoints.state[key].next_state for key2 in answer[key]['endpoint']: assert answer[key]['endpoint'][key2] == uss.endpoints.state[ key].endpoint_data[key2]
def test_make_endpoint_dict(): uss = Update_Switch_State() uss.first_time = False endpoint_data = dict({ 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None }) hash_value = '3da53a95ae5d034ae37b539a24370260a36f8bb2' state = 'KNOWN' uss.make_endpoint_dict(hash_value, state, endpoint_data) answer = dict({ '3da53a95ae5d034ae37b539a24370260a36f8bb2': { 'state': 'KNOWN', 'next-state': 'NONE', 'endpoint': { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None } } }) assert str(answer) == str(dict(uss.endpoint_states))
def test_return_endpoint_state(): uss = Update_Switch_State() uss.first_time = False endpoint_data = dict({ 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None }) hash_value = '3da53a95ae5d034ae37b539a24370260a36f8bb2' state = 'TEST_STATE' make_endpoint_dict(uss.endpoints.state, hash_value, state, endpoint_data) answer = dict({ '3da53a95ae5d034ae37b539a24370260a36f8bb2': EndPoint( { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None }, prev_state='NONE', state='TEST_STATE', next_state='NONE') }) eps = uss.return_endpoint_state() key = '3da53a95ae5d034ae37b539a24370260a36f8bb2' assert eps.state[key].state == answer[key].state assert eps.state[key].next_state == answer[key].next_state for key2 in eps.state[key].endpoint_data: assert eps.state[key].endpoint_data[key2] == answer[key].endpoint_data[ key2]
def test_file_new_machines_later(): uss = Update_Switch_State() uss.first_time = False machines = [{ 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'active': 1, 'port': 1, 'name': None }, { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'active': 1, 'port': 1, 'name': None }] answer = dict({ 'd502caea3609d553ab16a00c554f0602c1419f58': { 'state': 'UNKNOWN', 'next-state': 'NONE', 'endpoint': { 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'active': 1, 'port': 1, 'name': None } }, '3da53a95ae5d034ae37b539a24370260a36f8bb2': { 'state': 'UNKNOWN', 'next-state': 'NONE', 'endpoint': { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'active': 1, 'port': 1, 'name': None } } }) uss.find_new_machines(machines) #assert str(answer) == str(dict(uss.endpoint_states)) for key in answer: assert answer[key]['state'] == uss.endpoints.state[key].state assert answer[key]['next-state'] == uss.endpoints.state[key].next_state for key2 in answer[key]['endpoint']: assert answer[key]['endpoint'][key2] == uss.endpoints.state[ key].endpoint_data[key2]
def test_find_new_machines_first_time(): uss = Update_Switch_State() uss.first_time = True machines = [{ 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'active': 1, 'port': 1, 'name': None }, { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'active': 1, 'port': 1, 'name': None }] uss.find_new_machines(machines) answer = dict({ "d502caea3609d553ab16a00c554f0602c1419f58": { "state": "KNOWN", "next-state": "NONE", "endpoint": { "ip-address": "10.0.0.101", "mac": "f8:b1:56:fe:f2:de", "segment": "prod", "tenant": "FLOORPLATE", "active": 1, "port": 1, "name": None } }, "3da53a95ae5d034ae37b539a24370260a36f8bb2": { "state": "KNOWN", "next-state": "NONE", "endpoint": { "ip-address": "10.0.0.99", "mac": "20:4c:9e:5f:e3:c3", "segment": "to-core-router", "tenant": "EXTERNAL", "active": 1, "port": 1, "name": None } } }) #assert str(answer) == str(dict(uss.endpoint_states)) for key in answer: assert answer[key]['state'] == uss.endpoints.state[key].state for next_key in answer[key]['endpoint']: assert answer[key]['endpoint'][next_key] == uss.endpoints.state[ key].endpoint_data[next_key]
def test_print_endpoint_state(): uss = Update_Switch_State() uss.first_time = False endpoint_data = dict({'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None}) hash_value = '3da53a95ae5d034ae37b539a24370260a36f8bb2' state = 'TEST_STATE' make_endpoint_dict(uss.endpoints.state, hash_value, state, endpoint_data) uss.endpoints.print_endpoint_state()
def test_get_endpoint_ip(): class Mock_bcf(): def __init__(self): pass uss = Update_Switch_State() uss.first_time = False uss.bcf = Mock_bcf() machines = [{ 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'name': None }, { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None }] uss.find_new_machines(machines) retval = uss.get_endpoint_ip('3da53a95ae5d034ae37b539a24370260a36f8bb2') answer = '10.0.0.99' assert retval == answer retval = uss.get_endpoint_ip('NOT-A-HASH') answer = None assert retval == answer
def test_unmirror_endpoint(): class Mock_bcf(): def __init__(self): pass def unmirror_ip(self, my_ip): assert my_ip == '10.0.0.99' uss = Update_Switch_State() uss.first_time = False uss.sdnc = Mock_bcf() machines = [{ 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'name': None }, { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None }] uss.find_new_machines(machines) assert uss.unmirror_endpoint('3da53a95ae5d034ae37b539a24370260a36f8bb2') assert not uss.unmirror_endpoint( '4da53a95ae5d034ae37b539a24370260a36f8bb2')
def test_first_run(): uss = Update_Switch_State() uss.mod_configuration = dict() uss.configured = True uss.mod_configuration['controller_uri'] = 'TEST_URI' uss.mod_configuration['controller_user'] = '******' uss.mod_configuration['controller_pass'] = '******' uss.first_run() assert uss.controller['URI'] == 'TEST_URI' assert uss.controller['USER'] == 'TEST_USER' assert uss.controller['PASS'] == 'TEST_PASS'
def test_mirror_endpoint(): class Mockbcf(): def __init__(self): pass def mirror_ip(self, ip, messages=None): assert ip == '10.0.0.99' uss = Update_Switch_State() uss.sdnc = Mockbcf() stuff = dict({ 'd502caea3609d553ab16a00c554f0602c1419f58': EndPoint( { 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'active': 1, 'name': None }, prev_state='NONE', state='UNKNOWN', next_state='NONE'), '3da53a95ae5d034ae37b539a24370260a36f8bb2': EndPoint( { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'active': 1, 'name': None }, prev_state='NONE', state='KNOWN', next_state='NONE'), }) for s in stuff: uss.endpoints.state[s] = stuff[s] ret_val = uss.mirror_endpoint('3da53a95ae5d034ae37b539a24370260a36f8bb2') assert ret_val ret_val = uss.mirror_endpoint('NOT_A_HASH') assert not ret_val
def test_get_endpoinit_next(): uss = Update_Switch_State() uss.first_time = False endpoint_data = dict({'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None}) hash_value = '3da53a95ae5d034ae37b539a24370260a36f8bb2' state = 'KNOWN' make_endpoint_dict(uss.endpoints.state, hash_value, state, endpoint_data) next_state = uss.endpoints.get_endpoint_next( '3da53a95ae5d034ae37b539a24370260a36f8bb2') assert 'NONE' == next_state next_state = uss.endpoints.get_endpoint_next('NOT-A-HASH') assert next_state is None
def test_make_known_endpoint(): class Mock_bcf(): def __init__(self): pass def unmirror_ip(self, my_ip): assert my_ip == '10.0.0.99' uss = Update_Switch_State() uss.first_time = False uss.bcf = Mock_bcf() machines = [{ 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'name': None }, { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None }] uss.find_new_machines(machines) uss.make_known_endpoint('3da53a95ae5d034ae37b539a24370260a36f8bb2') answer = dict({ 'd502caea3609d553ab16a00c554f0602c1419f58': { 'state': 'UNKNOWN', 'next-state': 'NONE', 'endpoint': { 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'name': None } }, '3da53a95ae5d034ae37b539a24370260a36f8bb2': { 'state': 'KNOWN', 'next-state': 'NONE', 'endpoint': { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None } } }) assert str(answer) == str(dict(uss.endpoint_states))
def test_first_run_unknown(): uss = Update_Switch_State() uss.mod_configuration = dict() uss.mod_configuration['controller_type'] = 'dummy' uss.configured = True uss.first_run() assert uss.controller['TYPE'] == 'dummy'
def test_first_run(): def BcfProxy(uri, auth): internal_auth = {} internal_auth['password'] = '******' internal_auth['user'] = '******' assert uri == 'TEST_URI' assert str(auth) == str(internal_auth) uss = Update_Switch_State() uss.mod_configuration = dict() uss.configured = True uss.mod_configuration['controller_uri'] = 'TEST_URI' uss.mod_configuration['controller_user'] = '******' uss.mod_configuration['controller_pass'] = '******' uss.first_run() assert uss.controller['URI'] == 'TEST_URI' assert uss.controller['USER'] == 'TEST_USER' assert uss.controller['PASS'] == 'TEST_PASS'
def test_mirror_endpoint(): class Mockbcf(): def __init__(self): pass def mirror_ip(self, ip): assert ip == '10.0.0.99' uss = Update_Switch_State() uss.bcf = Mockbcf() uss.endpoint_states = dict({ 'd502caea3609d553ab16a00c554f0602c1419f58': { 'state': 'UNKNOWN', 'next-state': 'NONE', 'endpoint': { 'ip-address': '10.0.0.101', 'mac': 'f8:b1:56:fe:f2:de', 'segment': 'prod', 'tenant': 'FLOORPLATE', 'name': None } }, '3da53a95ae5d034ae37b539a24370260a36f8bb2': { 'state': 'KNOWN', 'next-state': 'NONE', 'endpoint': { 'ip-address': '10.0.0.99', 'mac': '20:4c:9e:5f:e3:c3', 'segment': 'to-core-router', 'tenant': 'EXTERNAL', 'name': None } } }) ret_val = uss.mirror_endpoint('3da53a95ae5d034ae37b539a24370260a36f8bb2') assert ret_val ret_val = uss.mirror_endpoint('NOT_A_HASH') assert not ret_val
def test_first_run_faucet(): uss = Update_Switch_State() uss.mod_configuration = dict() uss.mod_configuration['controller_type'] = 'faucet' uss.mod_configuration['controller_uri'] = 'localhost' uss.mod_configuration['controller_pass'] = '******' uss.mod_configuration['controller_user'] = '******' uss.mod_configuration[ 'controller_config_file'] = '/etc/ryu/faucet/faucet.yaml' uss.mod_configuration[ 'controller_log_file'] = '/var/log/ryu/faucet/faucet.log' uss.mod_configuration['controller_mirror_ports'] = '{"0xdeadbeef":"port1"}' uss.configured = True uss.first_run() assert uss.controller['TYPE'] == 'faucet' assert uss.controller['URI'] == 'localhost' assert uss.controller['USER'] == 'self' assert uss.controller['PASS'] == 'TEST_PASS' assert uss.controller['CONFIG_FILE'] == '/etc/ryu/faucet/faucet.yaml' assert uss.controller['LOG_FILE'] == '/var/log/ryu/faucet/faucet.log' assert uss.controller['MIRROR_PORTS'] == {'0xdeadbeef': 'port1'}
def test_first_run_bcf(): uss = Update_Switch_State() uss.mod_configuration = dict() uss.mod_configuration['controller_uri'] = 'TEST_URI' uss.mod_configuration['controller_user'] = '******' uss.mod_configuration['controller_pass'] = '******' uss.mod_configuration['controller_type'] = 'bcf' uss.mod_configuration['controller_span_fabric_name'] = 'TEST_SPAN' uss.mod_configuration['controller_interface_group'] = 'TEST_GROUP' uss.configured = True uss.first_run() assert uss.controller['URI'] == 'TEST_URI' assert uss.controller['USER'] == 'TEST_USER' assert uss.controller['PASS'] == 'TEST_PASS' assert uss.controller['TYPE'] == 'bcf' assert uss.controller['SPAN_FABRIC_NAME'] == 'TEST_SPAN' assert uss.controller['INTERFACE_GROUP'] == 'TEST_GROUP' uss = Update_Switch_State() uss.configured = False uss.first_run()