Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dump_args statement function for debugging puppet code #30

Merged
merged 1 commit into from
Feb 26, 2016

Conversation

logicminds
Copy link
Contributor

This adds a new function that outputs the variables to STDOUT. Its pretty dead simple and when used with an rspec test it can decrease debug time greatly. The only other option is to use a bunch of notify resources which to me is not as fast and does not display the variables correctly when you have hashes and arrays in your variables. If you think this is not useful imagine storing a large hash in hiera and wanting to know why your custom parser is blowing up on your data and the notify statement does a poor job of displaying your 50 line data hash. Its really nice to see whats in your puppet variables and this helps immensely.

Another use case is trying to find out why create_resources isn't working as expected because your data being passed in is all wrong.

Basically it works like this:

puppet code below:

$var1 = 'foo'
$var2 = ['bar']
$var3 = {'foo' => 'bar'}
dump_args($var1, $var2, $var3)

yields the following in STDOUT

[
  "foo",
  [
    "bar"
  ],
  {
    "foo": "bar"
  }
]

@igalic
Copy link
Contributor

igalic commented Oct 14, 2015

@logicminds thank you for updating this!
here's a diff that should make… a better config of rubocop pass:

diff --git a/lib/puppet/parser/functions/default_content.rb b/lib/puppet/parser/functions/default_content.rb
index dc38c02..8b16050 100644
--- a/lib/puppet/parser/functions/default_content.rb
+++ b/lib/puppet/parser/functions/default_content.rb
@@ -1,22 +1,20 @@
-module Puppet::Parser::Functions
-  Puppet::Parser::Functions.newfunction(:default_content,
-                                        :type => :rvalue,
-                                        :doc => <<-'ENDOFDOC'
+Puppet::Parser::Functions.newfunction(:default_content,
+                                      :type => :rvalue,
+                                      :doc => <<-'ENDOFDOC'
   Takes an optional content and an optional template name to calculate the actual
   contents of a file.

   This small function abbreviates the default initialisation boilerplate of
   stdmod modules.
   ENDOFDOC
-  ) do |args|
-    content = args[0]
-    template_name = args[1]
+) do |args|
+  content = args[0]
+  template_name = args[1]

-    Puppet::Parser::Functions.autoloader.loadall
+  Puppet::Parser::Functions.autoloader.loadall

-    return content if content != ''
-    return function_template(template_name) if template_name
+  return content if content != ''
+  return function_template(template_name) if template_name

-    return nil
-  end
+  return nil
 end
diff --git a/lib/puppet/parser/functions/dump_args.rb b/lib/puppet/parser/functions/dump_args.rb
index 37f7587..1db1c6b 100644
--- a/lib/puppet/parser/functions/dump_args.rb
+++ b/lib/puppet/parser/functions/dump_args.rb
@@ -1,17 +1,13 @@
 require 'json'
 require 'puppet/parser/functions'

-module Puppet::Parser::Functions
-  newfunction(:dump_args, :type => :statement,:doc => <<-EOS
+Puppet::Parser::Functions.newfunction(:dump_args, :type => :statement, :doc => <<-EOS
      dump_args - prints the args to STDOUT in Pretty JSON format.

      Useful for debugging purposes only. Ideally you would use this in
      conjunction with a rspec-puppet unit test.  Otherwise the output will
      be shown during a puppet run when verbose/debug options are enabled.
-
   EOS
-  )  do |args|
-    puts JSON.pretty_generate(args)
-  end
-
+) do |args|
+  puts JSON.pretty_generate(args)
 end
diff --git a/spec/unit/puppet/parser/functions/dump_args_spec.rb b/spec/unit/puppet/parser/functions/dump_args_spec.rb
index 79d32c9..b42e1e8 100644
--- a/spec/unit/puppet/parser/functions/dump_args_spec.rb
+++ b/spec/unit/puppet/parser/functions/dump_args_spec.rb
@@ -1,10 +1,9 @@
 require 'spec_helper'
 require 'json'
-describe "dump_args" do
+describe 'dump_args' do
   let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

-  it "should exist" do
-    Puppet::Parser::Functions.function("dump_args").should == "function_dump_args"
+  it 'should exist' do
+    Puppet::Parser::Functions.function('dump_args').should == 'function_dump_args'
   end
-
 end

@igalic
Copy link
Contributor

igalic commented Oct 14, 2015

you can join the rubocop discussion here: voxpupuli/modulesync_config#14

@logicminds
Copy link
Contributor Author

@igalic just rebased after applying the diff. Rubocop only warns on some things that should be excluded which I commented on in the rubocop discussion.

@jyaworski
Copy link
Member

@logicminds can you rebase and try again?

@logicminds
Copy link
Contributor Author

@jyaworski justed rebased

@jyaworski
Copy link
Member

Merging despite rubocop.....

jyaworski added a commit that referenced this pull request Feb 26, 2016
Added dump_args statement function for debugging puppet code
@jyaworski jyaworski merged commit e20d82d into voxpupuli:master Feb 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants