Skip to content

Instantly share code, notes, and snippets.

@optiz0r
Last active October 12, 2016 15:17
Show Gist options
  • Save optiz0r/285de6843d53d251447f9f84b5bce5c3 to your computer and use it in GitHub Desktop.
Save optiz0r/285de6843d53d251447f9f84b5bce5c3 to your computer and use it in GitHub Desktop.
require 'spec_helper'
require 'puppet/type/entropy_mask'
require 'puppet/type/entropy_unmask'
require 'puppet/type/entropy_splitdebug'
require 'puppet/type/entropy_splitdebug_mask'
types = {
:entropy_mask => Puppet::Type::Entropy_mask,
:entropy_unmask => Puppet::Type::Entropy_unmask,
:entropy_splitdebug => Puppet::Type::Entropy_splitdebug,
:entropy_splitdebug_mask => Puppet::Type::Entropy_splitdebug_mask,
}
default_targets = {
:entropy_mask => '/etc/entropy/packages/package.mask',
:entropy_unmask => '/etc/entropy/packages/package.unmask',
:entropy_splitdebug => '/etc/entropy/packages/package.splitdebug',
:entropy_splitdebug_mask => '/etc/entropy/packages/package.splitdebug.mask',
}
types.each do |type_name, type|
describe Puppet::Type.type(type_name).provider(:parsed) do
before do
described_class.stubs(:filetype).returns(Puppet::Util::FileType::FileTypeRam)
described_class.stubs(:filetype=)
@default_target = described_class.default_target
end
describe "should have a default target of #{default_targets[type_name]}" do
it do
expect(described_class.default_target).to eq(default_targets[type_name])
end
end
describe "when parsing" do
it "should parse out the name" do
line = 'app-admin/foobar ## Puppet Name: foobar'
expect(described_class.parse_line(line)[:name]).to eq('foobar')
end
context "with just a package name" do
line = 'app-admin/foobar ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
it "should parse out the package name" do
expect(parsed[:package]).to eq('app-admin/foobar')
end
it "should have all other parameters undefined" do
[:operator, :version, :slot, :use, :tag, :repo].each do |param|
expect(parsed[param]).to be_nil
end
end
end
context "with a versioned package" do
line = 'app-admin/foobar-1.2.3_alpha1-r1 ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
it "should parse out the package name" do
expect(parsed[:package]).to eq('app-admin/foobar')
end
it "should parse out the version" do
expect(parsed[:version]).to eq('1.2.3_alpha1-r1')
end
it "should have all other parameters undefined" do
[:operator, :slot, :use, :tag, :repo].each do |param|
expect(parsed[param]).to be_nil
end
end
end
context "with a package range" do
line = '>=app-admin/foobar-1.2.3_alpha1-r1 ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
it "should parse out the package name" do
expect(parsed[:package]).to eq('app-admin/foobar')
end
it "should parse out the version" do
expect(parsed[:version]).to eq('1.2.3_alpha1-r1')
end
it "should parse out the operator" do
expect(parsed[:operator]).to eq('>=')
end
it "should have all other parameters undefined" do
[:slot, :use, :tag, :repo].each do |param|
expect(parsed[param]).to be_nil
end
end
end
context "with a slotted package" do
line = 'app-admin/foobar:1.1 ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
it "should parse out the package name" do
expect(parsed[:package]).to eq('app-admin/foobar')
end
it "should parse out the slot" do
expect(parsed[:slot]).to eq('1.1')
end
it "should have all other parameters undefined" do
[:operator, :version, :use, :tag, :repo].each do |param|
expect(parsed[param]).to be_nil
end
end
end
context "with a package with use restrictions" do
line = 'app-admin/foobar[-foo,bar] ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
it "should parse out the package name" do
expect(parsed[:package]).to eq('app-admin/foobar')
end
it "should parse out the use" do
expect(parsed[:use]).to eq('-foo,bar')
end
it "should have all other parameters undefined" do
[:operator, :version, :slot, :tag, :repo].each do |param|
expect(parsed[param]).to be_nil
end
end
end
context "with a tagged package" do
line = 'app-admin/foobar#server ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
it "should parse out the package name" do
expect(parsed[:package]).to eq('app-admin/foobar')
end
it "should parse out the tag" do
expect(parsed[:tag]).to eq('server')
end
it "should have all other parameters undefined" do
[:operator, :version, :slot, :use, :repo].each do |param|
expect(parsed[param]).to be_nil
end
end
end
context "with a package from a specific repo" do
line = 'app-admin/foobar::community ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
it "should parse out the package name" do
expect(parsed[:package]).to eq('app-admin/foobar')
end
it "should parse out the repo" do
expect(parsed[:repo]).to eq('community')
end
it "should have all other parameters undefined" do
[:operator, :version, :slot, :use, :tag].each do |param|
expect(parsed[param]).to be_nil
end
end
end
context "with everything" do
line = '>=app-admin/foobar-1.2.3a_alpha1-r1:1[-foo]#server::community ## Puppet Name: foobar'
parsed = described_class.parse_line(line)
expected = {
:name => 'foobar',
:operator => '>=',
:package => 'app-admin/foobar',
:version => '1.2.3a_alpha1-r1',
:slot => '1',
:use => '-foo',
:tag => 'server',
:repo => 'community',
}
it "should parse out all parameters" do
expected.each do |param, value|
expect(parsed[param]).to eq(value)
end
end
end
end
describe "when flushing" do
before :each do
@ramfile = Puppet::Util::FileType::FileTypeRam.new(@default_target)
File.stubs(:exist?).with(default_targets[type_name]).returns(true)
described_class.any_instance.stubs(:target_object).returns(@ramfile)
resource = type.new(:name => 'test', :package => 'app-admin/foobar')
resource.stubs(:should).with(:target).returns(@default_target)
@providerinstance = described_class.new(resource)
@providerinstance.ensure = :present
# for debugging, try adding below line, causes 02-passes when present, 03-failures when commented out
#@providerinstance.package = 'app-admin/foobar'
end
after :each do
described_class.clear
end
it "should write an atom name to disk" do
@providerinstance.flush
expect(@ramfile.read.chomp).to eq ('app-admin/foobar ## Puppet Name: test')
end
end
end
end
......................................................................................................................................................................................
Finished in 2.24 seconds (files took 1.64 seconds to load)
182 examples, 0 failures
..........................................................................................................F........................F........................F........................F
Failures:
1) Puppet::Type::Entropy_mask::ProviderParsed when flushing should write an atom name to disk
Failure/Error: line += record[:package]
TypeError:
no implicit conversion of nil into String
# ./lib/puppet/provider/entropy_mask/parsed.rb:31:in `+'
# ./lib/puppet/provider/entropy_mask/parsed.rb:31:in `to_line'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/util/fileparsing.rb:319:in `to_line'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/util/fileparsing.rb:298:in `block in to_file'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/util/fileparsing.rb:298:in `collect'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/util/fileparsing.rb:298:in `to_file'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/provider/parsedfile.rb:381:in `to_file'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/provider/parsedfile.rb:98:in `flush_target'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/provider/parsedfile.rb:71:in `block in flush'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/provider/parsedfile.rb:68:in `each'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/provider/parsedfile.rb:68:in `flush'
# ./vendor/bundle/ruby/2.1.0/gems/puppet-4.7.0/lib/puppet/provider/parsedfile.rb:428:in `flush'
# ./spec/unit/provider/package_types/package_types_spec.rb:217:in `block (4 levels) in <top (required)>'
...
Finished in 2.23 seconds (files took 1.6 seconds to load)
182 examples, 4 failures
require 'puppet/provider/parsedfile'
masks = "/etc/entropy/packages/package.mask"
Puppet::Type.type(:entropy_mask).provide(:parsed,
:parent => Puppet::Provider::ParsedFile,
:default_target => masks,
:filetype => :flat
) do
desc "File mask provider for entropy packages"
defaultfor :operatingsystem => :sabayon
text_line :blank,
:match => /^\s*$/
text_line :comment,
:match => /^\s*#/
text_line :unmanaged,
:match => %r{^([<>]?=)?([a-zA-Z+\/-]*)(?:-(\d+(?:\.\d+)*[a-z]*(?:_(?:alpha|beta|pre|p|rc)\d*)?(?:-r\d+)?))?(?::([a-zA-Z0-9._-]+))?(?:\[([^\]]*)\])?(?:#([a-zA-Z0-9._-]+))?(?:::([a-zA-Z0-9\._-]+))?\s*$}
record_line :parsed,
:fields => %w{operator package version slot use tag repo name},
:match => %r{^([<>]?=)?([a-zA-Z+\/-]*)(?:-(\d+(?:\.\d+)*[a-z]*(?:_(?:alpha|beta|pre|p|rc)\d*)?(?:-r\d+)?))?(?::([a-zA-Z0-9._-]+))?(?:\[([^\]]*)\])?(?:#([a-zA-Z0-9._-]+))?(?:::([a-zA-Z0-9\._-]+))?\s+#+ Puppet Name: (.*)\s*$},
:block_eval => :instance do
def to_line(record)
line = ""
line += record[:operator] if record[:operator]
line += record[:package]
line += "-" + record[:version] if record[:version]
line += ":" + record[:slot] if record[:slot]
line += "[" + record[:use] + "]" if record[:use]
line += "#" + record[:tag] if record[:tag]
line += "::" + record[:repo] if record[:repo]
line += " ## Puppet Name: " + record[:name]
line
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment