Open Kilda Java Documentation
mininet_rest_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 mininet_rest_test.py: Validate mininet_rest works within the mininet framework, focused on PING
4 
5  This will launch an example network, with switches and hosts and rules, confirming that
6  ping will work across hosts that are connected properly.
7 
8 Dependencies:
9  This class depends on a controller - it is best to launch both mininet and floodlight.
10  $ docker-compose up -d mininet floodlight
11 
12  If possible, bring up the entire kilda controller.
13 
14 Usage (example uses VLAN ID=1000):
15  From the command line - run as a MODULE so that mininet_rest can be imported)
16  cd /; python -m app.examples.mininet_rest_test
17 
18 """
19 
20 from .. import mininet_rest
21 
22 import requests
23 import logging
24 from functools import partial
25 
26 logger = logging.getLogger(__name__)
27 
28 topo_json = {
29  "controllers": [
30  {
31  "host": "kilda",
32  "name": "floodlight",
33  "port": 6653
34  }
35  ],
36  "links": [
37  {
38  "node1": "00000001",
39  "node2": "00000002"
40  },
41  {
42  "node1": "00000002",
43  "node2": "00000003"
44  },
45  {
46  "node1": "00000003",
47  "node2": "00000004"
48  },
49  {
50  "node1": "00000004",
51  "node2": "00000005"
52  },
53  {
54  "node1": "00000005",
55  "node2": "00000006"
56  },
57  {
58  "node1": "00000006",
59  "node2": "00000007"
60  }
61  ],
62  "switches": [
63  {
64  "dpid": "deadbeef00000001",
65  "name": "00000001"
66  },
67  {
68  "dpid": "deadbeef00000002",
69  "name": "00000002"
70  },
71  {
72  "dpid": "deadbeef00000003",
73  "name": "00000003"
74  },
75  {
76  "dpid": "deadbeef00000004",
77  "name": "00000004"
78  },
79  {
80  "dpid": "deadbeef00000005",
81  "name": "00000005"
82  },
83  {
84  "dpid": "deadbeef00000006",
85  "name": "00000006"
86  },
87  {
88  "dpid": "deadbeef00000007",
89  "name": "00000007"
90  }
91  ]
92 }
93 
94 
95 def cleanup():
96  result = requests.post(url="http://localhost:38080/cleanup")
97  print result
98 
99 
101  result = requests.post(url="http://localhost:38080/topology",json=topo_json)
102  print result
103 
104 
105 def test_ping(src_switch, src_port, src_vlan, dst_switch, dst_port, dst_vlan):
106  """setup our rules for host / switch / switch and see if we can ping"""
107  urlbase = "http://localhost:38080/checkpingtraffic"
108  args = "srcswitch={}&srcport={}&srcvlan={}&dstswitch={}&dstport={}&dstvlan={}".format(
109  src_switch,src_port,src_vlan,dst_switch,dst_port,dst_vlan)
110  url = "{}?{}".format(urlbase,args)
111  print ("** PING: {}".format(url))
112  result = requests.get(url=url)
113  return result
114 
115 
116 def print_ping_result(response, should_ping):
117  if response.status_code == 503:
118  if should_ping:
119  print "FAILURE - CAN'T PING"
120  else:
121  print "SUCCESS - NO PING"
122  elif response.status_code == 200:
123  if should_ping:
124  print "SUCCESS - CAN PING"
125  else:
126  print "FAILURE - CAN PING"
127  else:
128  print "ERROR - WRONG CODE"
129 
130 
131 def call_pingagle(pingable, should_ping):
132  result = pingable()
133  print_ping_result( result, should_ping) # should fail
134  if result.status_code == 503 and not should_ping:
135  return 1
136  elif result.status_code == 200 and should_ping:
137  return 1
138  return 0
139 
140 
141 def test_scenario(name, pingable, psetup, pclear):
142  total_result = 0; # add a +1 for each passed test
143  total_expected = 3;
144 
145  # There should not be a ping beforehand
146  total_result += call_pingagle(pingable, should_ping=False)
147 
148  # Now there should be a ping, after adding the rule that Kilda should add when running
149  for setup in psetup:
150  setup()
151  total_result += call_pingagle(pingable, should_ping=True)
152 
153  # After removing rules, ping shouldn't work
154  for clear in pclear:
155  clear()
156  total_result += call_pingagle(pingable, should_ping=False)
157 
158  if total_result == total_expected:
159  print ("\n{} ... ALL TESTS PASSED\n".format(name))
160  else:
161  print ("\n{} ... FAILURE: {} of {} passed\n".format(name, total_result, total_expected))
162 
163 
164 s2 = "00000002"
165 s3 = "00000003"
166 s4 = "00000004"
167 s5 = "00000005"
168 s6 = "00000006"
169 
170 
172  """
173  This test is similar to the Kilda single switch test(s), but without kilda issuing the rules.
174  The intent is to confirm that the test harness works (ie pingable works as intended), but also
175  serve as an example of what the rules may look like for the kilda deployment.
176 
177  Examples: # flows without transit vlans and intermediate switches
178  | flow_id | source_switch | source_port | source_vlan | destination_switch | destination_port | destination_vlan | bandwidth |
179  | c1none | de:ad:be:ef:00:00:00:03 | 1 | 0 | de:ad:be:ef:00:00:00:03 | 2 | 0 | 10000 |
180 
181  """
182 
183  pingable = partial(test_ping,s2,2,0,s4,1,0)
184  psetup = [ partial(mininet_rest.add_single_switch_rules, s3, 1, 2, 0, 0 ) ]
185  pclear = [ partial(mininet_rest.clear_single_switch_rules, s3, 1, 2) ]
186  test_scenario("test_single_switch_scenario", pingable, psetup, pclear)
187 
188 
190  """
191  Examples: # flows with transit vlans and without intermediate switches
192  | flow_id | source_switch | source_port | source_vlan | destination_switch | destination_port | destination_vlan | bandwidth |
193  | c2none | de:ad:be:ef:00:00:00:03 | 1 | 0 | de:ad:be:ef:00:00:00:04 | 2 | 0 | 10000 |
194  """
195 
196  pingable = partial(test_ping, s2, 2, 0, s5, 1, 0)
197  psetup = [
198  partial( mininet_rest.add_single_switch_rules, s3, 1, 2, 0, 0 ),
199  partial( mininet_rest.add_single_switch_rules, s4, 1, 2, 0, 0 )
200  ]
201  pclear = [
202  partial(mininet_rest.clear_single_switch_rules, s3, 1, 2),
203  partial(mininet_rest.clear_single_switch_rules, s4, 1, 2)
204  ]
205  test_scenario("test_two_switch_scenario", pingable, psetup, pclear)
206 
207 
209  """
210  Examples: # flows with transit vlans and intermediate switches
211  | flow_id | source_switch | source_port | source_vlan | destination_switch | destination_port | destination_vlan | bandwidth |
212  | c3none | de:ad:be:ef:00:00:00:02 | 1 | 0 | de:ad:be:ef:00:00:00:04 | 2 | 0 | 10000 |
213  """
214 
215  pingable = partial(test_ping, s2, 2, 0, s6, 1, 0)
216  psetup = [
217  partial( mininet_rest.add_single_switch_rules, s3, 1, 2, 0, 0 ),
218  partial( mininet_rest.add_single_switch_rules, s4, 1, 2, 0, 0 ),
219  partial( mininet_rest.add_single_switch_rules, s5, 1, 2, 0, 0 )
220  ]
221  pclear = [
222  partial(mininet_rest.clear_single_switch_rules, s3, 1, 2),
223  partial(mininet_rest.clear_single_switch_rules, s4, 1, 2),
224  partial(mininet_rest.clear_single_switch_rules, s5, 1, 2)
225  ]
226  test_scenario("test_three_switch_scenario", pingable, psetup, pclear)
227 
228 
229 def main():
230  mininet_rest.init()
235  cleanup()
236 
237 
238 
239 if __name__ == '__main__':
240  main()
Definition: setup.py:1
def test_ping(src_switch, src_port, src_vlan, dst_switch, dst_port, dst_vlan)
def test_scenario(name, pingable, psetup, pclear)
def pingable(host1, host2)
Definition: rest.py:680
def print_ping_result(response, should_ping)
def call_pingagle(pingable, should_ping)