Statistics
| Branch: | Tag: | Revision:

one / src / vnm_mad / test / OpenNebulaNetwork_spec.rb @ 08024d57

History | View | Annotate | Download (7.07 KB)

1
#!/usr/bin/env ruby
2

    
3
$: << File.dirname(__FILE__) + '/..' \
4
   << './'
5

    
6
require 'rubygems'
7
require 'rspec'
8
require 'SystemMock'
9
require 'pp'
10

    
11
require 'OpenNebulaNetwork'
12
require 'Ebtables'
13
require 'Firewall'
14
require 'HostManaged'
15
require 'OpenvSwitch'
16

    
17
OUTPUT = Hash.new
18
Dir[File.dirname(__FILE__) + "/output/**"].each do |f|
19
    key = File.basename(f).to_sym
20
    OUTPUT[key] = File.read(f)
21
end
22

    
23
include SystemMock
24

    
25
RSpec.configure do |config|
26
    config.before(:each) do
27
        $capture_commands = Hash.new
28
        $collector = Hash.new
29
    end
30
end
31

    
32
describe 'networking' do
33
    it "get all nics in kvm" do
34
        $capture_commands = {
35
            /virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml]
36
        }
37
        onevlan = OpenNebulaNetwork.new(OUTPUT[:onevm_show],"kvm")
38
        nics_expected = [{:bridge=>"br0",
39
                      :ip=>"172.16.0.100",
40
                      :mac=>"02:00:ac:10:00:64",
41
                      :network=>"Small network",
42
                      :network_id=>"0",
43
                      :tap=>"vnet0"},
44
                     {:bridge=>"br1",
45
                      :ip=>"10.1.1.1",
46
                      :mac=>"02:00:0a:01:01:01",
47
                      :network=>"r1",
48
                      :network_id=>"1",
49
                      :tap=>"vnet1"},
50
                     {:bridge=>"br2",
51
                      :ip=>"10.1.2.1",
52
                      :mac=>"02:00:0a:01:02:01",
53
                      :network=>"r2",
54
                      :network_id=>"2",
55
                      :tap=>"vnet2"}]
56
        onevlan.vm.nics.should == nics_expected
57
    end
58

    
59
    it "filter nics in kvm" do
60
        $capture_commands = {
61
            /virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml]
62
        }
63
        onevlan = OpenNebulaNetwork.new(OUTPUT[:onevm_show],"kvm")
64
        onevlan.filter(:bridge => "br1")
65
        nics_expected = [{:bridge=>"br1",
66
                          :ip=>"10.1.1.1",
67
                          :mac=>"02:00:0a:01:01:01",
68
                          :network=>"r1",
69
                          :network_id=>"1",
70
                          :tap=>"vnet1"}]
71
 
72
        onevlan.vm.filtered_nics.should == nics_expected
73
    end
74
end
75

    
76
describe 'ebtables' do
77
    it "generate ebtable rules in kvm" do
78
        $capture_commands = {
79
            /virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml],
80
            /ebtables/       => nil
81
        }
82
        onevlan = EbtablesVLAN.new(OUTPUT[:onevm_show],"kvm")
83
        onevlan.activate
84
        ebtables_cmds = [
85
        "sudo /sbin/ebtables -A FORWARD -s ! 02:00:ac:10:00:00/ff:ff:ff:ff:ff:00 -o vnet0 -j DROP",
86
        "sudo /sbin/ebtables -A FORWARD -s ! 02:00:ac:10:00:64 -i vnet0 -j DROP",
87
        "sudo /sbin/ebtables -A FORWARD -s ! 02:00:0a:01:01:00/ff:ff:ff:ff:ff:00 -o vnet1 -j DROP",
88
        "sudo /sbin/ebtables -A FORWARD -s ! 02:00:0a:01:01:01 -i vnet1 -j DROP",
89
        "sudo /sbin/ebtables -A FORWARD -s ! 02:00:0a:01:02:00/ff:ff:ff:ff:ff:00 -o vnet2 -j DROP",
90
        "sudo /sbin/ebtables -A FORWARD -s ! 02:00:0a:01:02:01 -i vnet2 -j DROP"]
91
        $collector[:system].should == ebtables_cmds
92
    end
93
end
94

    
95
describe 'openvswitch' do
96
    it "tag tun/tap devices with vlans in kvm" do
97
        $capture_commands = {
98
            /virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml],
99
            /ovs-vsctl/      => nil
100
        }
101
        onevlan = OpenvSwitchVLAN.new(OUTPUT[:onevm_show],"kvm")
102
        onevlan.activate
103
        openvswitch_tags = [
104
            "sudo /usr/local/bin/ovs-vsctl set Port vnet0 tag=2",
105
            "sudo /usr/local/bin/ovs-vsctl set Port vnet1 tag=3",
106
            "sudo /usr/local/bin/ovs-vsctl set Port vnet2 tag=4"
107
            ]
108

    
109
        $collector[:system].should == openvswitch_tags
110
    end
111

    
112
    it "force VLAN_ID for Open vSwitch vlans in kvm" do
113
        $capture_commands = {
114
            /virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml_vlan_id],
115
            /brctl show/     => OUTPUT[:brctl_show],
116
            /ovs-vsctl/      => nil
117
        }
118
        onevlan = OpenvSwitchVLAN.new(OUTPUT[:onevm_show_vlan_id_kvm],"kvm")
119
        onevlan.activate
120
        
121
        onevlan_rules = ["sudo /usr/local/bin/ovs-vsctl set Port vnet0 tag=6",
122
                         "sudo /usr/local/bin/ovs-vsctl set Port vnet1 tag=50",
123
                         "sudo /usr/local/bin/ovs-vsctl set Port vnet1 tag=51"]
124

    
125
        $collector[:system].should == onevlan_rules
126
    end
127
end
128

    
129
describe 'firewall' do
130
    it "should activate FW rules in xen" do
131
        $capture_commands = {
132
            /uname/ => OUTPUT[:xen_uname_a],
133
            /lsmod/ => OUTPUT[:xen_lsmod],
134
            /network-list/ => OUTPUT[:xm_network_list],
135
            /domid/ => OUTPUT[:xm_domid],
136
            /iptables/ => nil
137
        }
138
        fw = OpenNebulaFirewall.new(OUTPUT[:onevm_show_xen])
139
        fw.activate
140

    
141
        fw_activate_rules = ["sudo /sbin/iptables -N one-36-3",
142
                 "sudo /sbin/iptables -A FORWARD -m physdev --physdev-out vif4.0 -j one-36-3",
143
                 "sudo /sbin/iptables -A one-36-3 -p tcp -m state --state ESTABLISHED -j ACCEPT",
144
                 "sudo /sbin/iptables -A one-36-3 -p tcp -m multiport --dports 22,80 -j ACCEPT",
145
                 "sudo /sbin/iptables -A one-36-3 -p tcp -j DROP",
146
                 "sudo /sbin/iptables -A one-36-3 -p icmp -m state --state ESTABLISHED -j ACCEPT",
147
                 "sudo /sbin/iptables -A one-36-3 -p icmp -j DROP"]
148

    
149
        $collector[:system].should == fw_activate_rules
150
    end
151
end
152

    
153
describe 'host-managed' do
154
    it "tag tun/tap devices with vlans in kvm" do
155
        $capture_commands = {
156
            /virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml_phydev],
157
            /brctl show/     => OUTPUT[:brctl_show],
158
            /brctl add/    => nil,
159
            /vconfig/        => nil,
160
            /ip link/        => nil
161
        }
162
        hm = OpenNebulaHM.new(OUTPUT[:onevm_show_phydev_kvm],"kvm")
163
        hm.activate
164

    
165
        hm_activate_rules = ["sudo /usr/sbin/brctl addbr onebr6",
166
                             "sudo /sbin/ip link show eth0.8",
167
                             "sudo /sbin/vconfig add eth0 8",
168
                             "sudo /sbin/ip link set eth0.8 up",
169
                             "sudo /usr/sbin/brctl addif onebr6 eth0.8"]
170
        $collector[:system].should == hm_activate_rules
171
    end
172

    
173
    it "force VLAN_ID for vlans in kvm" do
174
        $capture_commands = {
175
            /virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml_vlan_id],
176
            /brctl show/     => OUTPUT[:brctl_show],
177
            /brctl add/    => nil,
178
            /vconfig/        => nil,
179
            /ip link/        => nil
180
        }
181
        hm = OpenNebulaHM.new(OUTPUT[:onevm_show_vlan_id_kvm],"kvm")
182
        hm.activate
183

    
184
        hm_vlan_id = ["sudo /usr/sbin/brctl addbr onebr10",
185
                      "sudo /sbin/ip link show eth0.50",
186
                      "sudo /sbin/vconfig add eth0 50",
187
                      "sudo /sbin/ip link set eth0.50 up",
188
                      "sudo /usr/sbin/brctl addif onebr10 eth0.50",
189
                      "sudo /usr/sbin/brctl addbr specialbr",
190
                      "sudo /sbin/ip link show eth0.51",
191
                      "sudo /sbin/vconfig add eth0 51",
192
                      "sudo /sbin/ip link set eth0.51 up",
193
                      "sudo /usr/sbin/brctl addif specialbr eth0.51"]
194

    
195
        $collector[:system].should == hm_vlan_id
196
    end
197
end