├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── Vagrantfile ├── export.sh ├── puppet ├── manifests │ └── base.pp └── modules │ ├── carbon │ ├── files │ │ ├── carbon │ │ ├── carbon.conf │ │ └── storage-schemas.conf │ └── manifests │ │ └── init.pp │ ├── graphite │ ├── files │ │ └── local_settings.py │ └── manifests │ │ └── init.pp │ ├── statsd │ └── manifests │ │ └── init.pp │ └── stdlib │ ├── CHANGELOG │ ├── LICENSE │ ├── Modulefile │ ├── README.markdown │ ├── README_DEVELOPER.markdown │ ├── RELEASE_PROCESS.markdown │ ├── Rakefile │ ├── lib │ ├── facter │ │ ├── facter_dot_d.rb │ │ ├── pe_version.rb │ │ ├── puppet_vardir.rb │ │ ├── root_home.rb │ │ └── util │ │ │ └── puppet_settings.rb │ └── puppet │ │ ├── parser │ │ └── functions │ │ │ ├── abs.rb │ │ │ ├── bool2num.rb │ │ │ ├── capitalize.rb │ │ │ ├── chomp.rb │ │ │ ├── chop.rb │ │ │ ├── defined_with_params.rb │ │ │ ├── delete.rb │ │ │ ├── delete_at.rb │ │ │ ├── downcase.rb │ │ │ ├── empty.rb │ │ │ ├── ensure_packages.rb │ │ │ ├── ensure_resource.rb │ │ │ ├── flatten.rb │ │ │ ├── fqdn_rotate.rb │ │ │ ├── get_module_path.rb │ │ │ ├── getvar.rb │ │ │ ├── grep.rb │ │ │ ├── has_interface_with.rb │ │ │ ├── has_ip_address.rb │ │ │ ├── has_ip_network.rb │ │ │ ├── has_key.rb │ │ │ ├── hash.rb │ │ │ ├── is_array.rb │ │ │ ├── is_domain_name.rb │ │ │ ├── is_float.rb │ │ │ ├── is_hash.rb │ │ │ ├── is_integer.rb │ │ │ ├── is_ip_address.rb │ │ │ ├── is_mac_address.rb │ │ │ ├── is_numeric.rb │ │ │ ├── is_string.rb │ │ │ ├── join.rb │ │ │ ├── join_keys_to_values.rb │ │ │ ├── keys.rb │ │ │ ├── loadyaml.rb │ │ │ ├── lstrip.rb │ │ │ ├── max.rb │ │ │ ├── member.rb │ │ │ ├── merge.rb │ │ │ ├── min.rb │ │ │ ├── num2bool.rb │ │ │ ├── parsejson.rb │ │ │ ├── parseyaml.rb │ │ │ ├── pick.rb │ │ │ ├── prefix.rb │ │ │ ├── range.rb │ │ │ ├── reject.rb │ │ │ ├── reverse.rb │ │ │ ├── rstrip.rb │ │ │ ├── shuffle.rb │ │ │ ├── size.rb │ │ │ ├── sort.rb │ │ │ ├── squeeze.rb │ │ │ ├── str2bool.rb │ │ │ ├── str2saltedsha512.rb │ │ │ ├── strftime.rb │ │ │ ├── strip.rb │ │ │ ├── swapcase.rb │ │ │ ├── time.rb │ │ │ ├── to_bytes.rb │ │ │ ├── type.rb │ │ │ ├── unique.rb │ │ │ ├── upcase.rb │ │ │ ├── uriescape.rb │ │ │ ├── validate_absolute_path.rb │ │ │ ├── validate_array.rb │ │ │ ├── validate_bool.rb │ │ │ ├── validate_hash.rb │ │ │ ├── validate_re.rb │ │ │ ├── validate_slength.rb │ │ │ ├── validate_string.rb │ │ │ ├── values.rb │ │ │ ├── values_at.rb │ │ │ └── zip.rb │ │ ├── provider │ │ └── file_line │ │ │ └── ruby.rb │ │ └── type │ │ ├── anchor.rb │ │ └── file_line.rb │ ├── manifests │ ├── init.pp │ └── stages.pp │ ├── metadata.json │ ├── spec │ ├── functions │ │ ├── defined_with_params_spec.rb │ │ ├── ensure_packages_spec.rb │ │ └── ensure_resource_spec.rb │ ├── lib │ │ └── puppet_spec │ │ │ ├── files.rb │ │ │ ├── fixtures.rb │ │ │ ├── matchers.rb │ │ │ └── verbose.rb │ ├── monkey_patches │ │ ├── alias_should_to_must.rb │ │ └── publicize_methods.rb │ ├── spec.opts │ ├── spec_helper.rb │ ├── unit │ │ ├── facter │ │ │ ├── pe_version_spec.rb │ │ │ ├── root_home_spec.rb │ │ │ └── util │ │ │ │ └── puppet_settings_spec.rb │ │ └── puppet │ │ │ ├── parser │ │ │ └── functions │ │ │ │ ├── abs_spec.rb │ │ │ │ ├── bool2num_spec.rb │ │ │ │ ├── capitalize_spec.rb │ │ │ │ ├── chomp_spec.rb │ │ │ │ ├── chop_spec.rb │ │ │ │ ├── delete_at_spec.rb │ │ │ │ ├── delete_spec.rb │ │ │ │ ├── downcase_spec.rb │ │ │ │ ├── empty_spec.rb │ │ │ │ ├── flatten_spec.rb │ │ │ │ ├── fqdn_rotate_spec.rb │ │ │ │ ├── get_module_path_spec.rb │ │ │ │ ├── getvar_spec.rb │ │ │ │ ├── grep_spec.rb │ │ │ │ ├── has_interface_with_spec.rb │ │ │ │ ├── has_ip_address_spec.rb │ │ │ │ ├── has_ip_network_spec.rb │ │ │ │ ├── has_key_spec.rb │ │ │ │ ├── hash_spec.rb │ │ │ │ ├── is_array_spec.rb │ │ │ │ ├── is_domain_name_spec.rb │ │ │ │ ├── is_float_spec.rb │ │ │ │ ├── is_hash_spec.rb │ │ │ │ ├── is_integer_spec.rb │ │ │ │ ├── is_ip_address_spec.rb │ │ │ │ ├── is_mac_address_spec.rb │ │ │ │ ├── is_numeric_spec.rb │ │ │ │ ├── is_string_spec.rb │ │ │ │ ├── join_keys_to_values_spec.rb │ │ │ │ ├── join_spec.rb │ │ │ │ ├── keys_spec.rb │ │ │ │ ├── lstrip_spec.rb │ │ │ │ ├── max_spec.rb │ │ │ │ ├── member_spec.rb │ │ │ │ ├── merge_spec.rb │ │ │ │ ├── min_spec.rb │ │ │ │ ├── num2bool_spec.rb │ │ │ │ ├── parsejson_spec.rb │ │ │ │ ├── parseyaml_spec.rb │ │ │ │ ├── pick_spec.rb │ │ │ │ ├── prefix_spec.rb │ │ │ │ ├── range_spec.rb │ │ │ │ ├── reject_spec.rb │ │ │ │ ├── reverse_spec.rb │ │ │ │ ├── rstrip_spec.rb │ │ │ │ ├── shuffle_spec.rb │ │ │ │ ├── size_spec.rb │ │ │ │ ├── sort_spec.rb │ │ │ │ ├── squeeze_spec.rb │ │ │ │ ├── str2bool_spec.rb │ │ │ │ ├── str2saltedsha512_spec.rb │ │ │ │ ├── strftime_spec.rb │ │ │ │ ├── strip_spec.rb │ │ │ │ ├── swapcase_spec.rb │ │ │ │ ├── time_spec.rb │ │ │ │ ├── to_bytes_spec.rb │ │ │ │ ├── type_spec.rb │ │ │ │ ├── unique_spec.rb │ │ │ │ ├── upcase_spec.rb │ │ │ │ ├── uriescape_spec.rb │ │ │ │ ├── validate_absolute_path_spec.rb │ │ │ │ ├── validate_array_spec.rb │ │ │ │ ├── validate_bool_spec.rb │ │ │ │ ├── validate_hash_spec.rb │ │ │ │ ├── validate_re_spec.rb │ │ │ │ ├── validate_slength_spec.rb │ │ │ │ ├── validate_string_spec.rb │ │ │ │ ├── values_at_spec.rb │ │ │ │ ├── values_spec.rb │ │ │ │ └── zip_spec.rb │ │ │ ├── provider │ │ │ └── file_line │ │ │ │ └── ruby_spec.rb │ │ │ └── type │ │ │ ├── anchor_spec.rb │ │ │ └── file_line_spec.rb │ └── watchr.rb │ └── tests │ ├── file_line.pp │ ├── has_interface_with.pp │ ├── has_ip_address.pp │ ├── has_ip_network.pp │ └── init.pp ├── python-whisper_0.9.12-1_all.deb └── statsd_0.6.0-1_all.deb /.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant 2 | 3 | Graphite.ova 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "puppet/modules/archive"] 2 | path = puppet/modules/archive 3 | url = https://github.com/camptocamp/puppet-archive.git 4 | [submodule "puppet/modules/grafana"] 5 | path = puppet/modules/grafana 6 | url = https://github.com/bfraser/puppet-grafana.git 7 | [submodule "puppet/modules/concat"] 8 | path = puppet/modules/concat 9 | url = https://github.com/puppetlabs/puppetlabs-concat.git 10 | [submodule "puppet/modules/apache"] 11 | path = puppet/modules/apache 12 | url = https://github.com/puppetlabs/puppetlabs-apache.git 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Soenke Ruempler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # statsd + graphite to go 2 | 3 | Provision a virtual machine with vagrant and puppet to play around with statsd and graphite 4 | 5 | ## Details: 6 | 7 | * debian package for statsd (github.com/etsy) included 8 | * port forwardings enabled 9 | * graphite: http://localhost:8080/ 10 | * Grafana: http://localhost:8081 11 | * statsd: 8125:udp 12 | 13 | ## Installation 14 | 15 | ``` 16 | git clone --recursive https://github.com/tilmans/vagrant-statsd-graphite-puppet.git 17 | cd vagrant-statsd-graphite-puppet 18 | vagrant up 19 | open http://localhost:8080/ 20 | ``` 21 | 22 | The default account is admin/admin 23 | 24 | ## Contributors 25 | 26 | Created by jimdo https://github.com/Jimdo 27 | 28 | For the list of contributors see the [commit history](https://github.com/tilmans/vagrant-statsd-graphite-puppet/commits/master) 29 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # # vi: set ft=ruby : 3 | # 4 | Vagrant.configure('2') do |config| 5 | config.vm.box = "puppet_ubuntu12" 6 | config.vm.box_url = "http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210.box" 7 | 8 | config.vm.network "forwarded_port", guest: 80, host: 8080 9 | config.vm.network "forwarded_port", guest: 8081, host: 8081 10 | config.vm.network "forwarded_port", guest: 2003, host: 2003 11 | config.vm.network "forwarded_port", guest: 2003, host: 2003, protocol: 'udp' 12 | config.vm.network "forwarded_port", guest: 2004, host: 2004 13 | config.vm.network "forwarded_port", guest: 8125, host: 8125, protocol: 'udp' 14 | 15 | config.vm.provider 'virtualbox' do |v| 16 | v.name = "Graphite" 17 | v.customize ["modifyvm", :id, "--memory", 512] 18 | v.customize ["modifyvm", :id, "--cpus", 2] 19 | end 20 | 21 | config.vm.provision :puppet do |puppet| 22 | puppet.manifests_path = "puppet/manifests" 23 | puppet.module_path = "puppet/modules" 24 | puppet.manifest_file = "base.pp" 25 | puppet.options = "--verbose --debug --trace" 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /export.sh: -------------------------------------------------------------------------------- 1 | vagrant halt 2 | VBoxManage export Graphite -o Graphite.ova 3 | -------------------------------------------------------------------------------- /puppet/manifests/base.pp: -------------------------------------------------------------------------------- 1 | Exec { 2 | path => ["/usr/bin", "/usr/sbin", '/bin'] 3 | } 4 | 5 | Exec["apt-get-update"] -> Package <| |> 6 | 7 | exec { "apt-get-update" : 8 | command => "/usr/bin/apt-get update", 9 | require => File["/etc/apt/preferences"] 10 | } 11 | 12 | file { "/etc/apt/preferences" : 13 | content => " 14 | Package: * 15 | Pin: release a=stable 16 | Pin-Priority: 800 17 | 18 | Package: * 19 | Pin: release a=testing 20 | Pin-Priority: 750 21 | 22 | Package: * 23 | Pin: release a=unstable 24 | Pin-Priority: 650 25 | 26 | Package: * 27 | Pin: release a=oldstable 28 | Pin-Priority: 600 29 | 30 | Package: * 31 | Pin: release a=experimental 32 | Pin-Priority: 550 33 | ", 34 | ensure => present, 35 | } 36 | 37 | file { "/etc/issue": 38 | content => " 39 | Welcome to the Graphite VM. To access the Graphite UI open http://localhost:8080 in your browser. 40 | 41 | The Graphite account is admin/admin 42 | 43 | To login via SSH: ssh -p 2222 vagrant@localhost 44 | The login account is vagrant/vagrant. 45 | 46 | Service ports: 47 | Graphite Web: 8080 48 | Grafana: 8081 49 | SSH: 2222 50 | Graphite/Carbon input: 2003 TCP, 2003 UDP 51 | StatsD: 8125 UDP 52 | ", 53 | ensure => present 54 | } 55 | 56 | class { 'apache': 57 | default_vhost => false, 58 | } 59 | 60 | include carbon 61 | include statsd 62 | 63 | class { 'grafana': 64 | datasources => { 65 | 'graphite' => { 66 | 'type' => 'graphite', 67 | 'url' => 'http://\'+window.location.hostname+\':8080', 68 | 'default' => 'true' 69 | }, 70 | 'elasticsearch' => { 71 | 'type' => 'elasticsearch', 72 | 'url' => 'http://\'+window.location.hostname+\':9200', 73 | 'index' => 'grafana-dash', 74 | 'grafanaDB' => 'true', 75 | }, 76 | } 77 | } 78 | 79 | # Create Apache virtual host 80 | apache::vhost { 'grafana.example.com': 81 | servername => 'grafana.example.com', 82 | port => 8081, 83 | docroot => '/opt/grafana', 84 | error_log_file => 'grafana-error.log', 85 | access_log_file => 'grafana-access.log', 86 | directories => [ 87 | { 88 | path => '/opt/grafana', 89 | options => [ 'None' ], 90 | allow => 'from All', 91 | allow_override => [ 'None' ], 92 | order => 'Allow,Deny', 93 | } 94 | ] 95 | } 96 | -------------------------------------------------------------------------------- /puppet/modules/carbon/files/storage-schemas.conf: -------------------------------------------------------------------------------- 1 | [stats] 2 | priority = 100 3 | pattern = .* 4 | retentions = 10:2160,60:10080,600:262974 5 | 6 | [stats_counts] 7 | priority = 100 8 | pattern = .* 9 | retentions = 10:2160,60:10080,600:262974 10 | 11 | [statsd] 12 | priority = 100 13 | pattern = .* 14 | retentions = 10:2160,60:10080,600:262974 -------------------------------------------------------------------------------- /puppet/modules/carbon/manifests/init.pp: -------------------------------------------------------------------------------- 1 | class carbon($version = '0.9.12') { 2 | 3 | $build_dir = "/tmp" 4 | 5 | $carbon_url = "https://github.com/graphite-project/carbon/archive/${version}.tar.gz" 6 | 7 | $carbon_loc = "$build_dir/carbon.tar.gz" 8 | 9 | class {'graphite': 10 | version => $version, 11 | } 12 | 13 | file { "/etc/init.d/carbon" : 14 | source => "puppet:///modules/carbon/carbon", 15 | ensure => present, 16 | } 17 | 18 | file { "/opt/graphite/conf/carbon.conf" : 19 | source => "puppet:///modules/carbon/carbon.conf", 20 | ensure => present, 21 | notify => Service[carbon], 22 | subscribe => Exec[install-carbon], 23 | } 24 | 25 | file { "/opt/graphite/conf/storage-schemas.conf" : 26 | source => "puppet:///modules/carbon/storage-schemas.conf", 27 | ensure => present, 28 | notify => Service[carbon], 29 | subscribe => Exec[install-carbon], 30 | } 31 | 32 | file { "/var/log/carbon" : 33 | ensure => directory, 34 | owner => www-data, 35 | group => www-data, 36 | } 37 | 38 | exec { "download-graphite-carbon": 39 | command => "wget -O $carbon_loc $carbon_url", 40 | creates => "$carbon_loc" 41 | } 42 | 43 | exec { "unpack-carbon": 44 | command => "tar -zxvf $carbon_loc", 45 | cwd => $build_dir, 46 | subscribe => Exec[download-graphite-carbon], 47 | refreshonly => true, 48 | } 49 | 50 | # Downgrade Twisted to v12 for graphite 51 | package{['python-dev','python-pip']: 52 | require => Anchor['graphite::end'], 53 | } -> 54 | exec {'/usr/bin/pip install \'Twisted<12.0\' --upgrade':} -> 55 | exec { "install-carbon" : 56 | command => "python setup.py install", 57 | cwd => "$build_dir/carbon-${version}", 58 | require => Exec[unpack-carbon], 59 | creates => "/opt/graphite/bin/carbon-cache.py" 60 | } 61 | 62 | service { carbon : 63 | ensure => running, 64 | enable => true, 65 | require => [Exec['install-carbon']] 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /puppet/modules/statsd/manifests/init.pp: -------------------------------------------------------------------------------- 1 | class statsd { 2 | 3 | package { "nodejs" : 4 | ensure => "present" 5 | } 6 | 7 | package { "statsd" : 8 | provider => "dpkg", 9 | source => "/vagrant/statsd_0.6.0-1_all.deb", 10 | ensure => installed, 11 | require => Package[nodejs], 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /puppet/modules/stdlib/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011 Puppet Labs Inc 2 | 3 | and some parts: 4 | 5 | Copyright (C) 2011 Krzysztof Wilczynski 6 | 7 | Puppet Labs can be contacted at: info@puppetlabs.com 8 | 9 | Licensed under the Apache License, Version 2.0 (the "License"); 10 | you may not use this file except in compliance with the License. 11 | You may obtain a copy of the License at 12 | 13 | http://www.apache.org/licenses/LICENSE-2.0 14 | 15 | Unless required by applicable law or agreed to in writing, software 16 | distributed under the License is distributed on an "AS IS" BASIS, 17 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | See the License for the specific language governing permissions and 19 | limitations under the License. 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/Modulefile: -------------------------------------------------------------------------------- 1 | name 'puppetlabs-stdlib' 2 | version '2.6.0' 3 | source 'git://github.com/puppetlabs/puppetlabs-stdlib' 4 | author 'puppetlabs' 5 | license 'Apache 2.0' 6 | summary 'Puppet Module Standard Library' 7 | description 'Standard Library for Puppet Modules' 8 | project_page 'https://github.com/puppetlabs/puppetlabs-stdlib' 9 | 10 | ## Add dependencies, if any: 11 | # dependency 'username/name', '>= 1.2.0' 12 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/README.markdown: -------------------------------------------------------------------------------- 1 | # Puppet Labs Standard Library # 2 | 3 | This module provides a "standard library" of resources for developing Puppet 4 | Modules. This modules will include the following additions to Puppet 5 | 6 | * Stages 7 | * Facts 8 | * Functions 9 | * Defined resource types 10 | * Types 11 | * Providers 12 | 13 | This module is officially curated and provided by Puppet Labs. The modules 14 | Puppet Labs writes and distributes will make heavy use of this standard 15 | library. 16 | 17 | # Versions # 18 | 19 | This module follows semver.org (v1.0.0) versioning guidelines. The standard 20 | library module is released as part of [Puppet 21 | Enterprise](http://puppetlabs.com/puppet/puppet-enterprise/) and as a result 22 | older versions of Puppet Enterprise that Puppet Labs still supports will have 23 | bugfix maintenance branches periodically "merged up" into master. The current 24 | list of integration branches are: 25 | 26 | * v2.1.x (v2.1.1 released in PE 1.2, 1.2.1, 1.2.3, 1.2.4) 27 | * v2.2.x (Never released as part of PE, only to the Forge) 28 | * v2.3.x (Scheduled for next PE feature release) 29 | * master (mainline development branch) 30 | 31 | The first Puppet Enterprise version including the stdlib module is Puppet 32 | Enterprise 1.2. 33 | 34 | # Compatibility # 35 | 36 | ## stdlib v2.1.x, v2.2.x ## 37 | 38 | v2.1.x and v2.2.x of this module are designed to work with Puppet versions 39 | 2.6.x and 2.7.x. There are currently no plans for a Puppet 0.25 standard 40 | library module. 41 | 42 | ## stdlib v2.3.x ## 43 | 44 | While not yet released, the standard library may only work with Puppet 2.7.x. 45 | 46 | # Functions # 47 | 48 | Please see `puppet doc -r function` for documentation on each function. The 49 | current list of functions is: 50 | 51 | * getvar 52 | * has\_key 53 | * loadyaml 54 | * merge.rb 55 | * validate\_array 56 | * validate\_bool 57 | * validate\_hash 58 | * validate\_re 59 | * validate\_string 60 | 61 | ## validate\_hash ## 62 | 63 | $somehash = { 'one' => 'two' } 64 | validate\_hash($somehash) 65 | 66 | ## getvar() ## 67 | 68 | This function aims to look up variables in user-defined namespaces within 69 | puppet. Note, if the namespace is a class, it should already be evaluated 70 | before the function is used. 71 | 72 | $namespace = 'site::data' 73 | include "${namespace}" 74 | $myvar = getvar("${namespace}::myvar") 75 | 76 | ## Facts ## 77 | 78 | Facts in `/etc/facter/facts.d` and `/etc/puppetlabs/facter/facts.d` are now loaded 79 | automatically. This is a direct copy of R.I. Pienaar's custom facter fact 80 | located at: 81 | [https://github.com/ripienaar/facter-facts/tree/master/facts-dot-d](https://github.com/ripienaar/facter-facts/tree/master/facts-dot-d) 82 | 83 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/README_DEVELOPER.markdown: -------------------------------------------------------------------------------- 1 | Puppet Specific Facts 2 | ===================== 3 | 4 | Facter is meant to stand alone and apart from Puppet. However, Facter often 5 | runs inside Puppet and all custom facts included in the stdlib module will 6 | almost always be evaluated in the context of Puppet and Facter working 7 | together. 8 | 9 | Still, we don't want to write custom facts that blow up in the users face if 10 | Puppet is not loaded in memory. This is often the case if the user runs 11 | `facter` without also supplying the `--puppet` flag. 12 | 13 | Ah! But Jeff, the custom fact won't be in the `$LOAD_PATH` unless the user 14 | supplies `--facter`! You might say... 15 | 16 | Not (always) true I say! If the user happens to have a CWD of 17 | `/stdlib/lib` then the facts will automatically be evaluated and 18 | blow up. 19 | 20 | In any event, it's pretty easy to write a fact that has no value if Puppet is 21 | not loaded. Simply do it like this: 22 | 23 | Facter.add(:node_vardir) do 24 | setcode do 25 | # This will be nil if Puppet is not available. 26 | Facter::Util::PuppetSettings.with_puppet do 27 | Puppet[:vardir] 28 | end 29 | end 30 | end 31 | 32 | The `Facter::Util::PuppetSettings.with_puppet` method accepts a block and 33 | yields to it only if the Puppet library is loaded. If the Puppet library is 34 | not loaded, then the method silently returns `nil` which Facter interprets as 35 | an undefined fact value. The net effect is that the fact won't be set. 36 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/RELEASE_PROCESS.markdown: -------------------------------------------------------------------------------- 1 | # Contributing to this module # 2 | 3 | * Work in a topic branch 4 | * Submit a github pull request 5 | * Address any comments / feeback 6 | * Merge into master using --no-ff 7 | 8 | # Releasing this module # 9 | 10 | * This module adheres to http://semver.org/ 11 | * Look for API breaking changes using git diff vX.Y.Z..master 12 | * If no API breaking changes, the minor version may be bumped. 13 | * If there are API breaking changes, the major version must be bumped. 14 | * If there are only small minor changes, the patch version may be bumped. 15 | * Update the CHANGELOG 16 | * Update the Modulefile 17 | * Commit these changes with a message along the lines of "Update CHANGELOG and 18 | Modulefile for release" 19 | * Create an annotated tag with git tag -a vX.Y.Z -m 'version X.Y.Z' (NOTE the 20 | leading v as per semver.org) 21 | * Push the tag with git push origin --tags 22 | * Build a new package with puppet-module or the rake build task if it exists 23 | * Publish the new package to the forge 24 | * Bonus points for an announcement to puppet-users. 25 | 26 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rspec/core/rake_task' 3 | 4 | task :default => [:test] 5 | 6 | desc 'Run RSpec' 7 | RSpec::Core::RakeTask.new(:test) do |t| 8 | t.pattern = 'spec/{unit}/**/*.rb' 9 | t.rspec_opts = ['--color'] 10 | end 11 | 12 | desc 'Generate code coverage' 13 | RSpec::Core::RakeTask.new(:coverage) do |t| 14 | t.rcov = true 15 | t.rcov_opts = ['--exclude', 'spec'] 16 | end 17 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/facter/pe_version.rb: -------------------------------------------------------------------------------- 1 | # Fact: is_pe, pe_version, pe_major_version, pe_minor_version, pe_patch_version 2 | # 3 | # Purpose: Return various facts about the PE state of the system 4 | # 5 | # Resolution: Uses a regex match against puppetversion to determine whether the 6 | # machine has Puppet Enterprise installed, and what version (overall, major, 7 | # minor, patch) is installed. 8 | # 9 | # Caveats: 10 | # 11 | Facter.add("pe_version") do 12 | setcode do 13 | pe_ver = Facter.value("puppetversion").match(/Puppet Enterprise (\d+\.\d+\.\d+)/) 14 | pe_ver[1] if pe_ver 15 | end 16 | end 17 | 18 | Facter.add("is_pe") do 19 | setcode do 20 | if Facter.value(:pe_version).to_s.empty? then 21 | false 22 | else 23 | true 24 | end 25 | end 26 | end 27 | 28 | Facter.add("pe_major_version") do 29 | confine :is_pe => true 30 | setcode do 31 | if pe_version = Facter.value(:pe_version) 32 | pe_version.to_s.split('.')[0] 33 | end 34 | end 35 | end 36 | 37 | Facter.add("pe_minor_version") do 38 | confine :is_pe => true 39 | setcode do 40 | if pe_version = Facter.value(:pe_version) 41 | pe_version.to_s.split('.')[1] 42 | end 43 | end 44 | end 45 | 46 | Facter.add("pe_patch_version") do 47 | confine :is_pe => true 48 | setcode do 49 | if pe_version = Facter.value(:pe_version) 50 | pe_version.to_s.split('.')[2] 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/facter/puppet_vardir.rb: -------------------------------------------------------------------------------- 1 | # This facter fact returns the value of the Puppet vardir setting for the node 2 | # running puppet or puppet agent. The intent is to enable Puppet modules to 3 | # automatically have insight into a place where they can place variable data, 4 | # regardless of the node's platform. 5 | # 6 | # The value should be directly usable in a File resource path attribute. 7 | 8 | 9 | begin 10 | require 'facter/util/puppet_settings' 11 | rescue LoadError => e 12 | # puppet apply does not add module lib directories to the $LOAD_PATH (See 13 | # #4248). It should (in the future) but for the time being we need to be 14 | # defensive which is what this rescue block is doing. 15 | rb_file = File.join(File.dirname(__FILE__), 'util', 'puppet_settings.rb') 16 | load rb_file if File.exists?(rb_file) or raise e 17 | end 18 | 19 | Facter.add(:puppet_vardir) do 20 | setcode do 21 | # This will be nil if Puppet is not available. 22 | Facter::Util::PuppetSettings.with_puppet do 23 | Puppet[:vardir] 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/facter/root_home.rb: -------------------------------------------------------------------------------- 1 | # A facter fact to determine the root home directory. 2 | # This varies on PE supported platforms and may be 3 | # reconfigured by the end user. 4 | 5 | module Facter::Util::RootHome 6 | class << self 7 | def get_root_home 8 | root_ent = Facter::Util::Resolution.exec("getent passwd root") 9 | # The home directory is the sixth element in the passwd entry 10 | # If the platform doesn't have getent, root_ent will be nil and we should 11 | # return it straight away. 12 | root_ent && root_ent.split(":")[5] 13 | end 14 | end 15 | end 16 | 17 | Facter.add(:root_home) do 18 | setcode { Facter::Util::RootHome.get_root_home } 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/facter/util/puppet_settings.rb: -------------------------------------------------------------------------------- 1 | module Facter 2 | module Util 3 | module PuppetSettings 4 | # This method is intended to provide a convenient way to evaluate a 5 | # Facter code block only if Puppet is loaded. This is to account for the 6 | # situation where the fact happens to be in the load path, but Puppet is 7 | # not loaded for whatever reason. Perhaps the user is simply running 8 | # facter without the --puppet flag and they happen to be working in a lib 9 | # directory of a module. 10 | def self.with_puppet 11 | begin 12 | Module.const_get("Puppet") 13 | rescue NameError 14 | nil 15 | else 16 | yield 17 | end 18 | end 19 | end 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/abs.rb: -------------------------------------------------------------------------------- 1 | # 2 | # abs.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:abs, :type => :rvalue, :doc => <<-EOS 7 | Returns the absolute value of a number, for example -34.56 becomes 8 | 34.56. Takes a single integer and float value as an argument. 9 | EOS 10 | ) do |arguments| 11 | 12 | raise(Puppet::ParseError, "abs(): Wrong number of arguments " + 13 | "given (#{arguments.size} for 1)") if arguments.size < 1 14 | 15 | value = arguments[0] 16 | 17 | # Numbers in Puppet are often string-encoded which is troublesome ... 18 | if value.is_a?(String) 19 | if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) 20 | value = value.to_f 21 | elsif value.match(/^-?\d+$/) 22 | value = value.to_i 23 | else 24 | raise(Puppet::ParseError, 'abs(): Requires float or ' + 25 | 'integer to work with') 26 | end 27 | end 28 | 29 | # We have numeric value to handle ... 30 | result = value.abs 31 | 32 | return result 33 | end 34 | end 35 | 36 | # vim: set ts=2 sw=2 et : 37 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/bool2num.rb: -------------------------------------------------------------------------------- 1 | # 2 | # bool2num.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS 7 | Converts a boolean to a number. Converts the values: 8 | false, f, 0, n, and no to 0 9 | true, t, 1, y, and yes to 1 10 | Requires a single boolean or string as an input. 11 | EOS 12 | ) do |arguments| 13 | 14 | raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + 15 | "given (#{arguments.size} for 1)") if arguments.size < 1 16 | 17 | value = arguments[0] 18 | klass = value.class 19 | 20 | # We can have either true or false, or string which resembles boolean ... 21 | unless [FalseClass, TrueClass, String].include?(klass) 22 | raise(Puppet::ParseError, 'bool2num(): Requires either ' + 23 | 'boolean or string to work with') 24 | end 25 | 26 | if value.is_a?(String) 27 | # We consider all the yes, no, y, n and so on too ... 28 | value = case value 29 | # 30 | # This is how undef looks like in Puppet ... 31 | # We yield 0 (or false if you wish) in this case. 32 | # 33 | when /^$/, '' then false # Empty string will be false ... 34 | when /^(1|t|y|true|yes)$/ then true 35 | when /^(0|f|n|false|no)$/ then false 36 | when /^(undef|undefined)$/ then false # This is not likely to happen ... 37 | else 38 | raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') 39 | end 40 | end 41 | 42 | # We have real boolean values as well ... 43 | result = value ? 1 : 0 44 | 45 | return result 46 | end 47 | end 48 | 49 | # vim: set ts=2 sw=2 et : 50 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/capitalize.rb: -------------------------------------------------------------------------------- 1 | # 2 | # capitalize.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS 7 | Capitalizes the first letter of a string or array of strings. 8 | Requires either a single string or an array as an input. 9 | EOS 10 | ) do |arguments| 11 | 12 | raise(Puppet::ParseError, "capitalize(): Wrong number of arguments " + 13 | "given (#{arguments.size} for 1)") if arguments.size < 1 14 | 15 | value = arguments[0] 16 | klass = value.class 17 | 18 | unless [Array, String].include?(klass) 19 | raise(Puppet::ParseError, 'capitalize(): Requires either ' + 20 | 'array or string to work with') 21 | end 22 | 23 | if value.is_a?(Array) 24 | # Numbers in Puppet are often string-encoded which is troublesome ... 25 | result = value.collect { |i| i.is_a?(String) ? i.capitalize : i } 26 | else 27 | result = value.capitalize 28 | end 29 | 30 | return result 31 | end 32 | end 33 | 34 | # vim: set ts=2 sw=2 et : 35 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/chomp.rb: -------------------------------------------------------------------------------- 1 | # 2 | # chomp.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:chomp, :type => :rvalue, :doc => <<-'EOS' 7 | Removes the record separator from the end of a string or an array of 8 | strings, for example `hello\n` becomes `hello`. 9 | Requires a single string or array as an input. 10 | EOS 11 | ) do |arguments| 12 | 13 | raise(Puppet::ParseError, "chomp(): Wrong number of arguments " + 14 | "given (#{arguments.size} for 1)") if arguments.size < 1 15 | 16 | value = arguments[0] 17 | klass = value.class 18 | 19 | unless [Array, String].include?(klass) 20 | raise(Puppet::ParseError, 'chomp(): Requires either ' + 21 | 'array or string to work with') 22 | end 23 | 24 | if value.is_a?(Array) 25 | # Numbers in Puppet are often string-encoded which is troublesome ... 26 | result = value.collect { |i| i.is_a?(String) ? i.chomp : i } 27 | else 28 | result = value.chomp 29 | end 30 | 31 | return result 32 | end 33 | end 34 | 35 | # vim: set ts=2 sw=2 et : 36 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/chop.rb: -------------------------------------------------------------------------------- 1 | # 2 | # chop.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:chop, :type => :rvalue, :doc => <<-'EOS' 7 | Returns a new string with the last character removed. If the string ends 8 | with `\r\n`, both characters are removed. Applying chop to an empty 9 | string returns an empty string. If you wish to merely remove record 10 | separators then you should use the `chomp` function. 11 | Requires a string or array of strings as input. 12 | EOS 13 | ) do |arguments| 14 | 15 | raise(Puppet::ParseError, "chop(): Wrong number of arguments " + 16 | "given (#{arguments.size} for 1)") if arguments.size < 1 17 | 18 | value = arguments[0] 19 | klass = value.class 20 | 21 | unless [Array, String].include?(klass) 22 | raise(Puppet::ParseError, 'chop(): Requires either an ' + 23 | 'array or string to work with') 24 | end 25 | 26 | if value.is_a?(Array) 27 | # Numbers in Puppet are often string-encoded which is troublesome ... 28 | result = value.collect { |i| i.is_a?(String) ? i.chop : i } 29 | else 30 | result = value.chop 31 | end 32 | 33 | return result 34 | end 35 | end 36 | 37 | # vim: set ts=2 sw=2 et : 38 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/defined_with_params.rb: -------------------------------------------------------------------------------- 1 | # Test whether a given class or definition is defined 2 | require 'puppet/parser/functions' 3 | 4 | Puppet::Parser::Functions.newfunction(:defined_with_params, 5 | :type => :rvalue, 6 | :doc => <<-'ENDOFDOC' 7 | Takes a resource reference and an optional hash of attributes. 8 | 9 | Returns true if a resource with the specified attributes has already been added 10 | to the catalog, and false otherwise. 11 | 12 | user { 'dan': 13 | ensure => present, 14 | } 15 | 16 | if ! defined_with_params(User[dan], {'ensure' => 'present' }) { 17 | user { 'dan': ensure => present, } 18 | } 19 | ENDOFDOC 20 | ) do |vals| 21 | reference, params = vals 22 | raise(ArgumentError, 'Must specify a reference') unless reference 23 | if (! params) || params == '' 24 | params = {} 25 | end 26 | ret = false 27 | if resource = findresource(reference.to_s) 28 | matches = params.collect do |key, value| 29 | resource[key] == value 30 | end 31 | ret = params.empty? || !matches.include?(false) 32 | end 33 | Puppet.debug("Resource #{reference} was not determined to be defined") 34 | ret 35 | end 36 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/delete.rb: -------------------------------------------------------------------------------- 1 | # 2 | # delete.rb 3 | # 4 | 5 | # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... 6 | 7 | module Puppet::Parser::Functions 8 | newfunction(:delete, :type => :rvalue, :doc => <<-EOS 9 | Deletes all instances of a given element from an array, substring from a 10 | string, or key from a hash. 11 | 12 | *Examples:* 13 | 14 | delete(['a','b','c','b'], 'b') 15 | Would return: ['a','c'] 16 | 17 | delete({'a'=>1,'b'=>2,'c'=>3}, 'b') 18 | Would return: {'a'=>1,'c'=>3} 19 | 20 | delete('abracadabra', 'bra') 21 | Would return: 'acada' 22 | EOS 23 | ) do |arguments| 24 | 25 | if (arguments.size != 2) then 26 | raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ 27 | "given #{arguments.size} for 2.") 28 | end 29 | 30 | collection = arguments[0] 31 | item = arguments[1] 32 | 33 | case collection 34 | when Array, Hash 35 | collection.delete item 36 | when String 37 | collection.gsub! item, '' 38 | else 39 | raise(TypeError, "delete(): First argument must be an Array, " + 40 | "String, or Hash. Given an argument of class #{collection.class}.") 41 | end 42 | collection 43 | end 44 | end 45 | 46 | # vim: set ts=2 sw=2 et : 47 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/delete_at.rb: -------------------------------------------------------------------------------- 1 | # 2 | # delete_at.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS 7 | Deletes a determined indexed value from an array. 8 | 9 | *Examples:* 10 | 11 | delete_at(['a','b','c'], 1) 12 | 13 | Would return: ['a','c'] 14 | EOS 15 | ) do |arguments| 16 | 17 | raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " + 18 | "given (#{arguments.size} for 2)") if arguments.size < 2 19 | 20 | array = arguments[0] 21 | 22 | unless array.is_a?(Array) 23 | raise(Puppet::ParseError, 'delete_at(): Requires array to work with') 24 | end 25 | 26 | index = arguments[1] 27 | 28 | if index.is_a?(String) and not index.match(/^\d+$/) 29 | raise(Puppet::ParseError, 'delete_at(): You must provide ' + 30 | 'non-negative numeric index') 31 | end 32 | 33 | result = array.clone 34 | 35 | # Numbers in Puppet are often string-encoded which is troublesome ... 36 | index = index.to_i 37 | 38 | if index > result.size - 1 # First element is at index 0 is it not? 39 | raise(Puppet::ParseError, 'delete_at(): Given index ' + 40 | 'exceeds size of array given') 41 | end 42 | 43 | result.delete_at(index) # We ignore the element that got deleted ... 44 | 45 | return result 46 | end 47 | end 48 | 49 | # vim: set ts=2 sw=2 et : 50 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/downcase.rb: -------------------------------------------------------------------------------- 1 | # 2 | # downcase.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:downcase, :type => :rvalue, :doc => <<-EOS 7 | Converts the case of a string or all strings in an array to lower case. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "downcase(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | value = arguments[0] 15 | klass = value.class 16 | 17 | unless [Array, String].include?(klass) 18 | raise(Puppet::ParseError, 'downcase(): Requires either ' + 19 | 'array or string to work with') 20 | end 21 | 22 | if value.is_a?(Array) 23 | # Numbers in Puppet are often string-encoded which is troublesome ... 24 | result = value.collect { |i| i.is_a?(String) ? i.downcase : i } 25 | else 26 | result = value.downcase 27 | end 28 | 29 | return result 30 | end 31 | end 32 | 33 | # vim: set ts=2 sw=2 et : 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/empty.rb: -------------------------------------------------------------------------------- 1 | # 2 | # empty.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:empty, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the variable is empty. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "empty(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | value = arguments[0] 15 | klass = value.class 16 | 17 | unless [Array, Hash, String].include?(klass) 18 | raise(Puppet::ParseError, 'empty(): Requires either ' + 19 | 'array, hash or string to work with') 20 | end 21 | 22 | result = value.empty? 23 | 24 | return result 25 | end 26 | end 27 | 28 | # vim: set ts=2 sw=2 et : 29 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/ensure_packages.rb: -------------------------------------------------------------------------------- 1 | # 2 | # ensure_packages.rb 3 | # 4 | require 'puppet/parser/functions' 5 | 6 | module Puppet::Parser::Functions 7 | newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS 8 | Takes a list of packages and only installs them if they don't already exist. 9 | EOS 10 | ) do |arguments| 11 | 12 | raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " + 13 | "given (#{arguments.size} for 1)") if arguments.size != 1 14 | raise(Puppet::ParseError, "ensure_packages(): Requires array " + 15 | "given (#{arguments[0].class})") if !arguments[0].kind_of?(Array) 16 | 17 | Puppet::Parser::Functions.function(:ensure_resource) 18 | arguments[0].each { |package_name| 19 | function_ensure_resource(['package', package_name, {'ensure' => 'present' } ]) 20 | } 21 | end 22 | end 23 | 24 | # vim: set ts=2 sw=2 et : 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/ensure_resource.rb: -------------------------------------------------------------------------------- 1 | # Test whether a given class or definition is defined 2 | require 'puppet/parser/functions' 3 | 4 | Puppet::Parser::Functions.newfunction(:ensure_resource, 5 | :type => :statement, 6 | :doc => <<-'ENDOFDOC' 7 | Takes a resource type, title, and a list of attributes that describe a 8 | resource. 9 | 10 | user { 'dan': 11 | ensure => present, 12 | } 13 | 14 | This example only creates the resource if it does not already exist: 15 | 16 | ensure_resource('user, 'dan', {'ensure' => 'present' }) 17 | 18 | If the resource already exists but does not match the specified parameters, 19 | this function will attempt to recreate the resource leading to a duplicate 20 | resource definition error. 21 | 22 | ENDOFDOC 23 | ) do |vals| 24 | type, title, params = vals 25 | raise(ArgumentError, 'Must specify a type') unless type 26 | raise(ArgumentError, 'Must specify a title') unless title 27 | params ||= {} 28 | Puppet::Parser::Functions.function(:defined_with_params) 29 | if function_defined_with_params(["#{type}[#{title}]", params]) 30 | Puppet.debug("Resource #{type}[#{title}] not created b/c it already exists") 31 | else 32 | Puppet::Parser::Functions.function(:create_resources) 33 | function_create_resources([type.capitalize, { title => params }]) 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/flatten.rb: -------------------------------------------------------------------------------- 1 | # 2 | # flatten.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:flatten, :type => :rvalue, :doc => <<-EOS 7 | This function flattens any deeply nested arrays and returns a single flat array 8 | as a result. 9 | 10 | *Examples:* 11 | 12 | flatten(['a', ['b', ['c']]]) 13 | 14 | Would return: ['a','b','c'] 15 | EOS 16 | ) do |arguments| 17 | 18 | raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + 19 | "given (#{arguments.size} for 1)") if arguments.size < 1 20 | 21 | array = arguments[0] 22 | 23 | unless array.is_a?(Array) 24 | raise(Puppet::ParseError, 'flatten(): Requires array to work with') 25 | end 26 | 27 | result = array.flatten 28 | 29 | return result 30 | end 31 | end 32 | 33 | # vim: set ts=2 sw=2 et : 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/fqdn_rotate.rb: -------------------------------------------------------------------------------- 1 | # 2 | # fqdn_rotate.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:fqdn_rotate, :type => :rvalue, :doc => <<-EOS 7 | Rotates an array a random number of times based on a nodes fqdn. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | value = arguments[0] 15 | klass = value.class 16 | require 'digest/md5' 17 | 18 | unless [Array, String].include?(klass) 19 | raise(Puppet::ParseError, 'fqdn_rotate(): Requires either ' + 20 | 'array or string to work with') 21 | end 22 | 23 | result = value.clone 24 | 25 | string = value.is_a?(String) ? true : false 26 | 27 | # Check whether it makes sense to rotate ... 28 | return result if result.size <= 1 29 | 30 | # We turn any string value into an array to be able to rotate ... 31 | result = string ? result.split('') : result 32 | 33 | elements = result.size 34 | 35 | srand(Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex) 36 | rand(elements).times { 37 | result.push result.shift 38 | } 39 | 40 | result = string ? result.join : result 41 | 42 | return result 43 | end 44 | end 45 | 46 | # vim: set ts=2 sw=2 et : 47 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/get_module_path.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:get_module_path, :type =>:rvalue, :doc => <<-EOT 3 | Returns the absolute path of the specified module for the current 4 | environment. 5 | 6 | Example: 7 | $module_path = get_module_path('stdlib') 8 | EOT 9 | ) do |args| 10 | raise(Puppet::ParseError, "get_module_path(): Wrong number of arguments, expects one") unless args.size == 1 11 | if module_path = Puppet::Module.find(args[0], compiler.environment.to_s) 12 | module_path.path 13 | else 14 | raise(Puppet::ParseError, "Could not find module #{args[0]} in environment #{compiler.environment}") 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/getvar.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:getvar, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| 4 | Lookup a variable in a remote namespace. 5 | 6 | For example: 7 | 8 | $foo = getvar('site::data::foo') 9 | # Equivalent to $foo = $site::data::foo 10 | 11 | This is useful if the namespace itself is stored in a string: 12 | 13 | $datalocation = 'site::data' 14 | $bar = getvar("${datalocation}::bar") 15 | # Equivalent to $bar = $site::data::bar 16 | ENDHEREDOC 17 | 18 | unless args.length == 1 19 | raise Puppet::ParseError, ("getvar(): wrong number of arguments (#{args.length}; must be 1)") 20 | end 21 | 22 | self.lookupvar("#{args[0]}") 23 | 24 | end 25 | 26 | end 27 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/grep.rb: -------------------------------------------------------------------------------- 1 | # 2 | # grep.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:grep, :type => :rvalue, :doc => <<-EOS 7 | This function searches through an array and returns any elements that match 8 | the provided regular expression. 9 | 10 | *Examples:* 11 | 12 | grep(['aaa','bbb','ccc','aaaddd'], 'aaa') 13 | 14 | Would return: 15 | 16 | ['aaa','aaaddd'] 17 | EOS 18 | ) do |arguments| 19 | 20 | if (arguments.size != 2) then 21 | raise(Puppet::ParseError, "grep(): Wrong number of arguments "+ 22 | "given #{arguments.size} for 2") 23 | end 24 | 25 | a = arguments[0] 26 | pattern = Regexp.new(arguments[1]) 27 | 28 | a.grep(pattern) 29 | 30 | end 31 | end 32 | 33 | # vim: set ts=2 sw=2 et : 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/has_interface_with.rb: -------------------------------------------------------------------------------- 1 | # 2 | # has_interface_with 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:has_interface_with, :type => :rvalue, :doc => <<-EOS 7 | Returns boolean based on kind and value: 8 | * macaddress 9 | * netmask 10 | * ipaddress 11 | * network 12 | 13 | has_interface_with("macaddress", "x:x:x:x:x:x") 14 | has_interface_with("ipaddress", "127.0.0.1") => true 15 | etc. 16 | 17 | If no "kind" is given, then the presence of the interface is checked: 18 | has_interface_with("lo") => true 19 | EOS 20 | ) do |args| 21 | 22 | raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments " + 23 | "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2 24 | 25 | interfaces = lookupvar('interfaces') 26 | 27 | # If we do not have any interfaces, then there are no requested attributes 28 | return false if (interfaces == :undefined) 29 | 30 | interfaces = interfaces.split(',') 31 | 32 | if args.size == 1 33 | return interfaces.member?(args[0]) 34 | end 35 | 36 | kind, value = args 37 | 38 | if lookupvar(kind) == value 39 | return true 40 | end 41 | 42 | result = false 43 | interfaces.each do |iface| 44 | if value == lookupvar("#{kind}_#{iface}") 45 | result = true 46 | break 47 | end 48 | end 49 | 50 | result 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/has_ip_address.rb: -------------------------------------------------------------------------------- 1 | # 2 | # has_ip_address 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:has_ip_address, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the client has the requested IP address on some interface. 8 | 9 | This function iterates through the 'interfaces' fact and checks the 10 | 'ipaddress_IFACE' facts, performing a simple string comparison. 11 | EOS 12 | ) do |args| 13 | 14 | raise(Puppet::ParseError, "has_ip_address(): Wrong number of arguments " + 15 | "given (#{args.size} for 1)") if args.size != 1 16 | 17 | Puppet::Parser::Functions.autoloader.load(:has_interface_with) \ 18 | unless Puppet::Parser::Functions.autoloader.loaded?(:has_interface_with) 19 | 20 | function_has_interface_with(['ipaddress', args[0]]) 21 | 22 | end 23 | end 24 | 25 | # vim:sts=2 sw=2 26 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/has_ip_network.rb: -------------------------------------------------------------------------------- 1 | # 2 | # has_ip_network 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:has_ip_network, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the client has an IP address within the requested network. 8 | 9 | This function iterates through the 'interfaces' fact and checks the 10 | 'network_IFACE' facts, performing a simple string comparision. 11 | EOS 12 | ) do |args| 13 | 14 | raise(Puppet::ParseError, "has_ip_network(): Wrong number of arguments " + 15 | "given (#{args.size} for 1)") if args.size != 1 16 | 17 | Puppet::Parser::Functions.autoloader.load(:has_interface_with) \ 18 | unless Puppet::Parser::Functions.autoloader.loaded?(:has_interface_with) 19 | 20 | function_has_interface_with(['network', args[0]]) 21 | 22 | end 23 | end 24 | 25 | # vim:sts=2 sw=2 26 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/has_key.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:has_key, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| 4 | Determine if a hash has a certain key value. 5 | 6 | Example: 7 | 8 | $my_hash = {'key_one' => 'value_one'} 9 | if has_key($my_hash, 'key_two') { 10 | notice('we will not reach here') 11 | } 12 | if has_key($my_hash, 'key_one') { 13 | notice('this will be printed') 14 | } 15 | 16 | ENDHEREDOC 17 | 18 | unless args.length == 2 19 | raise Puppet::ParseError, ("has_key(): wrong number of arguments (#{args.length}; must be 2)") 20 | end 21 | unless args[0].is_a?(Hash) 22 | raise Puppet::ParseError, "has_key(): expects the first argument to be a hash, got #{args[0].inspect} which is of type #{args[0].class}" 23 | end 24 | args[0].has_key?(args[1]) 25 | 26 | end 27 | 28 | end 29 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/hash.rb: -------------------------------------------------------------------------------- 1 | # 2 | # hash.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:hash, :type => :rvalue, :doc => <<-EOS 7 | This function converts and array into a hash. 8 | 9 | *Examples:* 10 | 11 | hash(['a',1,'b',2,'c',3]) 12 | 13 | Would return: {'a'=>1,'b'=>2,'c'=>3} 14 | EOS 15 | ) do |arguments| 16 | 17 | raise(Puppet::ParseError, "hash(): Wrong number of arguments " + 18 | "given (#{arguments.size} for 1)") if arguments.size < 1 19 | 20 | array = arguments[0] 21 | 22 | unless array.is_a?(Array) 23 | raise(Puppet::ParseError, 'hash(): Requires array to work with') 24 | end 25 | 26 | result = {} 27 | 28 | begin 29 | # This is to make it compatible with older version of Ruby ... 30 | array = array.flatten 31 | result = Hash[*array] 32 | rescue Exception 33 | raise(Puppet::ParseError, 'hash(): Unable to compute ' + 34 | 'hash from array given') 35 | end 36 | 37 | return result 38 | end 39 | end 40 | 41 | # vim: set ts=2 sw=2 et : 42 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_array.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_array.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_array, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the variable passed to this function is an array. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "is_array(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | type = arguments[0] 15 | 16 | result = type.is_a?(Array) 17 | 18 | return result 19 | end 20 | end 21 | 22 | # vim: set ts=2 sw=2 et : 23 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_domain_name.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_domain_name.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_domain_name, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the string passed to this function is a syntactically correct domain name. 8 | EOS 9 | ) do |arguments| 10 | 11 | if (arguments.size != 1) then 12 | raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments "+ 13 | "given #{arguments.size} for 1") 14 | end 15 | 16 | domain = arguments[0] 17 | 18 | # Limits (rfc1035, 3.1) 19 | domain_max_length=255 20 | label_min_length=1 21 | label_max_length=63 22 | 23 | # Allow ".", it is the top level domain 24 | return true if domain == '.' 25 | 26 | # Remove the final dot, if present. 27 | domain.chomp!('.') 28 | 29 | # Check the whole domain 30 | return false if domain.empty? 31 | return false if domain.length > domain_max_length 32 | 33 | # Check each label in the domain 34 | labels = domain.split('.') 35 | vlabels = labels.each do |label| 36 | break if label.length < label_min_length 37 | break if label.length > label_max_length 38 | break if label[-1..-1] == '-' 39 | break if label[0..0] == '-' 40 | break unless /^[a-z\d-]+$/i.match(label) 41 | end 42 | return vlabels == labels 43 | 44 | end 45 | end 46 | 47 | # vim: set ts=2 sw=2 et : 48 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_float.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_float.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_float, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the variable passed to this function is a float. 8 | EOS 9 | ) do |arguments| 10 | 11 | if (arguments.size != 1) then 12 | raise(Puppet::ParseError, "is_float(): Wrong number of arguments "+ 13 | "given #{arguments.size} for 1") 14 | end 15 | 16 | value = arguments[0] 17 | 18 | if value != value.to_f.to_s then 19 | return false 20 | else 21 | return true 22 | end 23 | 24 | end 25 | end 26 | 27 | # vim: set ts=2 sw=2 et : 28 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_hash.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_hash.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_hash, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the variable passed to this function is a hash. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size != 1 13 | 14 | type = arguments[0] 15 | 16 | result = type.is_a?(Hash) 17 | 18 | return result 19 | end 20 | end 21 | 22 | # vim: set ts=2 sw=2 et : 23 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_integer.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_integer.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the variable returned to this string is an integer. 8 | EOS 9 | ) do |arguments| 10 | 11 | if (arguments.size != 1) then 12 | raise(Puppet::ParseError, "is_integer(): Wrong number of arguments "+ 13 | "given #{arguments.size} for 1") 14 | end 15 | 16 | value = arguments[0] 17 | 18 | if value != value.to_i.to_s then 19 | return false 20 | else 21 | return true 22 | end 23 | 24 | end 25 | end 26 | 27 | # vim: set ts=2 sw=2 et : 28 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_ip_address.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_ip_address.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_ip_address, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the string passed to this function is a valid IP address. 8 | EOS 9 | ) do |arguments| 10 | 11 | require 'ipaddr' 12 | 13 | if (arguments.size != 1) then 14 | raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments "+ 15 | "given #{arguments.size} for 1") 16 | end 17 | 18 | begin 19 | ip = IPAddr.new(arguments[0]) 20 | rescue ArgumentError 21 | return false 22 | end 23 | 24 | if ip.ipv4? or ip.ipv6? then 25 | return true 26 | else 27 | return false 28 | end 29 | end 30 | end 31 | 32 | # vim: set ts=2 sw=2 et : 33 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_mac_address.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_mac_address.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_mac_address, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the string passed to this function is a valid mac address. 8 | EOS 9 | ) do |arguments| 10 | 11 | if (arguments.size != 1) then 12 | raise(Puppet::ParseError, "is_mac_address(): Wrong number of arguments "+ 13 | "given #{arguments.size} for 1") 14 | end 15 | 16 | mac = arguments[0] 17 | 18 | if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then 19 | return true 20 | else 21 | return false 22 | end 23 | 24 | end 25 | end 26 | 27 | # vim: set ts=2 sw=2 et : 28 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_numeric.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_numeric.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the variable passed to this function is a number. 8 | EOS 9 | ) do |arguments| 10 | 11 | if (arguments.size != 1) then 12 | raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments "+ 13 | "given #{arguments.size} for 1") 14 | end 15 | 16 | value = arguments[0] 17 | 18 | if value == value.to_f.to_s or value == value.to_i.to_s then 19 | return true 20 | else 21 | return false 22 | end 23 | 24 | end 25 | end 26 | 27 | # vim: set ts=2 sw=2 et : 28 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/is_string.rb: -------------------------------------------------------------------------------- 1 | # 2 | # is_string.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:is_string, :type => :rvalue, :doc => <<-EOS 7 | Returns true if the variable passed to this function is a string. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "is_string(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | type = arguments[0] 15 | 16 | result = type.is_a?(String) 17 | 18 | if result and (type == type.to_f.to_s or type == type.to_i.to_s) then 19 | return false 20 | end 21 | 22 | return result 23 | end 24 | end 25 | 26 | # vim: set ts=2 sw=2 et : 27 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/join.rb: -------------------------------------------------------------------------------- 1 | # 2 | # join.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:join, :type => :rvalue, :doc => <<-EOS 7 | This function joins an array into a string using a seperator. 8 | 9 | *Examples:* 10 | 11 | join(['a','b','c'], ",") 12 | 13 | Would result in: "a,b,c" 14 | EOS 15 | ) do |arguments| 16 | 17 | # Technically we support two arguments but only first is mandatory ... 18 | raise(Puppet::ParseError, "join(): Wrong number of arguments " + 19 | "given (#{arguments.size} for 1)") if arguments.size < 1 20 | 21 | array = arguments[0] 22 | 23 | unless array.is_a?(Array) 24 | raise(Puppet::ParseError, 'join(): Requires array to work with') 25 | end 26 | 27 | suffix = arguments[1] if arguments[1] 28 | 29 | if suffix 30 | unless suffix.is_a?(String) 31 | raise(Puppet::ParseError, 'join(): Requires string to work with') 32 | end 33 | end 34 | 35 | result = suffix ? array.join(suffix) : array.join 36 | 37 | return result 38 | end 39 | end 40 | 41 | # vim: set ts=2 sw=2 et : 42 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/join_keys_to_values.rb: -------------------------------------------------------------------------------- 1 | # 2 | # join.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:join_keys_to_values, :type => :rvalue, :doc => <<-EOS 7 | This function joins each key of a hash to that key's corresponding value with a 8 | separator. Keys and values are cast to strings. The return value is an array in 9 | which each element is one joined key/value pair. 10 | 11 | *Examples:* 12 | 13 | join_keys_to_values({'a'=>1,'b'=>2}, " is ") 14 | 15 | Would result in: ["a is 1","b is 2"] 16 | EOS 17 | ) do |arguments| 18 | 19 | # Validate the number of arguments. 20 | if arguments.size != 2 21 | raise(Puppet::ParseError, "join_keys_to_values(): Takes exactly two " + 22 | "arguments, but #{arguments.size} given.") 23 | end 24 | 25 | # Validate the first argument. 26 | hash = arguments[0] 27 | if not hash.is_a?(Hash) 28 | raise(TypeError, "join_keys_to_values(): The first argument must be a " + 29 | "hash, but a #{hash.class} was given.") 30 | end 31 | 32 | # Validate the second argument. 33 | separator = arguments[1] 34 | if not separator.is_a?(String) 35 | raise(TypeError, "join_keys_to_values(): The second argument must be a " + 36 | "string, but a #{separator.class} was given.") 37 | end 38 | 39 | # Join the keys to their values. 40 | hash.map do |k,v| 41 | String(k) + separator + String(v) 42 | end 43 | 44 | end 45 | end 46 | 47 | # vim: set ts=2 sw=2 et : 48 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/keys.rb: -------------------------------------------------------------------------------- 1 | # 2 | # keys.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:keys, :type => :rvalue, :doc => <<-EOS 7 | Returns the keys of a hash as an array. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "keys(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | hash = arguments[0] 15 | 16 | unless hash.is_a?(Hash) 17 | raise(Puppet::ParseError, 'keys(): Requires hash to work with') 18 | end 19 | 20 | result = hash.keys 21 | 22 | return result 23 | end 24 | end 25 | 26 | # vim: set ts=2 sw=2 et : 27 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:loadyaml, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| 4 | Load a YAML file containing an array, string, or hash, and return the data 5 | in the corresponding native data type. 6 | 7 | For example: 8 | 9 | $myhash = loadyaml('/etc/puppet/data/myhash.yaml') 10 | ENDHEREDOC 11 | 12 | unless args.length == 1 13 | raise Puppet::ParseError, ("loadyaml(): wrong number of arguments (#{args.length}; must be 1)") 14 | end 15 | 16 | YAML.load_file(args[0]) 17 | 18 | end 19 | 20 | end 21 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/lstrip.rb: -------------------------------------------------------------------------------- 1 | # 2 | # lstrip.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS 7 | Strips leading spaces to the left of a string. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "lstrip(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | value = arguments[0] 15 | klass = value.class 16 | 17 | unless [Array, String].include?(klass) 18 | raise(Puppet::ParseError, 'lstrip(): Requires either ' + 19 | 'array or string to work with') 20 | end 21 | 22 | if value.is_a?(Array) 23 | # Numbers in Puppet are often string-encoded which is troublesome ... 24 | result = value.collect { |i| i.is_a?(String) ? i.lstrip : i } 25 | else 26 | result = value.lstrip 27 | end 28 | 29 | return result 30 | end 31 | end 32 | 33 | # vim: set ts=2 sw=2 et : 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/max.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:max, :type => :rvalue, :doc => <<-EOS 3 | Returns the highest value of all arguments. 4 | Requires at least one argument. 5 | EOS 6 | ) do |args| 7 | 8 | raise(Puppet::ParseError, "max(): Wrong number of arguments " + 9 | "need at least one") if args.size == 0 10 | 11 | return args.max 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/member.rb: -------------------------------------------------------------------------------- 1 | # 2 | # member.rb 3 | # 4 | 5 | # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... 6 | # TODO(Krzysztof Wilczynski): Support for strings and hashes too ... 7 | 8 | module Puppet::Parser::Functions 9 | newfunction(:member, :type => :rvalue, :doc => <<-EOS 10 | This function determines if a variable is a member of an array. 11 | 12 | *Examples:* 13 | 14 | member(['a','b'], 'b') 15 | 16 | Would return: true 17 | 18 | member(['a','b'], 'c') 19 | 20 | Would return: false 21 | EOS 22 | ) do |arguments| 23 | 24 | raise(Puppet::ParseError, "member(): Wrong number of arguments " + 25 | "given (#{arguments.size} for 2)") if arguments.size < 2 26 | 27 | array = arguments[0] 28 | 29 | unless array.is_a?(Array) 30 | raise(Puppet::ParseError, 'member(): Requires array to work with') 31 | end 32 | 33 | item = arguments[1] 34 | 35 | raise(Puppet::ParseError, 'member(): You must provide item ' + 36 | 'to search for within array given') if item.empty? 37 | 38 | result = array.include?(item) 39 | 40 | return result 41 | end 42 | end 43 | 44 | # vim: set ts=2 sw=2 et : 45 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/merge.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| 3 | Merges two or more hashes together and returns the resulting hash. 4 | 5 | For example: 6 | 7 | $hash1 = {'one' => 1, 'two', => 2} 8 | $hash2 = {'two' => 'dos', 'three', => 'tres'} 9 | $merged_hash = merge($hash1, $hash2) 10 | # The resulting hash is equivalent to: 11 | # $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'} 12 | 13 | When there is a duplicate key, the key in the rightmost hash will "win." 14 | 15 | ENDHEREDOC 16 | 17 | if args.length < 2 18 | raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)") 19 | end 20 | 21 | # The hash we accumulate into 22 | accumulator = Hash.new 23 | # Merge into the accumulator hash 24 | args.each do |arg| 25 | unless arg.is_a?(Hash) 26 | raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" 27 | end 28 | accumulator.merge!(arg) 29 | end 30 | # Return the fully merged hash 31 | accumulator 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/min.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:min, :type => :rvalue, :doc => <<-EOS 3 | Returns the lowest value of all arguments. 4 | Requires at least one argument. 5 | EOS 6 | ) do |args| 7 | 8 | raise(Puppet::ParseError, "min(): Wrong number of arguments " + 9 | "need at least one") if args.size == 0 10 | 11 | return args.min 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/num2bool.rb: -------------------------------------------------------------------------------- 1 | # 2 | # num2bool.rb 3 | # 4 | 5 | # TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... 6 | 7 | module Puppet::Parser::Functions 8 | newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS 9 | This function converts a number into a true boolean. Zero becomes false. Numbers 10 | higher then 0 become true. 11 | EOS 12 | ) do |arguments| 13 | 14 | raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + 15 | "given (#{arguments.size} for 1)") if arguments.size < 1 16 | 17 | number = arguments[0] 18 | 19 | # Only numbers allowed ... 20 | unless number.match(/^\-?\d+$/) 21 | raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') 22 | end 23 | 24 | result = case number 25 | when /^0$/ 26 | false 27 | when /^\-?\d+$/ 28 | # Numbers in Puppet are often string-encoded which is troublesome ... 29 | number = number.to_i 30 | # We yield true for any positive number and false otherwise ... 31 | number > 0 ? true : false 32 | else 33 | raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given') 34 | end 35 | 36 | return result 37 | end 38 | end 39 | 40 | # vim: set ts=2 sw=2 et : 41 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/parsejson.rb: -------------------------------------------------------------------------------- 1 | # 2 | # parsejson.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS 7 | This function accepts JSON as a string and converts into the correct Puppet 8 | structure. 9 | EOS 10 | ) do |arguments| 11 | 12 | if (arguments.size != 1) then 13 | raise(Puppet::ParseError, "parsejson(): Wrong number of arguments "+ 14 | "given #{arguments.size} for 1") 15 | end 16 | 17 | json = arguments[0] 18 | 19 | # PSON is natively available in puppet 20 | PSON.load(json) 21 | end 22 | end 23 | 24 | # vim: set ts=2 sw=2 et : 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/parseyaml.rb: -------------------------------------------------------------------------------- 1 | # 2 | # parseyaml.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS 7 | This function accepts YAML as a string and converts it into the correct 8 | Puppet structure. 9 | EOS 10 | ) do |arguments| 11 | 12 | if (arguments.size != 1) then 13 | raise(Puppet::ParseError, "parseyaml(): Wrong number of arguments "+ 14 | "given #{arguments.size} for 1") 15 | end 16 | 17 | require 'yaml' 18 | 19 | YAML::load(arguments[0]) 20 | 21 | end 22 | end 23 | 24 | # vim: set ts=2 sw=2 et : 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/pick.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:pick, :type => :rvalue, :doc => <<-EOS 3 | 4 | This function is similar to a coalesce function in SQL in that it will return 5 | the first value in a list of values that is not undefined or an empty string 6 | (two things in Puppet that will return a boolean false value). Typically, 7 | this function is used to check for a value in the Puppet Dashboard/Enterprise 8 | Console, and failover to a default value like the following: 9 | 10 | $real_jenkins_version = pick($::jenkins_version, '1.449') 11 | 12 | The value of $real_jenkins_version will first look for a top-scope variable 13 | called 'jenkins_version' (note that parameters set in the Puppet Dashboard/ 14 | Enterprise Console are brought into Puppet as top-scope variables), and, 15 | failing that, will use a default value of 1.449. 16 | 17 | EOS 18 | ) do |args| 19 | args = args.compact 20 | args.delete(:undef) 21 | args.delete(:undefined) 22 | args.delete("") 23 | if args[0].to_s.empty? then 24 | fail "Must provide non empty value." 25 | else 26 | return args[0] 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/prefix.rb: -------------------------------------------------------------------------------- 1 | # 2 | # prefix.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:prefix, :type => :rvalue, :doc => <<-EOS 7 | This function applies a prefix to all elements in an array. 8 | 9 | *Examles:* 10 | 11 | prefix(['a','b','c'], 'p') 12 | 13 | Will return: ['pa','pb','pc'] 14 | EOS 15 | ) do |arguments| 16 | 17 | # Technically we support two arguments but only first is mandatory ... 18 | raise(Puppet::ParseError, "prefix(): Wrong number of arguments " + 19 | "given (#{arguments.size} for 1)") if arguments.size < 1 20 | 21 | array = arguments[0] 22 | 23 | unless array.is_a?(Array) 24 | raise(Puppet::ParseError, 'prefix(): Requires array to work with') 25 | end 26 | 27 | prefix = arguments[1] if arguments[1] 28 | 29 | if prefix 30 | unless prefix.is_a?(String) 31 | raise(Puppet::ParseError, 'prefix(): Requires string to work with') 32 | end 33 | end 34 | 35 | # Turn everything into string same as join would do ... 36 | result = array.collect do |i| 37 | i = i.to_s 38 | prefix ? prefix + i : i 39 | end 40 | 41 | return result 42 | end 43 | end 44 | 45 | # vim: set ts=2 sw=2 et : 46 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/range.rb: -------------------------------------------------------------------------------- 1 | # 2 | # range.rb 3 | # 4 | 5 | # TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... 6 | 7 | module Puppet::Parser::Functions 8 | newfunction(:range, :type => :rvalue, :doc => <<-EOS 9 | When given range in the form of (start, stop) it will extrapolate a range as 10 | an array. 11 | 12 | *Examples:* 13 | 14 | range("0", "9") 15 | 16 | Will return: [0,1,2,3,4,5,6,7,8,9] 17 | 18 | range("00", "09") 19 | 20 | Will return: [0,1,2,3,4,5,6,7,8,9] (Zero padded strings are converted to 21 | integers automatically) 22 | 23 | range("a", "c") 24 | 25 | Will return: ["a","b","c"] 26 | 27 | range("host01", "host10") 28 | 29 | Will return: ["host01", "host02", ..., "host09", "host10"] 30 | EOS 31 | ) do |arguments| 32 | 33 | # We support more than one argument but at least one is mandatory ... 34 | raise(Puppet::ParseError, "range(): Wrong number of " + 35 | "arguments given (#{arguments.size} for 1)") if arguments.size < 1 36 | 37 | if arguments.size > 1 38 | start = arguments[0] 39 | stop = arguments[1] 40 | 41 | type = '..' # We select simplest type for Range available in Ruby ... 42 | 43 | elsif arguments.size > 0 44 | value = arguments[0] 45 | 46 | if m = value.match(/^(\w+)(\.\.\.?|\-)(\w+)$/) 47 | start = m[1] 48 | stop = m[3] 49 | 50 | type = m[2] 51 | 52 | elsif value.match(/^.+$/) 53 | raise(Puppet::ParseError, 'range(): Unable to compute range ' + 54 | 'from the value given') 55 | else 56 | raise(Puppet::ParseError, 'range(): Unknown format of range given') 57 | end 58 | end 59 | 60 | # Check whether we have integer value if so then make it so ... 61 | if start.match(/^\d+$/) 62 | start = start.to_i 63 | stop = stop.to_i 64 | else 65 | start = start.to_s 66 | stop = stop.to_s 67 | end 68 | 69 | range = case type 70 | when /^(\.\.|\-)$/ then (start .. stop) 71 | when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... 72 | end 73 | 74 | result = range.collect { |i| i } # Get them all ... Pokemon ... 75 | 76 | return result 77 | end 78 | end 79 | 80 | # vim: set ts=2 sw=2 et : 81 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/reject.rb: -------------------------------------------------------------------------------- 1 | # 2 | # reject.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:reject, :type => :rvalue, :doc => <<-EOS) do |args| 7 | This function searches through an array and rejects all elements that match 8 | the provided regular expression. 9 | 10 | *Examples:* 11 | 12 | reject(['aaa','bbb','ccc','aaaddd'], 'aaa') 13 | 14 | Would return: 15 | 16 | ['bbb','ccc'] 17 | EOS 18 | 19 | if (args.size != 2) 20 | raise Puppet::ParseError, 21 | "reject(): Wrong number of arguments given #{args.size} for 2" 22 | end 23 | 24 | ary = args[0] 25 | pattern = Regexp.new(args[1]) 26 | 27 | ary.reject { |e| e =~ pattern } 28 | end 29 | end 30 | 31 | # vim: set ts=2 sw=2 et : 32 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/reverse.rb: -------------------------------------------------------------------------------- 1 | # 2 | # reverse.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:reverse, :type => :rvalue, :doc => <<-EOS 7 | Reverses the order of a string or array. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "reverse(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | value = arguments[0] 15 | klass = value.class 16 | 17 | unless [Array, String].include?(klass) 18 | raise(Puppet::ParseError, 'reverse(): Requires either ' + 19 | 'array or string to work with') 20 | end 21 | 22 | result = value.reverse 23 | 24 | return result 25 | end 26 | end 27 | 28 | # vim: set ts=2 sw=2 et : 29 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/rstrip.rb: -------------------------------------------------------------------------------- 1 | # 2 | # rstrip.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS 7 | Strips leading spaces to the right of the string. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "rstrip(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | value = arguments[0] 15 | klass = value.class 16 | 17 | unless [Array, String].include?(klass) 18 | raise(Puppet::ParseError, 'rstrip(): Requires either ' + 19 | 'array or string to work with') 20 | end 21 | 22 | if value.is_a?(Array) 23 | result = value.collect { |i| i.is_a?(String) ? i.rstrip : i } 24 | else 25 | result = value.rstrip 26 | end 27 | 28 | return result 29 | end 30 | end 31 | 32 | # vim: set ts=2 sw=2 et : 33 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/shuffle.rb: -------------------------------------------------------------------------------- 1 | # 2 | # shuffle.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS 7 | Randomizes the order of a string or array elements. 8 | EOS 9 | ) do |arguments| 10 | 11 | raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " + 12 | "given (#{arguments.size} for 1)") if arguments.size < 1 13 | 14 | value = arguments[0] 15 | klass = value.class 16 | 17 | unless [Array, String].include?(klass) 18 | raise(Puppet::ParseError, 'shuffle(): Requires either ' + 19 | 'array or string to work with') 20 | end 21 | 22 | result = value.clone 23 | 24 | string = value.is_a?(String) ? true : false 25 | 26 | # Check whether it makes sense to shuffle ... 27 | return result if result.size <= 1 28 | 29 | # We turn any string value into an array to be able to shuffle ... 30 | result = string ? result.split('') : result 31 | 32 | elements = result.size 33 | 34 | # Simple implementation of Fisher–Yates in-place shuffle ... 35 | elements.times do |i| 36 | j = rand(elements - i) + i 37 | result[j], result[i] = result[i], result[j] 38 | end 39 | 40 | result = string ? result.join : result 41 | 42 | return result 43 | end 44 | end 45 | 46 | # vim: set ts=2 sw=2 et : 47 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/size.rb: -------------------------------------------------------------------------------- 1 | # 2 | # size.rb 3 | # 4 | 5 | # TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... 6 | 7 | module Puppet::Parser::Functions 8 | newfunction(:size, :type => :rvalue, :doc => <<-EOS 9 | Returns the number of elements in a string or array. 10 | EOS 11 | ) do |arguments| 12 | 13 | raise(Puppet::ParseError, "size(): Wrong number of arguments " + 14 | "given (#{arguments.size} for 1)") if arguments.size < 1 15 | 16 | item = arguments[0] 17 | 18 | if item.is_a?(String) 19 | 20 | begin 21 | # 22 | # Check whether your item is a numeric value or not ... 23 | # This will take care about positive and/or negative numbers 24 | # for both integer and floating-point values ... 25 | # 26 | # Please note that Puppet has no notion of hexadecimal 27 | # nor octal numbers for its DSL at this point in time ... 28 | # 29 | Float(item) 30 | 31 | raise(Puppet::ParseError, 'size(): Requires either ' + 32 | 'string or array to work with') 33 | 34 | rescue ArgumentError 35 | result = item.size 36 | end 37 | 38 | elsif item.is_a?(Array) 39 | result = item.size 40 | else 41 | raise(Puppet::ParseError, 'size(): Unknown type given') 42 | end 43 | 44 | return result 45 | end 46 | end 47 | 48 | # vim: set ts=2 sw=2 et : 49 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/sort.rb: -------------------------------------------------------------------------------- 1 | # 2 | # sort.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:sort, :type => :rvalue, :doc => <<-EOS 7 | Sorts strings and arrays lexically. 8 | EOS 9 | ) do |arguments| 10 | 11 | if (arguments.size != 1) then 12 | raise(Puppet::ParseError, "sort(): Wrong number of arguments "+ 13 | "given #{arguments.size} for 1") 14 | end 15 | 16 | value = arguments[0] 17 | 18 | if value.is_a?(Array) then 19 | value.sort 20 | elsif value.is_a?(String) then 21 | value.split("").sort.join("") 22 | end 23 | 24 | end 25 | end 26 | 27 | # vim: set ts=2 sw=2 et : 28 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/squeeze.rb: -------------------------------------------------------------------------------- 1 | # 2 | # squeeze.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS 7 | Returns a new string where runs of the same character that occur in this set are replaced by a single character. 8 | EOS 9 | ) do |arguments| 10 | 11 | if ((arguments.size != 2) and (arguments.size != 1)) then 12 | raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+ 13 | "given #{arguments.size} for 2 or 1") 14 | end 15 | 16 | item = arguments[0] 17 | squeezeval = arguments[1] 18 | 19 | if item.is_a?(Array) then 20 | if squeezeval then 21 | item.collect { |i| i.squeeze(squeezeval) } 22 | else 23 | item.collect { |i| i.squeeze } 24 | end 25 | else 26 | if squeezeval then 27 | item.squeeze(squeezeval) 28 | else 29 | item.squeeze 30 | end 31 | end 32 | 33 | end 34 | end 35 | 36 | # vim: set ts=2 sw=2 et : 37 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/str2bool.rb: -------------------------------------------------------------------------------- 1 | # 2 | # str2bool.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS 7 | This converts a string to a boolean. This attempt to convert strings that 8 | contain things like: y, 1, t, true to 'true' and strings that contain things 9 | like: 0, f, n, false, no to 'false'. 10 | EOS 11 | ) do |arguments| 12 | 13 | raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " + 14 | "given (#{arguments.size} for 1)") if arguments.size < 1 15 | 16 | string = arguments[0] 17 | 18 | unless string.is_a?(String) 19 | raise(Puppet::ParseError, 'str2bool(): Requires either ' + 20 | 'string to work with') 21 | end 22 | 23 | # We consider all the yes, no, y, n and so on too ... 24 | result = case string 25 | # 26 | # This is how undef looks like in Puppet ... 27 | # We yield false in this case. 28 | # 29 | when /^$/, '' then false # Empty string will be false ... 30 | when /^(1|t|y|true|yes)$/ then true 31 | when /^(0|f|n|false|no)$/ then false 32 | when /^(undef|undefined)$/ then false # This is not likely to happen ... 33 | else 34 | raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given') 35 | end 36 | 37 | return result 38 | end 39 | end 40 | 41 | # vim: set ts=2 sw=2 et : 42 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/str2saltedsha512.rb: -------------------------------------------------------------------------------- 1 | # 2 | # str2saltedsha512.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:str2saltedsha512, :type => :rvalue, :doc => <<-EOS 7 | This converts a string to a salted-SHA512 password hash (which is used for 8 | OS X versions >= 10.7). Given any simple string, you will get a hex version 9 | of a salted-SHA512 password hash that can be inserted into your Puppet 10 | manifests as a valid password attribute. 11 | EOS 12 | ) do |arguments| 13 | require 'digest/sha2' 14 | 15 | raise(Puppet::ParseError, "str2saltedsha512(): Wrong number of arguments " + 16 | "passed (#{arguments.size} but we require 1)") if arguments.size != 1 17 | 18 | password = arguments[0] 19 | 20 | unless password.is_a?(String) 21 | raise(Puppet::ParseError, 'str2saltedsha512(): Requires a ' + 22 | "String argument, you passed: #{password.class}") 23 | end 24 | 25 | seedint = rand(2**31 - 1) 26 | seedstring = Array(seedint).pack("L") 27 | saltedpass = Digest::SHA512.digest(seedstring + password) 28 | (seedstring + saltedpass).unpack('H*')[0] 29 | end 30 | end 31 | 32 | # vim: set ts=2 sw=2 et : 33 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/strip.rb: -------------------------------------------------------------------------------- 1 | # 2 | # strip.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:strip, :type => :rvalue, :doc => <<-EOS 7 | This function removes leading and trailing whitespace from a string or from 8 | every string inside an array. 9 | 10 | *Examples:* 11 | 12 | strip(" aaa ") 13 | 14 | Would result in: "aaa" 15 | EOS 16 | ) do |arguments| 17 | 18 | raise(Puppet::ParseError, "strip(): Wrong number of arguments " + 19 | "given (#{arguments.size} for 1)") if arguments.size < 1 20 | 21 | value = arguments[0] 22 | klass = value.class 23 | 24 | unless [Array, String].include?(klass) 25 | raise(Puppet::ParseError, 'strip(): Requires either ' + 26 | 'array or string to work with') 27 | end 28 | 29 | if value.is_a?(Array) 30 | result = value.collect { |i| i.is_a?(String) ? i.strip : i } 31 | else 32 | result = value.strip 33 | end 34 | 35 | return result 36 | end 37 | end 38 | 39 | # vim: set ts=2 sw=2 et : 40 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/swapcase.rb: -------------------------------------------------------------------------------- 1 | # 2 | # swapcase.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS 7 | This function will swap the existing case of a string. 8 | 9 | *Examples:* 10 | 11 | swapcase("aBcD") 12 | 13 | Would result in: "AbCd" 14 | EOS 15 | ) do |arguments| 16 | 17 | raise(Puppet::ParseError, "swapcase(): Wrong number of arguments " + 18 | "given (#{arguments.size} for 1)") if arguments.size < 1 19 | 20 | value = arguments[0] 21 | klass = value.class 22 | 23 | unless [Array, String].include?(klass) 24 | raise(Puppet::ParseError, 'swapcase(): Requires either ' + 25 | 'array or string to work with') 26 | end 27 | 28 | if value.is_a?(Array) 29 | # Numbers in Puppet are often string-encoded which is troublesome ... 30 | result = value.collect { |i| i.is_a?(String) ? i.swapcase : i } 31 | else 32 | result = value.swapcase 33 | end 34 | 35 | return result 36 | end 37 | end 38 | 39 | # vim: set ts=2 sw=2 et : 40 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/time.rb: -------------------------------------------------------------------------------- 1 | # 2 | # time.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:time, :type => :rvalue, :doc => <<-EOS 7 | This function will return the current time since epoch as an integer. 8 | 9 | *Examples:* 10 | 11 | time() 12 | 13 | Will return something like: 1311972653 14 | EOS 15 | ) do |arguments| 16 | 17 | # The Time Zone argument is optional ... 18 | time_zone = arguments[0] if arguments[0] 19 | 20 | if (arguments.size != 0) and (arguments.size != 1) then 21 | raise(Puppet::ParseError, "time(): Wrong number of arguments "+ 22 | "given #{arguments.size} for 0 or 1") 23 | end 24 | 25 | time = Time.new 26 | 27 | # There is probably a better way to handle Time Zone ... 28 | if time_zone and not time_zone.empty? 29 | original_zone = ENV['TZ'] 30 | 31 | local_time = time.clone 32 | local_time = local_time.utc 33 | 34 | ENV['TZ'] = time_zone 35 | 36 | time = local_time.localtime 37 | 38 | ENV['TZ'] = original_zone 39 | end 40 | 41 | # Calling Time#to_i on a receiver changes it. Trust me I am the Doctor. 42 | result = time.strftime('%s') 43 | result = result.to_i 44 | 45 | return result 46 | end 47 | end 48 | 49 | # vim: set ts=2 sw=2 et : 50 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/to_bytes.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:to_bytes, :type => :rvalue, :doc => <<-EOS 3 | Converts the argument into bytes, for example 4 kB becomes 4096. 4 | Takes a single string value as an argument. 5 | EOS 6 | ) do |arguments| 7 | 8 | raise(Puppet::ParseError, "to_bytes(): Wrong number of arguments " + 9 | "given (#{arguments.size} for 1)") if arguments.size != 1 10 | 11 | arg = arguments[0] 12 | 13 | return arg if arg.is_a? Numeric 14 | 15 | value,prefix = */([0-9.e+-]*)\s*([^bB]?)/.match(arg)[1,2] 16 | 17 | value = value.to_f 18 | case prefix 19 | when '' then return value.to_i 20 | when 'k' then return (value*(1<<10)).to_i 21 | when 'M' then return (value*(1<<20)).to_i 22 | when 'G' then return (value*(1<<30)).to_i 23 | when 'T' then return (value*(1<<40)).to_i 24 | when 'E' then return (value*(1<<50)).to_i 25 | else raise Puppet::ParseError, "to_bytes(): Unknown prefix #{prefix}" 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/type.rb: -------------------------------------------------------------------------------- 1 | # 2 | # type.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:type, :type => :rvalue, :doc => <<-EOS 7 | Returns the type when passed a variable. Type can be one of: 8 | 9 | * string 10 | * array 11 | * hash 12 | * float 13 | * integer 14 | * boolean 15 | EOS 16 | ) do |arguments| 17 | 18 | raise(Puppet::ParseError, "type(): Wrong number of arguments " + 19 | "given (#{arguments.size} for 1)") if arguments.size < 1 20 | 21 | value = arguments[0] 22 | 23 | klass = value.class 24 | 25 | if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) 26 | raise(Puppet::ParseError, 'type(): Unknown type') 27 | end 28 | 29 | klass = klass.to_s # Ugly ... 30 | 31 | # We note that Integer is the parent to Bignum and Fixnum ... 32 | result = case klass 33 | when /^(?:Big|Fix)num$/ then 'integer' 34 | when /^(?:True|False)Class$/ then 'boolean' 35 | else klass 36 | end 37 | 38 | if result == "String" then 39 | if value == value.to_i.to_s then 40 | result = "Integer" 41 | elsif value == value.to_f.to_s then 42 | result = "Float" 43 | end 44 | end 45 | 46 | return result.downcase 47 | end 48 | end 49 | 50 | # vim: set ts=2 sw=2 et : 51 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/unique.rb: -------------------------------------------------------------------------------- 1 | # 2 | # unique.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:unique, :type => :rvalue, :doc => <<-EOS 7 | This function will remove duplicates from strings and arrays. 8 | 9 | *Examples:* 10 | 11 | unique("aabbcc") 12 | 13 | Will return: 14 | 15 | abc 16 | 17 | You can also use this with arrays: 18 | 19 | unique(["a","a","b","b","c","c"]) 20 | 21 | This returns: 22 | 23 | ["a","b","c"] 24 | EOS 25 | ) do |arguments| 26 | 27 | raise(Puppet::ParseError, "unique(): Wrong number of arguments " + 28 | "given (#{arguments.size} for 1)") if arguments.size < 1 29 | 30 | value = arguments[0] 31 | klass = value.class 32 | 33 | unless [Array, String].include?(klass) 34 | raise(Puppet::ParseError, 'unique(): Requires either ' + 35 | 'array or string to work with') 36 | end 37 | 38 | result = value.clone 39 | 40 | string = value.is_a?(String) ? true : false 41 | 42 | # We turn any string value into an array to be able to shuffle ... 43 | result = string ? result.split('') : result 44 | result = result.uniq # Remove duplicates ... 45 | result = string ? result.join : result 46 | 47 | return result 48 | end 49 | end 50 | 51 | # vim: set ts=2 sw=2 et : 52 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/upcase.rb: -------------------------------------------------------------------------------- 1 | # 2 | # upcase.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:upcase, :type => :rvalue, :doc => <<-EOS 7 | Converts a string or an array of strings to uppercase. 8 | 9 | *Examples:* 10 | 11 | upcase("abcd") 12 | 13 | Will return: 14 | 15 | ASDF 16 | EOS 17 | ) do |arguments| 18 | 19 | raise(Puppet::ParseError, "upcase(): Wrong number of arguments " + 20 | "given (#{arguments.size} for 1)") if arguments.size < 1 21 | 22 | value = arguments[0] 23 | klass = value.class 24 | 25 | unless [Array, String].include?(klass) 26 | raise(Puppet::ParseError, 'upcase(): Requires either ' + 27 | 'array or string to work with') 28 | end 29 | 30 | if value.is_a?(Array) 31 | # Numbers in Puppet are often string-encoded which is troublesome ... 32 | result = value.collect { |i| i.is_a?(String) ? i.upcase : i } 33 | else 34 | result = value.upcase 35 | end 36 | 37 | return result 38 | end 39 | end 40 | 41 | # vim: set ts=2 sw=2 et : 42 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/uriescape.rb: -------------------------------------------------------------------------------- 1 | # 2 | # uriescape.rb 3 | # 4 | require 'uri' 5 | 6 | module Puppet::Parser::Functions 7 | newfunction(:uriescape, :type => :rvalue, :doc => <<-EOS 8 | Urlencodes a string or array of strings. 9 | Requires either a single string or an array as an input. 10 | EOS 11 | ) do |arguments| 12 | 13 | raise(Puppet::ParseError, "uriescape(): Wrong number of arguments " + 14 | "given (#{arguments.size} for 1)") if arguments.size < 1 15 | 16 | value = arguments[0] 17 | klass = value.class 18 | unsafe = ":/?#[]@!$&'()*+,;= " 19 | 20 | unless [Array, String].include?(klass) 21 | raise(Puppet::ParseError, 'uriescape(): Requires either ' + 22 | 'array or string to work with') 23 | end 24 | 25 | if value.is_a?(Array) 26 | # Numbers in Puppet are often string-encoded which is troublesome ... 27 | result = value.collect { |i| i.is_a?(String) ? URI.escape(i,unsafe) : i } 28 | else 29 | result = URI.escape(value,unsafe) 30 | end 31 | 32 | return result 33 | end 34 | end 35 | 36 | # vim: set ts=2 sw=2 et : 37 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/validate_absolute_path.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:validate_absolute_path, :doc => <<-'ENDHEREDOC') do |args| 3 | Validate the string represents an absolute path in the filesystem. This function works 4 | for windows and unix style paths. 5 | 6 | The following values will pass: 7 | 8 | $my_path = "C:/Program Files (x86)/Puppet Labs/Puppet" 9 | validate_absolute_path($my_path) 10 | $my_path2 = "/var/lib/puppet" 11 | validate_absolute_path($my_path2) 12 | 13 | 14 | The following values will fail, causing compilation to abort: 15 | 16 | validate_absolute_path(true) 17 | validate_absolute_path([ 'var/lib/puppet', '/var/foo' ]) 18 | validate_absolute_path([ '/var/lib/puppet', 'var/foo' ]) 19 | $undefined = undef 20 | validate_absolute_path($undefined) 21 | 22 | ENDHEREDOC 23 | 24 | require 'puppet/util' 25 | 26 | unless args.length > 0 then 27 | raise Puppet::ParseError, ("validate_absolute_path(): wrong number of arguments (#{args.length}; must be > 0)") 28 | end 29 | 30 | args.each do |arg| 31 | # This logic was borrowed from 32 | # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb) 33 | 34 | # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise. 35 | if Puppet::Util.respond_to?(:absolute_path?) then 36 | unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows) 37 | raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.") 38 | end 39 | else 40 | # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path? 41 | # Determine in a platform-specific way whether a path is absolute. This 42 | # defaults to the local platform if none is specified. 43 | # Escape once for the string literal, and once for the regex. 44 | slash = '[\\\\/]' 45 | name = '[^\\\\/]+' 46 | regexes = { 47 | :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i, 48 | :posix => %r!^/!, 49 | } 50 | 51 | rval = (!!(arg =~ regexes[:posix])) || (!!(arg =~ regexes[:windows])) 52 | rval or raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.") 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/validate_array.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:validate_array, :doc => <<-'ENDHEREDOC') do |args| 4 | Validate that all passed values are array data structures. Abort catalog 5 | compilation if any value fails this check. 6 | 7 | The following values will pass: 8 | 9 | $my_array = [ 'one', 'two' ] 10 | validate_array($my_array) 11 | 12 | The following values will fail, causing compilation to abort: 13 | 14 | validate_array(true) 15 | validate_array('some_string') 16 | $undefined = undef 17 | validate_array($undefined) 18 | 19 | ENDHEREDOC 20 | 21 | unless args.length > 0 then 22 | raise Puppet::ParseError, ("validate_array(): wrong number of arguments (#{args.length}; must be > 0)") 23 | end 24 | 25 | args.each do |arg| 26 | unless arg.is_a?(Array) 27 | raise Puppet::ParseError, ("#{arg.inspect} is not an Array. It looks to be a #{arg.class}") 28 | end 29 | end 30 | 31 | end 32 | 33 | end 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/validate_bool.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:validate_bool, :doc => <<-'ENDHEREDOC') do |args| 4 | Validate that all passed values are either true or false. Abort catalog 5 | compilation if any value fails this check. 6 | 7 | The following values will pass: 8 | 9 | $iamtrue = true 10 | validate_bool(true) 11 | validate_bool(true, true, false, $iamtrue) 12 | 13 | The following values will fail, causing compilation to abort: 14 | 15 | $some_array = [ true ] 16 | validate_bool("false") 17 | validate_bool("true") 18 | validate_bool($some_array) 19 | 20 | ENDHEREDOC 21 | 22 | unless args.length > 0 then 23 | raise Puppet::ParseError, ("validate_bool(): wrong number of arguments (#{args.length}; must be > 0)") 24 | end 25 | 26 | args.each do |arg| 27 | unless (arg.is_a?(TrueClass) || arg.is_a?(FalseClass)) 28 | raise Puppet::ParseError, ("#{arg.inspect} is not a boolean. It looks to be a #{arg.class}") 29 | end 30 | end 31 | 32 | end 33 | 34 | end 35 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/validate_hash.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:validate_hash, :doc => <<-'ENDHEREDOC') do |args| 4 | Validate that all passed values are hash data structures. Abort catalog 5 | compilation if any value fails this check. 6 | 7 | The following values will pass: 8 | 9 | $my_hash = { 'one' => 'two' } 10 | validate_hash($my_hash) 11 | 12 | The following values will fail, causing compilation to abort: 13 | 14 | validate_hash(true) 15 | validate_hash('some_string') 16 | $undefined = undef 17 | validate_hash($undefined) 18 | 19 | ENDHEREDOC 20 | 21 | unless args.length > 0 then 22 | raise Puppet::ParseError, ("validate_hash(): wrong number of arguments (#{args.length}; must be > 0)") 23 | end 24 | 25 | args.each do |arg| 26 | unless arg.is_a?(Hash) 27 | raise Puppet::ParseError, ("#{arg.inspect} is not a Hash. It looks to be a #{arg.class}") 28 | end 29 | end 30 | 31 | end 32 | 33 | end 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/validate_re.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | newfunction(:validate_re, :doc => <<-'ENDHEREDOC') do |args| 3 | Perform simple validation of a string against one or more regular 4 | expressions. The first argument of this function should be a string to 5 | test, and the second argument should be a stringified regular expression 6 | (without the // delimiters) or an array of regular expressions. If none 7 | of the regular expressions match the string passed in, compilation will 8 | abort with a parse error. 9 | 10 | If a third argument is specified, this will be the error message raised and 11 | seen by the user. 12 | 13 | The following strings will validate against the regular expressions: 14 | 15 | validate_re('one', '^one$') 16 | validate_re('one', [ '^one', '^two' ]) 17 | 18 | The following strings will fail to validate, causing compilation to abort: 19 | 20 | validate_re('one', [ '^two', '^three' ]) 21 | 22 | A helpful error message can be returned like this: 23 | 24 | validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7') 25 | 26 | ENDHEREDOC 27 | if (args.length < 2) or (args.length > 3) then 28 | raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2 or 3)") 29 | end 30 | 31 | msg = args[2] || "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}" 32 | 33 | # We're using a flattened array here because we can't call String#any? in 34 | # Ruby 1.9 like we can in Ruby 1.8 35 | raise Puppet::ParseError, (msg) unless [args[1]].flatten.any? do |re_str| 36 | args[0] =~ Regexp.compile(re_str) 37 | end 38 | 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/validate_slength.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:validate_slength, :doc => <<-'ENDHEREDOC') do |args| 4 | Validate that the first argument is a string (or an array of strings), and 5 | less/equal to than the length of the second argument. It fails if the first 6 | argument is not a string or array of strings, and if arg 2 is not convertable 7 | to a number. 8 | 9 | The following values will pass: 10 | 11 | validate_slength("discombobulate",17) 12 | validate_slength(["discombobulate","moo"],17) 13 | 14 | The following valueis will not: 15 | 16 | validate_slength("discombobulate",1) 17 | validate_slength(["discombobulate","thermometer"],5) 18 | 19 | ENDHEREDOC 20 | 21 | raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2 22 | 23 | unless (args[0].is_a?(String) or args[0].is_a?(Array)) 24 | raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}") 25 | end 26 | 27 | begin 28 | max_length = args[1].to_i 29 | rescue NoMethodError => e 30 | raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the length parameter to an integer - sorry: " + e.message ) 31 | end 32 | 33 | raise Puppet::ParseError, ("validate_slength(): please pass a positive number as max_length") unless max_length > 0 34 | 35 | case args[0] 36 | when String 37 | raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been less than or equal to #{max_length} characters") unless args[0].length <= max_length 38 | when Array 39 | args[0].each do |arg| 40 | if arg.is_a?(String) 41 | unless ( arg.is_a?(String) and arg.length <= max_length ) 42 | raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been less than or equal to #{max_length} characters") 43 | end 44 | else 45 | raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}") 46 | end 47 | end 48 | else 49 | raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}") 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/validate_string.rb: -------------------------------------------------------------------------------- 1 | module Puppet::Parser::Functions 2 | 3 | newfunction(:validate_string, :doc => <<-'ENDHEREDOC') do |args| 4 | Validate that all passed values are string data structures. Abort catalog 5 | compilation if any value fails this check. 6 | 7 | The following values will pass: 8 | 9 | $my_string = "one two" 10 | validate_string($my_string, 'three') 11 | 12 | The following values will fail, causing compilation to abort: 13 | 14 | validate_string(true) 15 | validate_string([ 'some', 'array' ]) 16 | $undefined = undef 17 | validate_string($undefined) 18 | 19 | ENDHEREDOC 20 | 21 | unless args.length > 0 then 22 | raise Puppet::ParseError, ("validate_string(): wrong number of arguments (#{args.length}; must be > 0)") 23 | end 24 | 25 | args.each do |arg| 26 | unless arg.is_a?(String) 27 | raise Puppet::ParseError, ("#{arg.inspect} is not a string. It looks to be a #{arg.class}") 28 | end 29 | end 30 | 31 | end 32 | 33 | end 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/values.rb: -------------------------------------------------------------------------------- 1 | # 2 | # values.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:values, :type => :rvalue, :doc => <<-EOS 7 | When given a hash this function will return the values of that hash. 8 | 9 | *Examples:* 10 | 11 | $hash = { 12 | 'a' => 1, 13 | 'b' => 2, 14 | 'c' => 3, 15 | } 16 | values($hash) 17 | 18 | This example would return: 19 | 20 | [1,2,3] 21 | EOS 22 | ) do |arguments| 23 | 24 | raise(Puppet::ParseError, "values(): Wrong number of arguments " + 25 | "given (#{arguments.size} for 1)") if arguments.size < 1 26 | 27 | hash = arguments[0] 28 | 29 | unless hash.is_a?(Hash) 30 | raise(Puppet::ParseError, 'values(): Requires hash to work with') 31 | end 32 | 33 | result = hash.values 34 | 35 | return result 36 | end 37 | end 38 | 39 | # vim: set ts=2 sw=2 et : 40 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/parser/functions/zip.rb: -------------------------------------------------------------------------------- 1 | # 2 | # zip.rb 3 | # 4 | 5 | module Puppet::Parser::Functions 6 | newfunction(:zip, :type => :rvalue, :doc => <<-EOS 7 | Takes one element from first array and merges corresponding elements from second array. This generates a sequence of n-element arrays, where n is one more than the count of arguments. 8 | 9 | *Example:* 10 | 11 | zip(['1','2','3'],['4','5','6']) 12 | 13 | Would result in: 14 | 15 | ["1", "4"], ["2", "5"], ["3", "6"] 16 | EOS 17 | ) do |arguments| 18 | 19 | # Technically we support three arguments but only first is mandatory ... 20 | raise(Puppet::ParseError, "zip(): Wrong number of arguments " + 21 | "given (#{arguments.size} for 2)") if arguments.size < 2 22 | 23 | a = arguments[0] 24 | b = arguments[1] 25 | 26 | unless a.is_a?(Array) and b.is_a?(Array) 27 | raise(Puppet::ParseError, 'zip(): Requires array to work with') 28 | end 29 | 30 | flatten = arguments[2] if arguments[2] 31 | 32 | if flatten 33 | klass = flatten.class 34 | 35 | # We can have either true or false, or string which resembles boolean ... 36 | unless [FalseClass, TrueClass, String].include?(klass) 37 | raise(Puppet::ParseError, 'zip(): Requires either ' + 38 | 'boolean or string to work with') 39 | end 40 | 41 | if flatten.is_a?(String) 42 | # We consider all the yes, no, y, n and so on too ... 43 | flatten = case flatten 44 | # 45 | # This is how undef looks like in Puppet ... 46 | # We yield false in this case. 47 | # 48 | when /^$/, '' then false # Empty string will be false ... 49 | when /^(1|t|y|true|yes)$/ then true 50 | when /^(0|f|n|false|no)$/ then false 51 | when /^(undef|undefined)$/ then false # This is not likely to happen ... 52 | else 53 | raise(Puppet::ParseError, 'zip(): Unknown type of boolean given') 54 | end 55 | end 56 | end 57 | 58 | result = a.zip(b) 59 | result = flatten ? result.flatten : result 60 | 61 | return result 62 | end 63 | end 64 | 65 | # vim: set ts=2 sw=2 et : 66 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/provider/file_line/ruby.rb: -------------------------------------------------------------------------------- 1 | 2 | Puppet::Type.type(:file_line).provide(:ruby) do 3 | 4 | def exists? 5 | lines.find do |line| 6 | line.chomp == resource[:line].chomp 7 | end 8 | end 9 | 10 | def create 11 | if resource[:match] 12 | handle_create_with_match() 13 | else 14 | handle_create_without_match() 15 | end 16 | end 17 | 18 | def destroy 19 | local_lines = lines 20 | File.open(resource[:path],'w') do |fh| 21 | fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join('')) 22 | end 23 | end 24 | 25 | private 26 | def lines 27 | # If this type is ever used with very large files, we should 28 | # write this in a different way, using a temp 29 | # file; for now assuming that this type is only used on 30 | # small-ish config files that can fit into memory without 31 | # too much trouble. 32 | @lines ||= File.readlines(resource[:path]) 33 | end 34 | 35 | def handle_create_with_match() 36 | regex = resource[:match] ? Regexp.new(resource[:match]) : nil 37 | match_count = lines.select { |l| regex.match(l) }.count 38 | if match_count > 1 39 | raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" 40 | end 41 | File.open(resource[:path], 'w') do |fh| 42 | lines.each do |l| 43 | fh.puts(regex.match(l) ? resource[:line] : l) 44 | end 45 | 46 | if (match_count == 0) 47 | fh.puts(resource[:line]) 48 | end 49 | end 50 | end 51 | 52 | def handle_create_without_match 53 | File.open(resource[:path], 'a') do |fh| 54 | fh.puts resource[:line] 55 | end 56 | end 57 | 58 | 59 | end 60 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/type/anchor.rb: -------------------------------------------------------------------------------- 1 | Puppet::Type.newtype(:anchor) do 2 | desc <<-'ENDOFDESC' 3 | A simple resource type intended to be used as an anchor in a composite class. 4 | 5 | In Puppet 2.6, when a class declares another class, the resources in the 6 | interior class are not contained by the exterior class. This interacts badly 7 | with the pattern of composing complex modules from smaller classes, as it 8 | makes it impossible for end users to specify order relationships between the 9 | exterior class and other modules. 10 | 11 | The anchor type lets you work around this. By sandwiching any interior 12 | classes between two no-op resources that _are_ contained by the exterior 13 | class, you can ensure that all resources in the module are contained. 14 | 15 | class ntp { 16 | # These classes will have the correct order relationship with each 17 | # other. However, without anchors, they won't have any order 18 | # relationship to Class['ntp']. 19 | class { 'ntp::package': } 20 | -> class { 'ntp::config': } 21 | -> class { 'ntp::service': } 22 | 23 | # These two resources "anchor" the composed classes within the ntp 24 | # class. 25 | anchor { 'ntp::begin': } -> Class['ntp::package'] 26 | Class['ntp::service'] -> anchor { 'ntp::end': } 27 | } 28 | 29 | This allows the end user of the ntp module to establish require and before 30 | relationships with Class['ntp']: 31 | 32 | class { 'ntp': } -> class { 'mcollective': } 33 | class { 'mcollective': } -> class { 'ntp': } 34 | 35 | ENDOFDESC 36 | 37 | newparam :name do 38 | desc "The name of the anchor resource." 39 | end 40 | 41 | end 42 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/lib/puppet/type/file_line.rb: -------------------------------------------------------------------------------- 1 | Puppet::Type.newtype(:file_line) do 2 | 3 | desc <<-EOT 4 | Ensures that a given line is contained within a file. The implementation 5 | matches the full line, including whitespace at the beginning and end. If 6 | the line is not contained in the given file, Puppet will add the line to 7 | ensure the desired state. Multiple resources may be declared to manage 8 | multiple lines in the same file. 9 | 10 | Example: 11 | 12 | file_line { 'sudo_rule': 13 | path => '/etc/sudoers', 14 | line => '%sudo ALL=(ALL) ALL', 15 | } 16 | file_line { 'sudo_rule_nopw': 17 | path => '/etc/sudoers', 18 | line => '%sudonopw ALL=(ALL) NOPASSWD: ALL', 19 | } 20 | 21 | In this example, Puppet will ensure both of the specified lines are 22 | contained in the file /etc/sudoers. 23 | 24 | EOT 25 | 26 | ensurable do 27 | defaultvalues 28 | defaultto :present 29 | end 30 | 31 | newparam(:name, :namevar => true) do 32 | desc 'An arbitrary name used as the identity of the resource.' 33 | end 34 | 35 | newparam(:match) do 36 | desc 'An optional regular expression to run against existing lines in the file;\n' + 37 | 'if a match is found, we replace that line rather than adding a new line.' 38 | end 39 | 40 | newparam(:line) do 41 | desc 'The line to be appended to the file located by the path parameter.' 42 | end 43 | 44 | newparam(:path) do 45 | desc 'The file Puppet will ensure contains the line specified by the line parameter.' 46 | validate do |value| 47 | unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/)) 48 | raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'") 49 | end 50 | end 51 | end 52 | 53 | # Autorequire the file resource if it's being managed 54 | autorequire(:file) do 55 | self[:path] 56 | end 57 | 58 | validate do 59 | unless self[:line] and self[:path] 60 | raise(Puppet::Error, "Both line and path are required attributes") 61 | end 62 | 63 | if (self[:match]) 64 | unless Regexp.new(self[:match]).match(self[:line]) 65 | raise(Puppet::Error, "When providing a 'match' parameter, the value must be a regex that matches against the value of your 'line' parameter") 66 | end 67 | end 68 | 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/manifests/init.pp: -------------------------------------------------------------------------------- 1 | # Class: stdlib 2 | # 3 | # This module manages stdlib. Most of stdlib's features are automatically 4 | # loaded by Puppet, but this class should be declared in order to use the 5 | # standardized run stages. 6 | # 7 | # Parameters: none 8 | # 9 | # Actions: 10 | # 11 | # Declares all other classes in the stdlib module. Currently, this consists 12 | # of stdlib::stages. 13 | # 14 | # Requires: nothing 15 | # 16 | class stdlib { 17 | 18 | class { 'stdlib::stages': } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/manifests/stages.pp: -------------------------------------------------------------------------------- 1 | # Class: stdlib::stages 2 | # 3 | # This class manages a standard set of run stages for Puppet. It is managed by 4 | # the stdlib class, and should not be declared independently. 5 | # 6 | # The high level stages are (in order): 7 | # 8 | # * setup 9 | # * main 10 | # * runtime 11 | # * setup_infra 12 | # * deploy_infra 13 | # * setup_app 14 | # * deploy_app 15 | # * deploy 16 | # 17 | # Parameters: none 18 | # 19 | # Actions: 20 | # 21 | # Declares various run-stages for deploying infrastructure, 22 | # language runtimes, and application layers. 23 | # 24 | # Requires: nothing 25 | # 26 | # Sample Usage: 27 | # 28 | # node default { 29 | # include stdlib 30 | # class { java: stage => 'runtime' } 31 | # } 32 | # 33 | class stdlib::stages { 34 | 35 | stage { 'setup': before => Stage['main'] } 36 | stage { 'runtime': require => Stage['main'] } 37 | -> stage { 'setup_infra': } 38 | -> stage { 'deploy_infra': } 39 | -> stage { 'setup_app': } 40 | -> stage { 'deploy_app': } 41 | -> stage { 'deploy': } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/functions/defined_with_params_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | require 'rspec-puppet' 5 | describe 'defined_with_params' do 6 | describe 'when a resource is not specified' do 7 | it { should run.with_params().and_raise_error(ArgumentError) } 8 | end 9 | describe 'when compared against a resource with no attributes' do 10 | let :pre_condition do 11 | 'user { "dan": }' 12 | end 13 | it do 14 | should run.with_params('User[dan]', {}).and_return(true) 15 | should run.with_params('User[bob]', {}).and_return(false) 16 | should run.with_params('User[dan]', {'foo' => 'bar'}).and_return(false) 17 | end 18 | end 19 | 20 | describe 'when compared against a resource with attributes' do 21 | let :pre_condition do 22 | 'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}' 23 | end 24 | it do 25 | should run.with_params('User[dan]', {}).and_return(true) 26 | should run.with_params('User[dan]', '').and_return(true) 27 | should run.with_params('User[dan]', {'ensure' => 'present'} 28 | ).and_return(true) 29 | should run.with_params('User[dan]', 30 | {'ensure' => 'present', 'managehome' => false} 31 | ).and_return(true) 32 | should run.with_params('User[dan]', 33 | {'ensure' => 'absent', 'managehome' => false} 34 | ).and_return(false) 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/functions/ensure_packages_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby 2 | 3 | require 'spec_helper' 4 | require 'rspec-puppet' 5 | 6 | describe 'ensure_packages' do 7 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 8 | 9 | describe 'argument handling' do 10 | it 'fails with no arguments' do 11 | should run.with_params().and_raise_error(Puppet::ParseError) 12 | end 13 | it 'requires an array' do 14 | lambda { scope.function_ensure_packages([['foo']]) }.should_not raise_error 15 | end 16 | it 'fails when given a string' do 17 | should run.with_params('foo').and_raise_error(Puppet::ParseError) 18 | end 19 | end 20 | 21 | context 'given a catalog containing Package[puppet]{ensure => absent}' do 22 | let :pre_condition do 23 | 'package { puppet: ensure => absent }' 24 | end 25 | 26 | # NOTE: should run.with_params has the side effect of making the compiler 27 | # available to the test harness. 28 | it 'has no effect on Package[puppet]' do 29 | should run.with_params(['puppet']) 30 | rsrc = compiler.catalog.resource('Package[puppet]') 31 | rsrc.to_hash.should == {:ensure => "absent"} 32 | end 33 | end 34 | 35 | context 'given a clean catalog' do 36 | it 'declares package resources with ensure => present' do 37 | should run.with_params(['facter']) 38 | rsrc = compiler.catalog.resource('Package[facter]') 39 | rsrc.to_hash.should == {:name => "facter", :ensure => "present"} 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/functions/ensure_resource_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | require 'rspec-puppet' 5 | describe 'ensure_resource' do 6 | describe 'when a type or title is not specified' do 7 | it do 8 | should run.with_params().and_raise_error(ArgumentError) 9 | should run.with_params(['type']).and_raise_error(ArgumentError) 10 | end 11 | end 12 | describe 'when compared against a resource with no attributes' do 13 | let :pre_condition do 14 | 'user { "dan": }' 15 | end 16 | it do 17 | should run.with_params('user', 'dan', {}) 18 | compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]' 19 | end 20 | end 21 | 22 | describe 'when compared against a resource with attributes' do 23 | let :pre_condition do 24 | 'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}' 25 | end 26 | it do 27 | # these first three should not fail 28 | should run.with_params('User', 'dan', {}) 29 | should run.with_params('User', 'dan', '') 30 | should run.with_params('User', 'dan', {'ensure' => 'present'}) 31 | should run.with_params('User', 'dan', 32 | {'ensure' => 'present', 'managehome' => false} 33 | ) 34 | # test that this fails 35 | should run.with_params('User', 'dan', 36 | {'ensure' => 'absent', 'managehome' => false} 37 | ).and_raise_error(Puppet::Error) 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/lib/puppet_spec/files.rb: -------------------------------------------------------------------------------- 1 | require 'fileutils' 2 | require 'tempfile' 3 | 4 | # A support module for testing files. 5 | module PuppetSpec::Files 6 | # This code exists only to support tests that run as root, pretty much. 7 | # Once they have finally been eliminated this can all go... --daniel 2011-04-08 8 | if Puppet.features.posix? then 9 | def self.in_tmp(path) 10 | path =~ /^\/tmp/ or path =~ /^\/var\/folders/ 11 | end 12 | elsif Puppet.features.microsoft_windows? 13 | def self.in_tmp(path) 14 | tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp")) 15 | path =~ /^#{tempdir}/ 16 | end 17 | else 18 | fail "Help! Can't find in_tmp for this platform" 19 | end 20 | 21 | def self.cleanup 22 | $global_tempfiles ||= [] 23 | while path = $global_tempfiles.pop do 24 | fail "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path) 25 | 26 | begin 27 | FileUtils.rm_r path, :secure => true 28 | rescue Errno::ENOENT 29 | # nothing to do 30 | end 31 | end 32 | end 33 | 34 | def tmpfile(name) 35 | # Generate a temporary file, just for the name... 36 | source = Tempfile.new(name) 37 | path = source.path 38 | source.close! 39 | 40 | # ...record it for cleanup, 41 | $global_tempfiles ||= [] 42 | $global_tempfiles << File.expand_path(path) 43 | 44 | # ...and bam. 45 | path 46 | end 47 | 48 | def tmpdir(name) 49 | path = tmpfile(name) 50 | FileUtils.mkdir_p(path) 51 | path 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/lib/puppet_spec/fixtures.rb: -------------------------------------------------------------------------------- 1 | module PuppetSpec::Fixtures 2 | def fixtures(*rest) 3 | File.join(PuppetSpec::FIXTURE_DIR, *rest) 4 | end 5 | def my_fixture_dir 6 | callers = caller 7 | while line = callers.shift do 8 | next unless found = line.match(%r{/spec/(.*)_spec\.rb:}) 9 | return fixtures(found[1]) 10 | end 11 | fail "sorry, I couldn't work out your path from the caller stack!" 12 | end 13 | def my_fixture(name) 14 | file = File.join(my_fixture_dir, name) 15 | unless File.readable? file then 16 | fail Puppet::DevError, "fixture '#{name}' for #{my_fixture_dir} is not readable" 17 | end 18 | return file 19 | end 20 | def my_fixtures(glob = '*', flags = 0) 21 | files = Dir.glob(File.join(my_fixture_dir, glob), flags) 22 | unless files.length > 0 then 23 | fail Puppet::DevError, "fixture '#{glob}' for #{my_fixture_dir} had no files!" 24 | end 25 | block_given? and files.each do |file| yield file end 26 | files 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/lib/puppet_spec/matchers.rb: -------------------------------------------------------------------------------- 1 | require 'stringio' 2 | 3 | ######################################################################## 4 | # Backward compatibility for Jenkins outdated environment. 5 | module RSpec 6 | module Matchers 7 | module BlockAliases 8 | alias_method :to, :should unless method_defined? :to 9 | alias_method :to_not, :should_not unless method_defined? :to_not 10 | alias_method :not_to, :should_not unless method_defined? :not_to 11 | end 12 | end 13 | end 14 | 15 | 16 | ######################################################################## 17 | # Custom matchers... 18 | RSpec::Matchers.define :have_matching_element do |expected| 19 | match do |actual| 20 | actual.any? { |item| item =~ expected } 21 | end 22 | end 23 | 24 | 25 | RSpec::Matchers.define :exit_with do |expected| 26 | actual = nil 27 | match do |block| 28 | begin 29 | block.call 30 | rescue SystemExit => e 31 | actual = e.status 32 | end 33 | actual and actual == expected 34 | end 35 | failure_message_for_should do |block| 36 | "expected exit with code #{expected} but " + 37 | (actual.nil? ? " exit was not called" : "we exited with #{actual} instead") 38 | end 39 | failure_message_for_should_not do |block| 40 | "expected that exit would not be called with #{expected}" 41 | end 42 | description do 43 | "expect exit with #{expected}" 44 | end 45 | end 46 | 47 | 48 | RSpec::Matchers.define :have_printed do |expected| 49 | match do |block| 50 | $stderr = $stdout = StringIO.new 51 | 52 | begin 53 | block.call 54 | ensure 55 | $stdout.rewind 56 | @actual = $stdout.read 57 | 58 | $stdout = STDOUT 59 | $stderr = STDERR 60 | end 61 | 62 | if @actual then 63 | case expected 64 | when String 65 | @actual.include? expected 66 | when Regexp 67 | expected.match @actual 68 | else 69 | raise ArgumentError, "No idea how to match a #{@actual.class.name}" 70 | end 71 | end 72 | end 73 | 74 | failure_message_for_should do |actual| 75 | if actual.nil? then 76 | "expected #{expected.inspect}, but nothing was printed" 77 | else 78 | "expected #{expected.inspect} to be printed; got:\n#{actual}" 79 | end 80 | end 81 | 82 | description do 83 | "expect #{expected.inspect} to be printed" 84 | end 85 | 86 | diffable 87 | end 88 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/lib/puppet_spec/verbose.rb: -------------------------------------------------------------------------------- 1 | # Support code for running stuff with warnings disabled. 2 | module Kernel 3 | def with_verbose_disabled 4 | verbose, $VERBOSE = $VERBOSE, nil 5 | result = yield 6 | $VERBOSE = verbose 7 | return result 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/monkey_patches/alias_should_to_must.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | 3 | class Object 4 | # This is necessary because the RAL has a 'should' 5 | # method. 6 | alias :must :should 7 | alias :must_not :should_not 8 | end 9 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/monkey_patches/publicize_methods.rb: -------------------------------------------------------------------------------- 1 | # Some monkey-patching to allow us to test private methods. 2 | class Class 3 | def publicize_methods(*methods) 4 | saved_private_instance_methods = methods.empty? ? self.private_instance_methods : methods 5 | 6 | self.class_eval { public(*saved_private_instance_methods) } 7 | yield 8 | self.class_eval { private(*saved_private_instance_methods) } 9 | end 10 | end 11 | 12 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/spec.opts: -------------------------------------------------------------------------------- 1 | --format 2 | s 3 | --colour 4 | --loadby 5 | mtime 6 | --backtrace 7 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | dir = File.expand_path(File.dirname(__FILE__)) 2 | $LOAD_PATH.unshift File.join(dir, 'lib') 3 | 4 | # Don't want puppet getting the command line arguments for rake or autotest 5 | ARGV.clear 6 | 7 | require 'puppet' 8 | require 'facter' 9 | require 'mocha' 10 | gem 'rspec', '>=2.0.0' 11 | require 'rspec/expectations' 12 | 13 | require 'puppetlabs_spec_helper/module_spec_helper' 14 | 15 | RSpec.configure do |config| 16 | # FIXME REVISIT - We may want to delegate to Facter like we do in 17 | # Puppet::PuppetSpecInitializer.initialize_via_testhelper(config) because 18 | # this behavior is a duplication of the spec_helper in Facter. 19 | config.before :each do 20 | # Ensure that we don't accidentally cache facts and environment between 21 | # test cases. This requires each example group to explicitly load the 22 | # facts being exercised with something like 23 | # Facter.collection.loader.load(:ipaddress) 24 | Facter::Util::Loader.any_instance.stubs(:load_all) 25 | Facter.clear 26 | Facter.clear_messages 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/facter/pe_version_spec.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe "PE Version specs" do 6 | before :each do 7 | # Explicitly load the pe_version.rb file which contains generated facts 8 | # that cannot be automatically loaded. Puppet 2.x implements 9 | # Facter.collection.load while Facter 1.x markes Facter.collection.load as 10 | # a private method. 11 | if Facter.collection.respond_to? :load 12 | Facter.collection.load(:pe_version) 13 | else 14 | Facter.collection.loader.load(:pe_version) 15 | end 16 | end 17 | 18 | context "If PE is installed" do 19 | %w{ 2.6.1 2.10.300 }.each do |version| 20 | puppetversion = "2.7.19 (Puppet Enterprise #{version})" 21 | context "puppetversion => #{puppetversion}" do 22 | before :each do 23 | Facter.fact(:puppetversion).stubs(:value).returns(puppetversion) 24 | end 25 | 26 | (major,minor,patch) = version.split(".") 27 | 28 | it "Should return true" do 29 | Facter.fact(:is_pe).value.should == true 30 | end 31 | 32 | it "Should have a version of #{version}" do 33 | Facter.fact(:pe_version).value.should == version 34 | end 35 | 36 | it "Should have a major version of #{major}" do 37 | Facter.fact(:pe_major_version).value.should == major 38 | end 39 | 40 | it "Should have a minor version of #{minor}" do 41 | Facter.fact(:pe_minor_version).value.should == minor 42 | end 43 | 44 | it "Should have a patch version of #{patch}" do 45 | Facter.fact(:pe_patch_version).value.should == patch 46 | end 47 | end 48 | end 49 | end 50 | 51 | context "When PE is not installed" do 52 | before :each do 53 | Facter.fact(:puppetversion).stubs(:value).returns("2.7.19") 54 | end 55 | 56 | it "is_pe is false" do 57 | Facter.fact(:is_pe).value.should == false 58 | end 59 | 60 | it "pe_version is nil" do 61 | Facter.fact(:pe_version).value.should be_nil 62 | end 63 | 64 | it "pe_major_version is nil" do 65 | Facter.fact(:pe_major_version).value.should be_nil 66 | end 67 | 68 | it "pe_minor_version is nil" do 69 | Facter.fact(:pe_minor_version).value.should be_nil 70 | end 71 | 72 | it "Should have a patch version" do 73 | Facter.fact(:pe_patch_version).value.should be_nil 74 | end 75 | end 76 | end 77 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/facter/root_home_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'facter/root_home' 3 | 4 | describe Facter::Util::RootHome do 5 | context "solaris" do 6 | let(:root_ent) { "root:x:0:0:Super-User:/:/sbin/sh" } 7 | let(:expected_root_home) { "/" } 8 | 9 | it "should return /" do 10 | Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent) 11 | Facter::Util::RootHome.get_root_home.should == expected_root_home 12 | end 13 | end 14 | context "linux" do 15 | let(:root_ent) { "root:x:0:0:root:/root:/bin/bash" } 16 | let(:expected_root_home) { "/root" } 17 | 18 | it "should return /root" do 19 | Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent) 20 | Facter::Util::RootHome.get_root_home.should == expected_root_home 21 | end 22 | end 23 | context "macosx" do 24 | let(:root_ent) { "root:*:0:0:System Administrator:/var/root:/bin/sh" } 25 | let(:expected_root_home) { "/var/root" } 26 | 27 | it "should return /var/root" do 28 | Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(root_ent) 29 | Facter::Util::RootHome.get_root_home.should == expected_root_home 30 | end 31 | end 32 | context "windows" do 33 | before :each do 34 | Facter::Util::Resolution.expects(:exec).with("getent passwd root").returns(nil) 35 | end 36 | it "should be nil on windows" do 37 | Facter::Util::RootHome.get_root_home.should be_nil 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/facter/util/puppet_settings_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'facter/util/puppet_settings' 3 | 4 | describe Facter::Util::PuppetSettings do 5 | 6 | describe "#with_puppet" do 7 | context "Without Puppet loaded" do 8 | before(:each) do 9 | Module.expects(:const_get).with("Puppet").raises(NameError) 10 | end 11 | 12 | it 'should be nil' do 13 | subject.with_puppet { Puppet[:vardir] }.should be_nil 14 | end 15 | it 'should not yield to the block' do 16 | Puppet.expects(:[]).never 17 | subject.with_puppet { Puppet[:vardir] }.should be_nil 18 | end 19 | end 20 | context "With Puppet loaded" do 21 | module Puppet; end 22 | let(:vardir) { "/var/lib/puppet" } 23 | 24 | before :each do 25 | Puppet.expects(:[]).with(:vardir).returns vardir 26 | end 27 | it 'should yield to the block' do 28 | subject.with_puppet { Puppet[:vardir] } 29 | end 30 | it 'should return the nodes vardir' do 31 | subject.with_puppet { Puppet[:vardir] }.should eq vardir 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/abs_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe "the abs function" do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | 8 | it "should exist" do 9 | Puppet::Parser::Functions.function("abs").should == "function_abs" 10 | end 11 | 12 | it "should raise a ParseError if there is less than 1 arguments" do 13 | lambda { scope.function_abs([]) }.should( raise_error(Puppet::ParseError)) 14 | end 15 | 16 | it "should convert a negative number into a positive" do 17 | result = scope.function_abs(["-34"]) 18 | result.should(eq(34)) 19 | end 20 | 21 | it "should do nothing with a positive number" do 22 | result = scope.function_abs(["5678"]) 23 | result.should(eq(5678)) 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/bool2num_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the bool2num function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("bool2num").should == "function_bool2num" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_bool2num([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should convert true to 1" do 16 | result = scope.function_bool2num([true]) 17 | result.should(eq(1)) 18 | end 19 | 20 | it "should convert false to 0" do 21 | result = scope.function_bool2num([false]) 22 | result.should(eq(0)) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/capitalize_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the capitalize function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("capitalize").should == "function_capitalize" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_capitalize([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should capitalize the beginning of a string" do 16 | result = scope.function_capitalize(["abc"]) 17 | result.should(eq("Abc")) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/chomp_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the chomp function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("chomp").should == "function_chomp" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_chomp([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should chomp the end of a string" do 16 | result = scope.function_chomp(["abc\n"]) 17 | result.should(eq("abc")) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/chop_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the chop function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("chop").should == "function_chop" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_chop([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should chop the end of a string" do 16 | result = scope.function_chop(["asdf\n"]) 17 | result.should(eq("asdf")) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/delete_at_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the delete_at function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("delete_at").should == "function_delete_at" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_delete_at([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should delete an item at specified location from an array" do 16 | result = scope.function_delete_at([['a','b','c'],1]) 17 | result.should(eq(['a','c'])) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/delete_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the delete function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("delete").should == "function_delete" 9 | end 10 | 11 | it "should raise a ParseError if there are fewer than 2 arguments" do 12 | lambda { scope.function_delete([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should raise a ParseError if there are greater than 2 arguments" do 16 | lambda { scope.function_delete([[], 'foo', 'bar']) }.should( raise_error(Puppet::ParseError)) 17 | end 18 | 19 | it "should raise a TypeError if a number is passed as the first argument" do 20 | lambda { scope.function_delete([1, 'bar']) }.should( raise_error(TypeError)) 21 | end 22 | 23 | it "should delete all instances of an element from an array" do 24 | result = scope.function_delete([['a','b','c','b'],'b']) 25 | result.should(eq(['a','c'])) 26 | end 27 | 28 | it "should delete all instances of a substring from a string" do 29 | result = scope.function_delete(['foobarbabarz','bar']) 30 | result.should(eq('foobaz')) 31 | end 32 | 33 | it "should delete a key from a hash" do 34 | result = scope.function_delete([{ 'a' => 1, 'b' => 2, 'c' => 3 },'b']) 35 | result.should(eq({ 'a' => 1, 'c' => 3 })) 36 | end 37 | 38 | end 39 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/downcase_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the downcase function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("downcase").should == "function_downcase" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_downcase([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should downcase a string" do 16 | result = scope.function_downcase(["ASFD"]) 17 | result.should(eq("asfd")) 18 | end 19 | 20 | it "should do nothing to a string that is already downcase" do 21 | result = scope.function_downcase(["asdf asdf"]) 22 | result.should(eq("asdf asdf")) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/empty_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the empty function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | it "should exist" do 7 | Puppet::Parser::Functions.function("empty").should == "function_empty" 8 | end 9 | 10 | it "should raise a ParseError if there is less than 1 arguments" do 11 | lambda { scope.function_empty([]) }.should( raise_error(Puppet::ParseError)) 12 | end 13 | 14 | it "should return a true for an empty string" do 15 | result = scope.function_empty(['']) 16 | result.should(eq(true)) 17 | end 18 | 19 | it "should return a false for a non-empty string" do 20 | result = scope.function_empty(['asdf']) 21 | result.should(eq(false)) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/flatten_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the flatten function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | it "should exist" do 7 | Puppet::Parser::Functions.function("flatten").should == "function_flatten" 8 | end 9 | 10 | it "should raise a ParseError if there is less than 1 arguments" do 11 | lambda { scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) 12 | end 13 | 14 | it "should flatten a complex data structure" do 15 | result = scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) 16 | result.should(eq(["a","b","c","d","e","f","g"])) 17 | end 18 | 19 | it "should do nothing to a structure that is already flat" do 20 | result = scope.function_flatten([["a","b","c","d"]]) 21 | result.should(eq(["a","b","c","d"])) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the fqdn_rotate function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("fqdn_rotate").should == "function_fqdn_rotate" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_fqdn_rotate([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should rotate a string and the result should be the same size" do 16 | scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1") 17 | result = scope.function_fqdn_rotate(["asdf"]) 18 | result.size.should(eq(4)) 19 | end 20 | 21 | it "should rotate a string to give the same results for one host" do 22 | scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice 23 | scope.function_fqdn_rotate(["abcdefg"]).should eql(scope.function_fqdn_rotate(["abcdefg"])) 24 | end 25 | 26 | it "should rotate a string to give different values on different hosts" do 27 | scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1") 28 | val1 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"]) 29 | scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.2") 30 | val2 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"]) 31 | val1.should_not eql(val2) 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/get_module_path_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe Puppet::Parser::Functions.function(:get_module_path) do 5 | Internals = PuppetlabsSpec::PuppetInternals 6 | class StubModule 7 | attr_reader :path 8 | def initialize(path) 9 | @path = path 10 | end 11 | end 12 | 13 | def scope(environment = "production") 14 | Internals.scope(:compiler => Internals.compiler(:node => Internals.node(:environment => environment))) 15 | end 16 | 17 | it 'should only allow one argument' do 18 | expect { scope.function_get_module_path([]) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) 19 | expect { scope.function_get_module_path(['1','2','3']) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) 20 | end 21 | it 'should raise an exception when the module cannot be found' do 22 | expect { scope.function_get_module_path(['foo']) }.should raise_error(Puppet::ParseError, /Could not find module/) 23 | end 24 | describe 'when locating a module' do 25 | let(:modulepath) { "/tmp/does_not_exist" } 26 | let(:path_of_module_foo) { StubModule.new("/tmp/does_not_exist/foo") } 27 | 28 | before(:each) { Puppet[:modulepath] = modulepath } 29 | 30 | it 'should be able to find module paths from the modulepath setting' do 31 | Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo) 32 | scope.function_get_module_path(['foo']).should == path_of_module_foo.path 33 | end 34 | it 'should be able to find module paths when the modulepath is a list' do 35 | Puppet[:modulepath] = modulepath + ":/tmp" 36 | Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo) 37 | scope.function_get_module_path(['foo']).should == path_of_module_foo.path 38 | end 39 | it 'should respect the environment' do 40 | pending("Disabled on Puppet 2.6.x") if Puppet.version =~ /^2\.6\b/ 41 | Puppet.settings[:environment] = 'danstestenv' 42 | Puppet::Module.expects(:find).with('foo', 'danstestenv').returns(path_of_module_foo) 43 | scope('danstestenv').function_get_module_path(['foo']).should == path_of_module_foo.path 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/getvar_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe Puppet::Parser::Functions.function(:getvar) do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | describe 'when calling getvar from puppet' do 7 | 8 | it "should not compile when no arguments are passed" do 9 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 10 | Puppet[:code] = '$foo = getvar()' 11 | expect { 12 | scope.compiler.compile 13 | }.to raise_error(Puppet::ParseError, /wrong number of arguments/) 14 | end 15 | 16 | it "should not compile when too many arguments are passed" do 17 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 18 | Puppet[:code] = '$foo = getvar("foo::bar", "baz")' 19 | expect { 20 | scope.compiler.compile 21 | }.to raise_error(Puppet::ParseError, /wrong number of arguments/) 22 | end 23 | 24 | it "should lookup variables in other namespaces" do 25 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 26 | Puppet[:code] = <<-'ENDofPUPPETcode' 27 | class site::data { $foo = 'baz' } 28 | include site::data 29 | $foo = getvar("site::data::foo") 30 | if $foo != 'baz' { 31 | fail('getvar did not return what we expect') 32 | } 33 | ENDofPUPPETcode 34 | scope.compiler.compile 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/grep_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the grep function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("grep").should == "function_grep" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_grep([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should grep contents from an array" do 16 | result = scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"]) 17 | result.should(eq(["aaabbb","bbbccc"])) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/has_interface_with_spec.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe Puppet::Parser::Functions.function(:has_interface_with) do 5 | 6 | let(:scope) do 7 | PuppetlabsSpec::PuppetInternals.scope 8 | end 9 | 10 | # The subject of these examples is the method itself. 11 | subject do 12 | function_name = Puppet::Parser::Functions.function(:has_interface_with) 13 | scope.method(function_name) 14 | end 15 | 16 | # We need to mock out the Facts so we can specify how we expect this function 17 | # to behave on different platforms. 18 | context "On Mac OS X Systems" do 19 | before :each do 20 | scope.stubs(:lookupvar).with("interfaces").returns('lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0') 21 | end 22 | it 'should have loopback (lo0)' do 23 | subject.call(['lo0']).should be_true 24 | end 25 | it 'should not have loopback (lo)' do 26 | subject.call(['lo']).should be_false 27 | end 28 | end 29 | context "On Linux Systems" do 30 | before :each do 31 | scope.stubs(:lookupvar).with("interfaces").returns('eth0,lo') 32 | scope.stubs(:lookupvar).with("ipaddress").returns('10.0.0.1') 33 | scope.stubs(:lookupvar).with("ipaddress_lo").returns('127.0.0.1') 34 | scope.stubs(:lookupvar).with("ipaddress_eth0").returns('10.0.0.1') 35 | scope.stubs(:lookupvar).with('muppet').returns('kermit') 36 | scope.stubs(:lookupvar).with('muppet_lo').returns('mspiggy') 37 | scope.stubs(:lookupvar).with('muppet_eth0').returns('kermit') 38 | end 39 | it 'should have loopback (lo)' do 40 | subject.call(['lo']).should be_true 41 | end 42 | it 'should not have loopback (lo0)' do 43 | subject.call(['lo0']).should be_false 44 | end 45 | it 'should have ipaddress with 127.0.0.1' do 46 | subject.call(['ipaddress', '127.0.0.1']).should be_true 47 | end 48 | it 'should have ipaddress with 10.0.0.1' do 49 | subject.call(['ipaddress', '10.0.0.1']).should be_true 50 | end 51 | it 'should not have ipaddress with 10.0.0.2' do 52 | subject.call(['ipaddress', '10.0.0.2']).should be_false 53 | end 54 | it 'should have muppet named kermit' do 55 | subject.call(['muppet', 'kermit']).should be_true 56 | end 57 | it 'should have muppet named mspiggy' do 58 | subject.call(['muppet', 'mspiggy']).should be_true 59 | end 60 | it 'should not have muppet named bigbird' do 61 | subject.call(['muppet', 'bigbird']).should be_false 62 | end 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/has_ip_address_spec.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe Puppet::Parser::Functions.function(:has_ip_address) do 5 | 6 | let(:scope) do 7 | PuppetlabsSpec::PuppetInternals.scope 8 | end 9 | 10 | subject do 11 | function_name = Puppet::Parser::Functions.function(:has_ip_address) 12 | scope.method(function_name) 13 | end 14 | 15 | context "On Linux Systems" do 16 | before :each do 17 | scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo') 18 | scope.stubs(:lookupvar).with('ipaddress').returns('10.0.2.15') 19 | scope.stubs(:lookupvar).with('ipaddress_eth0').returns('10.0.2.15') 20 | scope.stubs(:lookupvar).with('ipaddress_lo').returns('127.0.0.1') 21 | end 22 | 23 | it 'should have primary address (10.0.2.15)' do 24 | subject.call(['10.0.2.15']).should be_true 25 | end 26 | 27 | it 'should have lookupback address (127.0.0.1)' do 28 | subject.call(['127.0.0.1']).should be_true 29 | end 30 | 31 | it 'should not have other address' do 32 | subject.call(['192.1681.1.1']).should be_false 33 | end 34 | 35 | it 'should not have "mspiggy" on an interface' do 36 | subject.call(['mspiggy']).should be_false 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/has_ip_network_spec.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe Puppet::Parser::Functions.function(:has_ip_network) do 5 | 6 | let(:scope) do 7 | PuppetlabsSpec::PuppetInternals.scope 8 | end 9 | 10 | subject do 11 | function_name = Puppet::Parser::Functions.function(:has_ip_network) 12 | scope.method(function_name) 13 | end 14 | 15 | context "On Linux Systems" do 16 | before :each do 17 | scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo') 18 | scope.stubs(:lookupvar).with('network').returns(:undefined) 19 | scope.stubs(:lookupvar).with('network_eth0').returns('10.0.2.0') 20 | scope.stubs(:lookupvar).with('network_lo').returns('127.0.0.1') 21 | end 22 | 23 | it 'should have primary network (10.0.2.0)' do 24 | subject.call(['10.0.2.0']).should be_true 25 | end 26 | 27 | it 'should have loopback network (127.0.0.0)' do 28 | subject.call(['127.0.0.1']).should be_true 29 | end 30 | 31 | it 'should not have other network' do 32 | subject.call(['192.168.1.0']).should be_false 33 | end 34 | end 35 | end 36 | 37 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/has_key_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe Puppet::Parser::Functions.function(:has_key) do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | describe 'when calling has_key from puppet' do 8 | it "should not compile when no arguments are passed" do 9 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 10 | Puppet[:code] = '$x = has_key()' 11 | expect { 12 | scope.compiler.compile 13 | }.to raise_error(Puppet::ParseError, /wrong number of arguments/) 14 | end 15 | 16 | it "should not compile when 1 argument is passed" do 17 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 18 | Puppet[:code] = "$x = has_key('foo')" 19 | expect { 20 | scope.compiler.compile 21 | }.to raise_error(Puppet::ParseError, /wrong number of arguments/) 22 | end 23 | 24 | it "should require the first value to be a Hash" do 25 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 26 | Puppet[:code] = "$x = has_key('foo', 'bar')" 27 | expect { 28 | scope.compiler.compile 29 | }.to raise_error(Puppet::ParseError, /expects the first argument to be a hash/) 30 | end 31 | end 32 | 33 | describe 'when calling the function has_key from a scope instance' do 34 | it 'should detect existing keys' do 35 | scope.function_has_key([{'one' => 1}, 'one']).should be_true 36 | end 37 | 38 | it 'should detect existing keys' do 39 | scope.function_has_key([{'one' => 1}, 'two']).should be_false 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/hash_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the hash function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("hash").should == "function_hash" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_hash([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should convert an array to a hash" do 16 | result = scope.function_hash([['a',1,'b',2,'c',3]]) 17 | result.should(eq({'a'=>1,'b'=>2,'c'=>3})) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_array_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_array function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_array").should == "function_is_array" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_array([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if passed an array" do 16 | result = scope.function_is_array([[1,2,3]]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return false if passed a hash" do 21 | result = scope.function_is_array([{'a'=>1}]) 22 | result.should(eq(false)) 23 | end 24 | 25 | it "should return false if passed a string" do 26 | result = scope.function_is_array(["asdf"]) 27 | result.should(eq(false)) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_domain_name_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_domain_name function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_domain_name").should == "function_is_domain_name" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_domain_name([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if a valid short domain name" do 16 | result = scope.function_is_domain_name(["x.com"]) 17 | result.should(be_true) 18 | end 19 | 20 | it "should return true if the domain is ." do 21 | result = scope.function_is_domain_name(["."]) 22 | result.should(be_true) 23 | end 24 | 25 | it "should return true if the domain is x.com." do 26 | result = scope.function_is_domain_name(["x.com."]) 27 | result.should(be_true) 28 | end 29 | 30 | it "should return true if a valid domain name" do 31 | result = scope.function_is_domain_name(["foo.bar.com"]) 32 | result.should(be_true) 33 | end 34 | 35 | it "should allow domain parts to start with numbers" do 36 | result = scope.function_is_domain_name(["3foo.2bar.com"]) 37 | result.should(be_true) 38 | end 39 | 40 | it "should allow domain to end with a dot" do 41 | result = scope.function_is_domain_name(["3foo.2bar.com."]) 42 | result.should(be_true) 43 | end 44 | 45 | it "should allow a single part domain" do 46 | result = scope.function_is_domain_name(["orange"]) 47 | result.should(be_true) 48 | end 49 | 50 | it "should return false if domain parts start with hyphens" do 51 | result = scope.function_is_domain_name(["-3foo.2bar.com"]) 52 | result.should(be_false) 53 | end 54 | 55 | it "should return true if domain contains hyphens" do 56 | result = scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"]) 57 | result.should(be_true) 58 | end 59 | 60 | it "should return false if domain name contains spaces" do 61 | result = scope.function_is_domain_name(["not valid"]) 62 | result.should(be_false) 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_float_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_float function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_float").should == "function_is_float" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_float([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if a float" do 16 | result = scope.function_is_float(["0.12"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return false if a string" do 21 | result = scope.function_is_float(["asdf"]) 22 | result.should(eq(false)) 23 | end 24 | 25 | it "should return false if an integer" do 26 | result = scope.function_is_float(["3"]) 27 | result.should(eq(false)) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_hash_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_hash function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_hash").should == "function_is_hash" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_hash([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if passed a hash" do 16 | result = scope.function_is_hash([{"a"=>1,"b"=>2}]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return false if passed an array" do 21 | result = scope.function_is_hash([["a","b"]]) 22 | result.should(eq(false)) 23 | end 24 | 25 | it "should return false if passed a string" do 26 | result = scope.function_is_hash(["asdf"]) 27 | result.should(eq(false)) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_integer_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_integer function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_integer").should == "function_is_integer" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_integer([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if an integer" do 16 | result = scope.function_is_integer(["3"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return false if a float" do 21 | result = scope.function_is_integer(["3.2"]) 22 | result.should(eq(false)) 23 | end 24 | 25 | it "should return false if a string" do 26 | result = scope.function_is_integer(["asdf"]) 27 | result.should(eq(false)) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_ip_address_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_ip_address function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_ip_address").should == "function_is_ip_address" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_ip_address([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if an IPv4 address" do 16 | result = scope.function_is_ip_address(["1.2.3.4"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return true if a full IPv6 address" do 21 | result = scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) 22 | result.should(eq(true)) 23 | end 24 | 25 | it "should return true if a compressed IPv6 address" do 26 | result = scope.function_is_ip_address(["fe00::1"]) 27 | result.should(eq(true)) 28 | end 29 | 30 | it "should return false if not valid" do 31 | result = scope.function_is_ip_address(["asdf"]) 32 | result.should(eq(false)) 33 | end 34 | 35 | it "should return false if IP octets out of range" do 36 | result = scope.function_is_ip_address(["1.1.1.300"]) 37 | result.should(eq(false)) 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_mac_address_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_mac_address function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_mac_address").should == "function_is_mac_address" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_mac_address([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if a valid mac address" do 16 | result = scope.function_is_mac_address(["00:a0:1f:12:7f:a0"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return false if octets are out of range" do 21 | result = scope.function_is_mac_address(["00:a0:1f:12:7f:g0"]) 22 | result.should(eq(false)) 23 | end 24 | 25 | it "should return false if not valid" do 26 | result = scope.function_is_mac_address(["not valid"]) 27 | result.should(eq(false)) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_numeric_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_numeric function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_numeric").should == "function_is_numeric" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 argument" do 12 | lambda { scope.function_is_numeric([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if an integer" do 16 | result = scope.function_is_numeric(["3"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return true if a float" do 21 | result = scope.function_is_numeric(["3.2"]) 22 | result.should(eq(true)) 23 | end 24 | 25 | it "should return false if a string" do 26 | result = scope.function_is_numeric(["asdf"]) 27 | result.should(eq(false)) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/is_string_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the is_string function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("is_string").should == "function_is_string" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_is_string([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if a string" do 16 | result = scope.function_is_string(["asdf"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return false if an integer" do 21 | result = scope.function_is_string(["3"]) 22 | result.should(eq(false)) 23 | end 24 | 25 | it "should return false if a float" do 26 | result = scope.function_is_string(["3.23"]) 27 | result.should(eq(false)) 28 | end 29 | 30 | it "should return false if an array" do 31 | result = scope.function_is_string([["a","b","c"]]) 32 | result.should(eq(false)) 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/join_keys_to_values_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the join_keys_to_values function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("join_keys_to_values").should == "function_join_keys_to_values" 9 | end 10 | 11 | it "should raise a ParseError if there are fewer than two arguments" do 12 | lambda { scope.function_join_keys_to_values([{}]) }.should raise_error Puppet::ParseError 13 | end 14 | 15 | it "should raise a ParseError if there are greater than two arguments" do 16 | lambda { scope.function_join_keys_to_values([{}, 'foo', 'bar']) }.should raise_error Puppet::ParseError 17 | end 18 | 19 | it "should raise a TypeError if the first argument is an array" do 20 | lambda { scope.function_join_keys_to_values([[1,2], ',']) }.should raise_error TypeError 21 | end 22 | 23 | it "should raise a TypeError if the second argument is an array" do 24 | lambda { scope.function_join_keys_to_values([{}, [1,2]]) }.should raise_error TypeError 25 | end 26 | 27 | it "should raise a TypeError if the second argument is a number" do 28 | lambda { scope.function_join_keys_to_values([{}, 1]) }.should raise_error TypeError 29 | end 30 | 31 | it "should return an empty array given an empty hash" do 32 | result = scope.function_join_keys_to_values([{}, ":"]) 33 | result.should == [] 34 | end 35 | 36 | it "should join hash's keys to its values" do 37 | result = scope.function_join_keys_to_values([{'a'=>1,2=>'foo',:b=>nil}, ":"]) 38 | result.should =~ ['a:1','2:foo','b:'] 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/join_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the join function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("join").should == "function_join" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_join([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should join an array into a string" do 16 | result = scope.function_join([["a","b","c"], ":"]) 17 | result.should(eq("a:b:c")) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/keys_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the keys function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("keys").should == "function_keys" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_keys([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return an array of keys when given a hash" do 16 | result = scope.function_keys([{'a'=>1, 'b'=>2}]) 17 | # =~ performs 'array with same elements' (set) matching 18 | # For more info see RSpec::Matchers::MatchArray 19 | result.should =~ ['a','b'] 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/lstrip_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the lstrip function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("lstrip").should == "function_lstrip" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_lstrip([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should lstrip a string" do 16 | result = scope.function_lstrip([" asdf"]) 17 | result.should(eq('asdf')) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/max_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe "the max function" do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | 8 | it "should exist" do 9 | Puppet::Parser::Functions.function("max").should == "function_max" 10 | end 11 | 12 | it "should raise a ParseError if there is less than 1 arguments" do 13 | lambda { scope.function_max([]) }.should( raise_error(Puppet::ParseError)) 14 | end 15 | 16 | it "should be able to compare strings" do 17 | scope.function_max(["albatross","dog","horse"]).should(eq("horse")) 18 | end 19 | 20 | it "should be able to compare numbers" do 21 | scope.function_max([6,8,4]).should(eq(8)) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/member_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the member function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("member").should == "function_member" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_member([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if a member is in an array" do 16 | result = scope.function_member([["a","b","c"], "a"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should return false if a member is not in an array" do 21 | result = scope.function_member([["a","b","c"], "d"]) 22 | result.should(eq(false)) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/merge_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe Puppet::Parser::Functions.function(:merge) do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | 8 | describe 'when calling merge from puppet' do 9 | it "should not compile when no arguments are passed" do 10 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 11 | Puppet[:code] = '$x = merge()' 12 | expect { 13 | scope.compiler.compile 14 | }.to raise_error(Puppet::ParseError, /wrong number of arguments/) 15 | end 16 | 17 | it "should not compile when 1 argument is passed" do 18 | pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ 19 | Puppet[:code] = "$my_hash={'one' => 1}\n$x = merge($my_hash)" 20 | expect { 21 | scope.compiler.compile 22 | }.to raise_error(Puppet::ParseError, /wrong number of arguments/) 23 | end 24 | end 25 | 26 | describe 'when calling merge on the scope instance' do 27 | it 'should require all parameters are hashes' do 28 | expect { new_hash = scope.function_merge([{}, '2'])}.should raise_error(Puppet::ParseError, /unexpected argument type String/) 29 | end 30 | 31 | it 'should be able to merge two hashes' do 32 | new_hash = scope.function_merge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}]) 33 | new_hash['one'].should == '1' 34 | new_hash['two'].should == '2' 35 | new_hash['three'].should == '2' 36 | end 37 | 38 | it 'should merge multiple hashes' do 39 | hash = scope.function_merge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}]) 40 | hash['one'].should == '3' 41 | end 42 | 43 | it 'should accept empty hashes' do 44 | scope.function_merge([{},{},{}]).should == {} 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/min_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe "the min function" do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | 8 | it "should exist" do 9 | Puppet::Parser::Functions.function("min").should == "function_min" 10 | end 11 | 12 | it "should raise a ParseError if there is less than 1 arguments" do 13 | lambda { scope.function_min([]) }.should( raise_error(Puppet::ParseError)) 14 | end 15 | 16 | it "should be able to compare strings" do 17 | scope.function_min(["albatross","dog","horse"]).should(eq("albatross")) 18 | end 19 | 20 | it "should be able to compare numbers" do 21 | scope.function_min([6,8,4]).should(eq(4)) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/num2bool_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the num2bool function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("num2bool").should == "function_num2bool" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return true if 1" do 16 | result = scope.function_num2bool(["1"]) 17 | result.should(be_true) 18 | end 19 | 20 | it "should return false if 0" do 21 | result = scope.function_num2bool(["0"]) 22 | result.should(be_false) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/parsejson_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the parsejson function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("parsejson").should == "function_parsejson" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_parsejson([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should convert JSON to a data structure" do 16 | json = <<-EOS 17 | ["aaa","bbb","ccc"] 18 | EOS 19 | result = scope.function_parsejson([json]) 20 | result.should(eq(['aaa','bbb','ccc'])) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/parseyaml_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the parseyaml function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("parseyaml").should == "function_parseyaml" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_parseyaml([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should convert YAML to a data structure" do 16 | yaml = <<-EOS 17 | - aaa 18 | - bbb 19 | - ccc 20 | EOS 21 | result = scope.function_parseyaml([yaml]) 22 | result.should(eq(['aaa','bbb','ccc'])) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/pick_spec.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the pick function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("pick").should == "function_pick" 9 | end 10 | 11 | it 'should return the correct value' do 12 | scope.function_pick(['first', 'second']).should == 'first' 13 | end 14 | 15 | it 'should return the correct value if the first value is empty' do 16 | scope.function_pick(['', 'second']).should == 'second' 17 | end 18 | 19 | it 'should remove empty string values' do 20 | scope.function_pick(['', 'first']).should == 'first' 21 | end 22 | 23 | it 'should remove :undef values' do 24 | scope.function_pick([:undef, 'first']).should == 'first' 25 | end 26 | 27 | it 'should remove :undefined values' do 28 | scope.function_pick([:undefined, 'first']).should == 'first' 29 | end 30 | 31 | it 'should error if no values are passed' do 32 | expect { scope.function_pick([]) }.to raise_error(Puppet::Error, /Must provide non empty value./) 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/prefix_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the prefix function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("prefix").should == "function_prefix" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_prefix([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return a prefixed array" do 16 | result = scope.function_prefix([['a','b','c'], 'p']) 17 | result.should(eq(['pa','pb','pc'])) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/range_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the range function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("range").should == "function_range" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return a letter range" do 16 | result = scope.function_range(["a","d"]) 17 | result.should(eq(['a','b','c','d'])) 18 | end 19 | 20 | it "should return a number range" do 21 | result = scope.function_range(["1","4"]) 22 | result.should(eq([1,2,3,4])) 23 | end 24 | 25 | it "should work with padded hostname like strings" do 26 | expected = ("host01".."host10").to_a 27 | scope.function_range(["host01","host10"]).should eq expected 28 | end 29 | 30 | it "should coerce zero padded digits to integers" do 31 | expected = (0..10).to_a 32 | scope.function_range(["00", "10"]).should eq expected 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/reject_spec.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'spec_helper' 4 | 5 | describe "the reject function" do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | 8 | it "should exist" do 9 | Puppet::Parser::Functions.function("reject").should == "function_reject" 10 | end 11 | 12 | it "should raise a ParseError if there is less than 1 arguments" do 13 | lambda { scope.function_reject([]) }.should( raise_error(Puppet::ParseError)) 14 | end 15 | 16 | it "should reject contents from an array" do 17 | result = scope.function_reject([["1111", "aaabbb","bbbccc","dddeee"], "bbb"]) 18 | result.should(eq(["1111", "dddeee"])) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/reverse_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the reverse function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("reverse").should == "function_reverse" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_reverse([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should reverse a string" do 16 | result = scope.function_reverse(["asdfghijkl"]) 17 | result.should(eq('lkjihgfdsa')) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/rstrip_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the rstrip function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("rstrip").should == "function_rstrip" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_rstrip([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should rstrip a string" do 16 | result = scope.function_rstrip(["asdf "]) 17 | result.should(eq('asdf')) 18 | end 19 | 20 | it "should rstrip each element in an array" do 21 | result = scope.function_rstrip([["a ","b ", "c "]]) 22 | result.should(eq(['a','b','c'])) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/shuffle_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the shuffle function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("shuffle").should == "function_shuffle" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_shuffle([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should shuffle a string and the result should be the same size" do 16 | result = scope.function_shuffle(["asdf"]) 17 | result.size.should(eq(4)) 18 | end 19 | 20 | it "should shuffle a string but the sorted contents should still be the same" do 21 | result = scope.function_shuffle(["adfs"]) 22 | result.split("").sort.join("").should(eq("adfs")) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/size_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the size function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("size").should == "function_size" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_size([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return the size of a string" do 16 | result = scope.function_size(["asdf"]) 17 | result.should(eq(4)) 18 | end 19 | 20 | it "should return the size of an array" do 21 | result = scope.function_size([["a","b","c"]]) 22 | result.should(eq(3)) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/sort_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the sort function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("sort").should == "function_sort" 9 | end 10 | 11 | it "should raise a ParseError if there is not 1 arguments" do 12 | lambda { scope.function_sort(['','']) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should sort an array" do 16 | result = scope.function_sort([["a","c","b"]]) 17 | result.should(eq(['a','b','c'])) 18 | end 19 | 20 | it "should sort a string" do 21 | result = scope.function_sort(["acb"]) 22 | result.should(eq('abc')) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/squeeze_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the squeeze function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("squeeze").should == "function_squeeze" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 2 arguments" do 12 | lambda { scope.function_squeeze([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should squeeze a string" do 16 | result = scope.function_squeeze(["aaabbbbcccc"]) 17 | result.should(eq('abc')) 18 | end 19 | 20 | it "should squeeze all elements in an array" do 21 | result = scope.function_squeeze([["aaabbbbcccc","dddfff"]]) 22 | result.should(eq(['abc','df'])) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/str2bool_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the str2bool function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("str2bool").should == "function_str2bool" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_str2bool([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should convert string 'true' to true" do 16 | result = scope.function_str2bool(["true"]) 17 | result.should(eq(true)) 18 | end 19 | 20 | it "should convert string 'undef' to false" do 21 | result = scope.function_str2bool(["undef"]) 22 | result.should(eq(false)) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the str2saltedsha512 function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("str2saltedsha512").should == "function_str2saltedsha512" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 argument" do 12 | expect { scope.function_str2saltedsha512([]) }.should( raise_error(Puppet::ParseError) ) 13 | end 14 | 15 | it "should raise a ParseError if there is more than 1 argument" do 16 | expect { scope.function_str2saltedsha512(['foo', 'bar', 'baz']) }.should( raise_error(Puppet::ParseError) ) 17 | end 18 | 19 | it "should return a salted-sha512 password hash 136 characters in length" do 20 | result = scope.function_str2saltedsha512(["password"]) 21 | result.length.should(eq(136)) 22 | end 23 | 24 | it "should raise an error if you pass a non-string password" do 25 | expect { scope.function_str2saltedsha512([1234]) }.should( raise_error(Puppet::ParseError) ) 26 | end 27 | 28 | it "should generate a valid password" do 29 | # Allow the function to generate a password based on the string 'password' 30 | password_hash = scope.function_str2saltedsha512(["password"]) 31 | 32 | # Separate the Salt and Password from the Password Hash 33 | salt = password_hash[0..7] 34 | password = password_hash[8..-1] 35 | 36 | # Convert the Salt and Password from Hex to Binary Data 37 | str_salt = Array(salt.lines).pack('H*') 38 | str_password = Array(password.lines).pack('H*') 39 | 40 | # Combine the Binary Salt with 'password' and compare the end result 41 | saltedpass = Digest::SHA512.digest(str_salt + 'password') 42 | result = (str_salt + saltedpass).unpack('H*')[0] 43 | result.should == password_hash 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/strftime_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the strftime function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("strftime").should == "function_strftime" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_strftime([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "using %s should be higher then when I wrote this test" do 16 | result = scope.function_strftime(["%s"]) 17 | result.to_i.should(be > 1311953157) 18 | end 19 | 20 | it "using %s should be lower then 1.5 trillion" do 21 | result = scope.function_strftime(["%s"]) 22 | result.to_i.should(be < 1500000000) 23 | end 24 | 25 | it "should return a date when given %Y-%m-%d" do 26 | result = scope.function_strftime(["%Y-%m-%d"]) 27 | result.should =~ /^\d{4}-\d{2}-\d{2}$/ 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/strip_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the strip function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | it "should exist" do 7 | Puppet::Parser::Functions.function("strip").should == "function_strip" 8 | end 9 | 10 | it "should raise a ParseError if there is less than 1 arguments" do 11 | lambda { scope.function_strip([]) }.should( raise_error(Puppet::ParseError)) 12 | end 13 | 14 | it "should strip a string" do 15 | result = scope.function_strip([" ab cd "]) 16 | result.should(eq('ab cd')) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/swapcase_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the swapcase function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("swapcase").should == "function_swapcase" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_swapcase([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should swapcase a string" do 16 | result = scope.function_swapcase(["aaBBccDD"]) 17 | result.should(eq('AAbbCCdd')) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/time_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the time function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("time").should == "function_time" 9 | end 10 | 11 | it "should raise a ParseError if there is more than 2 arguments" do 12 | lambda { scope.function_time(['','']) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should return a number" do 16 | result = scope.function_time([]) 17 | result.should be_an(Integer) 18 | end 19 | 20 | it "should be higher then when I wrote this test" do 21 | result = scope.function_time([]) 22 | result.should(be > 1311953157) 23 | end 24 | 25 | it "should be lower then 1.5 trillion" do 26 | result = scope.function_time([]) 27 | result.should(be < 1500000000) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/to_bytes_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe "the to_bytes function" do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | 8 | it "should exist" do 9 | Puppet::Parser::Functions.function("to_bytes").should == "function_to_bytes" 10 | end 11 | 12 | it "should raise a ParseError if there is less than 1 arguments" do 13 | lambda { scope.function_to_bytes([]) }.should( raise_error(Puppet::ParseError)) 14 | end 15 | 16 | it "should convert kB to B" do 17 | result = scope.function_to_bytes(["4 kB"]) 18 | result.should(eq(4096)) 19 | end 20 | 21 | it "should work without B in unit" do 22 | result = scope.function_to_bytes(["4 k"]) 23 | result.should(eq(4096)) 24 | end 25 | 26 | it "should work without a space before unit" do 27 | result = scope.function_to_bytes(["4k"]) 28 | result.should(eq(4096)) 29 | end 30 | 31 | it "should work without a unit" do 32 | result = scope.function_to_bytes(["5678"]) 33 | result.should(eq(5678)) 34 | end 35 | 36 | it "should convert fractions" do 37 | result = scope.function_to_bytes(["1.5 kB"]) 38 | result.should(eq(1536)) 39 | end 40 | 41 | it "should convert scientific notation" do 42 | result = scope.function_to_bytes(["1.5e2 B"]) 43 | result.should(eq(150)) 44 | end 45 | 46 | it "should do nothing with a positive number" do 47 | result = scope.function_to_bytes([5678]) 48 | result.should(eq(5678)) 49 | end 50 | 51 | it "should should raise a ParseError if input isn't a number" do 52 | lambda { scope.function_to_bytes(["foo"]) }.should( raise_error(Puppet::ParseError)) 53 | end 54 | 55 | it "should should raise a ParseError if prefix is unknown" do 56 | lambda { scope.function_to_bytes(["5 uB"]) }.should( raise_error(Puppet::ParseError)) 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/type_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the type function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | it "should exist" do 7 | Puppet::Parser::Functions.function("type").should == "function_type" 8 | end 9 | 10 | it "should raise a ParseError if there is less than 1 arguments" do 11 | lambda { scope.function_type([]) }.should( raise_error(Puppet::ParseError)) 12 | end 13 | 14 | it "should return string when given a string" do 15 | result = scope.function_type(["aaabbbbcccc"]) 16 | result.should(eq('string')) 17 | end 18 | 19 | it "should return array when given an array" do 20 | result = scope.function_type([["aaabbbbcccc","asdf"]]) 21 | result.should(eq('array')) 22 | end 23 | 24 | it "should return hash when given a hash" do 25 | result = scope.function_type([{"a"=>1,"b"=>2}]) 26 | result.should(eq('hash')) 27 | end 28 | 29 | it "should return integer when given an integer" do 30 | result = scope.function_type(["1"]) 31 | result.should(eq('integer')) 32 | end 33 | 34 | it "should return float when given a float" do 35 | result = scope.function_type(["1.34"]) 36 | result.should(eq('float')) 37 | end 38 | 39 | it "should return boolean when given a boolean" do 40 | result = scope.function_type([true]) 41 | result.should(eq('boolean')) 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/unique_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the unique function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("unique").should == "function_unique" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_unique([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should remove duplicate elements in a string" do 16 | result = scope.function_unique(["aabbc"]) 17 | result.should(eq('abc')) 18 | end 19 | 20 | it "should remove duplicate elements in an array" do 21 | result = scope.function_unique([["a","a","b","b","c"]]) 22 | result.should(eq(['a','b','c'])) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/upcase_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the upcase function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("upcase").should == "function_upcase" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_upcase([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should upcase a string" do 16 | result = scope.function_upcase(["abc"]) 17 | result.should(eq('ABC')) 18 | end 19 | 20 | it "should do nothing if a string is already upcase" do 21 | result = scope.function_upcase(["ABC"]) 22 | result.should(eq('ABC')) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/uriescape_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | require 'spec_helper' 3 | 4 | describe "the uriescape function" do 5 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 6 | 7 | it "should exist" do 8 | Puppet::Parser::Functions.function("uriescape").should == "function_uriescape" 9 | end 10 | 11 | it "should raise a ParseError if there is less than 1 arguments" do 12 | lambda { scope.function_uriescape([]) }.should( raise_error(Puppet::ParseError)) 13 | end 14 | 15 | it "should uriescape a string" do 16 | result = scope.function_uriescape([":/?#[]@!$&'()*+,;= "]) 17 | result.should(eq('%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%20')) 18 | end 19 | 20 | it "should do nothing if a string is already safe" do 21 | result = scope.function_uriescape(["ABCdef"]) 22 | result.should(eq('ABCdef')) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/validate_absolute_path_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Puppet::Parser::Functions.function(:validate_absolute_path) do 4 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 5 | 6 | # The subject of these examples 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_absolute_path) 10 | scope.method(function_name) 11 | end 12 | 13 | describe "Valid Paths" do 14 | def self.valid_paths 15 | %w{ 16 | C:/ 17 | C:\\ 18 | C:\\WINDOWS\\System32 19 | C:/windows/system32 20 | X:/foo/bar 21 | X:\\foo\\bar 22 | /var/tmp 23 | /var/lib/puppet 24 | /var/opt/../lib/puppet 25 | } 26 | end 27 | 28 | context "Without Puppet::Util.absolute_path? (e.g. Puppet <= 2.6)" do 29 | before :each do 30 | # The intent here is to mock Puppet to behave like Puppet 2.6 does. 31 | # Puppet 2.6 does not have the absolute_path? method. This is only a 32 | # convenience test, stdlib should be run with the Puppet 2.6.x in the 33 | # $LOAD_PATH in addition to 2.7.x and master. 34 | Puppet::Util.expects(:respond_to?).with(:absolute_path?).returns(false) 35 | end 36 | valid_paths.each do |path| 37 | it "validate_absolute_path(#{path.inspect}) should not fail" do 38 | expect { subject.call [path] }.not_to raise_error Puppet::ParseError 39 | end 40 | end 41 | end 42 | 43 | context "Puppet without mocking" do 44 | valid_paths.each do |path| 45 | it "validate_absolute_path(#{path.inspect}) should not fail" do 46 | expect { subject.call [path] }.not_to raise_error Puppet::ParseError 47 | end 48 | end 49 | end 50 | end 51 | 52 | describe 'Invalid paths' do 53 | context 'Garbage inputs' do 54 | [ 55 | nil, 56 | [ nil ], 57 | { 'foo' => 'bar' }, 58 | { }, 59 | '', 60 | ].each do |path| 61 | it "validate_absolute_path(#{path.inspect}) should fail" do 62 | expect { subject.call [path] }.to raise_error Puppet::ParseError 63 | end 64 | end 65 | end 66 | 67 | context 'Relative paths' do 68 | %w{ 69 | relative1 70 | . 71 | .. 72 | ./foo 73 | ../foo 74 | etc/puppetlabs/puppet 75 | opt/puppet/bin 76 | }.each do |path| 77 | it "validate_absolute_path(#{path.inspect}) should fail" do 78 | expect { subject.call [path] }.to raise_error Puppet::ParseError 79 | end 80 | end 81 | end 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/validate_array_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby -S rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe Puppet::Parser::Functions.function(:validate_array) do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | describe 'when calling validate_array from puppet' do 8 | 9 | %w{ true false }.each do |the_string| 10 | it "should not compile when #{the_string} is a string" do 11 | Puppet[:code] = "validate_array('#{the_string}')" 12 | expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/) 13 | end 14 | 15 | it "should not compile when #{the_string} is a bare word" do 16 | Puppet[:code] = "validate_array(#{the_string})" 17 | expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/) 18 | end 19 | end 20 | 21 | it "should compile when multiple array arguments are passed" do 22 | Puppet[:code] = <<-'ENDofPUPPETcode' 23 | $foo = [ ] 24 | $bar = [ 'one', 'two' ] 25 | validate_array($foo, $bar) 26 | ENDofPUPPETcode 27 | scope.compiler.compile 28 | end 29 | 30 | it "should not compile when an undef variable is passed" do 31 | Puppet[:code] = <<-'ENDofPUPPETcode' 32 | $foo = undef 33 | validate_array($foo) 34 | ENDofPUPPETcode 35 | expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/) 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/validate_bool_spec.rb: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env/ruby -S rspec 2 | 3 | require 'spec_helper' 4 | 5 | describe Puppet::Parser::Functions.function(:validate_bool) do 6 | let(:scope) { PuppetlabsSpec::PuppetInternals.scope } 7 | describe 'when calling validate_bool from puppet' do 8 | 9 | %w{ true false }.each do |the_string| 10 | 11 | it "should not compile when #{the_string} is a string" do 12 | Puppet[:code] = "validate_bool('#{the_string}')" 13 | expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/) 14 | end 15 | 16 | it "should compile when #{the_string} is a bare word" do 17 | Puppet[:code] = "validate_bool(#{the_string})" 18 | scope.compiler.compile 19 | end 20 | 21 | end 22 | 23 | it "should not compile when an arbitrary string is passed" do 24 | Puppet[:code] = 'validate_bool("jeff and dan are awesome")' 25 | expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/) 26 | end 27 | 28 | it "should not compile when no arguments are passed" do 29 | Puppet[:code] = 'validate_bool()' 30 | expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /wrong number of arguments/) 31 | end 32 | 33 | it "should compile when multiple boolean arguments are passed" do 34 | Puppet[:code] = <<-'ENDofPUPPETcode' 35 | $foo = true 36 | $bar = false 37 | validate_bool($foo, $bar, true, false) 38 | ENDofPUPPETcode 39 | scope.compiler.compile 40 | end 41 | 42 | it "should compile when multiple boolean arguments are passed" do 43 | Puppet[:code] = <<-'ENDofPUPPETcode' 44 | $foo = true 45 | $bar = false 46 | validate_bool($foo, $bar, true, false, 'jeff') 47 | ENDofPUPPETcode 48 | expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/) 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/validate_hash_spec.rb: -------------------------------------------------------------------------------- 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 }.should 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 }.should 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 }.should raise_error(Puppet::ParseError, /is not a Hash/) 39 | end 40 | 41 | end 42 | 43 | end 44 | 45 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/validate_slength_spec.rb: -------------------------------------------------------------------------------- 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 49 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/validate_string_spec.rb: -------------------------------------------------------------------------------- 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 }.should 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 61 | 62 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/values_at_spec.rb: -------------------------------------------------------------------------------- 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 39 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/values_spec.rb: -------------------------------------------------------------------------------- 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 32 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/parser/functions/zip_spec.rb: -------------------------------------------------------------------------------- 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 16 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/unit/puppet/type/anchor_spec.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'puppet' 4 | 5 | anchor = Puppet::Type.type(:anchor).new(:name => "ntp::begin") 6 | 7 | describe anchor do 8 | it "should stringify normally" do 9 | anchor.to_s.should == "Anchor[ntp::begin]" 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/spec/watchr.rb: -------------------------------------------------------------------------------- 1 | ENV['FOG_MOCK'] ||= 'true' 2 | ENV['AUTOTEST'] = 'true' 3 | ENV['WATCHR'] = '1' 4 | 5 | system 'clear' 6 | 7 | def growl(message) 8 | growlnotify = `which growlnotify`.chomp 9 | title = "Watchr Test Results" 10 | image = case message 11 | when /(\d+)\s+?(failure|error)/i 12 | ($1.to_i == 0) ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png" 13 | else 14 | '~/.watchr_images/unknown.png' 15 | end 16 | options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'" 17 | system %(#{growlnotify} #{options} &) 18 | end 19 | 20 | def run(cmd) 21 | puts(cmd) 22 | `#{cmd}` 23 | end 24 | 25 | def run_spec_test(file) 26 | if File.exist? file 27 | result = run "rspec --format p --color #{file}" 28 | growl result.split("\n").last 29 | puts result 30 | else 31 | puts "FIXME: No test #{file} [#{Time.now}]" 32 | end 33 | end 34 | 35 | def filter_rspec(data) 36 | data.split("\n").find_all do |l| 37 | l =~ /^(\d+)\s+exampl\w+.*?(\d+).*?failur\w+.*?(\d+).*?pending/ 38 | end.join("\n") 39 | end 40 | 41 | def run_all_tests 42 | system('clear') 43 | files = Dir.glob("spec/**/*_spec.rb").join(" ") 44 | result = run "rspec #{files}" 45 | growl_results = filter_rspec result 46 | growl growl_results 47 | puts result 48 | puts "GROWL: #{growl_results}" 49 | end 50 | 51 | # Ctrl-\ 52 | Signal.trap 'QUIT' do 53 | puts " --- Running all tests ---\n\n" 54 | run_all_tests 55 | end 56 | 57 | @interrupted = false 58 | 59 | # Ctrl-C 60 | Signal.trap 'INT' do 61 | if @interrupted then 62 | @wants_to_quit = true 63 | abort("\n") 64 | else 65 | puts "Interrupt a second time to quit" 66 | @interrupted = true 67 | Kernel.sleep 1.5 68 | # raise Interrupt, nil # let the run loop catch it 69 | run_suite 70 | end 71 | end 72 | 73 | def file2spec(file) 74 | result = file.sub('lib/puppet/', 'spec/unit/puppet/').gsub(/\.rb$/, '_spec.rb') 75 | result = file.sub('lib/facter/', 'spec/unit/facter/').gsub(/\.rb$/, '_spec.rb') 76 | end 77 | 78 | 79 | watch( 'spec/.*_spec\.rb' ) do |md| 80 | #run_spec_test(md[0]) 81 | run_all_tests 82 | end 83 | watch( 'lib/.*\.rb' ) do |md| 84 | # run_spec_test(file2spec(md[0])) 85 | run_all_tests 86 | end 87 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/tests/file_line.pp: -------------------------------------------------------------------------------- 1 | # This is a simple smoke test 2 | # of the file_line resource type. 3 | file { '/tmp/dansfile': 4 | ensure => present 5 | }-> 6 | file_line { 'dans_line': 7 | line => 'dan is awesome', 8 | path => '/tmp/dansfile', 9 | } 10 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/tests/has_interface_with.pp: -------------------------------------------------------------------------------- 1 | include stdlib 2 | info("has_interface_with('lo'):", has_interface_with('lo')) 3 | info("has_interface_with('loX'):", has_interface_with('loX')) 4 | info("has_interface_with('ipaddress', '127.0.0.1'):", has_interface_with('ipaddress', '127.0.0.1')) 5 | info("has_interface_with('ipaddress', '127.0.0.100'):", has_interface_with('ipaddress', '127.0.0.100')) 6 | info("has_interface_with('network', '127.0.0.0'):", has_interface_with('network', '127.0.0.0')) 7 | info("has_interface_with('network', '128.0.0.0'):", has_interface_with('network', '128.0.0.0')) 8 | info("has_interface_with('netmask', '255.0.0.0'):", has_interface_with('netmask', '255.0.0.0')) 9 | info("has_interface_with('netmask', '256.0.0.0'):", has_interface_with('netmask', '256.0.0.0')) 10 | 11 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/tests/has_ip_address.pp: -------------------------------------------------------------------------------- 1 | include stdlib 2 | info("has_ip_address('192.168.1.256'):", has_ip_address('192.168.1.256')) 3 | info("has_ip_address('127.0.0.1'):", has_ip_address('127.0.0.1')) 4 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/tests/has_ip_network.pp: -------------------------------------------------------------------------------- 1 | include stdlib 2 | info("has_ip_network('127.0.0.0'):", has_ip_network('127.0.0.0')) 3 | info("has_ip_network('128.0.0.0'):", has_ip_network('128.0.0.0')) 4 | 5 | -------------------------------------------------------------------------------- /puppet/modules/stdlib/tests/init.pp: -------------------------------------------------------------------------------- 1 | include stdlib 2 | -------------------------------------------------------------------------------- /python-whisper_0.9.12-1_all.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tilmans/vagrant-statsd-graphite-puppet/19bca5a9ba0d4a6cccc8a6531147ee77ea467dc7/python-whisper_0.9.12-1_all.deb -------------------------------------------------------------------------------- /statsd_0.6.0-1_all.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tilmans/vagrant-statsd-graphite-puppet/19bca5a9ba0d4a6cccc8a6531147ee77ea467dc7/statsd_0.6.0-1_all.deb --------------------------------------------------------------------------------