puppetmodule-stdlib 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. data/CHANGELOG +253 -0
  2. data/CONTRIBUTING.md +65 -0
  3. data/Gemfile +33 -0
  4. data/LICENSE +19 -0
  5. data/Modulefile +11 -0
  6. data/README.markdown +1173 -0
  7. data/README_DEVELOPER.markdown +35 -0
  8. data/RELEASE_PROCESS.markdown +24 -0
  9. data/Rakefile +2 -0
  10. data/lib/facter/pe_version.rb +53 -0
  11. data/lib/facter/puppet_vardir.rb +26 -0
  12. data/lib/facter/root_home.rb +19 -0
  13. data/lib/facter/util/puppet_settings.rb +21 -0
  14. data/lib/puppet/parser/functions/abs.rb +36 -0
  15. data/lib/puppet/parser/functions/any2array.rb +33 -0
  16. data/lib/puppet/parser/functions/bool2num.rb +49 -0
  17. data/lib/puppet/parser/functions/capitalize.rb +34 -0
  18. data/lib/puppet/parser/functions/chomp.rb +35 -0
  19. data/lib/puppet/parser/functions/chop.rb +37 -0
  20. data/lib/puppet/parser/functions/concat.rb +37 -0
  21. data/lib/puppet/parser/functions/count.rb +22 -0
  22. data/lib/puppet/parser/functions/defined_with_params.rb +35 -0
  23. data/lib/puppet/parser/functions/delete.rb +46 -0
  24. data/lib/puppet/parser/functions/delete_at.rb +49 -0
  25. data/lib/puppet/parser/functions/downcase.rb +33 -0
  26. data/lib/puppet/parser/functions/empty.rb +28 -0
  27. data/lib/puppet/parser/functions/ensure_packages.rb +24 -0
  28. data/lib/puppet/parser/functions/ensure_resource.rb +35 -0
  29. data/lib/puppet/parser/functions/flatten.rb +33 -0
  30. data/lib/puppet/parser/functions/floor.rb +20 -0
  31. data/lib/puppet/parser/functions/fqdn_rotate.rb +46 -0
  32. data/lib/puppet/parser/functions/get_module_path.rb +17 -0
  33. data/lib/puppet/parser/functions/getparam.rb +35 -0
  34. data/lib/puppet/parser/functions/getvar.rb +26 -0
  35. data/lib/puppet/parser/functions/grep.rb +33 -0
  36. data/lib/puppet/parser/functions/has_interface_with.rb +52 -0
  37. data/lib/puppet/parser/functions/has_ip_address.rb +25 -0
  38. data/lib/puppet/parser/functions/has_ip_network.rb +25 -0
  39. data/lib/puppet/parser/functions/has_key.rb +28 -0
  40. data/lib/puppet/parser/functions/hash.rb +41 -0
  41. data/lib/puppet/parser/functions/is_array.rb +22 -0
  42. data/lib/puppet/parser/functions/is_domain_name.rb +47 -0
  43. data/lib/puppet/parser/functions/is_float.rb +27 -0
  44. data/lib/puppet/parser/functions/is_function_available.rb +23 -0
  45. data/lib/puppet/parser/functions/is_hash.rb +22 -0
  46. data/lib/puppet/parser/functions/is_integer.rb +27 -0
  47. data/lib/puppet/parser/functions/is_ip_address.rb +32 -0
  48. data/lib/puppet/parser/functions/is_mac_address.rb +27 -0
  49. data/lib/puppet/parser/functions/is_numeric.rb +27 -0
  50. data/lib/puppet/parser/functions/is_string.rb +26 -0
  51. data/lib/puppet/parser/functions/join.rb +41 -0
  52. data/lib/puppet/parser/functions/join_keys_to_values.rb +47 -0
  53. data/lib/puppet/parser/functions/keys.rb +26 -0
  54. data/lib/puppet/parser/functions/loadyaml.rb +20 -0
  55. data/lib/puppet/parser/functions/lstrip.rb +33 -0
  56. data/lib/puppet/parser/functions/max.rb +21 -0
  57. data/lib/puppet/parser/functions/member.rb +44 -0
  58. data/lib/puppet/parser/functions/merge.rb +33 -0
  59. data/lib/puppet/parser/functions/min.rb +21 -0
  60. data/lib/puppet/parser/functions/num2bool.rb +43 -0
  61. data/lib/puppet/parser/functions/parsejson.rb +24 -0
  62. data/lib/puppet/parser/functions/parseyaml.rb +24 -0
  63. data/lib/puppet/parser/functions/pick.rb +29 -0
  64. data/lib/puppet/parser/functions/prefix.rb +45 -0
  65. data/lib/puppet/parser/functions/range.rb +80 -0
  66. data/lib/puppet/parser/functions/reject.rb +31 -0
  67. data/lib/puppet/parser/functions/reverse.rb +28 -0
  68. data/lib/puppet/parser/functions/rstrip.rb +32 -0
  69. data/lib/puppet/parser/functions/shuffle.rb +46 -0
  70. data/lib/puppet/parser/functions/size.rb +48 -0
  71. data/lib/puppet/parser/functions/sort.rb +27 -0
  72. data/lib/puppet/parser/functions/squeeze.rb +36 -0
  73. data/lib/puppet/parser/functions/str2bool.rb +46 -0
  74. data/lib/puppet/parser/functions/str2saltedsha512.rb +32 -0
  75. data/lib/puppet/parser/functions/strftime.rb +107 -0
  76. data/lib/puppet/parser/functions/strip.rb +39 -0
  77. data/lib/puppet/parser/functions/suffix.rb +45 -0
  78. data/lib/puppet/parser/functions/swapcase.rb +39 -0
  79. data/lib/puppet/parser/functions/time.rb +49 -0
  80. data/lib/puppet/parser/functions/to_bytes.rb +28 -0
  81. data/lib/puppet/parser/functions/type.rb +50 -0
  82. data/lib/puppet/parser/functions/unique.rb +51 -0
  83. data/lib/puppet/parser/functions/upcase.rb +41 -0
  84. data/lib/puppet/parser/functions/uriescape.rb +36 -0
  85. data/lib/puppet/parser/functions/validate_absolute_path.rb +56 -0
  86. data/lib/puppet/parser/functions/validate_array.rb +33 -0
  87. data/lib/puppet/parser/functions/validate_augeas.rb +81 -0
  88. data/lib/puppet/parser/functions/validate_bool.rb +34 -0
  89. data/lib/puppet/parser/functions/validate_cmd.rb +47 -0
  90. data/lib/puppet/parser/functions/validate_hash.rb +33 -0
  91. data/lib/puppet/parser/functions/validate_re.rb +40 -0
  92. data/lib/puppet/parser/functions/validate_slength.rb +52 -0
  93. data/lib/puppet/parser/functions/validate_string.rb +33 -0
  94. data/lib/puppet/parser/functions/values.rb +39 -0
  95. data/lib/puppet/parser/functions/values_at.rb +98 -0
  96. data/lib/puppet/parser/functions/zip.rb +65 -0
  97. data/lib/puppet/provider/file_line/ruby.rb +59 -0
  98. data/lib/puppet/type/anchor.rb +41 -0
  99. data/lib/puppet/type/file_line.rb +70 -0
  100. data/manifests/init.pp +20 -0
  101. data/manifests/stages.pp +43 -0
  102. data/spec/functions/defined_with_params_spec.rb +37 -0
  103. data/spec/functions/ensure_packages_spec.rb +42 -0
  104. data/spec/functions/ensure_resource_spec.rb +40 -0
  105. data/spec/functions/getparam_spec.rb +34 -0
  106. data/spec/monkey_patches/alias_should_to_must.rb +8 -0
  107. data/spec/monkey_patches/publicize_methods.rb +10 -0
  108. data/spec/spec.opts +6 -0
  109. data/spec/spec_helper.rb +28 -0
  110. data/spec/unit/facter/pe_version_spec.rb +76 -0
  111. data/spec/unit/facter/root_home_spec.rb +40 -0
  112. data/spec/unit/facter/util/puppet_settings_spec.rb +35 -0
  113. data/spec/unit/puppet/parser/functions/abs_spec.rb +25 -0
  114. data/spec/unit/puppet/parser/functions/any2array_spec.rb +55 -0
  115. data/spec/unit/puppet/parser/functions/bool2num_spec.rb +24 -0
  116. data/spec/unit/puppet/parser/functions/capitalize_spec.rb +19 -0
  117. data/spec/unit/puppet/parser/functions/chomp_spec.rb +19 -0
  118. data/spec/unit/puppet/parser/functions/chop_spec.rb +19 -0
  119. data/spec/unit/puppet/parser/functions/concat_spec.rb +15 -0
  120. data/spec/unit/puppet/parser/functions/count_spec.rb +31 -0
  121. data/spec/unit/puppet/parser/functions/delete_at_spec.rb +19 -0
  122. data/spec/unit/puppet/parser/functions/delete_spec.rb +38 -0
  123. data/spec/unit/puppet/parser/functions/downcase_spec.rb +24 -0
  124. data/spec/unit/puppet/parser/functions/empty_spec.rb +23 -0
  125. data/spec/unit/puppet/parser/functions/flatten_spec.rb +27 -0
  126. data/spec/unit/puppet/parser/functions/floor_spec.rb +39 -0
  127. data/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb +33 -0
  128. data/spec/unit/puppet/parser/functions/get_module_path_spec.rb +46 -0
  129. data/spec/unit/puppet/parser/functions/getvar_spec.rb +37 -0
  130. data/spec/unit/puppet/parser/functions/grep_spec.rb +19 -0
  131. data/spec/unit/puppet/parser/functions/has_interface_with_spec.rb +64 -0
  132. data/spec/unit/puppet/parser/functions/has_ip_address_spec.rb +39 -0
  133. data/spec/unit/puppet/parser/functions/has_ip_network_spec.rb +36 -0
  134. data/spec/unit/puppet/parser/functions/has_key_spec.rb +42 -0
  135. data/spec/unit/puppet/parser/functions/hash_spec.rb +19 -0
  136. data/spec/unit/puppet/parser/functions/is_array_spec.rb +29 -0
  137. data/spec/unit/puppet/parser/functions/is_domain_name_spec.rb +64 -0
  138. data/spec/unit/puppet/parser/functions/is_float_spec.rb +33 -0
  139. data/spec/unit/puppet/parser/functions/is_function_available.rb +31 -0
  140. data/spec/unit/puppet/parser/functions/is_hash_spec.rb +29 -0
  141. data/spec/unit/puppet/parser/functions/is_integer_spec.rb +34 -0
  142. data/spec/unit/puppet/parser/functions/is_ip_address_spec.rb +39 -0
  143. data/spec/unit/puppet/parser/functions/is_mac_address_spec.rb +29 -0
  144. data/spec/unit/puppet/parser/functions/is_numeric_spec.rb +39 -0
  145. data/spec/unit/puppet/parser/functions/is_string_spec.rb +34 -0
  146. data/spec/unit/puppet/parser/functions/join_keys_to_values_spec.rb +40 -0
  147. data/spec/unit/puppet/parser/functions/join_spec.rb +19 -0
  148. data/spec/unit/puppet/parser/functions/keys_spec.rb +21 -0
  149. data/spec/unit/puppet/parser/functions/lstrip_spec.rb +19 -0
  150. data/spec/unit/puppet/parser/functions/max_spec.rb +27 -0
  151. data/spec/unit/puppet/parser/functions/member_spec.rb +24 -0
  152. data/spec/unit/puppet/parser/functions/merge_spec.rb +47 -0
  153. data/spec/unit/puppet/parser/functions/min_spec.rb +27 -0
  154. data/spec/unit/puppet/parser/functions/num2bool_spec.rb +67 -0
  155. data/spec/unit/puppet/parser/functions/parsejson_spec.rb +22 -0
  156. data/spec/unit/puppet/parser/functions/parseyaml_spec.rb +24 -0
  157. data/spec/unit/puppet/parser/functions/pick_spec.rb +34 -0
  158. data/spec/unit/puppet/parser/functions/prefix_spec.rb +19 -0
  159. data/spec/unit/puppet/parser/functions/range_spec.rb +34 -0
  160. data/spec/unit/puppet/parser/functions/reject_spec.rb +20 -0
  161. data/spec/unit/puppet/parser/functions/reverse_spec.rb +19 -0
  162. data/spec/unit/puppet/parser/functions/rstrip_spec.rb +24 -0
  163. data/spec/unit/puppet/parser/functions/shuffle_spec.rb +24 -0
  164. data/spec/unit/puppet/parser/functions/size_spec.rb +24 -0
  165. data/spec/unit/puppet/parser/functions/sort_spec.rb +24 -0
  166. data/spec/unit/puppet/parser/functions/squeeze_spec.rb +24 -0
  167. data/spec/unit/puppet/parser/functions/str2bool_spec.rb +31 -0
  168. data/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb +45 -0
  169. data/spec/unit/puppet/parser/functions/strftime_spec.rb +29 -0
  170. data/spec/unit/puppet/parser/functions/strip_spec.rb +18 -0
  171. data/spec/unit/puppet/parser/functions/suffix_spec.rb +19 -0
  172. data/spec/unit/puppet/parser/functions/swapcase_spec.rb +19 -0
  173. data/spec/unit/puppet/parser/functions/time_spec.rb +29 -0
  174. data/spec/unit/puppet/parser/functions/to_bytes_spec.rb +58 -0
  175. data/spec/unit/puppet/parser/functions/type_spec.rb +43 -0
  176. data/spec/unit/puppet/parser/functions/unique_spec.rb +24 -0
  177. data/spec/unit/puppet/parser/functions/upcase_spec.rb +24 -0
  178. data/spec/unit/puppet/parser/functions/uriescape_spec.rb +24 -0
  179. data/spec/unit/puppet/parser/functions/validate_absolute_path_spec.rb +83 -0
  180. data/spec/unit/puppet/parser/functions/validate_array_spec.rb +38 -0
  181. data/spec/unit/puppet/parser/functions/validate_augeas_spec.rb +102 -0
  182. data/spec/unit/puppet/parser/functions/validate_bool_spec.rb +51 -0
  183. data/spec/unit/puppet/parser/functions/validate_cmd_spec.rb +81 -0
  184. data/spec/unit/puppet/parser/functions/validate_hash_spec.rb +43 -0
  185. data/spec/unit/puppet/parser/functions/validate_re_spec.rb +76 -0
  186. data/spec/unit/puppet/parser/functions/validate_slength_spec.rb +48 -0
  187. data/spec/unit/puppet/parser/functions/validate_string_spec.rb +60 -0
  188. data/spec/unit/puppet/parser/functions/values_at_spec.rb +38 -0
  189. data/spec/unit/puppet/parser/functions/values_spec.rb +31 -0
  190. data/spec/unit/puppet/parser/functions/zip_spec.rb +15 -0
  191. data/spec/unit/puppet/provider/file_line/ruby_spec.rb +127 -0
  192. data/spec/unit/puppet/type/anchor_spec.rb +11 -0
  193. data/spec/unit/puppet/type/file_line_spec.rb +69 -0
  194. data/spec/watchr.rb +86 -0
  195. data/tests/file_line.pp +9 -0
  196. data/tests/has_interface_with.pp +10 -0
  197. data/tests/has_ip_address.pp +3 -0
  198. data/tests/has_ip_network.pp +4 -0
  199. data/tests/init.pp +1 -0
  200. metadata +252 -0
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Puppet::Parser::Functions.function(:validate_cmd) do
4
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
5
+
6
+ # The subject of these examplres is the method itself.
7
+ subject do
8
+ # This makes sure the function is loaded within each test
9
+ function_name = Puppet::Parser::Functions.function(:validate_cmd)
10
+ scope.method(function_name)
11
+ end
12
+
13
+ context 'Using Puppet::Parser::Scope.new' do
14
+
15
+ describe 'Garbage inputs' do
16
+ inputs = [
17
+ [ nil ],
18
+ [ [ nil ] ],
19
+ [ { 'foo' => 'bar' } ],
20
+ [ { } ],
21
+ [ '' ],
22
+ [ "one", "one", "MSG to User", "4th arg" ],
23
+ ]
24
+
25
+ inputs.each do |input|
26
+ it "validate_cmd(#{input.inspect}) should fail" do
27
+ expect { subject.call [input] }.to raise_error Puppet::ParseError
28
+ end
29
+ end
30
+ end
31
+
32
+ describe 'Valid inputs' do
33
+ inputs = [
34
+ [ '/full/path/to/something', '/bin/echo' ],
35
+ [ '/full/path/to/something', '/bin/cat' ],
36
+ ]
37
+
38
+ inputs.each do |input|
39
+ it "validate_cmd(#{input.inspect}) should not fail" do
40
+ expect { subject.call input }.not_to raise_error
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "Valid inputs which should raise an exception without a message" do
46
+ # The intent here is to make sure valid inputs raise exceptions when they
47
+ # don't specify an error message to display. This is the behvior in
48
+ # 2.2.x and prior.
49
+ inputs = [
50
+ [ "hello", "/bin/false" ],
51
+ ]
52
+
53
+ inputs.each do |input|
54
+ it "validate_cmd(#{input.inspect}) should fail" do
55
+ expect { subject.call input }.to raise_error /validate_cmd.*?failed to validate content with command/
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "Nicer Error Messages" do
61
+ # The intent here is to make sure the function returns the 3rd argument
62
+ # in the exception thrown
63
+ inputs = [
64
+ [ "hello", [ "bye", "later", "adios" ], "MSG to User" ],
65
+ [ "greetings", "salutations", "Error, greetings does not match salutations" ],
66
+ ]
67
+
68
+ inputs.each do |input|
69
+ it "validate_cmd(#{input.inspect}) should fail" do
70
+ expect { subject.call input }.to raise_error /#{input[2]}/
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "Test output message" do
76
+ it "validate_cmd('whatever', 'kthnksbye') should fail" do
77
+ expect { subject.call ['whatever', 'kthnksbye'] }.to raise_error /kthnksbye.* returned 1/
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,43 @@
1
+ #! /usr/bin/env ruby -S rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Puppet::Parser::Functions.function(:validate_hash) do
6
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7
+
8
+ describe 'when calling validate_hash from puppet' do
9
+
10
+ %w{ true false }.each do |the_string|
11
+
12
+ it "should not compile when #{the_string} is a string" do
13
+ Puppet[:code] = "validate_hash('#{the_string}')"
14
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
15
+ end
16
+
17
+ it "should not compile when #{the_string} is a bare word" do
18
+ Puppet[:code] = "validate_hash(#{the_string})"
19
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
20
+ end
21
+
22
+ end
23
+
24
+ it "should compile when multiple hash arguments are passed" do
25
+ Puppet[:code] = <<-'ENDofPUPPETcode'
26
+ $foo = {}
27
+ $bar = { 'one' => 'two' }
28
+ validate_hash($foo, $bar)
29
+ ENDofPUPPETcode
30
+ scope.compiler.compile
31
+ end
32
+
33
+ it "should not compile when an undef variable is passed" do
34
+ Puppet[:code] = <<-'ENDofPUPPETcode'
35
+ $foo = undef
36
+ validate_hash($foo)
37
+ ENDofPUPPETcode
38
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe Puppet::Parser::Functions.function(:validate_re) do
4
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
5
+
6
+ # The subject of these examplres is the method itself.
7
+ subject do
8
+ # This makes sure the function is loaded within each test
9
+ function_name = Puppet::Parser::Functions.function(:validate_re)
10
+ scope.method(function_name)
11
+ end
12
+
13
+ context 'Using Puppet::Parser::Scope.new' do
14
+
15
+ describe 'Garbage inputs' do
16
+ inputs = [
17
+ [ nil ],
18
+ [ [ nil ] ],
19
+ [ { 'foo' => 'bar' } ],
20
+ [ { } ],
21
+ [ '' ],
22
+ [ "one", "one", "MSG to User", "4th arg" ],
23
+ ]
24
+
25
+ inputs.each do |input|
26
+ it "validate_re(#{input.inspect}) should fail" do
27
+ expect { subject.call [input] }.to raise_error Puppet::ParseError
28
+ end
29
+ end
30
+ end
31
+
32
+ describe 'Valid inputs' do
33
+ inputs = [
34
+ [ '/full/path/to/something', '^/full' ],
35
+ [ '/full/path/to/something', 'full' ],
36
+ [ '/full/path/to/something', ['full', 'absent'] ],
37
+ [ '/full/path/to/something', ['full', 'absent'], 'Message to the user' ],
38
+ ]
39
+
40
+ inputs.each do |input|
41
+ it "validate_re(#{input.inspect}) should not fail" do
42
+ expect { subject.call input }.not_to raise_error
43
+ end
44
+ end
45
+ end
46
+ describe "Valid inputs which should raise an exception without a message" do
47
+ # The intent here is to make sure valid inputs raise exceptions when they
48
+ # don't specify an error message to display. This is the behvior in
49
+ # 2.2.x and prior.
50
+ inputs = [
51
+ [ "hello", [ "bye", "later", "adios" ] ],
52
+ [ "greetings", "salutations" ],
53
+ ]
54
+
55
+ inputs.each do |input|
56
+ it "validate_re(#{input.inspect}) should fail" do
57
+ expect { subject.call input }.to raise_error /validate_re.*?does not match/
58
+ end
59
+ end
60
+ end
61
+ describe "Nicer Error Messages" do
62
+ # The intent here is to make sure the function returns the 3rd argument
63
+ # in the exception thrown
64
+ inputs = [
65
+ [ "hello", [ "bye", "later", "adios" ], "MSG to User" ],
66
+ [ "greetings", "salutations", "Error, greetings does not match salutations" ],
67
+ ]
68
+
69
+ inputs.each do |input|
70
+ it "validate_re(#{input.inspect}) should fail" do
71
+ expect { subject.call input }.to raise_error /#{input[2]}/
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,48 @@
1
+ #! /usr/bin/env ruby -S rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "the validate_slength function" do
6
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7
+
8
+ it "should exist" do
9
+ Puppet::Parser::Functions.function("validate_slength").should == "function_validate_slength"
10
+ end
11
+
12
+ it "should raise a ParseError if there is less than 2 arguments" do
13
+ expect { scope.function_validate_slength([]) }.to(raise_error(Puppet::ParseError))
14
+ expect { scope.function_validate_slength(["asdf"]) }.to(raise_error(Puppet::ParseError))
15
+ end
16
+
17
+ it "should raise a ParseError if argument 2 doesn't convert to a fixnum" do
18
+ expect { scope.function_validate_slength(["moo",["2"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/))
19
+ end
20
+
21
+ it "should raise a ParseError if argument 2 converted, but to 0, e.g. a string" do
22
+ expect { scope.function_validate_slength(["moo","monkey"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/))
23
+ end
24
+
25
+ it "should raise a ParseError if argument 2 converted, but to 0" do
26
+ expect { scope.function_validate_slength(["moo","0"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/))
27
+ end
28
+
29
+ it "should fail if string greater then size" do
30
+ expect { scope.function_validate_slength(["test", 2]) }.to(raise_error(Puppet::ParseError, /It should have been less than or equal to/))
31
+ end
32
+
33
+ it "should fail if you pass an array of something other than strings" do
34
+ expect { scope.function_validate_slength([["moo",["moo"],Hash.new["moo" => 7]], 7]) }.to(raise_error(Puppet::ParseError, /is not a string, it's a/))
35
+ end
36
+
37
+ it "should fail if you pass something other than a string or array" do
38
+ expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError), /please pass a string, or an array of strings/)
39
+ end
40
+
41
+ it "should not fail if string is smaller or equal to size" do
42
+ expect { scope.function_validate_slength(["test", 5]) }.to_not(raise_error(Puppet::ParseError))
43
+ end
44
+
45
+ it "should not fail if array of string is are all smaller or equal to size" do
46
+ expect { scope.function_validate_slength([["moo","foo","bar"], 5]) }.to_not(raise_error(Puppet::ParseError))
47
+ end
48
+ end
@@ -0,0 +1,60 @@
1
+ #! /usr/bin/env ruby -S rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Puppet::Parser::Functions.function(:validate_string) do
6
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7
+
8
+ describe 'when calling validate_string from puppet' do
9
+
10
+ %w{ foo bar baz }.each do |the_string|
11
+
12
+ it "should compile when #{the_string} is a string" do
13
+ Puppet[:code] = "validate_string('#{the_string}')"
14
+ scope.compiler.compile
15
+ end
16
+
17
+ it "should compile when #{the_string} is a bare word" do
18
+ Puppet[:code] = "validate_string(#{the_string})"
19
+ scope.compiler.compile
20
+ end
21
+
22
+ end
23
+
24
+ %w{ true false }.each do |the_string|
25
+ it "should compile when #{the_string} is a string" do
26
+ Puppet[:code] = "validate_string('#{the_string}')"
27
+ scope.compiler.compile
28
+ end
29
+
30
+ it "should not compile when #{the_string} is a bare word" do
31
+ Puppet[:code] = "validate_string(#{the_string})"
32
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/)
33
+ end
34
+ end
35
+
36
+ it "should compile when multiple string arguments are passed" do
37
+ Puppet[:code] = <<-'ENDofPUPPETcode'
38
+ $foo = ''
39
+ $bar = 'two'
40
+ validate_string($foo, $bar)
41
+ ENDofPUPPETcode
42
+ scope.compiler.compile
43
+ end
44
+
45
+ it "should compile when an explicitly undef variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
46
+ Puppet[:code] = <<-'ENDofPUPPETcode'
47
+ $foo = undef
48
+ validate_string($foo)
49
+ ENDofPUPPETcode
50
+ scope.compiler.compile
51
+ end
52
+
53
+ it "should compile when an undefined variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
54
+ Puppet[:code] = <<-'ENDofPUPPETcode'
55
+ validate_string($foobarbazishouldnotexist)
56
+ ENDofPUPPETcode
57
+ scope.compiler.compile
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,38 @@
1
+ #! /usr/bin/env ruby -S rspec
2
+ require 'spec_helper'
3
+
4
+ describe "the values_at function" do
5
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6
+
7
+ it "should exist" do
8
+ Puppet::Parser::Functions.function("values_at").should == "function_values_at"
9
+ end
10
+
11
+ it "should raise a ParseError if there is less than 1 arguments" do
12
+ lambda { scope.function_values_at([]) }.should( raise_error(Puppet::ParseError))
13
+ end
14
+
15
+ it "should raise a ParseError if you try to use a range where stop is greater then start" do
16
+ lambda { scope.function_values_at([['a','b'],["3-1"]]) }.should( raise_error(Puppet::ParseError))
17
+ end
18
+
19
+ it "should return a value at from an array" do
20
+ result = scope.function_values_at([['a','b','c'],"1"])
21
+ result.should(eq(['b']))
22
+ end
23
+
24
+ it "should return a value at from an array when passed a range" do
25
+ result = scope.function_values_at([['a','b','c'],"0-1"])
26
+ result.should(eq(['a','b']))
27
+ end
28
+
29
+ it "should return chosen values from an array when passed number of indexes" do
30
+ result = scope.function_values_at([['a','b','c'],["0","2"]])
31
+ result.should(eq(['a','c']))
32
+ end
33
+
34
+ it "should return chosen values from an array when passed ranges and multiple indexes" do
35
+ result = scope.function_values_at([['a','b','c','d','e','f','g'],["0","2","4-5"]])
36
+ result.should(eq(['a','c','e','f']))
37
+ end
38
+ end
@@ -0,0 +1,31 @@
1
+ #! /usr/bin/env ruby -S rspec
2
+ require 'spec_helper'
3
+
4
+ describe "the values function" do
5
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6
+
7
+ it "should exist" do
8
+ Puppet::Parser::Functions.function("values").should == "function_values"
9
+ end
10
+
11
+ it "should raise a ParseError if there is less than 1 arguments" do
12
+ lambda { scope.function_values([]) }.should( raise_error(Puppet::ParseError))
13
+ end
14
+
15
+ it "should return values from a hash" do
16
+ result = scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}])
17
+ # =~ is the RSpec::Matchers::MatchArray matcher.
18
+ # A.K.A. "array with same elements" (multiset) matching
19
+ result.should =~ %w{ 1 2 3 }
20
+ end
21
+
22
+ it "should return a multiset" do
23
+ result = scope.function_values([{'a'=>'1','b'=>'3','c'=>'3'}])
24
+ result.should =~ %w{ 1 3 3 }
25
+ result.should_not =~ %w{ 1 3 }
26
+ end
27
+
28
+ it "should raise a ParseError unless a Hash is provided" do
29
+ lambda { scope.function_values([['a','b','c']]) }.should( raise_error(Puppet::ParseError))
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ #! /usr/bin/env ruby -S rspec
2
+ require 'spec_helper'
3
+
4
+ describe "the zip function" do
5
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6
+
7
+ it "should raise a ParseError if there is less than 1 arguments" do
8
+ lambda { scope.function_zip([]) }.should( raise_error(Puppet::ParseError))
9
+ end
10
+
11
+ it "should be able to zip an array" do
12
+ result = scope.function_zip([['1','2','3'],['4','5','6']])
13
+ result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
14
+ end
15
+ end
@@ -0,0 +1,127 @@
1
+ require 'puppet'
2
+ require 'tempfile'
3
+ provider_class = Puppet::Type.type(:file_line).provider(:ruby)
4
+ describe provider_class do
5
+ context "when adding" do
6
+ before :each do
7
+ # TODO: these should be ported over to use the PuppetLabs spec_helper
8
+ # file fixtures once the following pull request has been merged:
9
+ # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
10
+ tmp = Tempfile.new('tmp')
11
+ @tmpfile = tmp.path
12
+ tmp.close!
13
+ @resource = Puppet::Type::File_line.new(
14
+ {:name => 'foo', :path => @tmpfile, :line => 'foo'}
15
+ )
16
+ @provider = provider_class.new(@resource)
17
+ end
18
+ it 'should detect if the line exists in the file' do
19
+ File.open(@tmpfile, 'w') do |fh|
20
+ fh.write('foo')
21
+ end
22
+ @provider.exists?.should be_true
23
+ end
24
+ it 'should detect if the line does not exist in the file' do
25
+ File.open(@tmpfile, 'w') do |fh|
26
+ fh.write('foo1')
27
+ end
28
+ @provider.exists?.should be_nil
29
+ end
30
+ it 'should append to an existing file when creating' do
31
+ @provider.create
32
+ File.read(@tmpfile).chomp.should == 'foo'
33
+ end
34
+ end
35
+
36
+ context "when matching" do
37
+ before :each do
38
+ # TODO: these should be ported over to use the PuppetLabs spec_helper
39
+ # file fixtures once the following pull request has been merged:
40
+ # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
41
+ tmp = Tempfile.new('tmp')
42
+ @tmpfile = tmp.path
43
+ tmp.close!
44
+ @resource = Puppet::Type::File_line.new(
45
+ {
46
+ :name => 'foo',
47
+ :path => @tmpfile,
48
+ :line => 'foo = bar',
49
+ :match => '^foo\s*=.*$',
50
+ }
51
+ )
52
+ @provider = provider_class.new(@resource)
53
+ end
54
+
55
+ it 'should raise an error if more than one line matches, and should not have modified the file' do
56
+ File.open(@tmpfile, 'w') do |fh|
57
+ fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
58
+ end
59
+ @provider.exists?.should be_nil
60
+ expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/)
61
+ File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
62
+ end
63
+
64
+ it 'should replace a line that matches' do
65
+ File.open(@tmpfile, 'w') do |fh|
66
+ fh.write("foo1\nfoo=blah\nfoo2")
67
+ end
68
+ @provider.exists?.should be_nil
69
+ @provider.create
70
+ File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2")
71
+ end
72
+ it 'should add a new line if no lines match' do
73
+ File.open(@tmpfile, 'w') do |fh|
74
+ fh.write("foo1\nfoo2")
75
+ end
76
+ @provider.exists?.should be_nil
77
+ @provider.create
78
+ File.read(@tmpfile).should eql("foo1\nfoo2\nfoo = bar\n")
79
+ end
80
+ it 'should do nothing if the exact line already exists' do
81
+ File.open(@tmpfile, 'w') do |fh|
82
+ fh.write("foo1\nfoo = bar\nfoo2")
83
+ end
84
+ @provider.exists?.should be_true
85
+ @provider.create
86
+ File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2")
87
+ end
88
+ end
89
+
90
+ context "when removing" do
91
+ before :each do
92
+ # TODO: these should be ported over to use the PuppetLabs spec_helper
93
+ # file fixtures once the following pull request has been merged:
94
+ # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
95
+ tmp = Tempfile.new('tmp')
96
+ @tmpfile = tmp.path
97
+ tmp.close!
98
+ @resource = Puppet::Type::File_line.new(
99
+ {:name => 'foo', :path => @tmpfile, :line => 'foo', :ensure => 'absent' }
100
+ )
101
+ @provider = provider_class.new(@resource)
102
+ end
103
+ it 'should remove the line if it exists' do
104
+ File.open(@tmpfile, 'w') do |fh|
105
+ fh.write("foo1\nfoo\nfoo2")
106
+ end
107
+ @provider.destroy
108
+ File.read(@tmpfile).should eql("foo1\nfoo2")
109
+ end
110
+
111
+ it 'should remove the line without touching the last new line' do
112
+ File.open(@tmpfile, 'w') do |fh|
113
+ fh.write("foo1\nfoo\nfoo2\n")
114
+ end
115
+ @provider.destroy
116
+ File.read(@tmpfile).should eql("foo1\nfoo2\n")
117
+ end
118
+
119
+ it 'should remove any occurence of the line' do
120
+ File.open(@tmpfile, 'w') do |fh|
121
+ fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
122
+ end
123
+ @provider.destroy
124
+ File.read(@tmpfile).should eql("foo1\nfoo2\n")
125
+ end
126
+ end
127
+ end