diff --git a/.kitchen.openstack.yml b/.kitchen.openstack.yml index 92d6560..9a3189c 100644 --- a/.kitchen.openstack.yml +++ b/.kitchen.openstack.yml @@ -14,6 +14,19 @@ platforms: flavor: 'v.c1.m2048.d5.e0' server_create_timeout: 540 server_active_timeout: 540 + - name: windows + provisioner: + name: chef_solo + require_chef_omnibus: 12.4.2 + driver: + name: vagrant + driver_config: + box: 'windows-7-net45-2.0-chef-12.4.2' + customize: + flavor: 'v.c1.m2048.d20.e0' + server_create_timeout: 1200 + server_active_timeout: 1200 + server_name: 'kt-go-win' suites: - name: default diff --git a/Gemfile b/Gemfile index f712acd..770d7ea 100644 --- a/Gemfile +++ b/Gemfile @@ -9,3 +9,4 @@ gem 'kitchen-vagrant' gem 'kitchen-ec2', '~> 0.8.0' gem 'kitchen-digitalocean', '~> 0.7.3' gem 'kitchen-docker' +gem 'winrm-transport', '= 1.0.2' diff --git a/Gemfile.lock b/Gemfile.lock index 9a5279c..c701a0d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -202,6 +202,10 @@ GEM formatador (0.2.5) gherkin (2.12.2) multi_json (~> 1.3) + gssapi (1.2.0) + ffi (>= 1.0.1) + gyoku (1.3.1) + builder (>= 2.1.2) hashie (3.4.3) highline (1.7.8) hitimes (1.2.3) @@ -220,6 +224,10 @@ GEM kitchen-vagrant (0.19.0) test-kitchen (~> 1.4) libyajl2 (1.2.0) + little-plugger (1.1.4) + logging (1.8.2) + little-plugger (>= 1.1.3) + multi_json (>= 1.8.4) method_source (0.8.2) mime-types (2.6.2) mini_portile (0.6.2) @@ -244,6 +252,7 @@ GEM nio4r (1.1.1) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) + nori (2.6.0) octokit (3.8.0) sawyer (~> 0.6.0, >= 0.5.3) ohai (8.7.0) @@ -304,6 +313,8 @@ GEM rspec_junit_formatter (0.2.3) builder (< 4) rspec-core (>= 2, < 4, != 2.12.0) + rubyntlm (0.4.0) + rubyzip (1.1.7) rufus-lru (1.0.5) safe_yaml (1.0.4) sawyer (0.6.0) @@ -342,6 +353,18 @@ GEM varia_model (0.4.1) buff-extensions (~> 1.0) hashie (>= 2.0.2, < 4.0.0) + winrm (1.3.4) + builder (>= 2.1.2) + gssapi (~> 1.2) + gyoku (~> 1.0) + httpclient (~> 2.2, >= 2.2.0.2) + logging (~> 1.6, >= 1.6.1) + nori (~> 2.0) + rubyntlm (~> 0.4.0) + uuidtools (~> 2.1.2) + winrm-transport (1.0.2) + rubyzip (~> 1.1, >= 1.1.7) + winrm (~> 1.3) wmi-lite (1.0.0) yajl-ruby (1.2.1) @@ -358,6 +381,7 @@ DEPENDENCIES kitchen-ec2 (~> 0.8.0) kitchen-vagrant test-kitchen + winrm-transport (= 1.0.2) BUNDLED WITH 1.10.5 diff --git a/README.md b/README.md index 3f01d93..2681aba 100644 --- a/README.md +++ b/README.md @@ -36,21 +36,26 @@ By default installation source is done from apt or yum repositories from officia The **apt** repository can be overriden by changing any these attributes: -```ruby -default['gocd']['repository']['apt']['uri'] = 'https://download.go.cd/' -default['gocd']['repository']['apt']['components'] = [ '/' ] -default['gocd']['repository']['apt']['distribution'] = '' -default['gocd']['repository']['apt']['package_options'] = '' -default['gocd']['repository']['apt']['keyserver'] = 'pgp.mit.edu' -default['gocd']['repository']['apt']['key'] = '0xd8843f288816c449' -``` + * `node['gocd']['repository']['apt']['uri'] = 'https://download.go.cd/'` + * `node['gocd']['repository']['apt']['components'] = [ '/' ]` + * `node['gocd']['repository']['apt']['distribution'] = ''` + * `node['gocd']['repository']['apt']['package_options'] = ''` + * `node['gocd']['repository']['apt']['keyserver'] = 'pgp.mit.edu'` + * `node['gocd']['repository']['apt']['key'] = '0xd8843f288816c449'` The **yum** repository can be overriden by changing any these attributes: -```ruby -default['gocd']['repository']['yum']['baseurl'] = 'https://download.go.cd' -default['gocd']['repository']['yum']['gpgcheck'] = true -default['gocd']['repository']['yum']['gpgkey'] = 'https://download.go.cd/GOCD-GPG-KEY.asc' + * `node['gocd']['repository']['yum']['baseurl'] = 'https://download.go.cd'` + * `node['gocd']['repository']['yum']['gpgcheck'] = true` + * `node['gocd']['repository']['yum']['gpgkey'] = 'https://download.go.cd/GOCD-GPG-KEY.asc'` + +#### Experimental channel + +By default Go cookbook installs latest stable version. +You can install gocd from experimental channel by setting + +``` +node['gocd']['use_experimental'] = true ``` ### From remote file @@ -69,7 +74,8 @@ And assign base url where packages are available for download node['gocd']['package_file']['baseurl'] = 'http://my/custom/url' ``` -The final download URL of file is built based on platform and `node['gocd']['version']`. E.g. `http://my/custom/url/go-agent-15.2.0-2520.deb` +The final download URL of file is built based on platform and `node['gocd']['version']`. +E.g. `http://my/custom/url/go-agent-15.2.0-2520.deb` # GoCD Server diff --git a/attributes/repository.rb b/attributes/repository.rb index b7e99f9..647e9d6 100644 --- a/attributes/repository.rb +++ b/attributes/repository.rb @@ -1,50 +1,22 @@ -default['gocd']['install_method'] = 'repository' -default['gocd']['download']['baseurl'] = 'https://download.go.cd' +default['gocd']['version'] = nil # can be `latest` or specify a version `X.Y.Z-ABCD` +default['gocd']['use_experimental'] = false -default['gocd']['repository']['apt']['uri'] = node['gocd']['download']['baseurl'] -default['gocd']['repository']['apt']['components'] = [ '/' ] +if node['platform_family'] == 'windows' + default['gocd']['install_method'] = 'package_file' +else + default['gocd']['install_method'] = 'repository' +end + +default['gocd']['updates']['url'] = nil + +default['gocd']['repository']['apt']['components'] = ['/'] default['gocd']['repository']['apt']['distribution'] = '' -default['gocd']['repository']['apt']['package_options'] = '' default['gocd']['repository']['apt']['keyserver'] = 'pgp.mit.edu' default['gocd']['repository']['apt']['key'] = '0xd8843f288816c449' -default['gocd']['repository']['yum']['baseurl'] = node['gocd']['download']['baseurl'] default['gocd']['repository']['yum']['gpgcheck'] = true default['gocd']['repository']['yum']['gpgkey'] = 'https://download.go.cd/GOCD-GPG-KEY.asc' -case node['gocd']['install_method'] -when 'repository' -# version = nil so just pick latest available -else - default['gocd']['version'] = '16.2.1-3027' -end - -version = node['gocd']['version'] -os_dir = nil - -case node['platform_family'] -when 'debian' - default['gocd']['server']['package_file']['filename'] = "go-server-#{version}.deb" - default['gocd']['agent']['package_file']['filename'] = "go-agent-#{version}.deb" - default['gocd']['package_file']['baseurl'] = node['gocd']['download']['baseurl'] - os_dir = 'deb' -when 'rhel', 'fedora' - default['gocd']['server']['package_file']['filename'] = "go-server-#{version}.noarch.rpm" - default['gocd']['agent']['package_file']['filename'] = "go-agent-#{version}.noarch.rpm" - default['gocd']['package_file']['baseurl'] = node['gocd']['download']['baseurl'] - os_dir = 'rpm' -when 'windows' - default['gocd']['server']['package_file']['filename'] = "go-server-#{version}-setup.exe" - default['gocd']['agent']['package_file']['filename'] = "go-agent-#{version}-setup.exe" - default['gocd']['package_file']['baseurl'] = node['gocd']['download']['baseurl'] - os_dir = 'win' -end - -default['gocd']['server']['package_file']['path'] = - File.join(Chef::Config[:file_cache_path], node['gocd']['server']['package_file']['filename']) -default['gocd']['server']['package_file']['url'] = - "#{node['gocd']['package_file']['baseurl']}/binaries/#{version}/#{os_dir}/#{node['gocd']['server']['package_file']['filename']}" -default['gocd']['agent']['package_file']['path'] = - File.join(Chef::Config[:file_cache_path], node['gocd']['agent']['package_file']['filename']) -default['gocd']['agent']['package_file']['url'] = - "#{node['gocd']['package_file']['baseurl']}/binaries/#{version}/#{os_dir}/#{node['gocd']['agent']['package_file']['filename']}" +default['gocd']['package_file']['baseurl'] = nil # official - "https://download.go.cd/binaries" +default['gocd']['agent']['package_file']['url'] = nil # official +default['gocd']['server']['package_file']['url'] = nil # official diff --git a/libraries/helpers.rb b/libraries/helpers.rb index 1658556..a1307c9 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -1,11 +1,17 @@ +require 'open-uri' + module Gocd module Helpers + def fetch_content url + open(url, 'r').read + end + def get_agent_properties values = {} - values[:go_server_port] = node['gocd']['agent']['go_server_port'] + values[:go_server_port] = node['gocd']['agent']['go_server_port'] if Chef::Config['solo'] || node['gocd']['agent']['go_server_host'] Chef::Log.info("Attempting to use node['gocd']['agent']['go_server_host'] attribute for server host") - values[:go_server_host] = node['gocd']['agent']['go_server_host'] + values[:go_server_host] = node['gocd']['agent']['go_server_host'] values[:key] = node['gocd']['agent']['autoregister']['key'] else server_search_query = node['gocd']['agent']['server_search_query'] @@ -41,6 +47,159 @@ def go_server_config_file '/etc/go/cruise-config.xml' end end + + def latest_version? + user_requested_version == 'latest' + end + + def experimental? + node['gocd']['use_experimental'] + end + + # version to pass into 'package' resource + def user_requested_version + # just return attribute value, when nil it will default to installing stable + node['gocd']['version'] + end + + # Only needed when downloading package from URL + def remote_version + if latest_version? || user_requested_version.nil? || user_requested_version.empty? + fetch_go_version(experimental?) + else + user_requested_version + end + end + + def updates_base_feed + node['gocd']['updates']['baseurl'] + end + + def updates_url + if node['gocd']['updates']['url'] + # user provided updates url + node['gocd']['updates']['url'] + elsif node['gocd']['use_experimental'] + 'https://update.go.cd/channels/experimental/latest.json' + else + 'https://update.go.cd/channels/supported/latest.json' + end + end + + def fetch_go_version(_is_experimental) + url = updates_url + + begin + fetch_go_version_from_url url + rescue => e + Chef::Log.error("Failed to get Go version from updates service - #{e}") + # fallback to last known stable + '16.3.0-3183' + end + end + + def fetch_go_version_from_url url + text = fetch_content url + if text.empty? + fail 'text is empty' + end + parsed = JSON.parse(text) + fail 'Invalid format in version json file' unless parsed['message'] + message = JSON.parse(parsed['message']) + return message['latest-version'] + end + + def package_extension + value_for_platform_family('debian' => '.deb', + %w(rhel fedora) => '.noarch.rpm', + 'windows' => '-setup.exe', + 'default' => '.zip') + end + + def os_dir + value_for_platform_family('debian' => 'deb', + %w(rhel fedora) => 'rpm', + 'windows' => 'win', + 'default' => 'generic') + end + + def go_agent_remote_package_name + "go-agent-#{remote_version}#{package_extension}" + end + + def go_server_remote_package_name + "go-server-#{remote_version}#{package_extension}" + end + + def user_friendly_version(component) + if node['gocd']['version'] + return node['gocd']['version'] + elsif node['gocd'][component]['package_file']['url'] + return 'custom' + elsif experimental? + return 'experimental' + else + return 'stable' + end + end + + # user-friendly file names to use when downloading remote file + def go_agent_package_name + "go-agent-#{user_friendly_version('agent')}#{package_extension}" + end + + def go_server_package_name + "go-server-#{user_friendly_version('server')}#{package_extension}" + end + + def yum_uri + if node['gocd']['repository']['yum']['baseurl'] + # user provided yum URI + node['gocd']['repository']['yum']['baseurl'] + elsif node['gocd']['use_experimental'] + 'https://download.go.cd/experimental' + else + 'https://download.go.cd' + end + end + + def apt_uri + if node['gocd']['repository']['apt']['uri'] + # user provided apt URI + node['gocd']['repository']['apt']['uri'] + elsif node['gocd']['use_experimental'] + 'https://download.go.cd/experimental' + else + 'https://download.go.cd' + end + end + + def go_baseurl + if node['gocd']['package_file']['baseurl'] + # user specifed url to download packages from + node['gocd']['package_file']['baseurl'] + else + # use official source + "https://download.go.cd/binaries" + end + end + + def go_agent_package_url + if node['gocd']['agent']['package_file']['url'] + # user specifed explictly the URL to download from + node['gocd']['agent']['package_file']['url'] + else + "#{go_baseurl}/#{remote_version}/#{os_dir}/#{go_agent_remote_package_name}" + end + end + def go_server_package_url + if node['gocd']['server']['package_file']['url'] + # user specifed explictly the URL to download from + node['gocd']['server']['package_file']['url'] + else + "#{go_baseurl}/#{remote_version}/#{os_dir}/#{go_server_remote_package_name}" + end + end end end diff --git a/recipes/agent_linux_install.rb b/recipes/agent_linux_install.rb index ebbb2c3..eed4690 100644 --- a/recipes/agent_linux_install.rb +++ b/recipes/agent_linux_install.rb @@ -11,23 +11,28 @@ package_options = node['gocd']['repository']['apt']['package_options'] if node['platform_family'] == 'debian' package "go-agent" do notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately - version node['gocd']['version'] + if latest_version? + action :upgrade + else + version user_requested_version + end end when 'package_file' - remote_file node['gocd']['agent']['package_file']['filename'] do - path node['gocd']['agent']['package_file']['path'] - source node['gocd']['agent']['package_file']['url'] - mode 0644 + package_path = File.join(Chef::Config[:file_cache_path], go_agent_package_name) + remote_file go_agent_package_name do + path package_path + source go_agent_package_url + mode 00644 end case node['platform_family'] when 'debian' dpkg_package 'go-agent' do - source node['gocd']['agent']['package_file']['path'] + source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately end when 'rhel','fedora' rpm_package 'go-agent' do - source node['gocd']['agent']['package_file']['path'] + source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately end end diff --git a/recipes/agent_windows_install.rb b/recipes/agent_windows_install.rb index 3a689c7..345aa1b 100644 --- a/recipes/agent_windows_install.rb +++ b/recipes/agent_windows_install.rb @@ -1,6 +1,8 @@ -remote_file node['gocd']['agent']['package_file']['filename'] do - path node['gocd']['agent']['package_file']['path'] - source node['gocd']['agent']['package_file']['url'] +package_path = File.join(Chef::Config[:file_cache_path],go_agent_package_name) + +remote_file go_agent_package_name do + path package_path + source go_agent_package_url end autoregister_values = get_agent_properties @@ -12,16 +14,19 @@ opts = [] opts << "/SERVERIP=#{autoregister_values[:go_server_host]}" +opts << "/S" opts << '/D=C:\GoAgent' if defined?(Chef::Provider::Package::Windows) package 'Go Agent' do - source node['gocd']['agent']['package_file']['path'] + installer_type :nsis + source package_path options opts.join(" ") end else windows_package 'Go Agent' do - source node['gocd']['agent']['package_file']['path'] + installer_type :nsis + source package_path options opts.join(" ") end end diff --git a/recipes/repository.rb b/recipes/repository.rb index c97b8ed..455befd 100644 --- a/recipes/repository.rb +++ b/recipes/repository.rb @@ -1,9 +1,10 @@ case node['platform_family'] when 'debian' include_recipe 'apt' + package 'apt-transport-https' apt_repository 'gocd' do - uri node['gocd']['repository']['apt']['uri'] + uri apt_uri components node['gocd']['repository']['apt']['components'] distribution node['gocd']['repository']['apt']['distribution'] keyserver node['gocd']['repository']['apt']['keyserver'] unless node['gocd']['repository']['apt']['keyserver'] == false @@ -15,7 +16,7 @@ yum_repository 'gocd' do description "GoCD YUM Repository" - baseurl node['gocd']['repository']['yum']['baseurl'] + baseurl yum_uri gpgcheck node['gocd']['repository']['yum']['gpgcheck'] gpgkey node['gocd']['repository']['yum']['gpgkey'] end diff --git a/recipes/server_linux_install.rb b/recipes/server_linux_install.rb index e1c2281..1fe2333 100644 --- a/recipes/server_linux_install.rb +++ b/recipes/server_linux_install.rb @@ -13,25 +13,30 @@ include_recipe 'gocd::repository' package_options = node['gocd']['repository']['apt']['package_options'] if node['platform_family'] == 'debian' package "go-server" do - version node['gocd']['version'] + if latest_version? + action :upgrade + else + version user_requested_version + end options package_options notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately end when 'package_file' - remote_file node['gocd']['server']['package_file']['filename'] do - path node['gocd']['server']['package_file']['path'] - source node['gocd']['server']['package_file']['url'] + package_path = File.join(Chef::Config[:file_cache_path], go_server_package_name) + remote_file go_server_package_name do + path package_path + source go_server_package_url mode 0644 end case node['platform_family'] when 'debian' dpkg_package 'go-server' do - source node['gocd']['server']['package_file']['path'] + source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately end when 'rhel','fedora' rpm_package 'go-server' do - source node['gocd']['server']['package_file']['path'] + source package_path notifies :reload, 'ohai[reload_passwd_for_go_user]', :immediately end end diff --git a/recipes/server_windows_install.rb b/recipes/server_windows_install.rb index 4fc7d67..c6a6e53 100644 --- a/recipes/server_windows_install.rb +++ b/recipes/server_windows_install.rb @@ -1,6 +1,8 @@ -remote_file node['gocd']['server']['package_file']['filename'] do - path node['gocd']['server']['package_file']['path'] - source node['gocd']['server']['package_file']['url'] +package_path = File.join(Chef::Config[:file_cache_path],go_server_package_name) + +remote_file go_server_package_name do + path package_path + source go_server_package_url end opts = [] @@ -9,12 +11,14 @@ if defined?(Chef::Provider::Package::Windows) package 'Go Server' do - source node['gocd']['server']['package_file']['path'] + installer_type :nsis + source package_path options opts.join(" ") end else windows_package 'Go Server' do - source node['gocd']['server']['package_file']['path'] + installer_type :nsis + source package_path options opts.join(" ") end end diff --git a/spec/go_agent_spec.rb b/spec/go_agent_spec.rb index bf6accd..1944e96 100644 --- a/spec/go_agent_spec.rb +++ b/spec/go_agent_spec.rb @@ -62,6 +62,12 @@ 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 all attributes are default and platform is centos' do let(:chef_run) do @@ -81,6 +87,12 @@ 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 @@ -104,6 +116,13 @@ 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 + # https://github.com/gocd/gocd/blob/master/installers/agent/release/README.md it 'creates additional gocd_agent chef resource' do expect(chef_run).to create_gocd_agent('go-agent-1') @@ -157,6 +176,9 @@ node.normal['gocd']['agent']['go_server_host'] = 'localhost' node.normal['gocd']['install_method'] = 'package_file' end + allow_any_instance_of(Chef::Resource::RemoteFile).to receive(:fetch_content) + .with('https://update.go.cd/channels/supported/latest.json') + .and_return('{"message": "{\"latest-version\": \"16.2.1-3027\"}"}') run.converge(described_recipe) end before do @@ -164,8 +186,8 @@ end it_behaves_like :agent_recipe_linux it 'downloads go-agent .deb from remote URL' do - expect(chef_run).to create_remote_file('go-agent-16.1.0-2855.deb').with( - source: 'https://download.go.cd/binaries/16.1.0-2855/deb/go-agent-16.1.0-2855.deb') + expect(chef_run).to create_remote_file('go-agent-stable.deb').with( + source: 'https://download.go.cd/binaries/16.2.1-3027/deb/go-agent-16.2.1-3027.deb') end it 'installs go-agent package from file' do expect(chef_run).to install_dpkg_package('go-agent') @@ -180,6 +202,8 @@ node.normal['gocd']['agent']['go_server_host'] = 'localhost' 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 @@ -187,8 +211,8 @@ 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-16.1.0-2855.noarch.rpm').with( - source: 'https://download.go.cd/binaries/16.1.0-2855/rpm/go-agent-16.1.0-2855.noarch.rpm') + expect(chef_run).to create_remote_file('go-agent-stable.noarch.rpm').with( + source: 'https://download.go.cd/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') @@ -229,6 +253,13 @@ 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 installing from custom repository and platform is centos' do let(:chef_run) do @@ -255,8 +286,15 @@ 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) + expect(chef_run).to upgrade_package('go-agent') + end end end diff --git a/spec/go_agent_windows_spec.rb b/spec/go_agent_windows_spec.rb index 4eadedf..4f7caa2 100644 --- a/spec/go_agent_windows_spec.rb +++ b/spec/go_agent_windows_spec.rb @@ -16,13 +16,17 @@ node.automatic['platform'] = 'windows' node.automatic['os'] = 'windows' 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 'downloads official installer' do + expect(chef_run).to create_remote_file('go-agent-stable-setup.exe').with( + :source => 'https://download.go.cd/binaries/16.2.1-3027/win/go-agent-16.2.1-3027-setup.exe') + end it 'installs go-agent package' do - expect(chef_run).to install_package('Go Agent').with( - :source => 'https://download.go.cd/binaries/16.1.0-2855/win/go-agent-16.1.0-2855-setup.exe' - ) + expect(chef_run).to install_package('Go Agent') end end @@ -37,11 +41,12 @@ end run.converge(described_recipe) end - + it 'downloads specified installer' do + expect(chef_run).to create_remote_file('go-agent-custom-setup.exe').with( + :source => 'https://example.com/go-agent.exe') + end it 'installs go-agent package' do - expect(chef_run).to install_package('Go Agent').with( - :source => 'https://example.com/go-agent.exe' - ) + expect(chef_run).to install_package('Go Agent') end end end diff --git a/spec/go_server_spec.rb b/spec/go_server_spec.rb index 397f828..fa211de 100644 --- a/spec/go_server_spec.rb +++ b/spec/go_server_spec.rb @@ -28,9 +28,16 @@ end it_behaves_like :server_recipe it_behaves_like :apt_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 context 'When all attributes are default and platform is centos' do let(:chef_run) do @@ -46,6 +53,11 @@ 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 @@ -58,12 +70,14 @@ 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 .deb from remote URL' do - expect(chef_run).to create_remote_file('go-server-16.1.0-2855.deb').with( - source: 'https://download.go.cd/binaries/16.1.0-2855/deb/go-server-16.1.0-2855.deb') + expect(chef_run).to create_remote_file('go-server-stable.deb').with( + source: 'https://download.go.cd/binaries/16.2.1-3027/deb/go-server-16.2.1-3027.deb') end it 'installs go-server package from file' do expect(chef_run).to install_dpkg_package('go-server') @@ -77,12 +91,14 @@ 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-16.1.0-2855.noarch.rpm').with( - source: 'https://download.go.cd/binaries/16.1.0-2855/rpm/go-server-16.1.0-2855.noarch.rpm') + expect(chef_run).to create_remote_file('go-server-stable.noarch.rpm').with( + source: 'https://download.go.cd/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') @@ -116,9 +132,16 @@ key: nil, components: ['/']) 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) + expect(chef_run).to upgrade_package('go-server') + end end context 'When installing from custom repository and platform is centos' do let(:chef_run) do @@ -141,8 +164,16 @@ 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) + expect(chef_run).to upgrade_package('go-server') + end + end end diff --git a/spec/go_server_windows_spec.rb b/spec/go_server_windows_spec.rb index 531a3c6..deae3fb 100644 --- a/spec/go_server_windows_spec.rb +++ b/spec/go_server_windows_spec.rb @@ -16,13 +16,39 @@ node.automatic['platform'] = 'windows' node.automatic['os'] = 'windows' 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 'downloads official installer' do + expect(chef_run).to create_remote_file('go-server-stable-setup.exe').with( + :source => 'https://download.go.cd/binaries/16.2.1-3027/win/go-server-16.2.1-3027-setup.exe') + end + it 'installs go-server package' do - expect(chef_run).to install_package('Go Server').with( - :source => 'https://download.go.cd/binaries/16.1.0-2855/win/go-server-16.1.0-2855-setup.exe' - ) + expect(chef_run).to install_package('Go Server') + end + end + + context 'When experimental flag is set and platform is windows' do + it_behaves_like :service_recipe_windows + let(:chef_run) do + run = ChefSpec::SoloRunner.new(step_into: 'gocd_server') do |node| + node.automatic['platform_family'] = 'windows' + node.automatic['platform'] = 'windows' + node.automatic['os'] = 'windows' + node.normal['gocd']['use_experimental'] = true + end + allow_any_instance_of(Chef::Resource::RemoteFile).to receive(:fetch_content) + .with('https://update.go.cd/channels/experimental/latest.json') + .and_return('{"message": "{\"latest-version\": \"20.1.2-12345\"}"}') + run.converge(described_recipe) + end + + it 'downloads official experimental installer' do + expect(chef_run).to create_remote_file('go-server-experimental-setup.exe').with( + :source => 'https://download.go.cd/binaries/20.1.2-12345/win/go-server-20.1.2-12345-setup.exe') end end @@ -38,10 +64,13 @@ run.converge(described_recipe) end + it 'downloads specified installer' do + expect(chef_run).to create_remote_file('go-server-custom-setup.exe').with( + :source => 'https://example.com/go-server.exe') + end + it 'installs go-server package' do - expect(chef_run).to install_package('Go Server').with( - :source => 'https://example.com/go-server.exe' - ) + expect(chef_run).to install_package('Go Server') end end end diff --git a/spec/libraries/helpers_spec.rb b/spec/libraries/helpers_spec.rb new file mode 100644 index 0000000..fcf5e03 --- /dev/null +++ b/spec/libraries/helpers_spec.rb @@ -0,0 +1,12 @@ +require_relative '../spec_helper' +require_relative '../../libraries/helpers' + +RSpec.describe Gocd::Helpers do + let(:my_recipe) { Class.new { extend Gocd::Helpers } } + + it 'parses go version from json message in channel' do + allow(my_recipe).to receive(:fetch_content).with('url') + .and_return('{"message": "{\"latest-version\": \"16.2.1-3027\"}"}') + expect(my_recipe.fetch_go_version_from_url('url')).to eq '16.2.1-3027' + end +end diff --git a/spec/shared_examples.rb b/spec/shared_examples.rb index 95db916..a2e2165 100644 --- a/spec/shared_examples.rb +++ b/spec/shared_examples.rb @@ -12,7 +12,17 @@ key: "0xd8843f288816c449", components: ['/']) end + it 'adds gocd experimental apt repository if experimental flag is turned on' do + chef_run.node.set['gocd']['use_experimental'] = true + chef_run.converge(described_recipe) + expect(chef_run).to add_apt_repository('gocd').with( + uri: 'https://download.go.cd/experimental', + keyserver: "pgp.mit.edu", + key: "0xd8843f288816c449", + components: ['/']) + end end + shared_examples_for :yum_repository_recipe do before do stub_command("grep -q '# Provides: go-agent$' /etc/init.d/go-agent").and_return(false) @@ -28,14 +38,27 @@ gpgkey: 'https://download.go.cd/GOCD-GPG-KEY.asc' ) end + it 'adds gocd experimental yum repository if experimental flag is turned on' do + chef_run.node.set['gocd']['use_experimental'] = true + chef_run.converge(described_recipe) + expect(chef_run).to create_yum_repository('gocd').with( + baseurl: 'https://download.go.cd/experimental', + description: 'GoCD YUM Repository', + gpgcheck: true, + gpgkey: 'https://download.go.cd/GOCD-GPG-KEY.asc' + ) + end end + shared_examples_for :agent_linux_install do before do stub_command("grep -q '# Provides: go-agent$' /etc/init.d/go-agent").and_return(false) end + it 'includes java recipe' do expect(chef_run).to include_recipe('java::default') end + it 'includes gocd::agent_linux_install recipe' do expect(chef_run).to include_recipe('gocd::agent_linux_install') end