File: aix_object_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 (807 lines) | stat: -rw-r--r-- 24,573 bytes parent folder | download
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
require 'spec_helper'
require 'puppet/provider/aix_object'

describe 'Puppet::Provider::AixObject' do
  let(:resource) do
    Puppet::Type.type(:user).new(
      :name   => 'test_aix_user',
      :ensure => :present
    )
  end
  let(:klass) { Puppet::Provider::AixObject }
  let(:provider) do
    Puppet::Provider::AixObject.new(resource)
  end

  # Clear out the class-level + instance-level mappings
  def clear_attributes
    klass.instance_variable_set(:@mappings, nil)
  end

  before(:each) do
    clear_attributes
  end

  describe '.mapping' do
    let(:puppet_property) { :uid }
    let(:aix_attribute) { :id }
    let(:info) do
      {
        :puppet_property => puppet_property,
        :aix_attribute   => aix_attribute
      }
    end

    shared_examples 'a mapping' do |from, to|
      context "<#{from}> => <#{to}>" do
        let(:from_suffix) { from.to_s.split("_")[-1] }
        let(:to_suffix)   { to.to_s.split("_")[-1] }
        let(:conversion_fn) do
          "convert_#{from_suffix}_value".to_sym
        end

        it 'creates the mapping for a pure conversion function and defines it' do
          conversion_fn_lambda = "#{from_suffix}_to_#{to_suffix}".to_sym
          info[conversion_fn_lambda] = lambda { |x| x.to_s }
          provider.class.mapping(info)
    
          mappings = provider.class.mappings[to]
          expect(mappings).to include(info[from])

          mapping = mappings[info[from]]
          expect(mapping.public_methods).to include(conversion_fn)

          expect(mapping.send(conversion_fn, 3)).to eql('3')
        end

        it 'creates the mapping for an impure conversion function without defining it' do
          conversion_fn_lambda = "#{from_suffix}_to_#{to_suffix}".to_sym
          info[conversion_fn_lambda] = lambda { |provider, x| x.to_s }
          provider.class.mapping(info)
    
          mappings = provider.class.mappings[to]
          expect(mappings).to include(info[from])

          mapping = mappings[info[from]]
          expect(mapping.public_methods).not_to include(conversion_fn)
        end
  
        it 'uses the identity function as the conversion function if none is provided' do
          provider.class.mapping(info)

    
          mappings = provider.class.mappings[to]
          expect(mappings).to include(info[from])

          mapping = mappings[info[from]]
          expect(mapping.public_methods).to include(conversion_fn)

          expect(mapping.send(conversion_fn, 3)).to eql(3)
        end
      end
    end

    include_examples 'a mapping',
                     :puppet_property,
                     :aix_attribute

    include_examples 'a mapping',
                     :aix_attribute,
                     :puppet_property

    it 'sets the AIX attribute to the Puppet property if it is not provided' do
      info[:aix_attribute] = nil
      provider.class.mapping(info)

      mappings = provider.class.mappings[:puppet_property]
      expect(mappings).to include(info[:puppet_property])
    end
  end

  describe '.numeric_mapping' do
    let(:info) do
      info_hash = {
        :puppet_property => :uid,
        :aix_attribute   => :id
      }
      provider.class.numeric_mapping(info_hash)

      info_hash
    end
    let(:aix_attribute) do
      provider.class.mappings[:aix_attribute][info[:puppet_property]]
    end
    let(:puppet_property) do
      provider.class.mappings[:puppet_property][info[:aix_attribute]]
    end

    it 'raises an ArgumentError for a non-numeric Puppet property value' do
      value = 'foo'
      expect do
        aix_attribute.convert_property_value(value) 
      end.to raise_error do |error|
        expect(error).to be_a(ArgumentError)

        expect(error.message).to match(value)
        expect(error.message).to match(info[:puppet_property].to_s)
      end
    end

    it 'converts the numeric Puppet property to a numeric AIX attribute' do
      expect(aix_attribute.convert_property_value(10)).to eql('10')
    end

    it 'converts the numeric AIX attribute to a numeric Puppet property' do
      expect(puppet_property.convert_attribute_value('10')).to eql(10)
    end
  end

  describe '.mk_resource_methods' do
    before(:each) do
      # Add some Puppet properties
      provider.class.mapping(
        puppet_property: :foo,
        aix_attribute: :foo
      )
      provider.class.mapping(
        puppet_property: :bar,
        aix_attribute: :bar
      )

      provider.class.mk_resource_methods
    end

    it 'defines the property getters' do
      provider = Puppet::Provider::AixObject.new(resource)
      provider.instance_variable_set(:@object_info, { :foo => 'foo', :baz => 'baz' })

      (provider.class.mappings[:aix_attribute].keys + [:attributes]).each do |property|
        expect(provider).to receive(:get).with(property).and_return('value')

        expect(provider.send(property)).to eql('value')
      end
    end

    it 'defines the property setters' do
      provider = Puppet::Provider::AixObject.new(resource)

      value = '15'
      provider.class.mappings[:aix_attribute].keys.each do |property|
        expect(provider).to receive(:set).with(property, value)

        provider.send("#{property}=".to_sym, value)
      end
    end
  end

  describe '.parse_colon_separated_list' do
    it 'parses a single empty item' do
      input = ''
      output = ['']

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it 'parses a single nonempty item' do
      input = 'item'
      output = ['item']

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it "parses an escaped ':'" do
      input = '#!:'
      output = [':']

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it "parses a single item with an escaped ':'" do
      input = 'fd8c#!:215d#!:178#!:'
      output = ['fd8c:215d:178:']

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it "parses multiple items that do not have an escaped ':'" do
      input = "foo:bar baz:buu:1234"
      output = ["foo", "bar baz", "buu", "1234"]

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it "parses multiple items some of which have escaped ':'" do
      input = "1234#!:567:foo bar#!:baz:buu#!bob:sally:fd8c#!:215d#!:178"
      output = ["1234:567", "foo bar:baz", "buu#!bob", "sally", 'fd8c:215d:178']

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it "parses a list with several empty items" do
      input = "foo:::bar:baz:boo:"
      output = ["foo", "", "", "bar", "baz", "boo", ""]

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it "parses a list with an escaped ':' and empty item at the end" do
      input = "foo:bar#!::"
      output = ["foo", "bar:", ""]

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end

    it 'parses a real world example' do
      input = File.read(my_fixture('aix_colon_list_real_world_input.out')).chomp
      output = Object.instance_eval(File.read(my_fixture('aix_colon_list_real_world_output.out')))

      expect(provider.class.parse_colon_separated_list(input)).to eql(output)
    end
  end

  describe '.parse_aix_objects' do
    # parse_colon_separated_list is well tested, so we don't need to be
    # as strict on the formatting of the output here. Main point of these
    # tests is to capture the 'wholemeal' parsing that's going on, i.e.
    # that we can parse a bunch of objects together.
    let(:output) do
      <<-AIX_OBJECTS
#name:id:pgrp:groups
root:0:system:system,bin,sys,security,cron,audit,lp
#name:id:pgrp:groups:home:gecos
user:10000:staff:staff:/home/user3:Some User
AIX_OBJECTS
    end

    let(:expected_aix_attributes) do
      [
        {
          :name => 'root',
          :attributes => {
            :id     => '0',
            :pgrp   => 'system',
            :groups => 'system,bin,sys,security,cron,audit,lp',
          }
        },
        {
          :name => 'user',
          :attributes => {
            :id     => '10000',
            :pgrp   => 'staff',
            :groups => 'staff',
            :home   => '/home/user3',
            :gecos  => 'Some User'
          }
        }
      ]
    end

    it 'parses the AIX attributes from the command output' do
      expect(provider.class.parse_aix_objects(output)).to eql(expected_aix_attributes)
    end
  end

  describe 'list_all' do
    let(:output) do
      <<-OUTPUT
#name:id
system:0
#name:id
staff:1
#name:id
bin:2
      OUTPUT
    end

    it 'lists all of the objects' do
      lscmd = 'lsgroups'
      allow(provider.class).to receive(:command).with(:list).and_return(lscmd)
      allow(provider.class).to receive(:execute).with([lscmd, '-c', '-a', 'id', 'ALL']).and_return(output)

      expected_objects = [
        { :name => 'system', :id => '0' },
        { :name => 'staff', :id => '1' },
        { :name => 'bin', :id => '2' }
      ]
      expect(provider.class.list_all).to eql(expected_objects)
    end
  end

  describe '.instances' do
    let(:objects) do
      [
        { :name => 'group1', :id => '1' },
        { :name => 'group2', :id => '2' }
      ]
    end

    it 'returns all of the available instances' do
      allow(provider.class).to receive(:list_all).and_return(objects)

      expect(provider.class.instances.map(&:name)).to eql(['group1', 'group2'])
    end
  end

  describe '#mappings' do
    # Returns a pair [ instance_level_mapped_object, class_level_mapped_object ]
    def mapped_objects(type, input)
      [
        provider.mappings[type][input],
        provider.class.mappings[type][input]
      ]
    end

    before(:each) do
      # Create a pure mapping
      provider.class.numeric_mapping(
        puppet_property: :pure_puppet_property,
        aix_attribute: :pure_aix_attribute
      )

      # Create an impure mapping
      impure_conversion_fn = lambda do |provider, value|
        "Provider instance's name is #{provider.name}"
      end
      provider.class.mapping(
        puppet_property: :impure_puppet_property,
        aix_attribute: :impure_aix_attribute,
        property_to_attribute: impure_conversion_fn,
        attribute_to_property: impure_conversion_fn
      )
    end

    it 'memoizes the result' do
      provider.instance_variable_set(:@mappings, 'memoized')
      expect(provider.mappings).to eql('memoized')
    end

    it 'creates the instance-level mappings with the same structure as the class-level one' do
      expect(provider.mappings.keys).to eql(provider.class.mappings.keys)
      provider.mappings.keys.each do |type|
        expect(provider.mappings[type].keys).to eql(provider.class.mappings[type].keys)
      end
    end
    
    shared_examples 'uses the right mapped object for a given mapping' do |from_type, to_type|
      context "<#{from_type}> => <#{to_type}>" do
        it 'shares the class-level mapped object for pure mappings' do
          input = "pure_#{from_type}".to_sym

          instance_level_mapped_object, class_level_mapped_object = mapped_objects(to_type, input)
          expect(instance_level_mapped_object.object_id).to eql(class_level_mapped_object.object_id)
        end

        it 'dups the class-level mapped object for impure mappings' do
          input = "impure_#{from_type}".to_sym

          instance_level_mapped_object, class_level_mapped_object = mapped_objects(to_type, input)
          expect(instance_level_mapped_object.object_id).to_not eql(
            class_level_mapped_object.object_id
          )
        end

        it 'defines the conversion function for impure mappings' do
          from_type_suffix = from_type.to_s.split("_")[-1]
          conversion_fn = "convert_#{from_type_suffix}_value".to_sym

          input = "impure_#{from_type}".to_sym
          mapped_object, _ = mapped_objects(to_type, input)

          expect(mapped_object.public_methods).to include(conversion_fn)
          expect(mapped_object.send(conversion_fn, 3)).to match(provider.name)
        end
      end
    end

    include_examples 'uses the right mapped object for a given mapping',
                     :puppet_property,
                     :aix_attribute

    include_examples 'uses the right mapped object for a given mapping',
                     :aix_attribute,
                     :puppet_property
  end

  describe '#attributes_to_args' do
    let(:attributes) do
      {
        :attribute1 => 'value1',
        :attribute2 => 'value2'
      }
    end

    it 'converts the attributes hash to CLI arguments' do
      expect(provider.attributes_to_args(attributes)).to eql(
        ["attribute1=value1", "attribute2=value2"]
      )
    end
  end

  describe '#ia_module_args' do
    it 'returns no arguments if the ia_load_module parameter is not specified' do
      allow(provider.resource).to receive(:[]).with(:ia_load_module).and_return(nil)
      expect(provider.ia_module_args).to eql([])
    end

    it 'returns the ia_load_module as a CLI argument' do
      allow(provider.resource).to receive(:[]).with(:ia_load_module).and_return('module')
      expect(provider.ia_module_args).to eql(['-R', 'module'])
    end
  end

  describe '#lscmd' do
    it 'returns the lscmd' do
      allow(provider.class).to receive(:command).with(:list).and_return('list')
      allow(provider).to receive(:ia_module_args).and_return(['ia_module_args'])

      expect(provider.lscmd).to eql(
        ['list', '-c', 'ia_module_args', provider.resource.name]
      )
    end
  end

  describe '#addcmd' do
    let(:attributes) do
      {
        :attribute1 => 'value1',
        :attribute2 => 'value2'
      }
    end

    it 'returns the addcmd passing in the attributes as CLI arguments' do
      allow(provider.class).to receive(:command).with(:add).and_return('add')
      allow(provider).to receive(:ia_module_args).and_return(['ia_module_args'])

      expect(provider.addcmd(attributes)).to eql(
        ['add', 'ia_module_args', 'attribute1=value1', 'attribute2=value2', provider.resource.name]
      )
    end
  end

  describe '#deletecmd' do
    it 'returns the lscmd' do
      allow(provider.class).to receive(:command).with(:delete).and_return('delete')
      allow(provider).to receive(:ia_module_args).and_return(['ia_module_args'])

      expect(provider.deletecmd).to eql(
        ['delete', 'ia_module_args', provider.resource.name]
      )
    end
  end

  describe '#modifycmd' do
    let(:attributes) do
      {
        :attribute1 => 'value1',
        :attribute2 => 'value2'
      }
    end

    it 'returns the addcmd passing in the attributes as CLI arguments' do
      allow(provider.class).to receive(:command).with(:modify).and_return('modify')
      allow(provider).to receive(:ia_module_args).and_return(['ia_module_args'])

      expect(provider.modifycmd(attributes)).to eql(
        ['modify', 'ia_module_args', 'attribute1=value1', 'attribute2=value2', provider.resource.name]
      )
    end
  end

  describe '#modify_object' do
    let(:new_attributes) do
      {
        :nofiles => 10000,
        :fsize   => 30000
      }
    end

    it 'modifies the AIX object with the new attributes' do
      allow(provider).to receive(:modifycmd).with(new_attributes).and_return('modify_cmd')
      expect(provider).to receive(:execute).with('modify_cmd')
      expect(provider).to receive(:object_info).with(true)

      provider.modify_object(new_attributes)
    end
  end

  describe '#get' do
    # Input
    let(:property) { :uid }
    
    let!(:object_info) do
      hash = {}

      provider.instance_variable_set(:@object_info, hash)
      hash
    end

    it 'returns :absent if the AIX object does not exist' do
      allow(provider).to receive(:exists?).and_return(false)
      object_info[property] = 15
      
      expect(provider.get(property)).to eql(:absent)
    end

    it 'returns :absent if the property is not present on the system' do
      allow(provider).to receive(:exists?).and_return(true)

      expect(provider.get(property)).to eql(:absent)
    end

    it "returns the property's value" do
      allow(provider).to receive(:exists?).and_return(true)
      object_info[property] = 15

      expect(provider.get(property)).to eql(15)
    end
  end

  describe '#set' do
    # Input
    let(:property) { :uid }
    let(:value) { 10 }

    # AIX attribute params
    let(:aix_attribute) { :id }
    let(:property_to_attribute) do
      lambda { |x| x.to_s }
    end

    before(:each) do
      # Add an attribute
      provider.class.mapping(
        puppet_property: property,
        aix_attribute: aix_attribute,
        property_to_attribute: property_to_attribute
      )
    end

    it "raises a Puppet::Error if it fails to set the property's value" do
      allow(provider).to receive(:modify_object)
        .with({ :id => value.to_s })
        .and_raise(Puppet::ExecutionFailure, 'failed to modify the AIX object!')

      expect { provider.set(property, value) }.to raise_error do |error|
        expect(error).to be_a(Puppet::Error)
      end
    end

    it "sets the given property's value to the passed-in value" do
      expect(provider).to receive(:modify_object).with({ :id => value.to_s })

      provider.set(property, value)
    end
  end

  describe '#validate_new_attributes' do
    let(:new_attributes) do
      {
        :nofiles => 10000,
        :fsize   => 100000
      }
    end

    it 'raises a Puppet::Error if a specified attributes corresponds to a Puppet property, reporting all of the attribute-property conflicts' do
      provider.class.mapping(puppet_property: :uid, aix_attribute: :id)
      provider.class.mapping(puppet_property: :groups, aix_attribute: :groups)

      new_attributes[:id] = '25'
      new_attributes[:groups] = 'groups'

      expect { provider.validate_new_attributes(new_attributes) }.to raise_error do |error|
        expect(error).to be_a(Puppet::Error)

        expect(error.message).to match("'uid', 'groups'")
        expect(error.message).to match("'id', 'groups'")
      end
    end
  end

  describe '#attributes=' do
    let(:new_attributes) do
      {
        :nofiles => 10000,
        :fsize   => 100000
      }
    end

    it 'raises a Puppet::Error if one of the specified attributes corresponds to a Puppet property' do
      provider.class.mapping(puppet_property: :uid, aix_attribute: :id)
      new_attributes[:id] = '25'

      expect { provider.attributes = new_attributes }.to raise_error do |error|
        expect(error).to be_a(Puppet::Error)

        expect(error.message).to match('uid')
        expect(error.message).to match('id')
      end
    end

    it 'raises a Puppet::Error if it fails to set the new AIX attributes' do
      allow(provider).to receive(:modify_object)
        .with(new_attributes)
        .and_raise(Puppet::ExecutionFailure, 'failed to modify the AIX object!')
      
      expect { provider.attributes = new_attributes }.to raise_error do |error|
        expect(error).to be_a(Puppet::Error)

        expect(error.message).to match('failed to modify the AIX object!')
      end
    end

    it 'sets the new AIX attributes' do
      expect(provider).to receive(:modify_object).with(new_attributes)

      provider.attributes = new_attributes
    end
  end

  describe '#object_info' do
    before(:each) do
      # Add some Puppet properties
      provider.class.mapping(
        puppet_property: :uid,
        aix_attribute: :id,
        attribute_to_property: lambda { |x| x.to_i },
      )
      provider.class.mapping(
        puppet_property: :groups,
        aix_attribute: :groups
      )

      # Mock out our lscmd
      allow(provider).to receive(:lscmd).and_return("lsuser #{resource[:name]}")
    end

    it 'memoizes the result' do
      provider.instance_variable_set(:@object_info, {})
      expect(provider.object_info).to eql({})
    end

    it 'returns nil if the AIX object does not exist' do
      allow(provider).to receive(:execute).with(provider.lscmd).and_raise(
        Puppet::ExecutionFailure, 'lscmd failed!'
      )

      expect(provider.object_info).to be_nil
    end

    it 'collects the Puppet properties' do
      output = 'mock_output'
      allow(provider).to receive(:execute).with(provider.lscmd).and_return(output)

      # Mock the AIX attributes on the system
      mock_attributes = {
        :id         => '1',
        :groups     => 'foo,bar,baz',
        :attribute1 => 'value1',
        :attribute2 => 'value2'
      }
      allow(provider.class).to receive(:parse_aix_objects)
        .with(output)
        .and_return([{ :name => resource.name, :attributes => mock_attributes }])

      expected_property_values = {
        :uid        => 1,
        :groups     => 'foo,bar,baz',
        :attributes => {
          :attribute1 => 'value1',
          :attribute2 => 'value2'
        }
      }
      provider.object_info
      expect(provider.instance_variable_get(:@object_info)).to eql(expected_property_values)
    end
  end

  describe '#exists?' do
    it 'should return true if the AIX object exists' do
      allow(provider).to receive(:object_info).and_return({})
      expect(provider.exists?).to be(true)
    end

    it 'should return false if the AIX object does not exist' do
      allow(provider).to receive(:object_info).and_return(nil)
      expect(provider.exists?).to be(false)
    end
  end

  describe "#create" do
    let(:property_attributes) do
      {}
    end

    def stub_attributes_property(attributes)
      allow(provider.resource).to receive(:should).with(:attributes).and_return(attributes)
    end

    def set_property(puppet_property, aix_attribute, property_to_attribute, should_value = nil)
      property_to_attribute ||= lambda { |x| x }

      provider.class.mapping(
        puppet_property: puppet_property,
        aix_attribute: aix_attribute,
        property_to_attribute: property_to_attribute
      )
      allow(provider.resource).to receive(:should).with(puppet_property).and_return(should_value)

      if should_value
        property_attributes[aix_attribute] = property_to_attribute.call(should_value)
      end
    end

    before(:each) do
      clear_attributes
      
      # Clear out the :attributes property. We will be setting this later.
      stub_attributes_property(nil)

      # Add some properties
      set_property(:uid, :id, lambda { |x| x.to_s }, 10)
      set_property(:groups, :groups, nil, 'group1,group2,group3')
      set_property(:shell, :shell, nil)
    end

    it 'raises a Puppet::Error if one of the specified attributes corresponds to a Puppet property' do
      stub_attributes_property({ :id => 15 })
      provider.class.mapping(puppet_property: :uid, aix_attribute: :id)

      expect { provider.create }.to raise_error do |error|
        expect(error).to be_a(Puppet::Error)

        expect(error.message).to match('uid')
        expect(error.message).to match('id')
      end
    end

    it "raises a Puppet::Error if it fails to create the AIX object" do
      allow(provider).to receive(:addcmd)
      allow(provider).to receive(:execute).and_raise(
        Puppet::ExecutionFailure, "addcmd failed!"
      )

      expect { provider.create }.to raise_error do |error|
        expect(error).to be_a(Puppet::Error)

        expect(error.message).to match("not create")
      end
    end

    it "creates the AIX object with the given AIX attributes + Puppet properties" do
      attributes = { :fsize => 1000 }
      stub_attributes_property(attributes)

      expect(provider).to receive(:addcmd)
        .with(attributes.merge(property_attributes))
        .and_return('addcmd')
      expect(provider).to receive(:execute).with('addcmd')

      provider.create
    end
  end
 
  describe "#delete" do
    before(:each) do
      allow(provider).to receive(:deletecmd).and_return('deletecmd')
    end

    it "raises a Puppet::Error if it fails to delete the AIX object" do
      allow(provider).to receive(:execute).with(provider.deletecmd).and_raise(
        Puppet::ExecutionFailure, "deletecmd failed!"
      )

      expect { provider.delete }.to raise_error do |error|
        expect(error).to be_a(Puppet::Error)

        expect(error.message).to match("not delete")
      end
    end

    it "deletes the AIX object" do
      expect(provider).to receive(:execute).with(provider.deletecmd)
      expect(provider).to receive(:object_info).with(true)

      provider.delete
    end
  end
end