summaryrefslogtreecommitdiff
path: root/spec/functions/ensure_packages_spec.rb
blob: 436be10bc4e83a5dc807b36a04a035fbb3068873 (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
#! /usr/bin/env ruby

require 'spec_helper'
require 'rspec-puppet'
require 'puppet_spec/compiler'

describe 'ensure_packages' do
  include PuppetSpec::Compiler

  before :each do
    Puppet::Parser::Functions.autoloader.loadall
    Puppet::Parser::Functions.function(:ensure_packages)
    Puppet::Parser::Functions.function(:ensure_resource)
    Puppet::Parser::Functions.function(:defined_with_params)
    Puppet::Parser::Functions.function(:create_resources)
  end

  let :node     do Puppet::Node.new('localhost') end
  let :compiler do Puppet::Parser::Compiler.new(node) end
  let :scope    do
    if Puppet.version.to_f >= 3.0
      Puppet::Parser::Scope.new(compiler)
    else
      newscope = Puppet::Parser::Scope.new
      newscope.compiler = compiler
      newscope.source   = Puppet::Resource::Type.new(:node, :localhost)
      newscope
    end
  end

  describe 'argument handling' do
    it 'fails with no arguments' do
      expect {
        scope.function_ensure_packages([])
      }.to raise_error(Puppet::ParseError, /0 for 1 or 2/)
    end

    it 'accepts an array of values' do
      scope.function_ensure_packages([['foo']])
    end

    it 'accepts a single package name as a string' do
      scope.function_ensure_packages(['foo'])
    end
  end

  context 'given a catalog with puppet package => absent' do
    let :catalog do
      compile_to_catalog(<<-EOS
        ensure_packages(['facter'])
        package { puppet: ensure => absent }
      EOS
      )
    end

    it 'has no effect on Package[puppet]' do
      expect(catalog.resource(:package, 'puppet')['ensure']).to eq('absent')
    end
  end

  context 'given a clean catalog' do
    let :catalog do
      compile_to_catalog('ensure_packages(["facter"])')
    end

    it 'declares package resources with ensure => present' do
      expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
    end
  end

  context 'given a clean catalog and specified defaults' do
    let :catalog do
      compile_to_catalog('ensure_packages(["facter"], {"provider" => "gem"})')
    end

    it 'declares package resources with ensure => present' do
      expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
      expect(catalog.resource(:package, 'facter')['provider']).to eq('gem')
    end
  end
end