Learning to write custom types, I thought to validate the following property

Because we have thre possible values looking like minssf=<some value>, the newvalues cannot be used (i think)

And I wanted to add some specific validation for those types of values.

Is this a good thinking path ? Or is that a No Go thing ?

I started with the rspec, and wrote the code to make it pass the tests.

Details man slapd-config, complete code https://github.com/Open-Future-Belgium/puppet_rspec_demo


Grts and many thanks

Johan

Code snip Rspec code

    describe "the :saslsecprops property" do
# flags : none, noanonymous, noplain, noactive, nodict,forwardsec, passcred , minssf=<factor>, maxssf=<factor>, maxbufsize=<size>
      # factor : 0, 1, 56, 112, 128, MAX_INT
      # size : 0<>65536
      it "should return the default value if no value is given" do
# this check faild also, beacuse by default, only the first value is passed
        # and this is a multivalue property
described_class.new(:name => 'config0')[:saslsecprops].should == ['noanonymous','noplain']
      end
      it "should generate an error if unsupported flag is given" do
expect { described_class.new(:name => 'config0', :saslsecprops => 'faulty') }.to raise_error
      end
      it "should return the list of parameters if all are supported" do
described_class.new(:name => 'config0', :saslsecprops => ['noanonymous','noplain','passcred'])[:saslsecprops].should == ['noanonymous','noplain','passcred']
      end
it "should generate an error if only one flag is supported of the list" do expect { described_class.new(:name => 'config0', :saslsecprops => ['noanonymous','noplain','passcred','faulty']) }.to raise_error
      end
      describe "should validate flag minssf if given" do
        it "should generate an error if factor is not supported" do
expect { described_class.new(:name => 'config0', :saslsecprops => 'minssf=9999') }.to raise_error
        end
        it "should have a valid factor" do
described_class.new(:name => 'config0', :saslsecprops => 'minssf=112')[:saslsecprops].should == ['minssf=112']
        end
      end
      describe "should validate flag maxssf if given" do
        it "should generate an error if factor is not supported" do
expect { described_class.new(:name => 'config0', :saslsecprops => 'maxssf=9999') }.to raise_error
        end
        it "should have a valid factor" do
described_class.new(:name => 'config0', :saslsecprops => 'maxssf=112')[:saslsecprops].should == ['maxssf=112']
        end
      end
      describe "should validate flag maxbufsize" do
        it "should generate an error if subvalue is a string" do
expect { described_class.new(:name => 'config0', :saslsecprops => 'maxbuf=fault') }.to raise_error
        end
        it "should generate an error if not valid value" do
expect { described_class.new(:name => 'config0', :saslsecprops => 'maxbuf=99999') }.to raise_error
        end
        it "should have a maxbufzise" do
described_class.new(:name => 'config0', :saslsecprops => 'maxbufsize=1024')[:saslsecprops].should == ['maxbufsize=1024']
        end
      end
    end

Code snip type code

# we need to allow multiple values to be set, array_matching defaults to first
  newproperty(:saslsecprops, :array_matching => :all) do
    desc "The SASL secprops to apply. Defaults to \'noanonymous,noplain\'."
    defaultto ['noanonymous', 'noplain']
# we cannot use newvalues, because we have specific validations depending on the values used
    # order should not be  important
    validate do | value |
      # we fail on the first fail
      case value
when 'none', 'noanonymous', 'noplain', 'noactive', 'nodict', 'forwardsec', 'passcred'
        # value is accepted
      when /^minssf=/,/^maxssf=/
        case value.split('=',2)[1]
        when "0", "1", "56", "112", "128"
          # we have to validate the factor
        else
raise ArgumentError, "property saslsecprops : #{value}= should have a value of [0|1|56|112|128]"
        end
      when /^maxbufsize=/
# The Integer() function raises an error if it has a 'non' decimal string
        totest = Integer(value.split('=',2)[1])
        if totest >= 0 || totest <= 65536
          # passed
        else
raise ArgumentError, "property saslsecprops : #{value}= must be between 0 and 65536. See man slapd-config"
        end
      else
raise ArgumentError, "property saslsecprops : #{value} not allowed. See man slapd-config"
      end
    end
  end

And the rspec run results.
    the :saslsecprops property
      should return the default value if no value is given
      should generate an error if unsupported flag is given
      should return the list of parameters if all are supported
      should generate an error if only one flag is supported of the list
      should validate flag minssf if given
        should generate an error if factor is not supported
        should have a valid factor
      should validate flag maxssf if given
        should generate an error if factor is not supported
        should have a valid factor
      should validate flag maxbufsize
        should generate an error if subvalue is a string
        should generate an error if not valid value
        should have a maxbufzise

--
Johan De Wit

Open Source Consultant

Red Hat Certified Engineer         (805008667232363)
Puppet Certified Professional 2013 (PCP0000006)
_________________________________________________________
Open-Future Phone +32 (0)2/255 70 70
Zavelstraat 72              Fax       +32 (0)2/255 70 71
3071 KORTENBERG             Mobile    +32 (0)474/42 40 73
BELGIUM                     http://www.open-future.be
_________________________________________________________
Next Events:
Puppet Advanced Training | 
http://www.open-future.be/puppet-advanced-training-7-till-9th-january
Puppet Fundamentals Training | 
http://www.open-future.be/puppet-fundamentals-training-4-till-6th-february
Subscribe to our newsletter | http://eepurl.com/BUG8H

--
You received this message because you are subscribed to the Google Groups "Puppet 
Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-dev/52B21DC4.3050308%40open-future.be.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to