summaryrefslogtreecommitdiff
path: root/spec/unit/puppet
diff options
context:
space:
mode:
authorVarac <varac@leap.se>2017-10-13 14:22:08 +0200
committerVarac <varac@leap.se>2017-10-13 14:22:08 +0200
commitf7779ce2eacaca51790d4e8d7756442b02612d30 (patch)
tree56871c123e1c964fc26e36bb86831a4a3511bd4f /spec/unit/puppet
parent066c08f8362d53f0f30897cb8710d11260c726ea (diff)
parent2339ea8db67ac7ef02d707c2a6011ae50f5d82b5 (diff)
Merge remote-tracking branch 'origin/master' into leap_masterHEADmaster
Diffstat (limited to 'spec/unit/puppet')
-rwxr-xr-xspec/unit/puppet/provider/file_line/ruby_spec.rb149
-rwxr-xr-xspec/unit/puppet/type/file_line_spec.rb64
2 files changed, 189 insertions, 24 deletions
diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb
index 1f41f62..4ec9bae 100755
--- a/spec/unit/puppet/provider/file_line/ruby_spec.rb
+++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb
@@ -2,7 +2,8 @@
require 'spec_helper'
require 'tempfile'
provider_class = Puppet::Type.type(:file_line).provider(:ruby)
-describe provider_class do
+# These tests fail on windows when run as part of the rake task. Individually they pass
+describe provider_class, :unless => Puppet::Util::Platform.windows? do
context "when adding" do
let :tmpfile do
tmp = Tempfile.new('tmp')
@@ -29,7 +30,7 @@ describe provider_class do
File.open(tmpfile, 'w') do |fh|
fh.write('foo1')
end
- expect(provider.exists?).to be_nil
+ expect(provider.exists?).to eql (false)
end
it 'should append to an existing file when creating' do
provider.create
@@ -69,7 +70,7 @@ describe provider_class do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo2")
end
- expect(@provider.exists?).to be_nil
+ expect(@provider.exists?).to eql (false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
end
@@ -112,7 +113,7 @@ describe provider_class do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
- expect(@provider.exists?).to be_nil
+ expect(@provider.exists?).to eql(false)
expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
@@ -131,11 +132,30 @@ describe provider_class do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
- expect(@provider.exists?).to be_nil
+ expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
end
+ it 'should replace all lines that match, even when some lines are correct' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'neil',
+ :path => @tmpfile,
+ :line => "\thard\tcore\t0\n",
+ :match => '^[ \t]hard[ \t]+core[ \t]+.*',
+ :multiple => true,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("\thard\tcore\t90\n\thard\tcore\t0\n")
+ end
+ expect(@provider.exists?).to eql(false)
+ @provider.create
+ expect(File.read(@tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0")
+ end
+
it 'should raise an error with invalid values' do
expect {
@resource = Puppet::Type::File_line.new(
@@ -154,7 +174,7 @@ describe provider_class do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2")
end
- expect(@provider.exists?).to be_nil
+ expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
end
@@ -162,7 +182,7 @@ describe provider_class do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo2")
end
- expect(@provider.exists?).to be_nil
+ expect(@provider.exists?).to eql(false)
@provider.create
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
end
@@ -170,10 +190,28 @@ describe provider_class do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo = bar\nfoo2")
end
- expect(@provider.exists?).to be_truthy
+ expect(@provider.exists?).to eql(true)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
end
+
+ it 'should not add line after no matches found' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'inserted = line',
+ :match => '^foo3$',
+ :append_on_no_match => false,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
+ end
+ expect(@provider.exists?).to be true
+ expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
+ end
end
describe 'using after' do
@@ -274,7 +312,7 @@ describe provider_class do
}
)
@provider = provider_class.new(@resource)
- expect(@provider.exists?).to be_nil
+ expect(@provider.exists?).to eql (false)
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz")
end
@@ -299,7 +337,7 @@ describe provider_class do
end
end
- context "when removing" do
+ context "when removing with a line" do
before :each do
# TODO: these should be ported over to use the PuppetLabs spec_helper
# file fixtures once the following pull request has been merged:
@@ -340,6 +378,23 @@ describe provider_class do
@provider.destroy
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
end
+
+ it 'example in the docs' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'bashrc_proxy',
+ :ensure => 'absent',
+ :path => @tmpfile,
+ :line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
+ end
end
context "when removing with a match" do
@@ -367,7 +422,7 @@ describe provider_class do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo\nfoo2")
end
- expect(@provider.exists?).to be_truthy
+ expect(@provider.exists?).to eql (true)
end
it 'should remove one line if it matches' do
@@ -378,6 +433,41 @@ describe provider_class do
expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
end
+ it 'the line parameter is actually not used at all but is silently ignored if here' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'supercalifragilisticexpialidocious',
+ :ensure => 'absent',
+ :match => 'o$',
+ :match_for_absence => true,
+ }
+ )
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo\nfoo2")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
+ end
+
+ it 'and may not be here and does not need to be here' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :ensure => 'absent',
+ :match => 'o$',
+ :match_for_absence => true,
+ }
+ )
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo\nfoo2")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
+ end
+
it 'should raise an error if more than one line matches' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
@@ -442,6 +532,41 @@ describe provider_class do
expect(File.read(@tmpfile)).to eql("foo1\nfoo\n")
end
- end
+ it 'example in the docs' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'bashrc_proxy',
+ :ensure => 'absent',
+ :path => @tmpfile,
+ :line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
+ :match => '^export\ HTTP_PROXY\=',
+ :match_for_absence => true,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
+ end
+ it 'example in the docs showing line is redundant' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'bashrc_proxy',
+ :ensure => 'absent',
+ :path => @tmpfile,
+ :match => '^export\ HTTP_PROXY\=',
+ :match_for_absence => true,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
+ end
+ end
end
diff --git a/spec/unit/puppet/type/file_line_spec.rb b/spec/unit/puppet/type/file_line_spec.rb
index 48e2670..db07352 100755
--- a/spec/unit/puppet/type/file_line_spec.rb
+++ b/spec/unit/puppet/type/file_line_spec.rb
@@ -2,14 +2,28 @@
require 'spec_helper'
require 'tempfile'
describe Puppet::Type.type(:file_line) do
+ let :tmp_path do
+ if Puppet::Util::Platform.windows?
+ 'C:\tmp\path'
+ else
+ '/tmp/path'
+ end
+ end
+ let :my_path do
+ if Puppet::Util::Platform.windows?
+ 'C:\my\path'
+ else
+ '/my/path'
+ end
+ end
let :file_line do
- Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => '/tmp/path')
+ Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => tmp_path)
end
it 'should accept a line and path' do
file_line[:line] = 'my_line'
expect(file_line[:line]).to eq('my_line')
- file_line[:path] = '/my/path'
- expect(file_line[:path]).to eq('/my/path')
+ file_line[:path] = my_path
+ expect(file_line[:path]).to eq(my_path)
end
it 'should accept a match regex' do
file_line[:match] = '^foo.*$'
@@ -19,7 +33,7 @@ describe Puppet::Type.type(:file_line) do
expect {
Puppet::Type.type(:file_line).new(
:name => 'foo',
- :path => '/my/path',
+ :path => my_path,
:line => 'foo=bar',
:match => '^bar=blah$'
)}.not_to raise_error
@@ -28,23 +42,44 @@ describe Puppet::Type.type(:file_line) do
expect {
Puppet::Type.type(:file_line).new(
:name => 'foo',
- :path => '/my/path',
+ :path => my_path,
:line => 'foo=bar',
:match => '^\s*foo=.*$'
)}.not_to raise_error
end
+ it 'should accept utf8 characters' do
+ expect {
+ Puppet::Type.type(:file_line).new(
+ :name => 'ƒồỗ',
+ :path => my_path,
+ :line => 'ƒồỗ=ьåя',
+ :match => '^ьåя=βļάħ$'
+ )}.not_to raise_error
+ end
+ it 'should accept double byte characters' do
+ expect {
+ Puppet::Type.type(:file_line).new(
+ :name => 'フーバー',
+ :path => my_path,
+ :line => 'この=それ',
+ :match => '^この=ああ$'
+ )}.not_to raise_error
+ end
it 'should accept posix filenames' do
- file_line[:path] = '/tmp/path'
- expect(file_line[:path]).to eq('/tmp/path')
+ file_line[:path] = tmp_path
+ expect(file_line[:path]).to eq(tmp_path)
end
it 'should not accept unqualified path' do
expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, /File paths must be fully qualified/)
end
it 'should require that a line is specified' do
- expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.to raise_error(Puppet::Error, /line is a required attribute/)
+ expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path) }.to raise_error(Puppet::Error, /line is a required attribute/)
end
it 'should not require that a line is specified when matching for absence' do
- expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file', :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
+ expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
+ end
+ it 'although if a line is specified anyway when matching for absence it still works and the line is silently ignored' do
+ expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :line => 'i_am_irrelevant', :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
end
it 'should require that a file is specified' do
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /path is a required attribute/)
@@ -55,15 +90,20 @@ describe Puppet::Type.type(:file_line) do
it 'should default to replace => true' do
expect(file_line[:replace]).to eq :true
end
-
+ it 'should default to encoding => UTF-8' do
+ expect(file_line[:encoding]).to eq 'UTF-8'
+ end
+ it 'should accept encoding => iso-8859-1' do
+ expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :present, :encoding => 'iso-8859-1', :line => 'bar') }.not_to raise_error
+ end
it "should autorequire the file it manages" do
catalog = Puppet::Resource::Catalog.new
- file = Puppet::Type.type(:file).new(:name => "/tmp/path")
+ file = Puppet::Type.type(:file).new(:name => tmp_path)
catalog.add_resource file
catalog.add_resource file_line
relationship = file_line.autorequire.find do |rel|
- (rel.source.to_s == "File[/tmp/path]") and (rel.target.to_s == file_line.to_s)
+ (rel.source.to_s == "File[#{tmp_path}]") and (rel.target.to_s == file_line.to_s)
end
expect(relationship).to be_a Puppet::Relationship
end