Learning writing a custom type starting from the spec file

70 views
Skip to first unread message

Johan De Wit

unread,
Nov 21, 2013, 9:59:16 AM11/21/13
to puppe...@googlegroups.com
Hi,

Since i find it very hard to get started writing a custom type, just by
writing the spec file for the type first, then writing the code for the
type, to pass the unit spec,
I started a github where I will post my efforts to achieve this goal.

I will try to write to much comments in the code, so people with as less
as coding/ruby/rspec experience could possible follow the progress.

Any comments, corrections and best practice advice are very very welcome.

https://github.com/Open-Future-Belgium/puppet_rspec_demo

I hope this is a start of some useful documentation about rspec and
custom types/provider writing
(especially the rspec part)

happy learning.

Grts

johan

--
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 | https://www.open-future.be/puppet-advanced-training-12-till-14th-november
Zabbix Certified Training | http://www.open-future.be/zabbix-certified-training-18-till-20th-november
Zabbix Large Environments Training | http://www.open-future.be/zabbix-large-environments-training-21-till-22nd-november
Puppet Fundamentals Training | http://www.open-future.be/puppet-fundamentals-training-10-till-12th-december
Subscribe to our newsletter | http://eepurl.com/BUG8H

Andy Parker

unread,
Nov 21, 2013, 11:44:09 AM11/21/13
to puppe...@googlegroups.com
On Thu, Nov 21, 2013 at 6:59 AM, Johan De Wit <Jo...@open-future.be> wrote:
Hi,

Since i find it very hard to get started writing a custom type, just by writing the spec file for the type first, then writing the code for the type, to pass the unit spec,
I started a github where I will post my efforts to achieve this goal.

I will try to write to much comments in the code, so people with as less as coding/ruby/rspec experience could possible follow the progress.

Any comments, corrections and best practice advice are very very welcome.

https://github.com/Open-Future-Belgium/puppet_rspec_demo

I hope this is a start of some useful documentation about rspec and custom types/provider writing
(especially the rspec part)


This sounds great! Thanks for doing it. Are you thinking of writing any blog posts along the way or a summary of lessons learned when you are done?
 
happy learning.

Grts

johan

--
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 | https://www.open-future.be/puppet-advanced-training-12-till-14th-november
Zabbix Certified Training | http://www.open-future.be/zabbix-certified-training-18-till-20th-november
Zabbix Large Environments Training | http://www.open-future.be/zabbix-large-environments-training-21-till-22nd-november
Puppet Fundamentals Training | http://www.open-future.be/puppet-fundamentals-training-10-till-12th-december
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 puppet-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/528E1FC4.4000005%40open-future.be.
For more options, visit https://groups.google.com/groups/opt_out.



--
Andrew Parker
Freenode: zaphod42
Twitter: @aparker42
Software Developer

Join us at PuppetConf 2014September 23-24 in San Francisco

Johan De Wit

unread,
Nov 21, 2013, 12:52:50 PM11/21/13
to puppe...@googlegroups.com, Andy Parker
In the beginning, most of the things I learn, i will put in the code itself.  I started the git, so i can view/review the steps one has to, or could follow in writing puppet code.

The readme.md I will use to put some 'what did i do ....' descriptions per tag i make

And i think, the best place to put a more 'mature document/tutuorial' online should be the pupptlabs doc site.

For now on, i will be using the got for the first draft of this.

Grts
Johan
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.



--
Andrew Parker
Freenode: zaphod42
Twitter: @aparker42
Software Developer

Join us at PuppetConf 2014September 23-24 in San Francisco
--
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 puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/CANhgQXvdtD7ogN12qdm%3DXLU81t-t4b8%3D37bNd%3DhGV_no1E7QPQ%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.


-- 
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
_________________________________________________________
 

  

Upcoming Events:

Johan De Wit

unread,
Nov 21, 2013, 3:51:54 PM11/21/13
to puppe...@googlegroups.com, Andy Parker
Seems I once was very busy blogging, many years ago.  So i just dusted my antique wordpress and started a new blog, which i will use for my draft texts and to explain something.

http://linux.blogs.koewacht.net/

For more options, visit https://groups.google.com/groups/opt_out.

Johan De Wit

unread,
Dec 18, 2013, 5:12:20 PM12/18/13
to puppe...@googlegroups.com
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

John Bollinger

unread,
Dec 19, 2013, 2:19:14 PM12/19/13
to puppe...@googlegroups.com


On Wednesday, December 18, 2013 4:12:20 PM UTC-6, Johan De Wit wrote:
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 think it's good to validate resource properties when the validity criteria are known independent of the characteristics of the target node.

I think it's odd and a bit awkward to model saslsecprops as a single property instead of as separate properties for its several separate components.


John

Johan De Wit

unread,
Dec 20, 2013, 3:06:45 AM12/20/13
to puppe...@googlegroups.com
Thx John for your reply,
Doing it that way did not come into my mind.  

I was just mapping the slapd configuration to properties, as they are used in the configuration.

Jo







 





John

--
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 puppet-dev+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

-- 
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
_________________________________________________________
 

Upcoming Events:

Reply all
Reply to author
Forward
0 new messages