summaryrefslogtreecommitdiff
path: root/spec/integration/data_binding.rb
blob: 0e8db4dfa736423d3d2a69ccbc581701a883a3ce (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'spec_helper'

require 'puppet_spec/compiler'

describe "Data binding" do
  include PuppetSpec::Files
  include PuppetSpec::Compiler

  let(:dir) { tmpdir("puppetdir") }

  before do
    Puppet[:data_binding_terminus] = "hiera"
    Puppet[:modulepath] = dir
  end

  it "looks up data from hiera" do
    configure_hiera_for({
      "testing::binding::value" => "the value",
      "testing::binding::calling_class" => "%{calling_class}",
      "testing::binding::calling_module" => "%{calling_module}"
    })

    create_manifest_in_module("testing", "binding.pp",
                              <<-MANIFEST)
    class testing::binding($value,
                           $calling_class,
                           $calling_module) {}
    MANIFEST

    catalog = compile_to_catalog("include testing::binding")
    resource = catalog.resource('Class[testing::binding]')

    expect(resource[:value]).to eq("the value")
    expect(resource[:calling_class]).to eq("testing::binding")
    expect(resource[:calling_module]).to eq("testing")
  end

  it "works with the puppet backend configured, although it can't use it for lookup" do
    configure_hiera_for_puppet
    create_manifest_in_module("testing", "binding.pp",
                              <<-MANIFEST)
    # lookup via the puppet backend to ensure it works
    class testing::binding($value = hiera('variable')) {}
    MANIFEST

    create_manifest_in_module("testing", "data.pp",
                              <<-MANIFEST)
    class testing::data {
      $variable = "the value"
    }
    MANIFEST

    catalog = compile_to_catalog("include testing::binding")
    resource = catalog.resource('Class[testing::binding]')

    expect(resource[:value]).to eq("the value")
  end

  def configure_hiera_for(data)
    hiera_config_file = tmpfile("hiera.yaml")

    File.open(hiera_config_file, 'w') do |f|
      f.write("---
        :yaml:
          :datadir: #{dir}
        :hierarchy: ['global']
        :logger: 'noop'
        :backends: ['yaml']
      ")
    end

    File.open(File.join(dir, 'global.yaml'), 'w') do |f|
      f.write(YAML.dump(data))
    end

    Puppet[:hiera_config] = hiera_config_file
  end

  def configure_hiera_for_puppet
    hiera_config_file = tmpfile("hiera.yaml")

    File.open(hiera_config_file, 'w') do |f|
      f.write("---
        :logger: 'noop'
        :backends: ['puppet']
      ")
    end

    Puppet[:hiera_config] = hiera_config_file
  end

  def create_manifest_in_module(module_name, name, manifest)
    module_dir = File.join(dir, module_name, 'manifests')
    FileUtils.mkdir_p(module_dir)

    File.open(File.join(module_dir, name), 'w') do |f|
      f.write(manifest)
    end
  end
end