summaryrefslogtreecommitdiff
path: root/spec/unit/pops/binder/bindings_composer_spec.rb
blob: d8e4b6f9ca3b0d597bc676e20e9ab0e8120b959c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'spec_helper'
require 'puppet/pops'
require 'puppet_spec/pops'

describe 'BinderComposer' do
  include PuppetSpec::Pops

  def config_dir(config_name)
    my_fixture(config_name)
  end

  let(:acceptor)    { Puppet::Pops::Validation::Acceptor.new() }
  let(:diag)        { Puppet::Pops::Binder::Config::DiagnosticProducer.new(acceptor) }
  let(:issues)      { Puppet::Pops::Binder::Config::Issues }
  let(:node)        { Puppet::Node.new('localhost') }
  let(:compiler)    { Puppet::Parser::Compiler.new(node)}
  let(:scope)       { Puppet::Parser::Scope.new(compiler) }
  let(:parser)      { Puppet::Pops::Parser::Parser.new() }
  let(:factory)     { Puppet::Pops::Binder::BindingsFactory }

  before(:each) do
    Puppet[:binder] = true
  end

  it 'should load default config if no config file exists' do
    diagnostics = diag
    composer = Puppet::Pops::Binder::BindingsComposer.new()
    composer.compose(scope)
  end

  context "when loading a complete configuration with modules" do
    let(:config_directory) { config_dir('ok') }

    it 'should load everything without errors' do
      Puppet.settings[:confdir] = config_directory
      Puppet.settings[:libdir] = File.join(config_directory, 'lib')

      Puppet.override(:environments => Puppet::Environments::Static.new(Puppet::Node::Environment.create(:production, [File.join(config_directory, 'modules')]))) do
        # this ensure the binder is active at the right time
        # (issues with getting a /dev/null path for "confdir" / "libdir")
        raise "Binder not active" unless scope.compiler.is_binder_active?

        diagnostics = diag
        composer = Puppet::Pops::Binder::BindingsComposer.new()
        the_scope = scope
        the_scope['fqdn'] = 'localhost'
        the_scope['environment'] = 'production'
        layered_bindings = composer.compose(scope)
        # puts Puppet::Pops::Binder::BindingsModelDumper.new().dump(layered_bindings)
        binder = Puppet::Pops::Binder::Binder.new(layered_bindings)
        injector = Puppet::Pops::Binder::Injector.new(binder)
        expect(injector.lookup(scope, 'awesome_x')).to be == 'golden'
        expect(injector.lookup(scope, 'good_x')).to be == 'golden'
        expect(injector.lookup(scope, 'rotten_x')).to be == nil
        expect(injector.lookup(scope, 'the_meaning_of_life')).to be == 42
        expect(injector.lookup(scope, 'has_funny_hat')).to be == 'the pope'
        expect(injector.lookup(scope, 'all your base')).to be == 'are belong to us'
        expect(injector.lookup(scope, 'env_meaning_of_life')).to be == 'production thinks it is 42'
        expect(injector.lookup(scope, '::quick::brown::fox')).to be == 'echo: quick brown fox'
      end
    end
  end

  # TODO: test error conditions (see BinderConfigChecker for what to test)

end