pairing with luke, nagios_command provider skeleton
[vinpup.git] / test / util / filetype.rb
blob6c7ede07b9e496cfb0b4aeab62a1a7bc76bd4148
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../lib/puppettest'
5 require 'puppettest'
6 require 'puppet/util/filetype'
7 require 'mocha'
9 class TestFileType < Test::Unit::TestCase
10         include PuppetTest
12     def test_flat
13         obj = nil
14         path = tempfile()
15         type = nil
17         assert_nothing_raised {
18             type = Puppet::Util::FileType.filetype(:flat)
19         }
21         assert(type, "Could not retrieve flat filetype")
23         assert_nothing_raised {
24             obj = type.new(path)
25         }
27         text = "This is some text\n"
29         newtext = nil
30         assert_nothing_raised {
31             newtext = obj.read
32         }
34         # The base class doesn't allow a return of nil
35         assert_equal("", newtext, "Somehow got some text")
37         assert_nothing_raised {
38             obj.write(text)
39         }
40         assert_nothing_raised {
41             newtext = obj.read
42         }
44         assert_equal(text, newtext, "Text was changed somehow")
46         File.open(path, "w") { |f| f.puts "someyayness" }
48         text = File.read(path)
49         assert_nothing_raised {
50             newtext = obj.read
51         }
53         assert_equal(text, newtext, "Text was changed somehow")
54     end
56     # Make sure that modified files are backed up before they're changed.
57     def test_backup_is_called
58         path = tempfile
59         File.open(path, "w") { |f| f.print 'yay' }
61         obj = Puppet::Util::FileType.filetype(:flat).new(path)
63         obj.expects(:backup)
65         obj.write("something")
67         assert_equal("something", File.read(path), "File did not get changed")
68     end
70     def test_backup
71         path = tempfile
72         type = Puppet::Type.type(:filebucket)
74         obj = Puppet::Util::FileType.filetype(:flat).new(path)
76         # First try it when the file does not yet exist.
77         assert_nothing_raised("Could not call backup when file does not exist") do
78             obj.backup
79         end
81         # Then create the file
82         File.open(path, "w") { |f| f.print 'one' }
84         # Then try it with no filebucket objects
85         assert_nothing_raised("Could not call backup with no buckets") do
86             obj.backup
87         end
88         puppet = type["puppet"]
89         assert(puppet, "Did not create default filebucket")
91         assert_equal("one", puppet.bucket.getfile(Digest::MD5.hexdigest(File.read(path))), "Could not get file from backup")
93         # Try it again when the default already exists
94         File.open(path, "w") { |f| f.print 'two' }
95         assert_nothing_raised("Could not call backup with no buckets") do
96             obj.backup
97         end
99         assert_equal("two", puppet.bucket.getfile(Digest::MD5.hexdigest(File.read(path))), "Could not get file from backup")
100     end
102     if Facter["operatingsystem"].value == "Darwin"
103     def test_ninfotoarray
104         obj = nil
105         type = nil
107         assert_nothing_raised {
108             type = Puppet::Util::FileType.filetype(:netinfo)
109         }
111         assert(type, "Could not retrieve netinfo filetype")
112         %w{users groups aliases}.each do |map|
113             assert_nothing_raised {
114                 obj = type.new(map)
115             }
117             assert_nothing_raised("could not read map %s" % map) {
118                 obj.read
119             }
121             array = nil
123             assert_nothing_raised("Failed to parse %s map" % map) {
124                 array = obj.to_array
125             }
127             assert_instance_of(Array, array)
129             array.each do |record|
130                 assert_instance_of(Hash, record)
131                 assert(record.length != 0)
132             end
133         end
134     end
135     end