diff options
| author | Luke Kanies <luke@madstop.com> | 2007-11-23 15:35:18 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-11-23 15:35:18 -0600 |
| commit | eee9f5e3260a1b053333f3ab88e95241ef710d00 (patch) | |
| tree | d6592a0a31d8d12c3aaee6e0dcc085008ab3bc9d | |
| parent | 1a4e4fb46567fba6e99a2b9ba7327861edd3a739 (diff) | |
| download | puppet-eee9f5e3260a1b053333f3ab88e95241ef710d00.tar.gz puppet-eee9f5e3260a1b053333f3ab88e95241ef710d00.tar.xz puppet-eee9f5e3260a1b053333f3ab88e95241ef710d00.zip | |
Adding more tests to the redhat interface provider. It no
longer uses the :target parameter (which I'll be removing in
the next commit).
| -rw-r--r-- | lib/puppet/provider/interface/redhat.rb | 43 | ||||
| -rwxr-xr-x | spec/unit/ral/provider/interface/redhat.rb | 104 |
2 files changed, 128 insertions, 19 deletions
diff --git a/lib/puppet/provider/interface/redhat.rb b/lib/puppet/provider/interface/redhat.rb index 376f94667..aa217620e 100644 --- a/lib/puppet/provider/interface/redhat.rb +++ b/lib/puppet/provider/interface/redhat.rb @@ -5,8 +5,8 @@ Puppet::Type.type(:interface).provide(:redhat) do desc "Manage network interfaces on Red Hat operating systems. This provider parses and generates configuration files in ``/etc/sysconfig/network-scripts``." - @interface_dir = "/etc/sysconfig/network-scripts" - confine :exists => @interface_dir + INTERFACE_DIR = "/etc/sysconfig/network-scripts" + confine :exists => INTERFACE_DIR defaultfor :operatingsystem => [:fedora, :centos, :redhat] # Create the setter/gettor methods to match the model. @@ -34,7 +34,7 @@ BROADCAST= ALIAS - register_template :loopback, <<-LOOPBACKDUMMY + register_template :normal, <<-LOOPBACKDUMMY DEVICE=<%= self.device %> ONBOOT=<%= self.on_boot %> BOOTPROTO=static @@ -56,7 +56,7 @@ LOOPBACKDUMMY # use prior to needing to call self.next_dummy later on. def self.instances # parse all of the config files at once - Dir.glob("%s/ifcfg-*" % @interface_dir).collect do |file| + Dir.glob("%s/ifcfg-*" % INTERFACE_DIR).collect do |file| record = parse(file) # store the existing dummy interfaces @@ -117,7 +117,7 @@ LOOPBACKDUMMY end def create - @resource.class.validproperties.each do |property| + self.class.resource_type.validproperties.each do |property| if value = @resource.should(property) @property_hash[property] = value end @@ -128,11 +128,11 @@ LOOPBACKDUMMY end def destroy - File.unlink(@resource[:target]) + File.unlink(file_path) end def exists? - FileTest.exists?(@resource[:target]) + FileTest.exist?(file_path) end # generate the content for the interface file, so this is dependent @@ -140,19 +140,26 @@ LOOPBACKDUMMY # address (also dummy) on linux. For linux it's quite involved, and we # will use an ERB template def generate - self.class.template(@property_hash[:interface_type]).result(binding) + itype = self.interface_type == :alias ? :alias : :normal + self.class.template(itype).result(binding) end # Where should the file be written out? - # This defaults to @interface_dir/ifcfg-<namevar>, but can have a + # This defaults to INTERFACE_DIR/ifcfg-<namevar>, but can have a # more symbolic name by setting interface_desc in the type. def file_path - @resource[:interface_desc] ||= @resource[:name] - case @resource.should(:interface_type) - when :loopback - return File.join(@interface_dir, "ifcfg-" + @resource[:interface_desc]) - when :alias - return File.join(@interface_dir, "ifcfg-" + @resource[:interface] + ":" + @resource[:interface_desc]) + if resource and val = resource[:interface_desc] + desc = val + else + desc = self.name + end + + self.fail("Could not get name for interface") unless desc + + if self.interface_type == :alias + return File.join(INTERFACE_DIR, "ifcfg-" + self.interface + ":" + desc) + else + return File.join(INTERFACE_DIR, "ifcfg-" + desc) end end @@ -223,7 +230,7 @@ LOOPBACKDUMMY # Write the new file out. def flush # Don't flush to disk if we're removing the config. - return if @resource.should(:ensure) == :absent + return if self.ensure == :absent @property_hash.each do |name, val| if val == :absent @@ -231,13 +238,13 @@ LOOPBACKDUMMY end end - File.open(@resource[:target], "w") do |f| + File.open(file_path, "w") do |f| f.puts generate() end end def prefetch - @property_hash = self.class.parse(@resource[:target]) + @property_hash = self.class.parse(file_path) end end diff --git a/spec/unit/ral/provider/interface/redhat.rb b/spec/unit/ral/provider/interface/redhat.rb index 420cb10ef..0b3b75ffc 100755 --- a/spec/unit/ral/provider/interface/redhat.rb +++ b/spec/unit/ral/provider/interface/redhat.rb @@ -22,6 +22,51 @@ describe provider_class do end end +describe provider_class, " when determining the file path" do + it "should always contain '/etc/sysconfig/network-scripts/ifcfg-'" do + provider = provider_class.new(:name => "192.168.0.1") + provider.file_path.should =~ %r{^/etc/sysconfig/network-scripts/ifcfg-} + end + + it "should include the interface name and the description when the interface is an alias" do + provider = provider_class.new(:name => "192.168.0.1", :interface => "eth0") + provider.interface_type = :alias + resource = stub 'resource' + resource.stubs(:[]).with(:interface_desc).returns("blah") + provider.resource = resource + provider.file_path.should == "/etc/sysconfig/network-scripts/ifcfg-eth0:blah" + end + + it "should just include the description when the interface is not an alias" do + provider = provider_class.new(:name => "192.168.0.1") + provider.interface_type = :normal + resource = stub 'resource' + resource.stubs(:[]).with(:interface_desc).returns("eth0") + provider.resource = resource + provider.file_path.should == "/etc/sysconfig/network-scripts/ifcfg-eth0" + end + + it "should use the interface description if one is available" do + provider = provider_class.new(:name => "192.168.0.1") + provider.interface_type = :normal + resource = stub 'resource' + resource.stubs(:[]).with(:interface_desc).returns("eth0") + provider.resource = resource + provider.file_path.should == "/etc/sysconfig/network-scripts/ifcfg-eth0" + end + + it "should use the name if no interface description is available" do + provider = provider_class.new(:name => "192.168.0.1") + provider.interface_type = :normal + provider.file_path.should == "/etc/sysconfig/network-scripts/ifcfg-192.168.0.1" + end + + it "should fail if no name or interface description can be found" do + provider = provider_class.new() + proc { provider.file_path }.should raise_error + end +end + describe provider_class, " when returning instances" do it "should consider each file in the network-scripts directory an interface instance" do Dir.expects(:glob).with("/etc/sysconfig/network-scripts/ifcfg-*").returns(%w{one two}) @@ -131,8 +176,8 @@ describe provider_class, " when generating" do @provider.stubs(:device).returns("mydevice") @provider.stubs(:on_boot).returns("myboot") @provider.stubs(:name).returns("myname") - @provider.stubs(:interface_type).returns("myname") @provider.stubs(:netmask).returns("mynetmask") + @provider.interface_type = :alias @text = @provider.generate end @@ -167,3 +212,60 @@ describe provider_class, " when generating" do @text.should =~ /^IPADDR=myname$/ end end + +describe provider_class, " when creating and destroying" do + before do + @provider = provider_class.new(:interface => "eth0", :name => "testing") + @path = "/etc/sysconfig/network-scripts/ifcfg-testing" + end + + it "should consider the interface present if the file exists" do + FileTest.expects(:exist?).with(@path).returns(true) + @provider.should be_exists + end + + it "should consider the interface absent if the file does not exist" do + FileTest.expects(:exist?).with(@path).returns(false) + @provider.should_not be_exists + end + + it "should remove the file if the interface is being destroyed" do + File.expects(:unlink).with(@path) + @provider.destroy + end + + it "should mark :ensure as :absent if the interface is destroyed" do + File.stubs(:unlink) + @provider.destroy + @provider.ensure.should == :absent + end + + it "should mark :ensure as :present if the interface is being created" do + resource = stub 'resource', :name => 'testing' + resource.stubs(:should).with { |name| name == :ensure }.returns(:present) + resource.stubs(:should).with { |name| name != :ensure }.returns(nil) + @provider.resource = resource + @provider.create + @provider.ensure.should == :present + end + + it "should write the generated text to disk when the interface is flushed" do + fh = mock("filehandle") + File.expects(:open).yields(fh) + fh.expects(:puts).with("generated") + resource = stub 'resource', :name => 'testing' + resource.stubs(:[]).with(:interface_desc).returns(nil) + resource.stubs(:should).with { |name| name == :ensure }.returns(:present) + resource.stubs(:should).with { |name| name != :ensure }.returns(nil) + @provider.resource = resource + @provider.create + + @provider.stubs(:generate).returns("generated") + @provider.flush + end + + it "should not write the generated text to disk when the interface is flushed if :ensure == :absent" do + @provider.ensure = :absent + @provider.flush + end +end |
