diff --git a/spec/integration/dsl_spec.rb b/spec/integration/dsl_spec.rb index e4014fd4..a43b4bbc 100644 --- a/spec/integration/dsl_spec.rb +++ b/spec/integration/dsl_spec.rb @@ -241,25 +241,45 @@ describe Capistrano::DSL do end describe 'fetching all servers' do - subject { dsl.roles(:all).map { |server| "#{server.user}@#{server.hostname}:#{server.port}" } } - - it 'creates a server instance for each unique user@host:port combination' do - expect(subject).to eq %w{db@example1.com:1234 root@example1.com:1234 @example1.com:5678 deployer@example1.com:1234} + it 'creates one server per hostname, ignoring user and port combinations' do + expect(dsl.roles(:all).size).to eq(1) end end describe 'fetching servers for a role' do it 'roles defined using the `server` syntax are included' do - expect(dsl.roles(:web).size).to eq(2) + as = dsl.roles(:web).map { |server| "#{server.user}@#{server.hostname}:#{server.port}" } + expect(as.size).to eq(1) + expect(as[0]).to eq("deployer@example1.com:5678") end it 'roles defined using the `role` syntax are included' do - expect(dsl.roles(:app).size).to eq(2) + as = dsl.roles(:app).map { |server| "#{server.user}@#{server.hostname}:#{server.port}" } + expect(as.size).to eq(1) + expect(as[0]).to eq("deployer@example1.com:5678") end end end + describe 'when setting user and port' do + subject { dsl.roles(:all).map { |server| "#{server.user}@#{server.hostname}:#{server.port}" }.first } + + describe "using the :user property" do + it "takes precedence over in the host string" do + dsl.server 'db@example1.com:1234', roles: %w{db}, active: true, user: 'brian' + expect(subject).to eq("brian@example1.com:1234") + end + end + + describe "using the :port property" do + it "takes precedence over in the host string" do + dsl.server 'db@example1.com:9090', roles: %w{db}, active: true, port: 1234 + expect(subject).to eq("db@example1.com:1234") + end + end + end + end describe 'setting and fetching variables' do diff --git a/spec/lib/capistrano/configuration/server_spec.rb b/spec/lib/capistrano/configuration/server_spec.rb index 1028dc09..ad81f3de 100644 --- a/spec/lib/capistrano/configuration/server_spec.rb +++ b/spec/lib/capistrano/configuration/server_spec.rb @@ -33,7 +33,7 @@ module Capistrano end describe 'comparing identity' do - subject { server.matches? Server[hostname] } + subject { server.hostname == Server[hostname].hostname } context 'with the same user, hostname and port' do let(:hostname) { 'root@hostname:1234' } @@ -42,12 +42,12 @@ module Capistrano context 'with a different user' do let(:hostname) { 'deployer@hostname:1234' } - it { expect(subject).to be_falsey } + it { expect(subject).to be_truthy } end context 'with a different port' do let(:hostname) { 'root@hostname:5678' } - it { expect(subject).to be_falsey } + it { expect(subject).to be_truthy } end context 'with a different hostname' do @@ -94,6 +94,10 @@ module Capistrano it 'sets the user' do expect(server.user).to eq 'tomc' end + + it 'sets the netssh_options user' do + expect(server.netssh_options[:user]).to eq 'tomc' + end end context 'properties contains port' do @@ -285,6 +289,9 @@ module Capistrano it 'contains correct user' do expect(server.netssh_options[:user]).to eq 'another_user' end + it 'does not affect server user in host' do + expect(server.user).to eq 'user_name' + end it 'contains keys' do expect(server.netssh_options[:keys]).to eq %w(/home/another_user/.ssh/id_rsa) end diff --git a/spec/lib/capistrano/configuration/servers_spec.rb b/spec/lib/capistrano/configuration/servers_spec.rb index efacd189..6582524f 100644 --- a/spec/lib/capistrano/configuration/servers_spec.rb +++ b/spec/lib/capistrano/configuration/servers_spec.rb @@ -18,6 +18,12 @@ module Capistrano expect(servers.count).to eq 1 end + it 'handles de-duplification within roles with users' do + servers.add_role(:app, %w{1}, user: 'nick') + servers.add_role(:app, %w{1}, user: 'fred') + expect(servers.count).to eq 1 + end + it 'accepts instances of server objects' do servers.add_role(:app, [Capistrano::Configuration::Server.new('example.net'), 'example.com']) expect(servers.roles_for([:app]).length).to eq 2 @@ -134,7 +140,23 @@ module Capistrano servers.add_host('1', roles: [:app, 'web'], test: :value, user: 'root', port: 34) servers.add_host('1', roles: [:app, 'web'], test: :value, user: 'deployer', port: 34) servers.add_host('1', roles: [:app, 'web'], test: :value, user: 'deployer', port: 56) - expect(servers.count).to eq(8) + expect(servers.count).to eq(1) + end + + describe "with a :user property" do + + it 'sets the server ssh username' do + servers.add_host('1', roles: [:app, 'web'], user: 'nick') + expect(servers.count).to eq(1) + expect(servers.roles_for([:all]).first.user).to eq 'nick' + end + + it 'overwrites the value of a user specified in the hostname' do + servers.add_host('brian@1', roles: [:app, 'web'], user: 'nick') + expect(servers.count).to eq(1) + expect(servers.roles_for([:all]).first.user).to eq 'nick' + end + end it 'overwrites the value of a previously defined scalar property' do