forked from trema/trema
/
spec_helper.rb
160 lines (120 loc) · 3.69 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
# Copyright (C) 2008-2013 NEC Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
$LOAD_PATH << File.join( File.dirname( __FILE__ ), "..", "ruby" )
$LOAD_PATH.unshift File.expand_path( File.join File.dirname( __FILE__ ), "..", "vendor", "ruby-ifconfig-1.2", "lib" )
require "rubygems"
require "rspec"
require "rspec/autorun"
require "trema"
require "trema/dsl/configuration"
require "trema/dsl/context"
require "trema/ofctl"
require "trema/shell"
require "trema/util"
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir[ "#{ File.dirname( __FILE__ ) }/support/**/*.rb" ].each do | each |
require File.expand_path( each )
end
RSpec.configure do | config |
config.expect_with :rspec do | c |
# Ensure that 'expect' is used and disable 'should' for consistency
c.syntax = :expect
end
end
include Trema
def controller name
Trema::App[ name ]
end
def vswitch name
Trema::OpenflowSwitch[ name ]
end
def vhost name
Trema::Host[ name ]
end
def send_packets source, dest, options = {}
Trema::Shell.send_packets source, dest, options
end
include Trema::Util
class Network
def initialize &block
@context = Trema::DSL::Parser.new.eval( &block )
end
def run controller_class, &test
begin
trema_run controller_class
test.call
ensure
trema_kill
end
end
################################################################################
private
################################################################################
def trema_run controller_class
controller = controller_class.new
if not controller.is_a?( Trema::Controller )
raise "#{ controller_class } is not a subclass of Trema::Controller"
end
Trema::DSL::Context.new( @context ).dump
app_name = controller.name
rule = { :port_status => app_name, :packet_in => app_name, :state_notify => app_name }
sm = SwitchManager.new( rule, @context.port )
sm.no_flow_cleanup = true
sm.run!
@context.links.each do | name, each |
each.add!
end
@context.hosts.each do | name, each |
each.run!
end
@context.switches.each do | name, each |
each.run!
drop_packets_from_unknown_hosts each
end
@context.links.each do | name, each |
each.up!
end
@context.hosts.each do | name, each |
each.add_arp_entry @context.hosts.values - [ each ]
end
@th_controller = Thread.start do
controller.run!
end
sleep 5 # FIXME: wait until controller.up?
end
def trema_kill
cleanup_current_session
@th_controller.join if @th_controller
sleep 5 # FIXME: wait until switch_manager.down?
end
def drop_packets_from_unknown_hosts switch
ofctl = Trema::Ofctl.new
ofctl.add_flow switch, :priority => 0, :actions => "drop"
@context.hosts.each do | name, each |
ofctl.add_flow switch, :dl_type => "0x0800", :nw_src => each.ip, :priority => 1, :actions => "controller"
end
end
end
def network &block
Network.new &block
end
### Local variables:
### mode: Ruby
### coding: utf-8-unix
### indent-tabs-mode: nil
### End: