Skip to content

Commit

Permalink
Merge pull request #178 from Temikus/fix_key_loading
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
erjohnso authored Oct 19, 2017
2 parents 06a6d92 + b5a9508 commit 55846cb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
8 changes: 0 additions & 8 deletions lib/vagrant-google/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,6 @@ def zone_config(zone, attributes=nil, &block)
@__zone_config[zone] << block if block_given?
end

def expand_path(path, root_path)
return path if not path
return Pathname.new(path).expand_path(root_path)
end

#-------------------------------------------------------------------
# Internal methods.
#-------------------------------------------------------------------
Expand Down Expand Up @@ -352,9 +347,6 @@ def validate(machine)
if @zone
config = get_zone_config(@zone)

config.google_key_location = expand_path(config.google_key_location, machine.env.root_path)
config.google_json_key_location = expand_path(config.google_json_key_location, machine.env.root_path)

errors << I18n.t("vagrant_google.config.google_project_id_required") if \
config.google_project_id.nil?
errors << I18n.t("vagrant_google.config.google_client_email_required") if \
Expand Down
2 changes: 1 addition & 1 deletion tasks/acceptance.rake
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace :acceptance do
provisioner/shell
).map{ |s| "provider/google/#{s}" }

command = "bundle exec vagrant-spec test --components=#{components.join(" ")}"
command = "vagrant-spec test --components=#{components.join(" ")}"
puts command
puts
exec(command)
Expand Down
1 change: 0 additions & 1 deletion test/unit/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "rubygems"
require "rspec/autorun"

# Require Vagrant itself so we can reference the proper
# classes to test.
Expand Down
46 changes: 25 additions & 21 deletions test/unit/common/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Ensure tests are not affected by Google credential environment variables
before :each do
ENV.stub(:[] => nil)
allow(ENV).to receive_messages(:[] => nil)
end

describe "defaults" do
Expand All @@ -43,7 +43,7 @@
its("metadata") { should == {} }
its("tags") { should == [] }
its("service_accounts") { should == nil }
its("preemptible") { should be_false }
its("preemptible") { should be_falsey }
its("auto_restart") { should }
its("on_host_maintenance") { should == "MIGRATE" }
end
Expand All @@ -59,26 +59,24 @@
it "should not default #{attribute} if overridden" do
instance.send("#{attribute}=".to_sym, "foo")
instance.finalize!
instance.send(attribute).should == "foo"
expect(instance.send(attribute)).to eq "foo"
end
end

it "should raise error when preemptible and auto_restart is true" do
instance.preemptible = true
instance.auto_restart = true
expected_error = "en.vagrant_google.config.auto_restart_invalid_on_preemptible"
instance.finalize!
errors = instance.validate("foo")["Google Provider"]
errors.inject(false) { |a, e| a or e.include?(expected_error) }.should == true
expect(errors).to include(/auto_restart_invalid_on_preemptible/)
end

it "should raise error when preemptible and on_host_maintenance is not TERMINATE" do
instance.preemptible = true
instance.on_host_maintenance = "MIGRATE"
expected_error = "en.vagrant_google.config.on_host_maintenance_invalid_on_preemptible"
instance.finalize!
errors = instance.validate("foo")["Google Provider"]
errors.inject(false) { |a, e| a or e.include?(expected_error) }.should == true
expect(errors).to include(/on_host_maintenance_invalid_on_preemptible/)
end
end

Expand All @@ -97,9 +95,9 @@

