ruby-libvirt 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,34 +2,171 @@
2
2
 
3
3
  # Test the interface methods the bindings support
4
4
 
5
+ $: << File.dirname(__FILE__)
6
+
5
7
  require 'libvirt'
8
+ require 'test_utils.rb'
9
+
10
+ def find_valid_iface(conn)
11
+ conn.list_interfaces.each do |ifname|
12
+ iface = conn.lookup_interface_by_name(ifname)
13
+ if iface.mac == "00:00:00:00:00:00"
14
+ next
15
+ end
16
+ return iface
17
+ end
18
+ end
19
+
6
20
 
7
- conn = Libvirt::open
8
- puts "Number of Interfaces: #{conn.num_of_interfaces}"
9
- puts "Number of Defined Interfaces: #{conn.num_of_defined_interfaces}"
21
+ conn = Libvirt::open("qemu:///system")
22
+
23
+ begin
24
+ `rm -f /etc/sysconfig/network-scripts/ifcfg-ruby-libvirt-tester`
25
+ `brctl delbr ruby-libvirt-tester >& /dev/null`
26
+ rescue
27
+ end
10
28
 
11
29
  new_interface_xml = <<EOF
12
- <interface type='bridge' name='testbr7'>
13
- <bridge>
14
- <interface type='ethernet' name='dummy'>
15
- </interface>
30
+ <interface type="bridge" name="ruby-libvirt-tester">
31
+ <start mode="onboot"/>
32
+ <bridge delay="0">
16
33
  </bridge>
17
34
  </interface>
18
35
  EOF
19
36
 
20
- # FIXME: doesn't work at the moment
21
- #new_interface = conn.define_interface_xml(new_interface_xml)
22
- #new_interface.undefine
37
+ # TESTGROUP: conn.num_of_interfaces
38
+ expect_too_many_args(conn, "num_of_interfaces", 1)
39
+ expect_success(conn, "no args", "num_of_interfaces")
40
+
41
+ # TESTGROUP: conn.list_interfaces
42
+ expect_too_many_args(conn, "list_interfaces", 1)
43
+ expect_success(conn, "no args", "list_interfaces")
44
+
45
+ # TESTGROUP: conn.num_of_defined_interfaces
46
+ expect_too_many_args(conn, "num_of_defined_interfaces", 1)
47
+ expect_success(conn, "no args", "num_of_defined_interfaces")
48
+
49
+ # TESTGROUP: conn.list_defined_interfaces
50
+ expect_too_many_args(conn, "list_defined_interfaces", 1)
51
+ expect_success(conn, "no args", "list_defined_interfaces")
52
+
53
+ # TESTGROUP: conn.lookup_interface_by_name
54
+ newiface = conn.define_interface_xml(new_interface_xml)
55
+
56
+ expect_too_many_args(conn, "lookup_interface_by_name", 1, 2)
57
+ expect_too_few_args(conn, "lookup_interface_by_name")
58
+ expect_invalid_arg_type(conn, "lookup_interface_by_name", 1)
59
+ expect_fail(conn, Libvirt::RetrieveError, "non-existent name arg", "lookup_interface_by_name", "foobarbazsucker")
60
+
61
+ expect_success(conn, "name arg", "lookup_interface_by_name", "ruby-libvirt-tester")
62
+
63
+ newiface.destroy
23
64
 
24
- defined = conn.list_defined_interfaces
25
- running = conn.list_interfaces
65
+ expect_success(conn, "name arg", "lookup_interface_by_name", "ruby-libvirt-tester")
26
66
 
