File: slice_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (136 lines) | stat: -rw-r--r-- 4,889 bytes parent folder | download | duplicates (6)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
require 'matchers/resource'

describe 'methods' do
  include PuppetSpec::Compiler
  include Matchers::Resource

  before :each do
    node      = Puppet::Node.new("floppy", :environment => 'production')
    @compiler = Puppet::Parser::Compiler.new(node)
    @scope    = Puppet::Parser::Scope.new(@compiler)
    @topscope = @scope.compiler.topscope
    @scope.parent = @topscope
  end

  context "should be callable on array as" do

    it 'slice with explicit parameters' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = [1, present, 2, absent, 3, present]
        $a.slice(2) |$k,$v| {
          file { "/file_${$k}": ensure => $v }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'absent')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
    end

    it 'slice with captures last' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = [1, present, 2, absent, 3, present]
        $a.slice(2) |*$kv| {
          file { "/file_${$kv[0]}": ensure => $kv[1] }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'absent')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
    end

    it 'slice with one parameter' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = [1, present, 2, absent, 3, present]
        $a.slice(2) |$k| {
          file { "/file_${$k[0]}": ensure => $k[1] }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'absent')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
    end

    it 'slice with shorter last slice' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = [1, present, 2, present, 3, absent]
        $a.slice(4) |$a, $b, $c, $d| {
          file { "/file_$a.$c": ensure => $b }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1.2]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_3.]").with_parameter(:ensure, 'absent')
    end
  end

  context "should be callable on hash as" do
    it 'slice with explicit parameters, missing are empty' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = {1=>present, 2=>present, 3=>absent}
        $a.slice(2) |$a,$b| {
          file { "/file_${a[0]}.${b[0]}": ensure => $a[1] }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1.2]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_3.]").with_parameter(:ensure, 'absent')
    end
  end

  context "should be callable on enumerable types as" do
    it 'slice with integer range' do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = Integer[1,4]
        $a.slice(2) |$a,$b| {
          file { "/file_${a}.${b}": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1.2]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_3.4]").with_parameter(:ensure, 'present')
    end

    it 'slice with integer' do
      catalog = compile_to_catalog(<<-MANIFEST)
        4.slice(2) |$a,$b| {
          file { "/file_${a}.${b}": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_0.1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_2.3]").with_parameter(:ensure, 'present')
    end

    it 'slice with string' do
      catalog = compile_to_catalog(<<-MANIFEST)
        'abcd'.slice(2) |$a,$b| {
          file { "/file_${a}.${b}": ensure => present }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_a.b]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_c.d]").with_parameter(:ensure, 'present')
    end
  end

  context "when called without a block" do
    it "should produce an array with the result" do
      catalog = compile_to_catalog(<<-MANIFEST)
        $a = [1, present, 2, absent, 3, present]
        $a.slice(2).each |$k| {
          file { "/file_${$k[0]}": ensure => $k[1] }
        }
      MANIFEST

      expect(catalog).to have_resource("File[/file_1]").with_parameter(:ensure, 'present')
      expect(catalog).to have_resource("File[/file_2]").with_parameter(:ensure, 'absent')
      expect(catalog).to have_resource("File[/file_3]").with_parameter(:ensure, 'present')
    end
  end
end