# -*- coding: utf-8 -*- import random import validlib.power_strip as power_strip import validlib.link as link import validlib.spc300 as spc300 from validlib.spc300 import role, r_ping def test(): print print "Executing", __file__, "..." # The OK or NOK in the comments below refer to the results obtained with av-1.2.1 # test_shape() # test_kill() # test_split() # test_merge() # test_split_merge() # test_coexist() def test_shape(): test_shape_an_avln_cco_3_sta() # OK, except that everybody is UCCO def test_kill(): test_kill_cco_in_cco_2_sta() # OK, but the roles are not checked def test_split(): test_split_cco_1_sta_in_2_avlns_ucco() # OK, but the roles are not checked test_split_cco_3_sta_in_2_avlns_cco_1_sta() # (#2869) NOK, it happens that avln_2 cannot be shaped def test_merge(): test_merge_ucco_with_ucco() # OK, but the roles are not checked test_merge_cco_1_sta_with_ucco_scenario_1() # OK, but the roles are not checked test_merge_cco_1_sta_with_ucco_scenario_2() # (#2535) NOK, it happens that avln_1 cannot be shaped test_merge_cco_1_sta_with_cco_1_sta() # (#2243) NOK, the final AVLN can never be shaped def test_split_merge(): test_replace_cco_in_cco_1_sta() # OK, but the roles are not checked test_split_merge_split_cco_3_sta_in_2_avlns_cco_1_sta() # (#2543) To be done, when the split will work def test_coexist(): test_coexist_cco_1_sta_with_cco_1_sta_scenario_1() # (#2713) NOK test_coexist_cco_1_sta_with_cco_1_sta_scenario_2() # (#2713) NOK def test_shape_an_avln_cco_3_sta(): setup("attach") title("Test shape an AVLN CCO + 3 STA...") avln = [(1, 1), (1, 2), (2, 1), (2, 2)] change_nmk(avln) switch_on(avln) check_networks(avln, []) def test_kill_cco_in_cco_2_sta(): setup("attach") title("Test kill CCO in CCO + 2 STA...") avln = [(1, 1), (1, 2), (2, 1)] switch_on(avln) check_networks(avln, []) power_strip.switch("off", (1, 1)) check_networks([(1, 2), (2, 1)], []) def test_split_cco_1_sta_in_2_avlns_ucco(): setup("attach") title("Test split CCO + 1 STA in 2 AVLNs UCCOs...") avln = [(1, 1), (2, 1)] switch_on(avln) check_networks(avln, []) link.detach(1) check_networks([(1, 1)], [(2, 1)]) def test_split_cco_3_sta_in_2_avlns_cco_1_sta(): setup("attach") title("Test split CCO + 3 STA in 2 AVLNs CCO + 1 STA...") avln = [(1, 1), (1, 2), (2, 1), (2, 2)] switch_on(avln) check_networks(avln, []) link.detach(1) check_networks([(1, 1), (1, 2)], [(2, 1), (2, 2)]) def test_merge_ucco_with_ucco(): setup("detach") title("Test merge UCCO with UCCO...") switch_on([(1, 1), (2, 1)]) check_networks([(1, 1)], [(2, 1)]) link.attach(1) check_networks([(1, 1), (2, 1)], []) def test_merge_cco_1_sta_with_ucco_scenario_1(): setup("detach") title("Test merge CCO + 1 STA with UCCO...") switch_on([(1, 1), (1, 2)]) check_networks([(1, 1), (1, 2)], []) switch_on((2, 1)) check_networks([(1, 1), (1, 2)], [(2, 1)]) link.attach(1) check_networks([(1, 1), (1, 2), (2, 1)], []) def test_merge_cco_1_sta_with_ucco_scenario_2(): setup("detach") title("Test merge CCO + 1 STA with UCCO...") switch_on([(1, 1), (2, 1)]) check_networks([(1, 1)], [(2, 1)]) switch_on((1, 2)) check_networks([(1, 1), (1, 2)], [(2, 1)]) link.attach(1) check_networks([(1, 1), (1, 2), (2, 1)], []) def test_merge_cco_1_sta_with_cco_1_sta(): setup("detach") title("Test merge CCO + 1 STA with CCO + 1 STA...") avln_1 = [(1, 1), (1, 2)] avln_2 = [(2, 1), (2, 2)] switch_on(avln_1 + avln_2) check_networks(avln_1, avln_2) link.attach(1) check_networks(avln_1 + avln_2, []) def test_replace_cco_in_cco_1_sta(): setup("attach") title("Test replace CCO in CCO + 1 STA...") switch_on([(1, 1), (1, 2)]) check_networks([(1, 1), (1, 2)], []) power_strip.switch("off", (1, 1)) switch_on((2, 1)) check_networks([(1, 2), (2, 1)], []) def test_coexist_cco_1_sta_with_cco_1_sta_scenario_1(): setup("detach") title("Test coexistence of 2 (CCO + 1 STA) with a different NMK...") avln_1 = [(1, 1), (1, 2)] avln_2 = [(2, 1), (2, 2)] change_nmk(avln_1) switch_on(avln_1 + avln_2) check_networks(avln_1, avln_2) link.attach(1) check_networks(avln_1, avln_2) def test_coexist_cco_1_sta_with_cco_1_sta_scenario_2(): setup("attach") title("Test coexistence of 2 (CCO + 1 STA) with a different NMK...") avln_1 = [(1, 1), (1, 2)] avln_2 = [(2, 1), (2, 2)] change_nmk(avln_1) switch_on(avln_1 + avln_2) check_networks(avln_1, avln_2) def setup(action): title("Initialize the test bench...") power_strip.switch_off_all() if action == "detach": link.detach(1) if action == "attach": link.attach(1) def get_cco(avln): for key in avln: if role(key) == "CCO": return key assert false def title(String): print "+++++++++++++++++++++++++++" print "+", String print "+++++++++++++++++++++++++++" def check_networks(avln_1, avln_2): print "Checking the networks", avln_1, "and", avln_2, "...{" check_intra_network(avln_1) check_intra_network(avln_2) check_inter_networks(avln_1, avln_2) print"OK: the networks are as expected" print "}" def check_intra_network(avln): for key_1 in avln: for key_2 in avln: assert r_ping(key_1, key_2) == "pong", (key_1, key_2) if len(avln) > 1: roles = [ role(key) for key in avln ] roles.sort() #assert roles[0] == "CCO", roles #assert roles.count("STA") == len(avln) - 1, (roles, avln) def check_inter_networks(avln_1, avln_2): for key_1 in avln_1: for key_2 in avln_2: assert r_ping(key_1, key_2) == "pang", (key_1, key_2) def change_nmk(keys): new_nmk = str(random.random()) for key in keys: switch_on(key) spc300.change_nmk(key, new_nmk) power_strip.switch("off", key) def switch_on(keys): if type(keys) != list: keys = [keys] for key in keys: spc300.switch_on(key)