27
- (defined+running).each do |intname|
28
- interface = conn.lookup_interface_by_name(intname)
29
- int2 = conn.lookup_interface_by_mac(interface.mac)
30
- puts "Interface #{interface.name}:"
31
- puts " MAC: #{interface.mac}"
32
- puts " XML:"
33
- puts interface.xml_desc
67
+ newiface.undefine
68
+
69
+ # TESTGROUP: conn.lookup_interface_by_mac
70
+ newiface = conn.define_interface_xml(new_interface_xml)
71
+
72
+ expect_too_many_args(conn, "lookup_interface_by_mac", 1, 2)
73
+ expect_too_few_args(conn, "lookup_interface_by_mac")
74
+ expect_invalid_arg_type(conn, "lookup_interface_by_mac", 1)
75
+ expect_fail(conn, Libvirt::RetrieveError, "non-existent mac arg", "lookup_interface_by_mac", "foobarbazsucker")
76
+
77
+ testiface = find_valid_iface(conn)
78
+ if not testiface.nil?
79
+ expect_success(conn, "name arg", "lookup_interface_by_mac", testiface.mac) {|x| x.mac == testiface.mac}
34
80
  end
81
+
82
+ newiface.undefine
83
+
84
+ # TESTGROUP: conn.define_interface_xml
85
+ expect_too_many_args(conn, "define_interface_xml", 1, 2, 3)
86
+ expect_too_few_args(conn, "define_interface_xml")
87
+ expect_invalid_arg_type(conn, "define_interface_xml", 1)
88
+ expect_invalid_arg_type(conn, "define_interface_xml", nil)
89
+ expect_invalid_arg_type(conn, "define_interface_xml", "hello", 'foo')
90
+ expect_fail(conn, Libvirt::DefinitionError, "invalid XML", "define_interface_xml", "hello")
91
+
92
+ expect_success(conn, "interface XML", "define_interface_xml", new_interface_xml)
93
+ newiface.undefine
94
+
95
+ # TESTGROUP: iface.undefine
96
+ newiface = conn.define_interface_xml(new_interface_xml)
97
+
98
+ expect_too_many_args(newiface, "undefine", 1)
99
+
100
+ expect_success(newiface, "no args", "undefine")
101
+
102
+ # TESTGROUP: iface.create
103
+ newiface = conn.define_interface_xml(new_interface_xml)
104
+
105
+ expect_too_many_args(newiface, "create", 1, 2)
106
+ expect_invalid_arg_type(newiface, "create", 'foo')
107
+
108
+ #expect_success(newiface, "no args", "create")
109
+
110
+ #expect_fail(newiface, Libvirt::Error, "on already running interface", "create")
111
+
112
+ #newiface.destroy
113
+ newiface.undefine
114
+
115
+ # TESTGROUP: iface.destroy
116
+ newiface = conn.define_interface_xml(new_interface_xml)
117
+
118
+ expect_too_many_args(newiface, "destroy", 1, 2)
119
+ expect_invalid_arg_type(newiface, "destroy", 'foo')
120
+
121
+ expect_success(newiface, "no args", "destroy")
122
+
123
+ newiface.undefine
124
+
125
+ # TESTGROUP: iface.active?
126
+ newiface = conn.define_interface_xml(new_interface_xml)
127
+
128
+ expect_too_many_args(newiface, "active?", 1)
129
+
130
+ expect_success(newiface, "no args", "active?") {|x| x == false}
131
+
132
+ #newiface.create
133
+ #expect_success(newiface, "no args", "active?") {|x| x == true}
134
+
135
+ #newiface.destroy
136
+ newiface.undefine
137
+
138
+ # TESTGROUP: iface.name
139
+ newiface = conn.define_interface_xml(new_interface_xml)
140
+
141
+ expect_too_many_args(newiface, "name", 1)
142
+
143
+ expect_success(newiface, "no args", "name") {|x| x == "ruby-libvirt-tester"}
144
+
145
+ newiface.undefine
146
+
147
+ # TESTGROUP: iface.mac
148
+ testiface = find_valid_iface(conn)
149
+ if not testiface.nil?
150
+ expect_too_many_args(testiface, "mac", 1)
151
+
152
+ expect_success(testiface, "no args", "mac") {|x| x == testiface.mac}
153
+ end
154
+
155
+ # TESTGROUP: iface.xml_desc
156
+ testiface = find_valid_iface(conn)
157
+ if not testiface.nil?
158
+ expect_too_many_args(testiface, "xml_desc", 1, 2)
159
+ expect_invalid_arg_type(testiface, "xml_desc", "foo")
160
+ expect_success(testiface, "no args", "xml_desc")
161
+ end
162
+
163
+ # TESTGROUP: iface.free
164
+ newiface = conn.define_interface_xml(new_interface_xml)
165
+ newiface.undefine
166
+ expect_too_many_args(newiface, "free", 1)
167
+
168
+ expect_success(newiface, "no args", "free")
169
+
35
170
  conn.close
