kytos /
pathfinder
| 1 | """Module to help to create tests.""" |
||
| 2 | |||
| 3 | from unittest.mock import MagicMock |
||
| 4 | |||
| 5 | from kytos.lib.helpers import (get_interface_mock, get_link_mock, |
||
| 6 | get_switch_mock) |
||
| 7 | |||
| 8 | from kytos.core.interface import Interface |
||
| 9 | from kytos.core.switch import Switch |
||
| 10 | from kytos.core.link import Link |
||
| 11 | |||
| 12 | |||
| 13 | def get_topology_mock(): |
||
| 14 | """Create a default topology.""" |
||
| 15 | switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
||
| 16 | switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
||
| 17 | switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01) |
||
| 18 | |||
| 19 | interface_a1 = get_interface_mock("s1-eth1", 1, switch_a) |
||
| 20 | interface_a2 = get_interface_mock("s1-eth2", 2, switch_a) |
||
| 21 | |||
| 22 | interface_b1 = get_interface_mock("s2-eth1", 1, switch_b) |
||
| 23 | interface_b2 = get_interface_mock("s2-eth2", 2, switch_b) |
||
| 24 | |||
| 25 | interface_c1 = get_interface_mock("s3-eth1", 1, switch_c) |
||
| 26 | interface_c2 = get_interface_mock("s3-eth2", 2, switch_c) |
||
| 27 | |||
| 28 | switch_a.interfaces = {interface_a1.id: interface_a1, |
||
| 29 | interface_a2.id: interface_a2} |
||
| 30 | switch_b.interfaces = {interface_b1.id: interface_b1, |
||
| 31 | interface_b2.id: interface_b2} |
||
| 32 | switch_c.interfaces = {interface_c1.id: interface_c1, |
||
| 33 | interface_c2.id: interface_c2} |
||
| 34 | |||
| 35 | link_1 = get_link_mock(interface_a1, interface_b1) |
||
| 36 | link_2 = get_link_mock(interface_a2, interface_c1) |
||
| 37 | link_3 = get_link_mock(interface_b2, interface_c2) |
||
| 38 | |||
| 39 | topology = MagicMock() |
||
| 40 | topology.links = {"1": link_1, "2": link_2, "3": link_3} |
||
| 41 | topology.switches = {switch_a.dpid: switch_a, |
||
| 42 | switch_b.dpid: switch_b, |
||
| 43 | switch_c.dpid: switch_c} |
||
| 44 | return topology |
||
| 45 | |||
| 46 | |||
| 47 | def topology_setting(): |
||
| 48 | """Set the default values associated |
||
| 49 | to a "real" topology.""" |
||
| 50 | switches_to_interface_counts = {"S1": 2, "S2": 2, "S3": 6, "S4": 2, |
||
| 51 | "S5": 6, "S6": 5, "S7": 2, "S8": 8, |
||
| 52 | "S9": 4, "S10": 3, "S11": 3, |
||
| 53 | "User1": 4, "User2": 2, |
||
| 54 | "User3": 2, "User4": 3} |
||
| 55 | |||
| 56 | links_to_interfaces = [["S1:1", "S2:1"], ["S1:2", "User1:1"], |
||
| 57 | ["S2:2", "User4:1"], ["S3:1", "S5:1"], |
||
| 58 | ["S3:2", "S7:1"], ["S3:3", "S8:1"], |
||
| 59 | ["S3:4", "S11:1"], |
||
| 60 | ["S3:5", "User3:1"], ["S3:6", "User4:2"], |
||
| 61 | ["S4:1", "S5:2"], ["S4:2", "User1:2"], |
||
| 62 | ["S5:3", "S6:1"], |
||
| 63 | ["S5:4", "S6:2"], ["S5:5", "S8:2"], |
||
| 64 | ["S5:6", "User1:3"], ["S6:3", "S9:1"], |
||
| 65 | ["S6:4", "S9:2"], ["S6:5", "S10:1"], |
||
| 66 | ["S7:2", "S8:3"], |
||
| 67 | ["S8:4", "S9:3"], ["S8:5", "S9:4"], |
||
| 68 | ["S8:6", "S10:2"], |
||
| 69 | ["S8:7", "S11:2"], ["S8:8", "User3:2"], |
||
| 70 | ["S10:3", "User2:1"], ["S11:3", "User2:2"], |
||
| 71 | ["User1:4", "User4:3"]] |
||
| 72 | |||
| 73 | links_to_metadata = [ |
||
| 74 | {"reliability": 5, "bandwidth": 100, "delay": 105}, |
||
| 75 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
| 76 | {"reliability": 5, "bandwidth": 100, "delay": 10}, |
||
| 77 | {"reliability": 5, "bandwidth": 10, "delay": 112}, |
||
| 78 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
| 79 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
| 80 | {"reliability": 3, "bandwidth": 100, "delay": 6}, |
||
| 81 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
| 82 | {"reliability": 5, "bandwidth": 100, "delay": 10}, |
||
| 83 | {"reliability": 1, "bandwidth": 100, "delay": 30, "ownership": "A"}, |
||
| 84 | {"reliability": 3, "bandwidth": 100, "delay": 110, "ownership": "A"}, |
||
| 85 | {"reliability": 1, "bandwidth": 100, "delay": 40}, |
||
| 86 | {"reliability": 3, "bandwidth": 100, "delay": 40, "ownership": "A"}, |
||
| 87 | {"reliability": 5, "bandwidth": 100, "delay": 112}, |
||
| 88 | {"reliability": 3, "bandwidth": 100, "delay": 60}, |
||
| 89 | {"reliability": 3, "bandwidth": 100, "delay": 60}, |
||
| 90 | {"reliability": 5, "bandwidth": 100, "delay": 62}, |
||
| 91 | {"bandwidth": 100, "delay": 108, "ownership": "A"}, |
||
| 92 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
| 93 | {"reliability": 3, "bandwidth": 100, "delay": 32}, |
||
| 94 | {"reliability": 3, "bandwidth": 100, "delay": 110}, |
||
| 95 | {"reliability": 5, "bandwidth": 100, "ownership": "A"}, |
||
| 96 | {"reliability": 3, "bandwidth": 100, "delay": 7}, |
||
| 97 | {"reliability": 5, "bandwidth": 100, "delay": 1}, |
||
| 98 | {"reliability": 3, "bandwidth": 100, "delay": 10, "ownership": "A"}, |
||
| 99 | {"reliability": 3, "bandwidth": 100, "delay": 6}, |
||
| 100 | {"reliability": 5, "bandwidth": 10, "delay": 105}] |
||
| 101 | |||
| 102 | return links_to_interfaces, links_to_metadata, switches_to_interface_counts |
||
| 103 | |||
| 104 | |||
| 105 | def get_topology_with_metadata_mock(): |
||
| 106 | """Create a topology with metadata.""" |
||
| 107 | switches = {} |
||
| 108 | interfaces = {} |
||
| 109 | links = {} |
||
| 110 | i = 0 |
||
| 111 | |||
| 112 | links_to_interfaces, links_to_metadata, switches_to_interface_counts = topology_setting() |
||
| 113 | |||
| 114 | for switch in switches_to_interface_counts: |
||
| 115 | switches[switch] = get_switch_mock(switch) |
||
| 116 | |||
| 117 | for key, value in switches_to_interface_counts.items(): |
||
| 118 | switches[key].interfaces = {} |
||
| 119 | for interface in _get_interfaces(value, switches[key]): |
||
| 120 | switches[key].interfaces[interface.id] = interface |
||
| 121 | interfaces[interface.id] = interface |
||
| 122 | |||
| 123 | for interfaces_str in links_to_interfaces: |
||
| 124 | interface_a = interfaces[interfaces_str[0]] |
||
| 125 | interface_b = interfaces[interfaces_str[1]] |
||
| 126 | links[str(i)] = get_link_mock(interface_a, interface_b) |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 127 | links[str(i)].metadata = links_to_metadata[i] |
||
| 128 | i += 1 |
||
| 129 | |||
| 130 | topology = MagicMock() |
||
| 131 | topology.links = links |
||
| 132 | topology.switches = switches |
||
| 133 | return topology |
||
| 134 | |||
| 135 | |||
| 136 | def get_topology_with_metadata(): |
||
| 137 | """Create a topology with metadata.""" |
||
| 138 | switches = {} |
||
| 139 | interfaces = {} |
||
| 140 | links = {} |
||
| 141 | |||
| 142 | links_to_interfaces, links_to_metadata, switches_to_interface_counts = topology_setting() |
||
| 143 | |||
| 144 | for switch in switches_to_interface_counts: |
||
| 145 | switches[switch] = Switch(switch) |
||
| 146 | |||
| 147 | for key, value in switches_to_interface_counts.items(): |
||
| 148 | switches[key].interfaces = {} |
||
| 149 | for i in range(1, value + 1): |
||
| 150 | str1 = "{}:{}".format(switches[key].dpid, i) |
||
| 151 | interface = Interface(str1, i, switches[key]) |
||
| 152 | switches[key].update_interface(interface) |
||
| 153 | interfaces[interface.id] = interface |
||
| 154 | |||
| 155 | i = 0 |
||
| 156 | for interfaces_str in links_to_interfaces: |
||
| 157 | interface_a = interfaces[interfaces_str[0]] |
||
| 158 | interface_b = interfaces[interfaces_str[1]] |
||
| 159 | links[str(i)] = Link(interface_a, interface_b) |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 160 | links[str(i)].metadata = links_to_metadata[i] |
||
| 161 | i += 1 |
||
| 162 | |||
| 163 | topology = MagicMock() |
||
| 164 | topology.links = links |
||
| 165 | topology.switches = switches |
||
| 166 | return topology |
||
| 167 | |||
| 168 | |||
| 169 | def _get_interfaces(count, switch): |
||
| 170 | """Add a new interface to the list of interfaces.""" |
||
| 171 | for i in range(1, count + 1): |
||
| 172 | yield get_interface_mock("", i, switch) |
||
| 173 | |||
| 174 | |||
| 175 | def get_filter_links_fake(links, metadata=True, **metrics): |
||
| 176 | """Get test links with optional metadata.""" |
||
| 177 | # pylint: disable=unused-argument |
||
| 178 | filtered_links = ["a", "b", "c"] |
||
| 179 | filtered_links_without_metadata = filtered_links[0:2] |
||
| 180 | if not metadata: |
||
| 181 | return filtered_links_without_metadata |
||
| 182 | return filtered_links |
||
| 183 | # pylint: enable=unused-argument |
||
| 184 | |||
| 185 | |||
| 186 | def get_test_filter_function(): |
||
| 187 | """Get minimum filter function.""" |
||
| 188 | return lambda x: (lambda y: y >= x) |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 189 |