cookbook 'mode_example', '~> 1.0.3'
mode_example (4) Versions 1.0.3 Follow0
Contains tests to show unexpected behavior for the mode property on a Chef resource
cookbook 'mode_example', '~> 1.0.3', :supermarket
knife supermarket install mode_example
knife supermarket download mode_example
mode_example
This cookbook exists to show Chef users how to use the mode
property, which is available to several common Chef resources, like file and directory.
This cookbook is just a long-winded way to show that there are several varying ways to set the mode
property on a Chef resource.
This cookbook doesn't actually do anything. It just has unit and integration tests, and a test cookbook to configure the state of a virtual machine. InSpec verifies the configuration.
Overview
If you're new to Ruby, you might be unfamiliar with how Ruby interprets and coerces strings, integers, and octal numbers. Take a look at the following examples.
irb(main):001:0> puts 'this is a string' this is a string => nil irb(main):002:0> puts "This is also a string" This is also a string => nil irb(main):003:0> puts '0644' 0644 => nil irb(main):004:0> puts 0644 420 => nil irb(main):005:0> puts 644 644 => nil irb(main):006:0> puts 0o644 420 => nil
Notice that 0644
and 0o644
are ways to write octal numbers in Ruby. This may be a gotcha, since the documentation for quite a few resources—including file and directory—mentions that you can use a Fixnum or a String as a value for your mode
properties.
The Chef resource documentation doesn't have many examples to show how to use Fixnum and String property values. I wrote some InSpec integration tests to help illustrate how to avoid common usage problems. Leading zeroes may result in unexpected values. There are some examples in the tests in this cookbook of how to use special modes (4-digit modes) correctly with both Fixnum and String property values.
Testing
Supported platforms
Keep in mind that the mode
property will operate differently on different platforms. (For example, this cookbook doesn't really make sense if you're testing against Windows.)
We only need Ubuntu to illustrate the examples.
Platform | Version |
---|---|
ubuntu | 14.04 |
Unit tests
To run the ChefSpec unit tests, just run the following command in your shell.
$ rspec
Integration tests
To run the InSpec-driven test suite, run the following command in the directory where you have downloaded or cloned this cookbook.
Note: Make sure you've installed ChefDK v0.12.0 or greater.
$ kitchen verify
The test cookbook
This cookbook uses a single test cookbook to configure the virtual machine, located in test/cookbooks/mode_example_test.
Locate and read through test/cookbooks/mode_example_test/recipes/file.rb. It just writes out a few files using the mode
property in a few different ways.
The integration test suite
Locate and read through test/integration/mode_example/fixnum_mode_spec.rb and test/integration/mode_example/string_mode_spec.rb. It simply shows that we have some expectations that the mode we intended was actually the mode we wrote to the file.
This shows that there are different ways to send values to the mode
parameter of your resources. It's a pretty common parameter that you'll use with a lot of basic and common resources.
Discussion
Static linting
Static linting will catch problems outlined in this cookbook. If you use Foodcritic, you may inadvertently trigger Foodcritic rule FC006. This is for good reason—you might end up configuring files to have a different modes than you intended them to have.
Examples of unintended modes
Example 1
This example produces a file with a mode of 1217, not 644. This is because Ruby coerces a Fixnum to octal if it has a leftmost digit of 0
.
file '/home/vagrant/some_file' do owner 'vagrant' group 'vagrant' mode 644 action :create end
In shell:
$ stat -c '%a' /home/vagrant/some_file 1217
In Ruby:
> 01217 == 655 => true
Example 2
In this example if the mode is set to 0644
, the mode results to 644, despite the fact that the Fixnum has a leftmost digit of 0
.
file '/home/vagrant/some_file' do owner 'vagrant' group 'vagrant' mode 0644 action :create end
In shell:
$ stat -c '%a' /home/vagrant/some_file 644
Example 3
This example also produces a file with a mode of 644.
file '/home/vagrant/some_file' do owner 'vagrant' group 'vagrant' mode '644' action :create end
In shell:
$ stat -c '%a' /home/vagrant/some_file 644
Dependent cookbooks
This cookbook has no specified dependencies.
Contingent cookbooks
There are no cookbooks that are contingent upon this one.
Change Log
This project adheres to Semantic Versioning.
1.0.3 (03/31/2016)
- Fix broken link
1.0.2 (03/31/2016)
- Bump patch version for Chef Supermarket
1.0.1 (03/31/2016)
- Updates readme with additions and corrections
1.0.0 (03/30/2016)
- Moves the GitHub repository to the kevindickerson-cookbooks organization
- Adds InSpec integration tests
- Removes ServerSpec integration tests
- Adds Travis CI configuration
- Adds ChefSpec unit tests
- Adds detail to readme
- Updates test cookbook
- Updates change log to use present simple verb tense
0.1.0 (12/16/2015)
- Includes initial release
Collaborator Number Metric
1.0.3 failed this metric
Failure: Cookbook has 0 collaborators. A cookbook must have at least 2 collaborators to pass this metric.
Contributing File Metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a CONTRIBUTING.md file
Foodcritic Metric
1.0.3 failed this metric
FC066: Ensure chef_version is set in metadata: mode_example/metadata.rb:1
FC069: Ensure standardized license defined in metadata: mode_example/metadata.rb:1
Run with Foodcritic Version 16.3.0 with tags metadata,correctness ~FC031 ~FC045 and failure tags any
No Binaries Metric
1.0.3 passed this metric
Testing File Metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a TESTING.md file
Version Tag Metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number
1.0.3 failed this metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a CONTRIBUTING.md file
Foodcritic Metric
1.0.3 failed this metric
FC066: Ensure chef_version is set in metadata: mode_example/metadata.rb:1
FC069: Ensure standardized license defined in metadata: mode_example/metadata.rb:1
Run with Foodcritic Version 16.3.0 with tags metadata,correctness ~FC031 ~FC045 and failure tags any
No Binaries Metric
1.0.3 passed this metric
Testing File Metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a TESTING.md file
Version Tag Metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number
1.0.3 failed this metric
FC069: Ensure standardized license defined in metadata: mode_example/metadata.rb:1
Run with Foodcritic Version 16.3.0 with tags metadata,correctness ~FC031 ~FC045 and failure tags any
1.0.3 passed this metric
Testing File Metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a TESTING.md file
Version Tag Metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number
1.0.3 failed this metric
1.0.3 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number