The pdk validate command

The PDK provides pdk validate to check both syntax and style. Syntax checks make sure that your code can compile, and that you're not missing things such as commas or closing braces in manifests or JSON metadata. Syntax checks can also be performed manually on manifests with puppet parser validate. Style checking looks at the code to make sure that it adheres to a standard style guide. Puppet-lint is used to provide style checks to Puppet, and all of the rules can be found at http://puppet-lint.com/. When a module is healthy, the PDK will return check marks against all tasks:

$ pdk validate
pdk (INFO): Running all available validators...
pdk (INFO): Using Ruby 2.4.4
pdk (INFO): Using Puppet 5.5.1
[] Checking metadata syntax (metadata.json tasks/*.json).
[] Checking module metadata style (metadata.json).
[] Checking task metadata style (tasks/*.json).
[] Checking Puppet manifest syntax (**/**.pp).
[] Checking Puppet manifest style (**/*.pp).
[] Checking Ruby code style (**/**.rb).

An invalid metadata.json will prevent the uploading of modules to the Forge and the running of RSpec tests. This file details the author of the module, and other information, such as dependencies and supported operating systems:

#Invalid Metadata.json

$ pdk validate
/opt/puppetlabs/pdk/private/ruby/2.4.4/lib/ruby/gems/2.4.0/gems/pdk-1.5.0/lib/pdk/module/metadata.rb:142:in `validate_name': Invalid 'name' field in metadata.json: Field must be a dash-separated user name and module name. (ArgumentError)

pdk validate also runs Puppet parser validation across every manifest in the module. In the following example, a curly brace was forgotten at the end of init.pp, and the PDK is informing us that the code will not compile:

# Failed Parser Validation
# Can be ran alone with puppet parser validate

$ pdk validate
pdk (INFO): Running all available validators...
pdk (INFO): Using Ruby 2.4.4
pdk (INFO): Using Puppet 5.5.1
[] Checking metadata syntax (metadata.json tasks/*.json).
[] Checking module metadata style (metadata.json).
[] Checking Puppet manifest syntax (**/**.pp).
[] Checking Ruby code style (**/**.rb).
info: task-metadata-lint: ./: Target does not contain any files to validate (tasks/*.json).
Error: puppet-syntax: manifests/init.pp:9:1: Could not parse for environment production: Syntax error at '}'

If the Puppet parser validation passes, puppet-lint will run on all manifests. It will print out errors and warnings in the code, based on the Puppet Style Guide. In the following example, we run pdk validate against a manifest has a line that continues beyond 140 characters on line 10 and trailing whitespace after line 9:

$ pdk validate
pdk (INFO): Running all available validators...
pdk (INFO): Using Ruby 2.4.4
pdk (INFO): Using Puppet 5.5.1
[] Checking metadata syntax (metadata.json tasks/*.json).
[] Checking module metadata style (metadata.json).
[]Checking Puppet manifest syntax (**/**.pp).
[] Checking Puppet manifest style (**/*.pp).
[] Checking Ruby code style (**/**.rb).
info: task-metadata-lint: ./: Target does not contain any files to validate (tasks/*.json).
warning: puppet-lint: manifests/init.pp:10:140: line has more than 140 characters
error: puppet-lint: manifests/init.pp:9:28: trailing whitespace found

In some cases, rather than print out a warning or error, we want to disable it. A list of checks can be found at http://puppet-lint.com/checks/, and can be used to disable individual checks. In the following example, notice the comment after the message statement, telling lint to ignore the 140-character limit:

# A description of what this class does
#
# @summary A short summary of the purpose of this class
#
# @example
# include module
class module {

notify {'String-trigger':
message =>'This is the string that never ends. Yes it goes on and on my friends. Some developer just started writing without line breaks not knowing what they do, so this string will go on forever just because...' # lint:ignore:140chars
}

}

If we have multiple places in a single manifest that we'd like to ignore, we can use the lint block ignore by placing the comment on a line alone and ending it with # lint:endignore. In the following example, we have two large strings that won't be alerted on puppet-lint:

class module::strings {

# lint:ignore:140chars
notify {'Long String A':
message =>'This is the string that never ends. Yes it goes on and on my friends. Some developer just started writing without line breaks not knowing what they do, so this string will go on forever just because this is the string that never ends...'
}

notify {'Long String B':
message =>'This is another string that never ends. Yes it goes on and on my friends. Some developer just started writing without line breaks not knowing what they do, so this string will go on forever just because this is the string that never ends...'
}

# lint:endignore

}

If you have a check that you'd like to disable, you can also create a puppet-lint.rc file. This file can be placed in /etc for a global config, as .puppet-lint.rc in the home directory for a user config, or at the base of a module, as .puppet-lint.rc. If your team uses local development workstations, consider adding a .puppet-lint.rc to your PDK template, to enforce a standard on each repository:

# Permanently ignore ALL 140 character checks
$ cat puppet-lint.rc
--no-140chars-check

Finally, any Ruby code will be validated by RuboCop. RuboCop will check the style of all Ruby files in a module. This provides style checking to custom facts, types, providers, and even tasks written in Ruby:

$ pdk validate
pdk (INFO): Running all available validators...
pdk (INFO): Using Ruby 2.4.4
pdk (INFO): Using Puppet 5.5.1
[] Checking metadata syntax (metadata.json tasks/*.json).
[] Checking module metadata style (metadata.json).
[] Checking Puppet manifest syntax (**/**.pp).
[] Checking Puppet manifest style (**/*.pp).
[] Checking Ruby code style (**/**.rb).
info: task-metadata-lint: ./: Target does not contain any files to validate (tasks/*.json).
error: rubocop: spec/classes/config_spec.rb:8:38: unexpected token tRCURLY
(Using Ruby 2.1 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)

pdk validate provides a quick check of the style and syntax of your code. It does not check the functionality of your code. The PDK also provides a boiler template for RSpec tests out of the box, so that when a new class is created with pdk new class, a simple corresponding RSpec test is created along with it. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.55.14