2014-05-07 11:49:25 +02:00
|
|
|
#! /usr/bin/env ruby -S rspec
|
2014-03-05 21:43:58 +01:00
|
|
|
require 'spec_helper'
|
2011-07-27 00:27:42 +02:00
|
|
|
require 'tempfile'
|
2011-08-05 04:17:48 +02:00
|
|
|
provider_class = Puppet::Type.type(:file_line).provider(:ruby)
|
2011-07-27 00:27:42 +02:00
|
|
|
describe provider_class do
|
2012-06-07 18:09:14 +02:00
|
|
|
context "when adding" do
|
2013-08-30 01:27:27 +02:00
|
|
|
let :tmpfile do
|
2012-02-09 15:56:09 +01:00
|
|
|
tmp = Tempfile.new('tmp')
|
2013-08-30 01:27:27 +02:00
|
|
|
path = tmp.path
|
2012-02-09 15:56:09 +01:00
|
|
|
tmp.close!
|
2013-08-30 01:27:27 +02:00
|
|
|
path
|
|
|
|
end
|
|
|
|
let :resource do
|
|
|
|
Puppet::Type::File_line.new(
|
|
|
|
{:name => 'foo', :path => tmpfile, :line => 'foo'}
|
2012-02-09 15:56:09 +01:00
|
|
|
)
|
2011-07-27 00:27:42 +02:00
|
|
|
end
|
2013-08-30 01:27:27 +02:00
|
|
|
let :provider do
|
|
|
|
provider_class.new(resource)
|
|
|
|
end
|
|
|
|
|
2012-02-09 15:56:09 +01:00
|
|
|
it 'should detect if the line exists in the file' do
|
2013-08-30 01:27:27 +02:00
|
|
|
File.open(tmpfile, 'w') do |fh|
|
2012-02-09 15:56:09 +01:00
|
|
|
fh.write('foo')
|
|
|
|
end
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(provider.exists?).to be_truthy
|
2012-02-09 15:56:09 +01:00
|
|
|
end
|
|
|
|
it 'should detect if the line does not exist in the file' do
|
2013-08-30 01:27:27 +02:00
|
|
|
File.open(tmpfile, 'w') do |fh|
|
2012-02-09 15:56:09 +01:00
|
|
|
fh.write('foo1')
|
|
|
|
end
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(provider.exists?).to be_nil
|
2012-02-09 15:56:09 +01:00
|
|
|
end
|
|
|
|
it 'should append to an existing file when creating' do
|
2013-08-30 01:27:27 +02:00
|
|
|
provider.create
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(tmpfile).chomp).to eq('foo')
|
2011-07-27 00:27:42 +02:00
|
|
|
end
|
|
|
|
end
|
2012-02-09 15:56:09 +01:00
|
|
|
|
2012-06-07 18:09:14 +02:00
|
|
|
context "when matching" do
|
2012-02-09 15:56:09 +01:00
|
|
|
before :each do
|
2012-06-07 18:09:14 +02:00
|
|
|
# TODO: these should be ported over to use the PuppetLabs spec_helper
|
|
|
|
# file fixtures once the following pull request has been merged:
|
|
|
|
# https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
|
|
|
|
tmp = Tempfile.new('tmp')
|
|
|
|
@tmpfile = tmp.path
|
|
|
|
tmp.close!
|
|
|
|
@resource = Puppet::Type::File_line.new(
|
2015-04-09 20:02:29 +02:00
|
|
|
{
|
|
|
|
:name => 'foo',
|
|
|
|
:path => @tmpfile,
|
|
|
|
:line => 'foo = bar',
|
|
|
|
:match => '^foo\s*=.*$',
|
|
|
|
}
|
2012-06-07 18:09:14 +02:00
|
|
|
)
|
|
|
|
@provider = provider_class.new(@resource)
|
|
|
|
end
|
|
|
|
|
2013-08-30 01:27:27 +02:00
|
|
|
describe 'using match' do
|
|
|
|
it 'should raise an error if more than one line matches, and should not have modified the file' do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
|
|
|
|
end
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(@provider.exists?).to be_nil
|
2013-08-30 01:27:27 +02:00
|
|
|
expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
|
2012-06-07 18:09:14 +02:00
|
|
|
end
|
|
|
|
|
2013-08-30 01:27:27 +02:00
|
|
|
it 'should replace all lines that matches' do
|
|
|
|
@resource = Puppet::Type::File_line.new(
|
2013-06-22 00:33:44 +02:00
|
|
|
{
|
2015-04-09 20:02:29 +02:00
|
|
|
:name => 'foo',
|
|
|
|
:path => @tmpfile,
|
|
|
|
:line => 'foo = bar',
|
|
|
|
:match => '^foo\s*=.*$',
|
|
|
|
:multiple => true,
|
2013-06-22 00:33:44 +02:00
|
|
|
}
|
2013-08-30 01:27:27 +02:00
|
|
|
)
|
|
|
|
@provider = provider_class.new(@resource)
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
|
|
|
|
end
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(@provider.exists?).to be_nil
|
2013-08-30 01:27:27 +02:00
|
|
|
@provider.create
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
|
2013-06-22 00:33:44 +02:00
|
|
|
end
|
|
|
|
|
2013-08-30 01:27:27 +02:00
|
|
|
it 'should raise an error with invalid values' do
|
|
|
|
expect {
|
|
|
|
@resource = Puppet::Type::File_line.new(
|
|
|
|
{
|
2015-04-09 20:02:29 +02:00
|
|
|
:name => 'foo',
|
|
|
|
:path => @tmpfile,
|
|
|
|
:line => 'foo = bar',
|
|
|
|
:match => '^foo\s*=.*$',
|
|
|
|
:multiple => 'asgadga',
|
2013-08-30 01:27:27 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should replace a line that matches' do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo=blah\nfoo2")
|
|
|
|
end
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(@provider.exists?).to be_nil
|
2013-08-30 01:27:27 +02:00
|
|
|
@provider.create
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
|
2013-08-30 01:27:27 +02:00
|
|
|
end
|
|
|
|
it 'should add a new line if no lines match' do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo2")
|
|
|
|
end
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(@provider.exists?).to be_nil
|
2013-08-30 01:27:27 +02:00
|
|
|
@provider.create
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
|
2013-08-30 01:27:27 +02:00
|
|
|
end
|
|
|
|
it 'should do nothing if the exact line already exists' do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo = bar\nfoo2")
|
|
|
|
end
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(@provider.exists?).to be_truthy
|
2013-08-30 01:27:27 +02:00
|
|
|
@provider.create
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
|
2013-08-29 18:19:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-30 01:27:27 +02:00
|
|
|
describe 'using after' do
|
|
|
|
let :resource do
|
|
|
|
Puppet::Type::File_line.new(
|
2013-06-22 00:33:44 +02:00
|
|
|
{
|
2013-08-30 01:27:27 +02:00
|
|
|
:name => 'foo',
|
|
|
|
:path => @tmpfile,
|
|
|
|
:line => 'inserted = line',
|
|
|
|
:after => '^foo1',
|
2013-06-22 00:33:44 +02:00
|
|
|
}
|
|
|
|
)
|
2013-08-30 01:27:27 +02:00
|
|
|
end
|
2013-06-22 00:33:44 +02:00
|
|
|
|
2013-08-30 01:27:27 +02:00
|
|
|
let :provider do
|
|
|
|
provider_class.new(resource)
|
2012-06-07 18:09:14 +02:00
|
|
|
end
|
2015-04-09 20:02:29 +02:00
|
|
|
context 'match and after set' do
|
|
|
|
shared_context 'resource_create' do
|
|
|
|
let(:match) { '^foo2$' }
|
|
|
|
let(:after) { '^foo1$' }
|
|
|
|
let(:resource) {
|
|
|
|
Puppet::Type::File_line.new(
|
|
|
|
{
|
|
|
|
:name => 'foo',
|
|
|
|
:path => @tmpfile,
|
|
|
|
:line => 'inserted = line',
|
|
|
|
:after => after,
|
|
|
|
:match => match,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
before :each do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo2\nfoo = baz")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
describe 'inserts at match' do
|
|
|
|
include_context 'resource_create'
|
|
|
|
it {
|
|
|
|
provider.create
|
|
|
|
expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
describe 'inserts a new line after when no match' do
|
|
|
|
include_context 'resource_create' do
|
|
|
|
let(:match) { '^nevergoingtomatch$' }
|
|
|
|
end
|
|
|
|
it {
|
|
|
|
provider.create
|
|
|
|
expect(File.read(@tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
describe 'append to end of file if no match for both after and match' do
|
|
|
|
include_context 'resource_create' do
|
|
|
|
let(:match) { '^nevergoingtomatch$' }
|
|
|
|
let(:after) { '^stillneverafter' }
|
|
|
|
end
|
|
|
|
it {
|
|
|
|
provider.create
|
|
|
|
expect(File.read(@tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2013-08-30 01:27:27 +02:00
|
|
|
context 'with one line matching the after expression' do
|
|
|
|
before :each do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'inserts the specified line after the line matching the "after" expression' do
|
|
|
|
provider.create
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz")
|
2013-08-30 01:27:27 +02:00
|
|
|
end
|
2012-06-07 18:09:14 +02:00
|
|
|
end
|
2013-08-30 01:27:27 +02:00
|
|
|
|
2015-05-29 06:27:08 +02:00
|
|
|
context 'with multiple lines matching the after expression' do
|
2013-08-30 01:27:27 +02:00
|
|
|
before :each do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'errors out stating "One or no line must match the pattern"' do
|
|
|
|
expect { provider.create }.to raise_error(Puppet::Error, /One or no line must match the pattern/)
|
|
|
|
end
|
2015-05-29 06:27:08 +02:00
|
|
|
|
|
|
|
it 'adds the line after all lines matching the after expression' do
|
|
|
|
@resource = Puppet::Type::File_line.new(
|
|
|
|
{
|
|
|
|
:name => 'foo',
|
|
|
|
:path => @tmpfile,
|
|
|
|
:line => 'inserted = line',
|
|
|
|
:after => '^foo1$',
|
|
|
|
:multiple => true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@provider = provider_class.new(@resource)
|
|
|
|
expect(@provider.exists?).to be_nil
|
|
|
|
@provider.create
|
|
|
|
expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz")
|
|
|
|
end
|
2013-08-30 01:27:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no lines matching the after expression' do
|
|
|
|
let :content do
|
|
|
|
"foo3\nfoo = blah\nfoo2\nfoo = baz\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write(content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends the specified line to the file' do
|
|
|
|
provider.create
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile)).to eq(content << resource[:line] << "\n")
|
2013-08-30 01:27:27 +02:00
|
|
|
end
|
2012-06-07 18:09:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when removing" 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:
|
|
|
|
# https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
|
2012-02-09 15:56:09 +01:00
|
|
|
tmp = Tempfile.new('tmp')
|
|
|
|
@tmpfile = tmp.path
|
|
|
|
tmp.close!
|
|
|
|
@resource = Puppet::Type::File_line.new(
|
2015-04-09 20:02:29 +02:00
|
|
|
{
|
|
|
|
:name => 'foo',
|
|
|
|
:path => @tmpfile,
|
|
|
|
:line => 'foo',
|
|
|
|
:ensure => 'absent',
|
|
|
|
}
|
2012-02-09 15:56:09 +01:00
|
|
|
)
|
|
|
|
@provider = provider_class.new(@resource)
|
|
|
|
end
|
|
|
|
it 'should remove the line if it exists' do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo\nfoo2")
|
|
|
|
end
|
|
|
|
@provider.destroy
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
|
2012-02-09 15:56:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should remove the line without touching the last new line' do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo\nfoo2\n")
|
|
|
|
end
|
|
|
|
@provider.destroy
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
|
2012-02-09 15:56:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should remove any occurence of the line' do
|
|
|
|
File.open(@tmpfile, 'w') do |fh|
|
|
|
|
fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
|
|
|
|
end
|
|
|
|
@provider.destroy
|
2014-06-04 20:37:45 +02:00
|
|
|
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
|
2012-02-09 15:56:09 +01:00
|
|
|
end
|
2011-07-27 00:27:42 +02:00
|
|
|
end
|
|
|
|
end
|