diff --git a/Berksfile b/Berksfile index 77d0da8..8de1376 100644 --- a/Berksfile +++ b/Berksfile @@ -2,9 +2,4 @@ source 'https://supermarket.chef.io' metadata -group :test do - cookbook 'chef-services', - '>= 4.0.0', - git: 'https://github.com/stephenlauck/chef-services.git', - branch: 'ad/simplify_pkgs' -end +cookbook 'chef_test', path: 'test/fixtures/cookbooks/chef_test' diff --git a/libraries/matchers.rb b/libraries/matchers.rb new file mode 100644 index 0000000..9d759f9 --- /dev/null +++ b/libraries/matchers.rb @@ -0,0 +1,57 @@ +if defined?(ChefSpec) + def create_chef_automate(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_automate, :create, name) + end + + def create_backend_cluster(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_backend, :create, name) + end + + def join_backend_cluster(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_backend, :join, name) + end + + def create_chef_org(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_org, :create, name) + end + + def delete_chef_org(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_org, :delete, name) + end + + def create_chef_user(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_user, :create, name) + end + + def delete_chef_user(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_user, :delete, name) + end + + def install_chef_client(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_client, :install, name) + end + + def register_chef_client(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_client, :register, name) + end + + def run_chef_client(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_client, :run, name) + end + + def create_chef_file(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_file, :create, name) + end + + def create_chef_server(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_server, :create, name) + end + + def create_chef_supermarket(name) + ChefSpec::Matchers::ResourceMatcher.new(:chef_supermarket, :create, name) + end + + def create_build_node(name) + ChefSpec::Matchers::ResourceMatcher.new(:workflow_builder, :create, name) + end +end diff --git a/metadata.rb b/metadata.rb index facaaa6..130d0df 100644 --- a/metadata.rb +++ b/metadata.rb @@ -7,6 +7,6 @@ long_description 'Resource Cookbook for Managing Chef Software Inc Product Suite' issues_url 'https://github.com/ncerny/chef_stack/issues' if respond_to?(:issues_url) source_url 'https://github.com/ncerny/chef_stack' if respond_to?(:source_url) -version '0.6.1' +version '0.6.2' depends 'chef-ingredient' diff --git a/resources/automate.rb b/resources/automate.rb index cd9b191..773525e 100644 --- a/resources/automate.rb +++ b/resources/automate.rb @@ -61,11 +61,10 @@ platform_version new_resource.platform_version if new_resource.platform_version end - directory '/etc/delivery' - directory '/etc/chef' - - directory '/var/opt/delivery/license/' do - recursive true + %w(/etc/delivery /etc/chef /var/opt/delivery/license).each do |dir| + directory dir do + recursive true + end end { @@ -89,16 +88,16 @@ mode '0644' end - directory '/var/opt/delivery/nginx/etc/addon.d/' do + directory '/var/opt/delivery/nginx/etc/addon.d' do recursive true end file '/var/opt/delivery/nginx/etc/addon.d/99-installer_internal.conf' do content <<-EOF - location /installer { - alias /opt/delivery/embedded/service/omnibus-ctl/installer; - } - EOF +location /installer { + alias /opt/delivery/embedded/service/omnibus-ctl/installer; +} +EOF end ingredient_config 'automate' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1dd5126..4e00004 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,4 @@ require 'chefspec' require 'chefspec/berkshelf' + +at_exit { ChefSpec::Coverage.report! } diff --git a/spec/unit/chef_automate_spec.rb b/spec/unit/chef_automate_spec.rb new file mode 100644 index 0000000..c633456 --- /dev/null +++ b/spec/unit/chef_automate_spec.rb @@ -0,0 +1,141 @@ +# +# Cookbook Name:: chef_stack +# Spec:: chef_automate +# +# Copyright 2016 Chef Software Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +insecure_key = <<-EOS +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI +w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP +kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2 +hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO +Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW +yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQIBIwKCAQEA4iqWPJXtzZA68mKd +ELs4jJsdyky+ewdZeNds5tjcnHU5zUYE25K+ffJED9qUWICcLZDc81TGWjHyAqD1 +Bw7XpgUwFgeUJwUlzQurAv+/ySnxiwuaGJfhFM1CaQHzfXphgVml+fZUvnJUTvzf +TK2Lg6EdbUE9TarUlBf/xPfuEhMSlIE5keb/Zz3/LUlRg8yDqz5w+QWVJ4utnKnK +iqwZN0mwpwU7YSyJhlT4YV1F3n4YjLswM5wJs2oqm0jssQu/BT0tyEXNDYBLEF4A +sClaWuSJ2kjq7KhrrYXzagqhnSei9ODYFShJu8UWVec3Ihb5ZXlzO6vdNQ1J9Xsf +4m+2ywKBgQD6qFxx/Rv9CNN96l/4rb14HKirC2o/orApiHmHDsURs5rUKDx0f9iP +cXN7S1uePXuJRK/5hsubaOCx3Owd2u9gD6Oq0CsMkE4CUSiJcYrMANtx54cGH7Rk +EjFZxK8xAv1ldELEyxrFqkbE4BKd8QOt414qjvTGyAK+OLD3M2QdCQKBgQDtx8pN +CAxR7yhHbIWT1AH66+XWN8bXq7l3RO/ukeaci98JfkbkxURZhtxV/HHuvUhnPLdX +3TwygPBYZFNo4pzVEhzWoTtnEtrFueKxyc3+LjZpuo+mBlQ6ORtfgkr9gBVphXZG +YEzkCD3lVdl8L4cw9BVpKrJCs1c5taGjDgdInQKBgHm/fVvv96bJxc9x1tffXAcj +3OVdUN0UgXNCSaf/3A/phbeBQe9xS+3mpc4r6qvx+iy69mNBeNZ0xOitIjpjBo2+ +dBEjSBwLk5q5tJqHmy/jKMJL4n9ROlx93XS+njxgibTvU6Fp9w+NOFD/HvxB3Tcz +6+jJF85D5BNAG3DBMKBjAoGBAOAxZvgsKN+JuENXsST7F89Tck2iTcQIT8g5rwWC +P9Vt74yboe2kDT531w8+egz7nAmRBKNM751U/95P9t88EDacDI/Z2OwnuFQHCPDF +llYOUI+SpLJ6/vURRbHSnnn8a/XG+nzedGH5JGqEJNQsz+xT2axM0/W/CRknmGaJ +kda/AoGANWrLCz708y7VYgAtW2Uf1DPOIYMdvo6fxIB5i9ZfISgcJ/bbCUkFrhoH ++vq/5CIWxCPp0f85R4qxxQ5ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ +NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s= +-----END RSA PRIVATE KEY----- +EOS + +require 'spec_helper' + +describe 'chef_test::automate' do + cached(:centos_7_2) do + stub_command('delivery-ctl list-enterprises --ssh-pub-key-file=/etc/delivery/builder_key.pub | grep -w test').and_return(false) + stub_command('delivery-ctl status').and_return(true) + ChefSpec::ServerRunner.new( + step_into: 'chef_automate', + platform: 'centos', + version: '7.2.1511' + ).converge(described_recipe) + end + + context 'compiling the recipe' do + it 'creates chef_automate[automate.chefstack.local]' do + expect(centos_7_2).to create_chef_automate('automate.chefstack.local') + end + end + + context 'stepping into chef_automate' do + it 'upgrades chef_ingredient[automate]' do + expect(centos_7_2).to upgrade_chef_ingredient('automate') + .with( + channel: :current, + version: '0.6.64', + accept_license: true, + enterprise: ['test'], + license: 'license', + chef_user: 'chef_user', + chef_user_pem: insecure_key, + validation_pem: insecure_key, + builder_pem: insecure_key + ) + end + + it 'creates required directories' do + [ + '/var/opt/delivery/license', + '/etc/delivery', + '/etc/chef', + ].each do |dir| + expect(centos_7_2).to create_directory(dir) + end + end + + it 'creates automate keys' do + { + '/var/opt/delivery/license/delivery.license' => 'license', + '/etc/delivery/chef_user.pem' => insecure_key, + '/etc/chef/validation.pem' => insecure_key, + '/etc/delivery/builder_key' => insecure_key, + }.each do |file, src| + expect(centos_7_2).to create_chef_file(file) + .with( + source: src, + user: 'root', + group: 'root', + mode: '0600' + ) + end + end + + it 'creates the builder public key' do + expect(centos_7_2).to create_file('/etc/delivery/builder_key.pub') + .with( + user: 'root', + group: 'root', + mode: '0644' + ) + end + + it 'configures nginx to host installation files' do + expect(centos_7_2).to create_directory('/var/opt/delivery/nginx/etc/addon.d') + expect(centos_7_2).to create_file('/var/opt/delivery/nginx/etc/addon.d/99-installer_internal.conf') + .with( + content: <<-EOF +location /installer { + alias /opt/delivery/embedded/service/omnibus-ctl/installer; +} +EOF + ) + end + + it 'reconfigures automate' do + expect(centos_7_2).to render_ingredient_config('automate') + expect(centos_7_2.ingredient_config('automate')).to notify('chef_ingredient[automate]').to(:reconfigure).immediately + end + + it 'configures automate enterprises' do + expect(centos_7_2).to run_execute('create enterprise test') + end + end +end diff --git a/spec/unit/chef_file_spec.rb b/spec/unit/chef_file_spec.rb new file mode 100644 index 0000000..2dddd88 --- /dev/null +++ b/spec/unit/chef_file_spec.rb @@ -0,0 +1,73 @@ +# +# Cookbook Name:: chef_stack +# Spec:: chef_file +# +# Copyright 2016 Chef Software Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'spec_helper' + +describe 'chef_test::file' do + cached(:centos_7_2) do + ChefSpec::ServerRunner.new( + step_into: 'chef_file', + platform: 'centos', + version: '7.2.1511' + ).converge(described_recipe) + end + + context 'compiling the recipe' do + it 'creates chef_file[/tmp/uri.test]' do + expect(centos_7_2).to create_chef_file('/tmp/remote_file') + end + it 'creates chef_file[/tmp/cookbook.test]' do + expect(centos_7_2).to create_chef_file('/tmp/cookbook_file') + end + it 'creates chef_file[/tmp/content.test]' do + expect(centos_7_2).to create_chef_file('/tmp/file') + end + end + + context 'stepping into chef_file' do + it 'correctly creates cookbook_files' do + expect(centos_7_2).to create_cookbook_file('/tmp/cookbook_file') + .with( + source: 'testfile', + cookbook: 'chef_test', + user: 'root', + group: 'root', + mode: '0600' + ) + end + it 'correctly creates remote_files' do + expect(centos_7_2).to create_remote_file('/tmp/remote_file') + .with( + source: 'https://www.example.com/test', + user: 'root', + group: 'root', + mode: '0600' + ) + end + it 'correctly creates files' do + expect(centos_7_2).to create_file('/tmp/file') + .with( + content: 'abcdef', + user: 'root', + group: 'root', + mode: '0600' + ) + expect(centos_7_2).to render_file('/tmp/file').with_content('abcdef') + end + end +end diff --git a/spec/unit/recipes/default_spec.rb b/spec/unit/recipes/default_spec.rb deleted file mode 100644 index 5bc9b22..0000000 --- a/spec/unit/recipes/default_spec.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# Cookbook Name:: chef_stack -# Spec:: default -# -# Copyright 2016 Chef Software Inc -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require 'spec_helper' - -describe 'chef_stack::default' do - context 'When all attributes are default, on an unspecified platform' do - let(:chef_run) do - runner = ChefSpec::ServerRunner.new - runner.converge(described_recipe) - end - - it 'converges successfully' do - expect { chef_run }.to_not raise_error - end - end -end diff --git a/test/fixtures/cookbooks/chef_test/metadata.rb b/test/fixtures/cookbooks/chef_test/metadata.rb new file mode 100644 index 0000000..3df8494 --- /dev/null +++ b/test/fixtures/cookbooks/chef_test/metadata.rb @@ -0,0 +1,4 @@ +name 'chef_test' +version '0.1.0' + +depends 'chef_stack' diff --git a/test/fixtures/cookbooks/chef_test/recipes/automate.rb b/test/fixtures/cookbooks/chef_test/recipes/automate.rb new file mode 100644 index 0000000..7254ed5 --- /dev/null +++ b/test/fixtures/cookbooks/chef_test/recipes/automate.rb @@ -0,0 +1,44 @@ +insecure_key = <<-EOS +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI +w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP +kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2 +hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO +Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW +yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQIBIwKCAQEA4iqWPJXtzZA68mKd +ELs4jJsdyky+ewdZeNds5tjcnHU5zUYE25K+ffJED9qUWICcLZDc81TGWjHyAqD1 +Bw7XpgUwFgeUJwUlzQurAv+/ySnxiwuaGJfhFM1CaQHzfXphgVml+fZUvnJUTvzf +TK2Lg6EdbUE9TarUlBf/xPfuEhMSlIE5keb/Zz3/LUlRg8yDqz5w+QWVJ4utnKnK +iqwZN0mwpwU7YSyJhlT4YV1F3n4YjLswM5wJs2oqm0jssQu/BT0tyEXNDYBLEF4A +sClaWuSJ2kjq7KhrrYXzagqhnSei9ODYFShJu8UWVec3Ihb5ZXlzO6vdNQ1J9Xsf +4m+2ywKBgQD6qFxx/Rv9CNN96l/4rb14HKirC2o/orApiHmHDsURs5rUKDx0f9iP +cXN7S1uePXuJRK/5hsubaOCx3Owd2u9gD6Oq0CsMkE4CUSiJcYrMANtx54cGH7Rk +EjFZxK8xAv1ldELEyxrFqkbE4BKd8QOt414qjvTGyAK+OLD3M2QdCQKBgQDtx8pN +CAxR7yhHbIWT1AH66+XWN8bXq7l3RO/ukeaci98JfkbkxURZhtxV/HHuvUhnPLdX +3TwygPBYZFNo4pzVEhzWoTtnEtrFueKxyc3+LjZpuo+mBlQ6ORtfgkr9gBVphXZG +YEzkCD3lVdl8L4cw9BVpKrJCs1c5taGjDgdInQKBgHm/fVvv96bJxc9x1tffXAcj +3OVdUN0UgXNCSaf/3A/phbeBQe9xS+3mpc4r6qvx+iy69mNBeNZ0xOitIjpjBo2+ +dBEjSBwLk5q5tJqHmy/jKMJL4n9ROlx93XS+njxgibTvU6Fp9w+NOFD/HvxB3Tcz +6+jJF85D5BNAG3DBMKBjAoGBAOAxZvgsKN+JuENXsST7F89Tck2iTcQIT8g5rwWC +P9Vt74yboe2kDT531w8+egz7nAmRBKNM751U/95P9t88EDacDI/Z2OwnuFQHCPDF +llYOUI+SpLJ6/vURRbHSnnn8a/XG+nzedGH5JGqEJNQsz+xT2axM0/W/CRknmGaJ +kda/AoGANWrLCz708y7VYgAtW2Uf1DPOIYMdvo6fxIB5i9ZfISgcJ/bbCUkFrhoH ++vq/5CIWxCPp0f85R4qxxQ5ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ +NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s= +-----END RSA PRIVATE KEY----- +EOS + +chef_automate 'automate.chefstack.local' do + channel :current + version '0.6.64' + config <<-EOS + nginx['ssl_protocols'] = 'TLSv1.2' + EOS + accept_license true + enterprise 'test' + license 'license' + chef_user 'chef_user' + chef_user_pem insecure_key + validation_pem insecure_key + builder_pem insecure_key +end diff --git a/test/fixtures/cookbooks/chef_test/recipes/file.rb b/test/fixtures/cookbooks/chef_test/recipes/file.rb new file mode 100644 index 0000000..18a56c1 --- /dev/null +++ b/test/fixtures/cookbooks/chef_test/recipes/file.rb @@ -0,0 +1,20 @@ +chef_file '/tmp/remote_file' do + source 'https://www.example.com/test' + user 'root' + group 'root' + mode '0600' +end + +chef_file '/tmp/cookbook_file' do + source 'cookbook_file://chef_test::testfile' + user 'root' + group 'root' + mode '0600' +end + +chef_file '/tmp/file' do + source 'abcdef' + user 'root' + group 'root' + mode '0600' +end diff --git a/test/recipes/default_test.rb b/test/functional/default_test.rb similarity index 100% rename from test/recipes/default_test.rb rename to test/functional/default_test.rb