context "with Google credential environment variables" do
before :each do
ENV.stub(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
ENV.stub(:[]).with("GOOGLE_KEY_LOCATION").and_return("/path/to/key")
ENV.stub(:[]).with("GOOGLE_JSON_KEY_LOCATION").and_return("/path/to/json/key")
allow(ENV).to receive(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
allow(ENV).to receive(:[]).with("GOOGLE_KEY_LOCATION").and_return("/path/to/key")
allow(ENV).to receive(:[]).with("GOOGLE_JSON_KEY_LOCATION").and_return("/path/to/json/key")
end

subject do
Expand All @@ -115,9 +113,9 @@

context "With both Google credential environment variables" do
before :each do
ENV.stub(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
ENV.stub(:[]).with("GOOGLE_KEY_LOCATION").and_return("/path/to/key")
ENV.stub(:[]).with("GOOGLE_JSON_KEY_LOCATION").and_return("/path/to/json/key")
allow(ENV).to receive(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
allow(ENV).to receive(:[]).with("GOOGLE_KEY_LOCATION").and_return("/path/to/key")
allow(ENV).to receive(:[]).with("GOOGLE_JSON_KEY_LOCATION").and_return("/path/to/json/key")
end

it "Should return duplicate key location errors" do
Expand All @@ -128,7 +126,7 @@

context "With none of the Google credential environment variables set" do
before :each do
ENV.stub(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
allow(ENV).to receive(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
end

it "Should return no key set errors" do
Expand Down Expand Up @@ -165,7 +163,7 @@ def set_test_values(instance)

it "should raise an exception if not finalized" do
expect { instance.get_zone_config("us-central1-f") }.
to raise_error
to raise_error(RuntimeError,/Configuration must be finalized/)
end

context "with no specific config set" do
Expand Down Expand Up @@ -260,10 +258,10 @@ def set_test_values(instance)
second.metadata["two"] = "bar"

third = first.merge(second)
third.metadata.should == {
expect(third.metadata).to eq({
"one" => "foo",
"two" => "bar"
}
})
end
end

Expand All @@ -282,16 +280,22 @@ def set_test_values(instance)
end
end

before :each do
# Stub out required env to make sure we produce only errors we're looking for.
allow(ENV).to receive(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
allow(ENV).to receive(:[]).with("GOOGLE_PROJECT_ID").and_return("my-awesome-project")
allow(ENV).to receive(:[]).with("GOOGLE_JSON_KEY_LOCATION").and_return("/path/to/json/key")
allow(ENV).to receive(:[]).with("GOOGLE_SSH_KEY_LOCATION").and_return("/path/to/ssh/key")
end

it "should fail auto_restart validation" do
expected_error = "en.vagrant_google.config.auto_restart_invalid_on_preemptible"
errors = subject.validate("foo")["Google Provider"]
errors.inject(false) { |a, e| a or e.include?(expected_error) }.should == true
expect(errors).to include(/auto_restart_invalid_on_preemptible/)
end

it "should fail on_host_maintenance validation" do
expected_error = "en.vagrant_google.config.on_host_maintenance_invalid_on_preemptible"
errors = subject.validate("foo")["Google Provider"]
errors.inject(false) { |a, e| a or e.include?(expected_error) }.should == true
expect(errors).to include(/on_host_maintenance_invalid_on_preemptible/)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions vagrant-google.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ Gem::Specification.new do |s|
# This is a restriction to avoid an error "undefined method 'last_comment'"
# which is deprecated (see https://github.com/ruby/rake/issues/116)
# Remove it after update rspec-core to version greater than or equal to 3.4.4
s.add_development_dependency "rake", "< 11.0"
s.add_development_dependency "rspec", "~> 2.14"
s.add_development_dependency "rubocop", "~> 0.35"
# s.add_development_dependency "rake", "< 11.0"
s.add_development_dependency "rspec", ">= 3.5.0", "<= 3.6"
s.add_development_dependency "rspec-its", "~> 1.2"
s.add_development_dependency "rubocop", "~> 0.50"
s.add_development_dependency "highline"

# The following block of code determines the files that should be included
Expand Down
2 changes: 1 addition & 1 deletion vagrant-spec.config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Vagrant::Spec::Acceptance.configure do |c|
c.component_paths << File.expand_path("../test/acceptance", __FILE__)
c.skeleton_paths << File.expand_path("../test/acceptance/skeletons", __FILE__)

c.assert_retries = 1
c.provider "google",
box: File.expand_path("../google-test.box", __FILE__),
contexts: ["provider-context/google"]
Expand Down

0 comments on commit 55846cb

Please sign in to comment.