171
+
172
+ finish_tests
@@ -2,39 +2,250 @@
2
2
 
3
3
  # Test the network methods the bindings support
4
4
 
5
+ $: << File.dirname(__FILE__)
6
+
5
7
  require 'libvirt'
8
+ require 'test_utils.rb'
9
+
10
+ UUID = "04068860-d9a2-47c5-bc9d-9e047ae901da"
6
11
 
7
- conn = Libvirt::open
8
- puts "Number of Networks: #{conn.num_of_networks}"
9
- puts "Number of Defined Networks: #{conn.num_of_defined_networks}"
12
+ conn = Libvirt::open("qemu:///system")
10
13
 
11
- new_network_xml = <<EOF
14
+ # initial cleanup for previous run
15
+ begin
16
+ oldnet = conn.lookup_network_by_name("ruby-libvirt-tester")
17
+ oldnet.destroy
18
+ oldnet.undefine
19
+ rescue
20
+ # in case we didn't find it, don't do anything
21
+ end
22
+
23
+ new_net_xml = <<EOF
12
24
  <network>
13
- <name>testnetwork</name>
14
- <uuid>e0eed9fa-cb64-433f-066c-257a29b1c13a</uuid>
25
+ <name>ruby-libvirt-tester</name>
26
+ <uuid>#{UUID}</uuid>
27
+ <forward mode='nat'/>
28
+ <bridge name='rubybr0' stp='on' delay='0' />
29
+ <ip address='192.168.134.1' netmask='255.255.255.0'>
30
+ <dhcp>
31
+ <range start='192.168.134.2' end='192.168.134.254' />
32
+ </dhcp>
33
+ </ip>
15
34
  </network>
16
35
  EOF
17
36
 
