summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2017-04-04 11:26:27 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2017-04-04 11:35:29 -0700
commit446cceb02e4766442b51892c0d2d4cb3036dd525 (patch)
tree42adef21071464097cd45e17242507c712778418
parent427dc75a1688c60ebad03554415a1dd137866c3f (diff)
downloadchef-446cceb02e4766442b51892c0d2d4cb3036dd525.tar.gz
add better specs around modifying gem sources
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--lib/chef/provider/package/rubygems.rb4
-rw-r--r--spec/unit/cookbook/gem_installer_spec.rb15
-rw-r--r--spec/unit/provider/package/rubygems_spec.rb91
3 files changed, 103 insertions, 7 deletions
diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb
index 1269b24c93..cdb2269c73 100644
--- a/lib/chef/provider/package/rubygems.rb
+++ b/lib/chef/provider/package/rubygems.rb
@@ -423,6 +423,7 @@ class Chef
def source_is_remote?
return true if new_resource.source.nil?
+ return true if new_resource.source.is_a?(Array)
scheme = URI.parse(new_resource.source).scheme
# URI.parse gets confused by MS Windows paths with forward slashes.
scheme = nil if scheme =~ /^[a-z]$/
@@ -469,7 +470,8 @@ class Chef
end
def gem_sources
- new_resource.source ? Array(new_resource.source) : nil
+ srcs = new_resource.source || Chef::Config[:rubygems_url]
+ srcs ? Array(srcs) : nil
end
def load_current_resource
diff --git a/spec/unit/cookbook/gem_installer_spec.rb b/spec/unit/cookbook/gem_installer_spec.rb
index 69b714d977..91e6959331 100644
--- a/spec/unit/cookbook/gem_installer_spec.rb
+++ b/spec/unit/cookbook/gem_installer_spec.rb
@@ -67,4 +67,19 @@ describe Chef::Cookbook::GemInstaller do
expect(bundler_dsl.dependencies.find { |d| d.name == "httpclient" }.requirements_list.length).to eql(2)
end
+
+ it "generates a valid Gemfile when Chef::Config[:rubygems_url] is set to a String" do
+ Chef::Config[:rubygems_url] = "https://www.rubygems.org"
+ expect { gem_installer.install }.to_not raise_error
+
+ expect(bundler_dsl.dependencies.find { |d| d.name == "httpclient" }.requirements_list.length).to eql(2)
+ end
+
+ it "generates a valid Gemfile when Chef::Config[:rubygems_url] is set to an Array" do
+ Chef::Config[:rubygems_url] = [ "https://www.rubygems.org" ]
+
+ expect { gem_installer.install }.to_not raise_error
+
+ expect(bundler_dsl.dependencies.find { |d| d.name == "httpclient" }.requirements_list.length).to eql(2)
+ end
end
diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb
index 626a5585a6..08dd888a2b 100644
--- a/spec/unit/provider/package/rubygems_spec.rb
+++ b/spec/unit/provider/package/rubygems_spec.rb
@@ -534,6 +534,16 @@ describe Chef::Provider::Package::Rubygems do
end
end
+ context "when the source is from the rubygems_uri" do
+ it "determines the candidate version by querying the remote gem servers" do
+ Chef::Config[:rubygems_url] = "https://mirror1/"
+ expect(provider.gem_env).to receive(:candidate_version_from_remote)
+ .with(gem_dep, "https://mirror1/")
+ .and_return(Gem::Version.new(target_version))
+ expect(provider.candidate_version).to eq(target_version)
+ end
+ end
+
context "when the requested source is a remote server" do
let(:source) { "http://mygems.example.com" }
@@ -543,6 +553,33 @@ describe Chef::Provider::Package::Rubygems do
.and_return(Gem::Version.new(target_version))
expect(provider.candidate_version).to eq(target_version)
end
+
+ it "overwrites the config variable" do
+ Chef::Config[:rubygems_url] = "https://overridden"
+ expect(provider.gem_env).to receive(:candidate_version_from_remote)
+ .with(gem_dep, source)
+ .and_return(Gem::Version.new(target_version))
+ expect(provider.candidate_version).to eq(target_version)
+ end
+ end
+
+ context "when the requested source is an array" do
+ let(:source) { [ "https://mirror1", "https://mirror2" ] }
+
+ it "determines the candidate version by querying the remote gem servers" do
+ expect(provider.gem_env).to receive(:candidate_version_from_remote)
+ .with(gem_dep, *source)
+ .and_return(Gem::Version.new(target_version))
+ expect(provider.candidate_version).to eq(target_version)
+ end
+
+ it "overwrites the config variable" do
+ Chef::Config[:rubygems_url] = "https://overridden"
+ expect(provider.gem_env).to receive(:candidate_version_from_remote)
+ .with(gem_dep, *source)
+ .and_return(Gem::Version.new(target_version))
+ expect(provider.candidate_version).to eq(target_version)
+ end
end
context "when the requested source is a file" do
@@ -566,13 +603,14 @@ describe Chef::Provider::Package::Rubygems do
current_resource
end
+ let(:version) { Gem::Version.new(candidate_version) }
+
before do
- version = Gem::Version.new(candidate_version)
- args = [gem_dep]
- args << source if source
- allow(provider.gem_env).to receive(:candidate_version_from_remote)
- .with(*args)
- .and_return(version)
+ if source
+ allow(provider.gem_env).to receive(:candidate_version_from_remote).with(gem_dep, *source).and_return(version)
+ else
+ allow(provider.gem_env).to receive(:candidate_version_from_remote).with(gem_dep).and_return(version)
+ end
end
describe "in the current gem environment" do
@@ -633,6 +671,19 @@ describe Chef::Provider::Package::Rubygems do
end
end
+ context "when the Chef::Config[:rubygems_url] option is provided" do
+ let(:gem_binary) { "/foo/bar" }
+
+ it "installs the gem with rubygems.org as an added source" do
+ Chef::Config[:rubygems_url] = "https://mirror1"
+ expect(provider.gem_env).to receive(:candidate_version_from_remote).with(gem_dep, Chef::Config[:rubygems_url]).and_return(version)
+ expected = "#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=https://mirror1"
+ expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900)
+ provider.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+
context "when another source and binary are provided" do
let(:source) { "http://mirror.ops.rhcloud.com/mirror/ruby" }
let(:gem_binary) { "/foo/bar" }
@@ -643,6 +694,34 @@ describe Chef::Provider::Package::Rubygems do
provider.run_action(:install)
expect(new_resource).to be_updated_by_last_action
end
+
+ it "ignores the Chef::Config setting" do
+ Chef::Config[:rubygems_url] = "https://ignored"
+ expected = "#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=#{source}"
+ expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900)
+ provider.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+
+ context "when the source is an array" do
+ let(:source) { [ "https://mirror1" , "https://mirror2" ] }
+ let(:gem_binary) { "/foo/bar" }
+
+ it "installs the gem with an array as an added source" do
+ expected = "#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=https://mirror1 --source=https://mirror2"
+ expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900)
+ provider.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
+
+ it "ignores the Chef::Config setting" do
+ Chef::Config[:rubygems_url] = "https://ignored"
+ expected = "#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=https://mirror1 --source=https://mirror2"
+ expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900)
+ provider.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
end
context "when we have cleared sources and an explict source is specified" do