From 885e97118fed6d9fb36810daf98e30b19ee5d783 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Fri, 8 Dec 2017 16:21:26 -0600 Subject: [PATCH] Support Chef 13 and amazon platform_family --- libraries/helpers.rb | 4 +- recipes/agent_linux_install.rb | 2 +- recipes/repository.rb | 2 +- recipes/server_linux_install.rb | 2 +- spec/go_agent_spec.rb | 86 +++++++++++++++++++++++++++++++++ spec/go_server_spec.rb | 73 ++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+), 5 deletions(-) diff --git a/libraries/helpers.rb b/libraries/helpers.rb index e3139a3..71730bf 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -129,14 +129,14 @@ def fetch_go_version_from_url(url) def package_extension value_for_platform_family('debian' => '.deb', - %w(rhel fedora) => '.noarch.rpm', + %w(rhel fedora amazon) => '.noarch.rpm', 'windows' => '-setup.exe', 'default' => '.zip') end def os_dir value_for_platform_family('debian' => 'deb', - %w(rhel fedora) => 'rpm', + %w(rhel fedora amazon) => 'rpm', 'windows' => 'win', 'default' => 'generic') end diff --git a/recipes/agent_linux_install.rb b/recipes/agent_linux_install.rb index da2a0c2..02aaada 100644 --- a/recipes/agent_linux_install.rb +++ b/recipes/agent_linux_install.rb @@ -44,7 +44,7 @@ source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately end - when 'rhel', 'fedora' + when 'rhel', 'fedora', 'amazon' rpm_package 'go-agent' do source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately diff --git a/recipes/repository.rb b/recipes/repository.rb index ba84619..99b108b 100644 --- a/recipes/repository.rb +++ b/recipes/repository.rb @@ -26,7 +26,7 @@ key node['gocd']['repository']['apt']['key'] unless node['gocd']['repository']['apt']['key'] == false end -when 'rhel', 'fedora' +when 'rhel', 'fedora', 'amazon' include_recipe 'yum' yum_repository 'gocd' do diff --git a/recipes/server_linux_install.rb b/recipes/server_linux_install.rb index 1e7bbc0..39ccc7b 100644 --- a/recipes/server_linux_install.rb +++ b/recipes/server_linux_install.rb @@ -43,7 +43,7 @@ source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately end - when 'rhel', 'fedora' + when 'rhel', 'fedora', 'amazon' rpm_package 'go-server' do source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately diff --git a/spec/go_agent_spec.rb b/spec/go_agent_spec.rb index 357a836..ef689df 100644 --- a/spec/go_agent_spec.rb +++ b/spec/go_agent_spec.rb @@ -140,6 +140,31 @@ expect(chef_run).to upgrade_package('go-agent') end end + context 'When all attributes are default and platform is amazon' do + let(:chef_run) do + run = ChefSpec::SoloRunner.new(step_into: 'gocd_agent') do |node| + node.automatic['platform_family'] = 'amazon' + node.automatic['platform'] = 'amazon' + node.automatic['os'] = 'linux' + node.normal['gocd']['agent']['go_server_url'] = 'https://localhost:8154/go' + end + run.converge(described_recipe) + end + before do + stub_command("grep -q '# Provides: go-agent$' /etc/init.d/go-agent").and_return(false) + end + it_behaves_like :agent_recipe_linux + it_behaves_like :yum_repository_recipe + it 'installs go-agent package' do + expect(chef_run).to install_package('go-agent') + end + + it 'upgrades go-agent package if version is set to `latest`' do + chef_run.node.set['gocd']['version'] = 'latest' + chef_run.converge(described_recipe) + expect(chef_run).to upgrade_package('go-agent') + end + end context 'When many agents and all attributes are default and platform is debian' do let(:chef_run) do @@ -264,6 +289,31 @@ expect(chef_run).to install_rpm_package('go-agent') end end + context 'When installing from package file and platform is amazon' do + let(:chef_run) do + run = ChefSpec::SoloRunner.new(step_into: 'gocd_agent') do |node| + node.automatic['platform_family'] = 'amazon' + node.automatic['platform'] = 'amazon' + node.automatic['os'] = 'linux' + node.normal['gocd']['agent']['go_server_url'] = 'https://localhost:8154/go' + node.normal['gocd']['install_method'] = 'package_file' + end + allow_any_instance_of(Chef::Resource::RemoteFile).to receive(:fetch_content) + .and_return('{"message": "{\"latest-version\": \"16.2.1-3027\"}"}') + run.converge(described_recipe) + end + before do + stub_command("grep -q '# Provides: go-agent$' /etc/init.d/go-agent").and_return(false) + end + it_behaves_like :agent_recipe_linux + it 'downloads go-agent .rpm from remote URL' do + expect(chef_run).to create_remote_file('go-agent-stable.noarch.rpm').with( + source: 'https://download.gocd.org/binaries/16.2.1-3027/rpm/go-agent-16.2.1-3027.noarch.rpm') + end + it 'installs go-agent package from file' do + expect(chef_run).to install_rpm_package('go-agent') + end + end context 'When installing from custom repository and platform is debian' do let(:chef_run) do @@ -333,6 +383,42 @@ expect(chef_run).to install_package('go-agent') end + it 'upgrades go-agent package if version is set to `latest`' do + chef_run.node.set['gocd']['version'] = 'latest' + chef_run.converge(described_recipe) + expect(chef_run).to upgrade_package('go-agent') + end + end + context 'When installing from custom repository and platform is amazon' do + let(:chef_run) do + run = ChefSpec::SoloRunner.new(step_into: 'gocd_agent') do |node| + node.automatic['platform_family'] = 'amazon' + node.automatic['platform'] = 'amazon' + node.automatic['os'] = 'linux' + node.normal['gocd']['agent']['go_server_url'] = 'https://localhost:8154/go' + node.normal['gocd']['install_method'] = 'repository' + node.normal['gocd']['repository']['yum']['baseurl'] = 'http://mycustom/gocd-rpm' + end + run.converge(described_recipe) + end + before do + stub_command("grep -q '# Provides: go-agent$' /etc/init.d/go-agent").and_return(false) + end + it_behaves_like :agent_recipe_linux + it 'includes yum recipe' do + expect(chef_run).to include_recipe('yum') + end + it 'adds my custom gocd yum repository' do + expect(chef_run).to create_yum_repository('gocd').with( + baseurl: 'http://mycustom/gocd-rpm', + gpgcheck: true + ) + end + + it 'installs go-agent package' do + expect(chef_run).to install_package('go-agent') + end + it 'upgrades go-agent package if version is set to `latest`' do chef_run.node.set['gocd']['version'] = 'latest' chef_run.converge(described_recipe) diff --git a/spec/go_server_spec.rb b/spec/go_server_spec.rb index 182bc67..2711588 100644 --- a/spec/go_server_spec.rb +++ b/spec/go_server_spec.rb @@ -78,6 +78,26 @@ expect(chef_run).to upgrade_package('go-server') end end + context 'When all attributes are default and platform is amazon' do + let(:chef_run) do + run = ChefSpec::SoloRunner.new do |node| + node.automatic['platform_family'] = 'amazon' + node.automatic['platform'] = 'amazon' + node.automatic['os'] = 'linux' + end + run.converge(described_recipe) + end + it_behaves_like :server_recipe + it_behaves_like :yum_repository_recipe + it 'installs go-server package' do + expect(chef_run).to install_package('go-server') + end + it 'upgrades go-server package if version is set to `latest`' do + chef_run.node.set['gocd']['version'] = 'latest' + chef_run.converge(described_recipe) + expect(chef_run).to upgrade_package('go-server') + end + end # TODO: server on windows context 'When installing from package_file and platform is debian' do @@ -123,6 +143,27 @@ expect(chef_run).to install_rpm_package('go-server') end end + context 'When installing from package file and platform is amazon' do + let(:chef_run) do + run = ChefSpec::SoloRunner.new do |node| + node.automatic['platform_family'] = 'amazon' + node.automatic['platform'] = 'amazon' + node.automatic['os'] = 'linux' + node.normal['gocd']['install_method'] = 'package_file' + end + allow_any_instance_of(Chef::Resource::RemoteFile).to receive(:fetch_content) + .and_return('{"message": "{\"latest-version\": \"16.2.1-3027\"}"}') + run.converge(described_recipe) + end + it_behaves_like :server_recipe + it 'downloads go-server .rpm from remote URL' do + expect(chef_run).to create_remote_file('go-server-stable.noarch.rpm').with( + source: 'https://download.gocd.org/binaries/16.2.1-3027/rpm/go-server-16.2.1-3027.noarch.rpm') + end + it 'installs go-server package from file' do + expect(chef_run).to install_rpm_package('go-server') + end + end context 'When installing from custom repository and platform is debian' do let(:chef_run) do @@ -185,6 +226,38 @@ expect(chef_run).to install_package('go-server') end + it 'upgrades go-server package if version is set to `latest`' do + chef_run.node.set['gocd']['version'] = 'latest' + chef_run.converge(described_recipe) + expect(chef_run).to upgrade_package('go-server') + end + end + context 'When installing from custom repository and platform is amazon' do + let(:chef_run) do + run = ChefSpec::SoloRunner.new do |node| + node.automatic['platform_family'] = 'amazon' + node.automatic['platform'] = 'amazon' + node.automatic['os'] = 'linux' + node.normal['gocd']['install_method'] = 'repository' + node.normal['gocd']['repository']['yum']['baseurl'] = 'http://mycustom/gocd-rpm' + end + run.converge(described_recipe) + end + it_behaves_like :server_recipe + it 'includes yum recipe' do + expect(chef_run).to include_recipe('yum') + end + it 'adds my custom gocd yum repository' do + expect(chef_run).to create_yum_repository('gocd').with( + baseurl: 'http://mycustom/gocd-rpm', + gpgcheck: true + ) + end + + it 'installs go-server package' do + expect(chef_run).to install_package('go-server') + end + it 'upgrades go-server package if version is set to `latest`' do chef_run.node.set['gocd']['version'] = 'latest' chef_run.converge(described_recipe)