18
- newnetwork = conn.define_network_xml(new_network_xml)
19
- newnetwork.create
20
- newnetwork.destroy
21
- newnetwork.undefine
22
-
23
- defined = conn.list_defined_networks
24
- running = conn.list_networks
25
-
26
- (defined+running).each do |netname|
27
- network = conn.lookup_network_by_name(netname)
28
- net2 = conn.lookup_network_by_uuid(network.uuid)
29
-
30
- puts "Network #{network.name}:"
31
- puts " UUID: #{network.uuid}"
32
- puts " Autostart?: #{network.autostart?}"
33
- puts " Active?: #{network.active?}"
34
- puts " Persistent?: #{network.persistent?}"
35
- puts " Bridge Name: #{network.bridge_name}"
36
- puts " XML:"
37
- puts network.xml_desc
37
+ # TESTGROUP: conn.num_of_networks
38
+ expect_too_many_args(conn, "num_of_networks", 1)
39
+ expect_success(conn, "no args", "num_of_networks")
40
+
41
+ # TESTGROUP: conn.list_networks
42
+ expect_too_many_args(conn, "list_networks", 1)
43
+ expect_success(conn, "no args", "list_networks")
44
+
45
+ # TESTGROUP: conn.num_of_defined_networks
46
+ expect_too_many_args(conn, "num_of_defined_networks", 1)
47
+ expect_success(conn, "no args", "num_of_defined_networks")
48
+
49
+ # TESTGROUP: conn.list_defined_networks
50
+ expect_too_many_args(conn, "list_defined_networks", 1)
51
+ expect_success(conn, "no args", "list_defined_networks")
52
+
53
+ # TESTGROUP: conn.lookup_network_by_name
54
+ newnet = conn.create_network_xml(new_net_xml)
55
+
56
+ expect_too_many_args(conn, "lookup_network_by_name", 1, 2)
57
+ expect_too_few_args(conn, "lookup_network_by_name")
58
+ expect_invalid_arg_type(conn, "lookup_network_by_name", 1)
59
+ expect_fail(conn, Libvirt::RetrieveError, "non-existent name arg", "lookup_network_by_name", "foobarbazsucker")
60
+
61
+ expect_success(conn, "name arg", "lookup_network_by_name", "ruby-libvirt-tester")
62
+ newnet.destroy
63
+
64
+ newnet = conn.define_network_xml(new_net_xml)
65
+ expect_success(conn, "name arg", "lookup_network_by_name", "ruby-libvirt-tester")
66
+ newnet.undefine
67
+
68
+ # TESTGROUP: conn.lookup_network_by_uuid
69
+ newnet = conn.create_network_xml(new_net_xml)
70
+
71
+ expect_too_many_args(conn, "lookup_network_by_uuid", 1, 2)
72
+ expect_too_few_args(conn, "lookup_network_by_uuid")
73
+ expect_invalid_arg_type(conn, "lookup_network_by_uuid", 1)
74
+ expect_fail(conn, Libvirt::RetrieveError, "non-existent uuid arg", "lookup_network_by_uuid", "foobarbazsucker")
75
+
76
+ expect_success(conn, "uuid arg", "lookup_network_by_uuid", UUID)
77
+ newnet.destroy
78
+
79
+ newnet = conn.define_network_xml(new_net_xml)
80
+ expect_success(conn, "uuid arg", "lookup_network_by_uuid", UUID)
81
+ newnet.undefine
82
+
83
+ # TESTGROUP: conn.create_network_xml
84
+ expect_too_many_args(conn, "create_network_xml", new_net_xml, 0)
85
+ expect_too_few_args(conn, "create_network_xml")
86
+ expect_invalid_arg_type(conn, "create_network_xml", nil)
87
+ expect_invalid_arg_type(conn, "create_network_xml", 1)
88
+ expect_fail(conn, Libvirt::Error, "invalid xml", "create_network_xml", "hello")
89
+
90
+ newnet = expect_success(conn, "network XML", "create_network_xml", new_net_xml)
91
+
92
+ expect_fail(conn, Libvirt::Error, "already existing network", "create_network_xml", new_net_xml)
93
+
94
+ newnet.destroy
95
+
96
+ # TESTGROUP: conn.define_network_xml
97
+ expect_too_many_args(conn, "define_network_xml", 1, 2)
98
+ expect_too_few_args(conn, "define_network_xml")
99
+ expect_invalid_arg_type(conn, "define_network_xml", 1)
100
+ expect_invalid_arg_type(conn, "define_network_xml", nil)
101
+ expect_fail(conn, Libvirt::DefinitionError, "invalid XML", "define_network_xml", "hello")
102
+
103
+ newnet = expect_success(conn, "network XML", "define_network_xml", new_net_xml)
104
+ newnet.undefine
105
+
106
+ # TESTGROUP: net.undefine
107
+ newnet = conn.define_network_xml(new_net_xml)
108
+
109
+ expect_too_many_args(newnet, "undefine", 1)
110
+
111
+ expect_success(newnet, "no args", "undefine")
112
+
113
+ # TESTGROUP: net.create
114
+ newnet = conn.define_network_xml(new_net_xml)
115
+
116
+ expect_too_many_args(newnet, "create", 1)
117
+
118
+ expect_success(newnet, "no args", "create")
119
+
120
+ expect_fail(newnet, Libvirt::Error, "on already running network", "create")
121
+
122
+ newnet.destroy
123
+ newnet.undefine
124
+
125
+ # TESTGROUP: net.destroy
126
+ newnet = conn.create_network_xml(new_net_xml)
127
+
128
+ expect_too_many_args(newnet, "destroy", 1)
129
+
130
+ expect_success(newnet, "no args", "destroy")
131
+
132
+ # TESTGROUP: net.name
133
+ newnet = conn.create_network_xml(new_net_xml)
134
+
135
+ expect_too_many_args(newnet, "name", 1)
136
+
137
+ expect_success(newnet, "no args", "name") {|x| x == "ruby-libvirt-tester"}
138
+
139
+ newnet.destroy
140
+
141
+ # TESTGROUP: net.uuid
142
+ newnet = conn.create_network_xml(new_net_xml)
143
+
144
+ expect_too_many_args(newnet, "uuid", 1)
145
+
146
+ expect_success(newnet, "no args", "uuid") {|x| x == UUID}
147
+
148
+ newnet.destroy
149
+
150
+ # TESTGROUP: net.xml_desc
151
+ newnet = conn.create_network_xml(new_net_xml)
152
+
153
+ expect_too_many_args(newnet, "xml_desc", 1, 2)
154
+ expect_invalid_arg_type(newnet, "xml_desc", "foo")
155
+
156
+ expect_success(newnet, "no args", "xml_desc")
157
+
158
+ newnet.destroy
159
+
160
+ # TESTGROUP: net.bridge_name
161
+ newnet = conn.create_network_xml(new_net_xml)
162
+
163
+ expect_too_many_args(newnet, "bridge_name", 1)
164
+
165
+ expect_success(newnet, "no args", "bridge_name") {|x| x == "rubybr0"}
166
+
167
+ newnet.destroy
168
+
169
+ # TESTGROUP: net.autostart?
170
+ newnet = conn.define_network_xml(new_net_xml)
171
+
172
+ expect_too_many_args(newnet, "autostart?", 1)
173
+
174
+ expect_success(newnet, "no args", "autostart?") {|x| x == false}
175
+
176
+ newnet.autostart = true
177
+
178
+ expect_success(newnet, "no args", "autostart?") {|x| x == true}
179
+
180
+ newnet.undefine
181
+
182
+ # TESTGROUP: net.autostart=
183
+ newnet = conn.define_network_xml(new_net_xml)
184
+
185
+ expect_too_many_args(newnet, "autostart=", 1, 2)
186
+ expect_invalid_arg_type(newnet, "autostart=", 'foo')
187
+ expect_invalid_arg_type(newnet, "autostart=", nil)
188
+ expect_invalid_arg_type(newnet, "autostart=", 1234)
189
+
190
+ expect_success(newnet, "boolean arg", "autostart=", true)
191
+ if not newnet.autostart?
192
+ puts_fail "net.autostart= did not set autostart to true"
193
+ else
194
+ puts_ok "net.autostart= set autostart to true"
38
195
  end
