File: datatypes_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 (352 lines) | stat: -rw-r--r-- 9,819 bytes parent folder | download | duplicates (3)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require 'spec_helper'
require 'puppet_spec/compiler'
require 'puppet_spec/files'
require 'puppet/pops'

module PuppetSpec::DataTypes
describe "Puppet::DataTypes" do
  include PuppetSpec::Compiler
  include PuppetSpec::Files

  let(:modules) { { 'mytest' => mytest } }
  let(:datatypes) { {} }
  let(:environments_dir) { Puppet[:environmentpath] }

  let(:mytest) {{
    'lib' => {
      'puppet' => {
        'datatypes' => mytest_datatypes,
        'functions' => mytest_functions },
      'puppetx' => { 'mytest' => mytest_classes },
    }
  }}

  let(:mytest_datatypes) { {} }
  let(:mytest_classes) { {} }
  let(:mytest_functions) { {
    'mytest' => {
      'to_data.rb' => <<-RUBY.unindent,
        Puppet::Functions.create_function('mytest::to_data') do
          def to_data(data)
            Puppet::Pops::Serialization::ToDataConverter.convert(data, {
              :rich_data => true,
              :symbol_as_string => true,
              :type_by_reference => true,
              :message_prefix => 'test'
            })
          end
        end
        RUBY

      'from_data.rb' => <<-RUBY.unindent,
        Puppet::Functions.create_function('mytest::from_data') do
          def from_data(data)
            Puppet::Pops::Serialization::FromDataConverter.convert(data)
          end
        end
        RUBY

      'serialize.rb' => <<-RUBY.unindent,
        Puppet::Functions.create_function('mytest::serialize') do
          def serialize(data)
            buffer = ''
            serializer = Puppet::Pops::Serialization::Serializer.new(
              Puppet::Pops::Serialization::JSON::Writer.new(buffer))
            serializer.write(data)
            serializer.finish
            buffer
          end
        end
        RUBY

      'deserialize.rb' => <<-RUBY.unindent,
        Puppet::Functions.create_function('mytest::deserialize') do
          def deserialize(data)
            deserializer = Puppet::Pops::Serialization::Deserializer.new(
              Puppet::Pops::Serialization::JSON::Reader.new(data), Puppet::Pops::Loaders.find_loader(nil))
            deserializer.read
          end
        end
        RUBY
      }
  } }

  let(:testing_env_dir) do
    dir_contained_in(environments_dir, testing_env)
    env_dir = File.join(environments_dir, 'testing')
    PuppetSpec::Files.record_tmp(env_dir)
    env_dir
  end

  let(:modules_dir) { File.join(testing_env_dir, 'modules') }
  let(:env) { Puppet::Node::Environment.create(:testing, [modules_dir]) }
  let(:node) { Puppet::Node.new('test', :environment => env) }

  let(:testing_env) do
    {
      'testing' => {
        'lib' => { 'puppet' => { 'datatypes' => datatypes } },
        'modules' => modules,
      }
    }
  end

  before(:each) do
    Puppet[:environment] = 'testing'
  end

  context 'when creating type with derived attributes using implementation' do
    let(:datatypes) {
      {
        'mytype.rb' => <<-RUBY.unindent,
          Puppet::DataTypes.create_type('Mytype') do
            interface <<-PUPPET
              attributes => {
                name => { type => String },
                year_of_birth => { type => Integer },
                age => { type => Integer, kind => derived },
              }
              PUPPET

            implementation do
              def age
                DateTime.now.year - @year_of_birth
              end
            end
          end
          RUBY
      }
    }

    it 'loads and returns value of attribute' do
      expect(eval_and_collect_notices('notice(Mytype("Bob", 1984).age)', node)).to eql(["#{DateTime.now.year - 1984}"])
    end

    it 'can convert value to and from data' do
      expect(eval_and_collect_notices(<<-PUPPET.unindent, node)).to eql(['false', 'true', 'true', "#{DateTime.now.year - 1984}"])
        $m = Mytype("Bob", 1984)
        $d = $m.mytest::to_data
        notice($m == $d)
        notice($d =~ Data)
        $m2 = $d.mytest::from_data
        notice($m == $m2)
        notice($m2.age)
      PUPPET
    end
  end

  context 'when creating type for an already implemented class' do
    let(:datatypes) {
      {
        'mytest.rb' => <<-RUBY.unindent,
          Puppet::DataTypes.create_type('Mytest') do
            interface <<-PUPPET
              attributes => {
                name => { type => String },
                year_of_birth => { type => Integer },
                age => { type => Integer, kind => derived },
              },
              functions => {
                '[]' => Callable[[String[1]], Variant[String, Integer]]
              }
              PUPPET

            implementation_class PuppetSpec::DataTypes::MyTest
          end
      RUBY
      }
    }

    before(:each) do
      class ::PuppetSpec::DataTypes::MyTest
        attr_reader :name, :year_of_birth

        def initialize(name, year_of_birth)
          @name = name
          @year_of_birth = year_of_birth
        end

        def age
          DateTime.now.year - @year_of_birth
        end

        def [](key)
          case key
          when 'name'
            @name
          when 'year_of_birth'
            @year_of_birth
          when 'age'
            age
          else
            nil
          end
        end

        def ==(o)
          self.class == o.class && @name == o.name && @year_of_birth == o.year_of_birth
        end
      end
    end

    after(:each) do
      ::PuppetSpec::DataTypes.send(:remove_const, :MyTest)
    end

    it 'loads and returns value of attribute' do
      expect(eval_and_collect_notices('notice(Mytest("Bob", 1984).age)', node)).to eql(["#{DateTime.now.year - 1984}"])
    end

    it 'can convert value to and from data' do
      expect(eval_and_collect_notices(<<-PUPPET.unindent, node)).to eql(['true', 'true', "#{DateTime.now.year - 1984}"])
        $m = Mytest("Bob", 1984)
        $d = $m.mytest::to_data
        notice($d =~ Data)
        $m2 = $d.mytest::from_data
        notice($m == $m2)
        notice($m2.age)
        PUPPET
    end

    it 'can access using implemented [] method' do
      expect(eval_and_collect_notices(<<-PUPPET.unindent, node)).to eql(['Bob', "#{DateTime.now.year - 1984}"])
        $m = Mytest("Bob", 1984)
        notice($m['name'])
        notice($m['age'])
      PUPPET
    end

    it 'can serialize and deserialize data' do
      expect(eval_and_collect_notices(<<-PUPPET.unindent, node)).to eql(['true', 'true', "#{DateTime.now.year - 1984}"])
        $m = Mytest("Bob", 1984)
        $d = $m.mytest::serialize
        notice($d =~ String)
        $m2 = $d.mytest::deserialize
        notice($m == $m2)
        notice($m2.age)
        PUPPET
    end
  end

  context 'when creating type with custom new_function' do
    let(:datatypes) {
      {
        'mytest.rb' => <<-RUBY.unindent,
          Puppet::DataTypes.create_type('Mytest') do
            interface <<-PUPPET
              attributes => {
                strings => { type => Array[String] },
                ints => { type => Array[Integer] },
              }
              PUPPET

          implementation_class PuppetSpec::DataTypes::MyTest
        end
      RUBY
      }
    }

    before(:each) do
      class ::PuppetSpec::DataTypes::MyTest
        def self.create_new_function(t)
          Puppet::Functions.create_function('new_%s' % t.name) do
            dispatch :create do
              repeated_param 'Variant[String,Integer]', :args
            end

            def create(*args)
              ::PuppetSpec::DataTypes::MyTest.new(*args.partition { |arg| arg.is_a?(String) })
            end
          end
        end
        attr_reader :strings, :ints

        def initialize(strings, ints)
          @strings = strings
          @ints = ints
        end
      end
    end

    after(:each) do
      ::PuppetSpec::DataTypes.send(:remove_const, :MyTest)
    end

    it 'loads and calls custom new function' do
      expect(eval_and_collect_notices('notice(Mytest("A", 32, "B", 20).ints)', node)).to eql(['[32, 20]'])
    end
  end

  context 'with data type and class defined in a module' do
    let(:mytest_classes) {
      {
        'position.rb' => <<-RUBY
            module PuppetX; module Mytest; class Position
              attr_reader :x, :y

              def initialize(x, y)
                @x = x
                @y = y
              end
            end; end; end
      RUBY
      }
    }

    after(:each) do
      ::PuppetX.send(:remove_const, :Mytest)
    end

    context 'in module namespace' do
      let(:mytest_datatypes) {
        {
          'mytest' => { 'position.rb' => <<-RUBY
            Puppet::DataTypes.create_type('Mytest::Position') do
              interface <<-PUPPET
                attributes => {
                  x => Integer,
                  y => Integer
                }
                PUPPET

              load_file('puppetx/mytest/position')

              implementation_class PuppetX::Mytest::Position
            end
            RUBY
          }
        }
      }

      it 'loads and returns value of attribute' do
        expect(eval_and_collect_notices('notice(Mytest::Position(23, 12).x)', node)).to eql(['23'])
      end
    end

    context 'in top namespace' do
      let(:mytest_datatypes) {
        {
          'position.rb' => <<-RUBY
            Puppet::DataTypes.create_type('Position') do
              interface <<-PUPPET
                attributes => {
                  x => Integer,
                  y => Integer
                }
                PUPPET

              load_file('puppetx/mytest/position')

              implementation_class PuppetX::Mytest::Position
            end
        RUBY
        }
      }

      it 'loads and returns value of attribute' do
        expect(eval_and_collect_notices('notice(Position(23, 12).x)', node)).to eql(['23'])
      end
    end
  end
end
end