|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# frozen_string_literal: true |
| 3 | +# |
| 4 | +# Cookbook Name:: lyraphase_workstation |
| 5 | +# Site:: https://github.com/LyraPhase/lyraphase_workstation/ |
| 6 | +# |
| 7 | +# Copyright (C) © 🄯 2016-2022 James Cuzella |
| 8 | +# |
| 9 | +# This program is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 3 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +## Helper Methods |
| 23 | +## Gets rid of const redefinition warnings |
| 24 | +def create_singleton_struct name, fields |
| 25 | + if Struct::const_defined? name |
| 26 | + Struct.const_get name |
| 27 | + else |
| 28 | + Struct.new name, *fields |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +def stringify_keys(hash) |
| 33 | + hash.each_with_object({}) do |(k, v), base| |
| 34 | + v = stringify_keys(v) if v.is_a? Hash |
| 35 | + base[k.to_s] = v |
| 36 | + base |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +# Note: Chef >= 15 no longer runs Ohai :Passwd plugin |
| 41 | +# Now you must manually enable this plugin because this cookbook depends on it |
| 42 | +# Add to client.rb: |
| 43 | +# ohai.optional_plugins = [ :Passwd ] |
| 44 | + |
| 45 | +## RSpec Shared Contexts |
| 46 | +# D.R.Y. ChefSpec tests to mock Ohai /etc/passwd data |
| 47 | +# Source: https://github.com/sous-chefs/ark/blob/bcdf8108a3754a5c9c1257f9535dd1e6554bc998/spec/spec_helper.rb |
| 48 | +# References: |
| 49 | +# - https://www.chef.io/blog/watch-writing-elegant-tests |
| 50 | +# - https://www.rubydoc.info/github/rspec/rspec-core/RSpec%2FCore%2FConfiguration:alias_example_group_to |
| 51 | +# - https://github.com/chefspec/fauxhai#fauxhai-ng |
| 52 | +# - https://github.com/chef/chef/issues/8888 |
| 53 | +# - https://docs.chef.io/ohai/#optional-plugins |
| 54 | +# - https://stackoverflow.com/a/57953198/645491 |
| 55 | +RSpec.shared_context 'recipe tests', type: :recipe do |
| 56 | + |
| 57 | + # Individual spec Example Groups can override this to inject node attributes |
| 58 | + let(:chefspec_options) { |
| 59 | + { |
| 60 | + default_attributes: {}, |
| 61 | + normal_attributes: {}, |
| 62 | + automatic_attributes: {} |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + let(:chef_run) { |
| 67 | + klass = ChefSpec.constants.include?(:SoloRunner) ? ChefSpec::SoloRunner : ChefSpec::Runner |
| 68 | + klass.new(chefspec_options) do |node| |
| 69 | + node.normal.merge!(node_attributes) |
| 70 | + end.converge(described_recipe) |
| 71 | + } |
| 72 | + |
| 73 | + let(:node) { chef_run.node } |
| 74 | + |
| 75 | + def lyraphase_workstation_user |
| 76 | + create_singleton_struct "EtcPasswd", [ :name, :passwd, :uid, :gid, :gecos, :dir, :shell, :change, :uclass, :expire ] |
| 77 | + Struct::EtcPasswd.new('brubble', '********', 501, 20, 'Barney Rubble', '/Users/brubble', '/bin/bash', 0, '', 0) |
| 78 | + end |
| 79 | + |
| 80 | + # Global ChefSpec attributes for all Example Groups |
| 81 | + def node_attributes |
| 82 | + attributes = { |
| 83 | + 'platform': 'mac_os_x', |
| 84 | + 'version': '10.15', |
| 85 | + 'etc': { |
| 86 | + 'passwd': { |
| 87 | + 'brubble': lyraphase_workstation_user |
| 88 | + } |
| 89 | + }, |
| 90 | + 'sprout': { |
| 91 | + 'home': '/Users/brubble', |
| 92 | + 'user': 'brubble' |
| 93 | + }, |
| 94 | + 'lyraphase_workstation': { |
| 95 | + 'user': 'brubble', |
| 96 | + 'home': '/Users/brubble' |
| 97 | + } |
| 98 | + } |
| 99 | + stringify_keys(attributes) |
| 100 | + end |
| 101 | + |
| 102 | + def cookbook_recipe_names |
| 103 | + described_recipe.split('::', 2) |
| 104 | + end |
| 105 | + |
| 106 | + def cookbook_name |
| 107 | + cookbook_recipe_names.first |
| 108 | + end |
| 109 | + |
| 110 | + def recipe_name |
| 111 | + cookbook_recipe_names.last |
| 112 | + end |
| 113 | + |
| 114 | + def default_cookbook_attribute(attribute_name) |
| 115 | + node[cookbook_name][attribute_name] |
| 116 | + end |
| 117 | +end |
| 118 | + |
| 119 | +RSpec.shared_context 'helpers tests', type: :helpers do |
| 120 | + include described_class |
| 121 | + |
| 122 | + let(:new_resource) { OpenStruct.new(resource_properties) } |
| 123 | + |
| 124 | + def resource_properties |
| 125 | + @resource_properties || {} |
| 126 | + end |
| 127 | + |
| 128 | + def with_resource_properties(properties) |
| 129 | + @resource_properties = properties |
| 130 | + end |
| 131 | + |
| 132 | + let(:node) do |
| 133 | + Fauxhai.mock { |node| node.merge!(node_attributes) }.data |
| 134 | + end |
| 135 | + |
| 136 | + def node_attributes |
| 137 | + stringify_keys(@node_attributes || {}) |
| 138 | + end |
| 139 | + |
| 140 | + def with_node_attributes(attributes) |
| 141 | + @node_attributes = attributes |
| 142 | + end |
| 143 | +end |
| 144 | + |
| 145 | +RSpec.shared_context 'resource tests', type: :resource do |
| 146 | + let(:chef_run) do |
| 147 | + ChefSpec::SoloRunner.new(node_attributes.merge(step_into)).converge(example_recipe) |
| 148 | + end |
| 149 | + |
| 150 | + let(:example_recipe) do |
| 151 | + raise %( |
| 152 | +Please specify the name of the test recipe that executes your recipe: |
| 153 | + let(:example_recipe) do |
| 154 | + "lyraphase_workstation::example_recipe" |
| 155 | + end |
| 156 | +) |
| 157 | + end |
| 158 | + |
| 159 | + let(:node) { chef_run.node } |
| 160 | + |
| 161 | + def node_attributes |
| 162 | + {} |
| 163 | + end |
| 164 | + |
| 165 | + let(:step_into) do |
| 166 | + { step_into: [cookbook_name] } |
| 167 | + end |
| 168 | + |
| 169 | + def cookbook_recipe_names |
| 170 | + described_recipe.split('::', 2) |
| 171 | + end |
| 172 | + |
| 173 | + def cookbook_name |
| 174 | + cookbook_recipe_names.first |
| 175 | + end |
| 176 | + |
| 177 | + def recipe_name |
| 178 | + cookbook_recipe_names.last |
| 179 | + end |
| 180 | +end |
| 181 | + |
| 182 | +shared_context 'Chef 14.x no EtcPasswd' do |
| 183 | + let(:chef_run) { |
| 184 | + ## Note: We run chef_run.converge here to raise exceptions for RSpec to check |
| 185 | + ChefSpec::SoloRunner.new(node_attributes).converge(described_recipe) |
| 186 | + } |
| 187 | + let(:node) { chef_run.node } |
| 188 | + |
| 189 | + let(:chef_log_warnings) { |
| 190 | + ["Chef >= 14.x removed node['etc']['passwd'] Ohai attributes that sprout cookbooks depend on!", |
| 191 | + "Please manually enable Ohai Passwd plugin, or populate cookbook attributes!", |
| 192 | + "References: ", |
| 193 | + " https://docs.chef.io/ohai/#optional-plugins", |
| 194 | + " https://github.com/chef/chef/issues/8888", |
| 195 | + " https://stackoverflow.com/a/57953198/645491" |
| 196 | + ] |
| 197 | + } |
| 198 | + |
| 199 | + let(:chef_log_fatal_msgs) { |
| 200 | + ['node[\'etc\'][\'passwd\'] Ohai attributes are nil!'] |
| 201 | + } |
| 202 | + |
| 203 | + def node_attributes |
| 204 | + {} |
| 205 | + end |
| 206 | + |
| 207 | + let(:fake_chef_version) do |
| 208 | + fake_chef_version = double('Chef::VERSION') |
| 209 | + allow(fake_chef_version).to receive(:to_s).and_return('14.0.190') |
| 210 | + fake_chef_version |
| 211 | + end |
| 212 | + |
| 213 | + before(:each) do |
| 214 | + chef_log_warnings.each do |warning_msg| |
| 215 | + allow(Chef::Log).to receive(:warn).with(warning_msg).and_return(nil) |
| 216 | + end |
| 217 | + chef_log_fatal_msgs.each do |fatal_msg| |
| 218 | + allow(Chef::Log).to receive(:fatal).with(fatal_msg).and_return(nil) |
| 219 | + end |
| 220 | + stub_const('::Chef::VERSION', fake_chef_version) |
| 221 | + end |
| 222 | +end |
0 commit comments