39
196
 
197
+ expect_success(newnet, "boolean arg", "autostart=", false)
198
+ if newnet.autostart?
199
+ puts_fail "net.autostart= did not set autostart to false"
200
+ else
201
+ puts_ok "net.autostart= set autostart to false"
202
+ end
203
+
204
+ newnet.undefine
205
+
206
+ # TESTGROUP: net.free
207
+ newnet = conn.define_network_xml(new_net_xml)
208
+ newnet.undefine
209
+ expect_too_many_args(newnet, "free", 1)
210
+
211
+ expect_success(newnet, "no args", "free")
212
+
213
+ # TESTGROUP: net.active?
214
+ newnet = conn.create_network_xml(new_net_xml)
215
+
216
+ expect_too_many_args(newnet, "active?", 1)
217
+
218
+ expect_success(newnet, "no args", "active?") {|x| x == true}
219
+
220
+ newnet.destroy
221
+
222
+ newnet = conn.define_network_xml(new_net_xml)
223
+
224
+ expect_success(newnet, "no args", "active?") {|x| x == false}
225
+
226
+ newnet.create
227
+
228
+ expect_success(newnet, "no args", "active?") {|x| x == true}
229
+
230
+ newnet.destroy
231
+ newnet.undefine
232
+
233
+ # TESTGROUP: net.persistent?
234
+ newnet = conn.create_network_xml(new_net_xml)
235
+
236
+ expect_too_many_args(newnet, "persistent?", 1)
237
+
238
+ expect_success(newnet, "no args", "persistent?") {|x| x == false}
239
+
240
+ newnet.destroy
241
+
242
+ newnet = conn.define_network_xml(new_net_xml)
243
+
244
+ expect_success(newnet, "no args", "persistent?") {|x| x == true}
245
+
246
+ newnet.undefine
247
+
248
+
40
249
  conn.close
250
+
251
+ finish_tests