require 'spec_helper' require "vagrant-profitbricks/config" describe VagrantPlugins::ProfitBricks::Config do describe "defaults" do let(:vagrant_public_key) { Vagrant.source_root.join("keys/vagrant.pub") } subject do super().tap do |o| o.finalize! end end its(:password) { should be_nil } its(:profitbricks_url) { should be_nil } its(:profitbricks_cores) { should eq(1) } its(:profitbricks_ram) { should eq(256) } its(:image) { should eq(/ubuntu/) } its(:server_name) { should be_nil } its(:username) { should be_nil } its(:networks) { should be_nil } its(:admin_password) { should be_nil } end describe "overriding defaults" do [:password, :profitbricks_url, :profitbricks_cores, :profitbricks_ram, :image, :server_name, :username, :admin_password ].each do |attribute| it "should not default #{attribute} if overridden" do subject.send("#{attribute}=".to_sym, "foo") subject.finalize! subject.send(attribute).should == "foo" end end it "should not default networks if overridden" do net_id = "deadbeef-0000-0000-0000-000000000000" subject.send(:network, net_id) subject.finalize! subject.send(:networks).should include(net_id) subject.send(:networks).should include(VagrantPlugins::ProfitBricks::Config::PUBLIC_NET_ID) subject.send(:networks).should include(VagrantPlugins::ProfitBricks::Config::SERVICE_NET_ID) end it "should not default rsync_includes if overridden" do inc = "core" subject.send(:rsync_include, inc) subject.finalize! subject.send(:rsync_includes).should include(inc) end end describe "validation" do let(:machine) { double("machine") } let(:validation_errors) { subject.validate(machine)['ProfitBricks Provider'] } let(:error_message) { double("error message") } before(:each) do machine.stub_chain(:env, :root_path).and_return '/' subject.username = 'foo' subject.password = 'bar' subject.datacenter_id = 'dc' end subject do super().tap do |o| o.finalize! end end context "with invalid key" do it "should raise an error" do subject.nonsense1 = true subject.nonsense2 = false I18n.should_receive(:t).with('vagrant.config.common.bad_field', { :fields => 'nonsense1, nonsense2' }) .and_return error_message validation_errors.first.should == error_message end end context "with good values" do it "should validate" do validation_errors.should be_empty end end context "the API password" do it "should error if not given" do subject.password = nil I18n.should_receive(:t).with('vagrant_profitbricks.config.password_required').and_return error_message validation_errors.first.should == error_message end end # context "the public key path" do # it "should have errors if the key doesn't exist" do # subject.public_key_path = 'missing' # I18n.should_receive(:t).with('vagrant_profitbricks.config.public_key_not_found').and_return error_message # validation_errors.first.should == error_message # end # it "should not have errors if the key exists with an absolute path" do # subject.public_key_path = File.expand_path 'locales/en.yml', Dir.pwd # validation_errors.should be_empty # end # it "should not have errors if the key exists with a relative path" do # machine.stub_chain(:env, :root_path).and_return '.' # subject.public_key_path = 'locales/en.yml' # validation_errors.should be_empty # end # end context "the username" do it "should error if not given" do subject.username = nil I18n.should_receive(:t).with('vagrant_profitbricks.config.username_required').and_return error_message validation_errors.first.should == error_message end end [:profitbricks_url].each do |url| context "the #{url}" do it "should not validate if the URL is invalid" do subject.send "#{url}=", 'baz' I18n.should_receive(:t).with('vagrant_profitbricks.config.invalid_uri', {:key => url, :uri => 'baz'}).and_return error_message validation_errors.first.should == error_message end end end end describe "network" do it "should remove SERVICE_NET_ID if :service_net is detached" do subject.send(:network, :service_net, :attached => false) subject.send(:networks).should_not include(VagrantPlugins::ProfitBricks::Config::SERVICE_NET_ID) end it "should not allow duplicate networks" do net_id = "deadbeef-0000-0000-0000-000000000000" subject.send(:network, net_id) subject.send(:network, net_id) subject.send(:networks).count(net_id).should == 1 end end end