rspec-puppet-facts-unsupported 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e0fe41b4f40e37a85a23c758b22835ed095a3c7
4
+ data.tar.gz: 9f7c7726e1c7137e3d2f7edf20afe4100ae26c86
5
+ SHA512:
6
+ metadata.gz: a482a03b818766392cff6d048ac846988f91e367c1ce9138344f8270cacd44bf2329a58ed8bdc688db894ae5c460f4645bb632b728287f8956884894ab634698
7
+ data.tar.gz: 747181dd99adecc779b0f00181e522e105a7d89c1334e71907350fab87df401de6e29d450c7ebb78fdda9677ee338d0744a70e8a298b47578149ace19a2441a9
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /vendor/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --order rand
@@ -0,0 +1,19 @@
1
+ inherit_from: .rubocop_todo.yml
2
+ AllCops:
3
+ TargetRubyVersion: 1.9
4
+ Include:
5
+ - ./**/*.rb
6
+ Exclude:
7
+ - vendor/**/*
8
+ - .vendor/**/*
9
+ Metrics/LineLength:
10
+ Max: 140
11
+ Style/FileName:
12
+ Exclude:
13
+ - 'Gemfile'
14
+ - 'Rakefile'
15
+ - '*.gemspec'
16
+ - lib/rspec-puppet-facts-unsupported.rb
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'spec/**/*_spec.rb'
@@ -0,0 +1,7 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-06-14 18:06:57 +0200 using RuboCop version 0.49.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ matrix:
4
+ fast_finish: true
5
+ include:
6
+ - rvm: '1.9'
7
+ bundler_args: '--no-deployment --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle}'
8
+ - rvm: '2.1'
9
+ - rvm: '2.3'
10
+ - rvm: '2.4'
11
+ before_install: gem install bundler -v 1.15.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at krzysztof.suszynski@coi.gov.pl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,23 @@
1
+ source ENV['GEM_SOURCE'] || 'https://rubygems.org'
2
+
3
+ RVERSION = Gem::Version.new(RUBY_VERSION.dup)
4
+
5
+ def req(req_s)
6
+ Gem::Requirement.new(req_s)
7
+ end
8
+
9
+ group :test do
10
+ gem 'rspec', '~> 3', require: false
11
+ gem 'rspec-collection_matchers', '~> 1.1.3', require: false
12
+ gem 'rubocop', '~> 0.49', require: false if req('>= 2.0') =~ RVERSION
13
+ gem 'simplecov', '~> 0.14.1', require: false
14
+ end
15
+
16
+ group :development do
17
+ gem 'bundler', '~> 1.15.1', require: false
18
+ gem 'pry-byebug', '~> 3.4', '>= 3.4.2', require: false if req('>= 2.0') =~ RVERSION
19
+ gem 'rake', '~> 10', require: false
20
+ end
21
+
22
+ # Specify your gem's dependencies in rspec-puppet-facts-unsupported.gemspec
23
+ gemspec
@@ -0,0 +1,110 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rspec-puppet-facts-unsupported (0.1.0)
5
+ rspec-puppet-facts (~> 1.8.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.3.0)
11
+ byebug (9.0.6)
12
+ coderay (1.1.1)
13
+ diff-lcs (1.3)
14
+ docile (1.1.5)
15
+ facter (2.4.6)
16
+ facterdb (0.3.11)
17
+ facter
18
+ jgrep
19
+ fast_gettext (1.1.0)
20
+ gettext (3.2.3)
21
+ locale (>= 2.0.5)
22
+ text (>= 1.3.0)
23
+ gettext-setup (0.25)
24
+ fast_gettext (~> 1.1.0)
25
+ gettext (>= 3.0.2)
26
+ locale
27
+ hiera (3.4.0)
28
+ jgrep (1.4.1)
29
+ json
30
+ json (1.8.6)
31
+ locale (2.1.2)
32
+ mcollective-client (2.11.0)
33
+ json
34
+ stomp
35
+ systemu
36
+ method_source (0.8.2)
37
+ parallel (1.11.2)
38
+ parser (2.4.0.0)
39
+ ast (~> 2.2)
40
+ powerpack (0.1.1)
41
+ pry (0.10.4)
42
+ coderay (~> 1.1.0)
43
+ method_source (~> 0.8.1)
44
+ slop (~> 3.4)
45
+ pry-byebug (3.4.2)
46
+ byebug (~> 9.0)
47
+ pry (~> 0.10)
48
+ puppet (5.0.0)
49
+ facter (> 2.0, < 4)
50
+ gettext-setup (>= 0.10, < 1)
51
+ hiera (>= 3.2.1, < 4)
52
+ locale (~> 2.1)
53
+ rainbow (2.2.2)
54
+ rake
55
+ rake (10.5.0)
56
+ rspec (3.6.0)
57
+ rspec-core (~> 3.6.0)
58
+ rspec-expectations (~> 3.6.0)
59
+ rspec-mocks (~> 3.6.0)
60
+ rspec-collection_matchers (1.1.3)
61
+ rspec-expectations (>= 2.99.0.beta1)
62
+ rspec-core (3.6.0)
63
+ rspec-support (~> 3.6.0)
64
+ rspec-expectations (3.6.0)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.6.0)
67
+ rspec-mocks (3.6.0)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.6.0)
70
+ rspec-puppet-facts (1.8.0)
71
+ facter
72
+ facterdb (>= 0.3.0)
73
+ json
74
+ mcollective-client
75
+ puppet
76
+ rspec-support (3.6.0)
77
+ rubocop (0.49.1)
78
+ parallel (~> 1.10)
79
+ parser (>= 2.3.3.1, < 3.0)
80
+ powerpack (~> 0.1)
81
+ rainbow (>= 1.99.1, < 3.0)
82
+ ruby-progressbar (~> 1.7)
83
+ unicode-display_width (~> 1.0, >= 1.0.1)
84
+ ruby-progressbar (1.8.1)
85
+ simplecov (0.14.1)
86
+ docile (~> 1.1.0)
87
+ json (>= 1.8, < 3)
88
+ simplecov-html (~> 0.10.0)
89
+ simplecov-html (0.10.1)
90
+ slop (3.6.0)
91
+ stomp (1.4.4)
92
+ systemu (2.6.5)
93
+ text (1.3.1)
94
+ unicode-display_width (1.3.0)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ bundler (~> 1.15.1)
101
+ pry-byebug (~> 3.4, >= 3.4.2)
102
+ rake (~> 10)
103
+ rspec (~> 3)
104
+ rspec-collection_matchers (~> 1.1.3)
105
+ rspec-puppet-facts-unsupported!
106
+ rubocop (~> 0.49)
107
+ simplecov (~> 0.14.1)
108
+
109
+ BUNDLED WITH
110
+ 1.15.1
@@ -0,0 +1,13 @@
1
+ Copyright 2017 Center for Information Technology, Poland
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,97 @@
1
+ # RspecPuppetFactsUnsupported
2
+
3
+ [![Build Status](https://travis-ci.org/coi-gov-pl/rspec-puppet-facts-unsupported.svg?branch=develop)](https://travis-ci.org/coi-gov-pl/rspec-puppet-facts-unsupported)
4
+
5
+ Helpers to generate unsupported OS facts to test for proper fail.
6
+
7
+ Using new `on_unsupported_os` method you can get a number of random provided OS's with their facts. Those facts can be used in rspec-puppet tests just like shown below:
8
+
9
+ ```ruby
10
+ on_unsupported_os.each do |os, facts|
11
+ context "on unsupported OS #{os}" do
12
+ let(:facts) { facts }
13
+ it { is_expected.to compile.and_raise_error(/Unsupported operating system/) }
14
+ end
15
+ end
16
+ ```
17
+
18
+ ## Installation
19
+
20
+ Add this to your puppet module's Gemfile:
21
+
22
+ ```ruby
23
+ group :system_tests do
24
+ # [..]
25
+ gem 'rspec-puppet-facts-unsupported'
26
+ end
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install rspec-puppet-facts-unsupported
36
+
37
+ ## Usage
38
+
39
+ ### Simple example
40
+
41
+ randomized, limited to 2 records, unsupported os - facts pair
42
+
43
+ ```ruby
44
+ # in rspec-puppet test
45
+ require 'spec_helper'
46
+ require 'rspec-puppet-facts-unsupported'
47
+
48
+ describe '::vagrant', type: :class do
49
+ include RspecPuppetFactsUnsupported
50
+
51
+ # Generates by default 2 records from shuffled library of unsupported OS's
52
+ on_unsupported_os.each do |os, facts|
53
+ context "on unsupported OS #{os}" do
54
+ let(:facts) { facts }
55
+ it { is_expected.to compile.and_raise_error(/Unsupported operating system/) }
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ ### Examples with options
62
+
63
+ ```ruby
64
+ # in rspec-puppet test
65
+ require 'spec_helper'
66
+ require 'rspec-puppet-facts-unsupported'
67
+
68
+ describe '::vagrant', type: :class do
69
+ include RspecPuppetFactsUnsupported
70
+
71
+ # Generates by up to 6 records from library in original order filtering to return only RedHat systems
72
+ on_unsupported_os(limit: 6, order: :original, filters: { osfamily: 'RedHat' }).each do |os, facts|
73
+ context "on unsupported OS #{os}" do
74
+ let(:facts) { facts }
75
+ it { is_expected.to compile.and_raise_error(/Unsupported operating system: RedHat/) }
76
+ end
77
+ end
78
+ end
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bundle` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bundle exec rake console` for an interactive prompt that will allow you to experiment.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, use GitFlow convention - remember to update the version number in `version.rb`. Push branches and new tag. When finished checkout master and run `bundle exec rake build` and then run `gem push pkg/*.gem`, which will push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/coi-gov-pl/rspec-puppet-facts-unsupported. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [Apache 2.0](https://opensource.org/licenses/Apache-2.0).
94
+
95
+ ## Code of Conduct
96
+
97
+ Everyone interacting in the Puppet::Examples::Helper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/coi-gov-pl/rspec-puppet-facts-unsupported/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,20 @@
1
+ def gem_present(name)
2
+ !Bundler.rubygems.find_name(name).empty?
3
+ end
4
+
5
+ require 'bundler/gem_tasks'
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ test_tasks = [:spec]
11
+
12
+ if gem_present 'rubocop'
13
+ require 'rubocop/rake_task'
14
+ RuboCop::RakeTask.new
15
+ test_tasks.unshift(:rubocop)
16
+ end
17
+
18
+ task test: test_tasks
19
+
20
+ task default: :test
@@ -0,0 +1,7 @@
1
+ # A main module of rspec-puppet-facts-unsupported
2
+ module RspecPuppetFactsUnsupported
3
+ # a placeholder module
4
+ end
5
+
6
+ require 'rspec-puppet-facts-unsupported/version'
7
+ require 'rspec-puppet-facts-unsupported/on_unsupported_os'
@@ -0,0 +1,291 @@
1
+ require 'rspec-puppet-facts'
2
+
3
+ # A main module of rspec-puppet-facts-unsupported
4
+ module RspecPuppetFactsUnsupported
5
+ # Fetches an unsupported list of operating system's facts. List doesn't contains operating system
6
+ # described in Puppet's metadata.json file.
7
+ # @return Hash[String => Hash] A hash of key being os description and value being example
8
+ # machine facts
9
+ # @param Hash[Symbol => Object] opts A configuration hash with options
10
+ # @option opts [String,Array<String>] :hardwaremodels The OS architecture names, i.e. 'x86_64', /IBM/
11
+ # or 'i86pc'
12
+ # @option opts [Array<Hash>] :supported_os If this options is provided the data
13
+ # will be used instead of the "operatingsystem_support" section if the metadata file
14
+ # even if the file is missing.
15
+ # @option opts [Array<Hash>] :filters An array of extra filters to be passed to FacterDB to
16
+ # narrow the search
17
+ # @option opts [Array<Hash>] :order An order in which records should be returned. You can pass values
18
+ # like :random - to randomly shuffle records (exact shuffle seed will be printed on stderr to be able
19
+ # to reproduce invalid behaivior), `:original` - to return original list, an integer seed - to reproduce
20
+ # failiures. By default it is set to `:random`
21
+ # @option opts [Array<Hash>] :limit A limit of records to be returned. By default it is set to 2.
22
+ def on_unsupported_os(opts = {})
23
+ process_opts(opts)
24
+ filters = calculate_filters(opts)
25
+ facts = FacterDB.get_facts(filters)
26
+ op = UnsupportedFilteringOperation.new(facts, opts)
27
+ op.facts
28
+ end
29
+
30
+ def self.verbose=(verbose)
31
+ @@verbose = (verbose == true) # rubocop:disable Style/ClassVars
32
+ end
33
+
34
+ def self.verbose?
35
+ @@verbose
36
+ end
37
+
38
+ def verbose?
39
+ RspecPuppetFactsUnsupported.verbose?
40
+ end
41
+
42
+ protected
43
+
44
+ def factname(fact, opts = {})
45
+ opts[:era] ||= :legancy
46
+ fact_sym = fact.to_s.to_sym
47
+ if opts[:era] == :legancy
48
+ fact_sym
49
+ elsif opts[:era] == :current
50
+ CURRENT_FACT_NAMES[fact_sym]
51
+ else
52
+ raise "invalid era: #{opts[:era].inspect}"
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ CURRENT_FACT_NAMES = {
59
+ hardwaremodel: :'os.hardware',
60
+ operatingsystem: :'os.name',
61
+ operatingsystemrelease: :'os.release.full',
62
+ operatingsystemmajrelease: :'os.release.major'
63
+ }.freeze
64
+
65
+ @@verbose = true # rubocop:disable Style/ClassVars
66
+
67
+ # Private class to perform randomization
68
+ class Randomizer
69
+ attr_reader :order, :seed
70
+
71
+ def initialize(opts, envkey = :RSPEC_PUPPET_FACTS_UNSUPPORTED_ORDER)
72
+ opts[:order] ||= :random
73
+ randomize_seed
74
+ envvar = ENV[envkey.to_s]
75
+ ordervalue = envvar.nil? ? opts[:order] : envvar.to_sym
76
+ ilike = Integerlike.new(ordervalue)
77
+ self.seed = ilike.to_i if ilike.integer?
78
+ @order = ordervalue
79
+ @repetitive_random = Random.new(42)
80
+ end
81
+
82
+ def get
83
+ should_randomize? ? @randomizer : @repetitive_random
84
+ end
85
+
86
+ def should_randomize?
87
+ @order == :random || Integerlike.new(@order).integer?
88
+ end
89
+
90
+ private
91
+
92
+ SHORT_MAX = 2**16
93
+
94
+ def randomize_seed
95
+ self.seed = Random.new.rand(0..SHORT_MAX)
96
+ end
97
+
98
+ def seed=(seed)
99
+ @seed = seed
100
+ @randomizer = Random.new(@seed)
101
+ end
102
+ end
103
+
104
+ def process_opts(opts)
105
+ opts[:randomizer] = Randomizer.new(opts)
106
+ ensure_default_hardwaremodels opts
107
+ ensure_default_filters opts
108
+ opts[:limit] ||= 2
109
+ opts[:supported_os] ||= RspecPuppetFacts.meta_supported_os
110
+ process_order opts
111
+ end
112
+
113
+ def ensure_default_hardwaremodels(opts)
114
+ opts[:hardwaremodels] ||= ['x86_64']
115
+ opts[:hardwaremodels] = [opts[:hardwaremodels]] unless opts[:hardwaremodels].is_a? Array
116
+ end
117
+
118
+ def ensure_default_filters(opts)
119
+ opts[:filters] ||= []
120
+ opts[:filters] = [opts[:filters]] unless opts[:filters].is_a? Array
121
+ end
122
+
123
+ def process_order(opts)
124
+ rnd = opts[:randomizer]
125
+ message = "Shuffling unsupported OS's facts with seed: #{rnd.seed}\nSet environment variable " \
126
+ "to reproduce this order, for ex. on Linux \`export RSPEC_PUPPET_FACTS_UNSUPPORTED_ORDER=#{rnd.seed}\`"
127
+ $stderr.puts message if verbose? && rnd.should_randomize?
128
+ end
129
+
130
+ def calculate_filters(opts)
131
+ filters = []
132
+ opts[:hardwaremodels].each do |hardwaremodel|
133
+ filters << {
134
+ facterversion: "/^#{Regexp.quote(system_facter_version)}/",
135
+ hardwaremodel: hardwaremodel
136
+ }
137
+ end
138
+ filters = filters.product(opts[:filters]).collect { |x, y| x.merge(y) } unless opts[:filters].empty?
139
+ postprocess_filters(filters)
140
+ end
141
+
142
+ def system_facter_version
143
+ Facter.version.split('.')[0..-2].join('.')
144
+ end
145
+
146
+ def find_facter_version_matching_regexp(regexp)
147
+ (1..4).each do |major|
148
+ (0..7).each do |minor|
149
+ candidate = "#{major}.#{minor}.42"
150
+ stripped_regexp = regexp.gsub(%r{^/+|/+$}, '')
151
+ return candidate if Regexp.new(stripped_regexp).match(candidate)
152
+ end
153
+ end
154
+ nil
155
+ end
156
+
157
+ def postprocess_filters(filters)
158
+ filters.map do |filter|
159
+ facterversion = find_facter_version_matching_regexp(filter[:facterversion])
160
+ if facterversion.split('.').first >= '3'
161
+ current = factname(:hardwaremodel, era: :current)
162
+ legancy = factname(:hardwaremodel)
163
+ filter[current] = filter[legancy]
164
+ filter.delete(legancy)
165
+ end
166
+ filter
167
+ end
168
+ end
169
+
170
+ # Integerlike private class
171
+ class Integerlike
172
+ def initialize(obj)
173
+ @obj = obj
174
+ end
175
+
176
+ def integer?
177
+ to_s == to_i.to_s
178
+ end
179
+
180
+ def to_i
181
+ to_s.to_i
182
+ end
183
+
184
+ def to_s
185
+ @obj.to_s
186
+ end
187
+ end
188
+
189
+ # Private class
190
+ class Facts
191
+ include RspecPuppetFactsUnsupported
192
+ def initialize(facts)
193
+ @facts = facts
194
+ end
195
+ attr_reader :facts
196
+
197
+ def [](key)
198
+ normalized_key = factkey(key)
199
+ facts_by_path(facts, normalized_key)
200
+ end
201
+
202
+ private
203
+
204
+ def factkey(fact)
205
+ era = facter_current? ? :current : :legancy
206
+ factname(fact, era: era)
207
+ end
208
+
209
+ def facts_by_path(facts, path)
210
+ stringified = Hash[facts.map { |k, v| [k.to_s, v] }]
211
+ path.to_s.split('.').inject(stringified) { |hash, key| hash[key] }
212
+ end
213
+
214
+ def facter_current?
215
+ facterversion >= '3.0.0'
216
+ end
217
+
218
+ def facterversion
219
+ facts[:facterversion]
220
+ end
221
+ end
222
+
223
+ # Private opertion wrapper class
224
+ class UnsupportedFilteringOperation
225
+ include RspecPuppetFactsUnsupported
226
+ def initialize(facts_list, opts)
227
+ @opts = opts
228
+ @facts_list = facts_list
229
+ end
230
+
231
+ def facts
232
+ postprocess_facts(reject_supported_os)
233
+ end
234
+
235
+ private
236
+
237
+ def reject_supported_os
238
+ metadata_supported = @opts[:supported_os]
239
+ @facts_list.reject do |candidate_raw|
240
+ candidate = Facts.new(candidate_raw)
241
+ req = metadata_supported.select do |single_req|
242
+ single_req['operatingsystem'] == candidate[:operatingsystem]
243
+ end
244
+ should_be_rejected?(req, candidate)
245
+ end
246
+ end
247
+
248
+ def postprocess_facts(facts_list)
249
+ os_facts_hash = {}
250
+ facts_list.map do |facts|
251
+ description = describe_os(Facts.new(facts))
252
+ facts.merge! RspecPuppetFacts.common_facts
253
+ os_facts_hash[description] = RspecPuppetFacts.with_custom_facts(description, facts)
254
+ end
255
+ shuffle_and_limit(os_facts_hash)
256
+ end
257
+
258
+ def shuffle_and_limit(os_facts_hash)
259
+ randomizer = @opts[:randomizer]
260
+ as_array = os_facts_hash.to_a
261
+ as_array = as_array.sort_by { |os, _| os }
262
+ as_array = as_array.shuffle(random: randomizer.get)
263
+ as_array = as_array[0..@opts[:limit]]
264
+ Hash[*as_array.flatten]
265
+ end
266
+
267
+ def describe_os(facts)
268
+ "#{facts[:operatingsystem].downcase}-" \
269
+ "#{facts[:operatingsystemmajrelease]}-" \
270
+ "#{facts[:hardwaremodel]}"
271
+ end
272
+
273
+ def should_be_rejected?(req, candidate)
274
+ if req.empty?
275
+ false
276
+ else
277
+ req = req.first
278
+ operatingsystemrelease_matches?(req['operatingsystemrelease'], candidate)
279
+ end
280
+ end
281
+
282
+ def operatingsystemrelease_matches?(operatingsystemrelease, candidate)
283
+ if operatingsystemrelease.nil?
284
+ true
285
+ else
286
+ candidate_release = candidate[:operatingsystemrelease]
287
+ operatingsystemrelease.select { |elem| candidate_release.start_with?(elem) }.any?
288
+ end
289
+ end
290
+ end
291
+ end
@@ -0,0 +1,3 @@
1
+ module RspecPuppetFactsUnsupported
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "this-is-not-a-puppet-module",
3
+ "version": "999999999.0.0",
4
+ "author": "coi",
5
+ "summary": "Not a module, used just to verify RspecPuppetFactsUnsupported.on_unsupported_os method",
6
+ "license": "Apache-2.0",
7
+ "operatingsystem_support": [
8
+ { "operatingsystem": "Ubuntu", "operatingsystemrelease": [ "14.04", "16.04" ] },
9
+ { "operatingsystem": "CentOS", "operatingsystemrelease": [ "6", "7" ] },
10
+ { "operatingsystem": "OracleLinux" }
11
+ ]
12
+ }
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'rspec-puppet-facts-unsupported/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'rspec-puppet-facts-unsupported'
9
+ spec.version = RspecPuppetFactsUnsupported::VERSION
10
+ spec.authors = ['Suszyński Krzysztof']
11
+ spec.email = ['krzysztof.suszynski@coi.gov.pl']
12
+
13
+ spec.summary = 'Helpers to generate unsupported OS facts to test for proper fail'
14
+ spec.description = 'Using new `on_unsupported_os` method you can get a number of random provided OS\'s' \
15
+ ' with their factsto be used in rspec-puppet tests'
16
+ spec.homepage = 'https://github.com/coi-gov-pl/rspec-puppet-facts-unsupported'
17
+ spec.license = 'Apache-2.0'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+ spec.required_ruby_version = '>= 1.9.0'
26
+
27
+ spec.add_dependency 'rspec-puppet-facts', '~> 1.8.0'
28
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-puppet-facts-unsupported
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Suszyński Krzysztof
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-puppet-facts
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.0
27
+ description: Using new `on_unsupported_os` method you can get a number of random provided
28
+ OS's with their factsto be used in rspec-puppet tests
29
+ email:
30
+ - krzysztof.suszynski@coi.gov.pl
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - ".rubocop_todo.yml"
39
+ - ".travis.yml"
40
+ - CODE_OF_CONDUCT.md
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - lib/rspec-puppet-facts-unsupported.rb
47
+ - lib/rspec-puppet-facts-unsupported/on_unsupported_os.rb
48
+ - lib/rspec-puppet-facts-unsupported/version.rb
49
+ - metadata.json
50
+ - rspec-puppet-facts-unsupported.gemspec
51
+ homepage: https://github.com/coi-gov-pl/rspec-puppet-facts-unsupported
52
+ licenses:
53
+ - Apache-2.0
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.9.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.6.8
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Helpers to generate unsupported OS facts to test for proper fail
75
+ test_files: []