Skip to content
Browse files

Networks can be created/defined

  • Loading branch information...
1 parent 80e5e34 commit ac9af4d109077442cf96d142419abf113d393123 @mitchellh committed Dec 14, 2010
Showing with 68 additions and 0 deletions.
  1. +1 −0 CHANGELOG.md
  2. +18 −0 lib/libvirt/collection/network_collection.rb
  3. +49 −0 test/libvirt/collection/network_collection_test.rb
View
1 CHANGELOG.md
@@ -5,6 +5,7 @@
- `Libvirt::Spec::Network` added to parse network interfaces.
- Interface device XML properly outputs `<source>` tag if `source_network`
is specified.
+ - Networks can be defined/created via the network collection.
## 0.2.0 (December 7, 2010)
View
18 lib/libvirt/collection/network_collection.rb
@@ -26,6 +26,24 @@ def find_by_uuid(uuid)
nil_or_object(FFI::Libvirt.virNetworkLookupByUUIDString(interface, uuid), Network)
end
+ # Defines a new network with the given valid specification.
+ #
+ # @param [Object] spec
+ # @return [Domain]
+ def define(spec)
+ spec = spec.is_a?(String) ? spec : spec.to_xml
+ nil_or_object(FFI::Libvirt.virNetworkDefineXML(interface, spec), Network)
+ end
+
+ # Creates a new network with the given valid specification.
+ #
+ # @param [Object] spec
+ # @return [Domain]
+ def create(spec)
+ spec = spec.is_a?(String) ? spec : spec.to_xml
+ nil_or_object(FFI::Libvirt.virNetworkCreateXML(interface, spec), Network)
+ end
+
# Returns all the active networks for the connection which this
# collection belongs to.
#
View
49 test/libvirt/collection/network_collection_test.rb
@@ -23,6 +23,55 @@
end
end
+ context "a new network" do
+ setup do
+ @spec = Libvirt::Spec::Network.new
+ @spec.name = "My New Network"
+ end
+
+ context "defining a new network" do
+ should "define the new network when the spec is valid" do
+ result = nil
+ assert_nothing_raised { result = @instance.define(@spec) }
+ assert result.is_a?(Libvirt::Network)
+ assert !result.active?
+ assert_equal @spec.name, result.name
+ end
+
+ should "define the new network when the spec is a string" do
+ result = nil
+ assert_nothing_raised { result = @instance.define(@spec.to_xml) }
+ assert result.is_a?(Libvirt::Network)
+ assert !result.active?
+ assert_equal @spec.name, result.name
+ end
+
+ should "raise an error when the specification is not valid" do
+ assert_raise(Libvirt::Exception::LibvirtError) {
+ @instance.define("")
+ }
+ end
+ end
+
+ context "creating a new network" do
+ should "create the new network with the specification" do
+ result = nil
+ assert_nothing_raised { result = @instance.create(@spec) }
+ assert result.is_a?(Libvirt::Network)
+ assert result.active?
+ assert_equal @spec.name, result.name
+ end
+
+ should "create the new network with the string spec" do
+ result = nil
+ assert_nothing_raised { result = @instance.create(@spec.to_xml) }
+ assert result.is_a?(Libvirt::Network)
+ assert result.active?
+ assert_equal @spec.name, result.name
+ end
+ end
+ end
+
should "provide a list of active networks" do
active = @instance.active
assert active.is_a?(Array)

0 comments on commit ac9af4d

Please sign in to comment.
Something went wrong with that request. Please try again.