1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Compare servers by user, hostname and port

To check if servers match compare them by
user, hostname and port.
This commit is contained in:
Pavel Shpak 2014-02-02 23:05:33 +03:00
parent aeab6b6a1e
commit 0bee4f6fb8
4 changed files with 26 additions and 19 deletions

View file

@ -51,7 +51,7 @@ module Capistrano
end end
def matches?(other) def matches?(other)
hostname == other.hostname && port == other.port user == other.user && hostname == other.hostname && port == other.port
end end
private private

View file

@ -201,16 +201,18 @@ describe Capistrano::DSL do
describe 'when defining a host using a combination of the `server` and `role` syntax' do describe 'when defining a host using a combination of the `server` and `role` syntax' do
before do before do
dsl.server 'example1.com:1234', roles: %w{web}, active: true dsl.server 'db@example1.com:1234', roles: %w{db}, active: true
dsl.server 'root@example1.com:1234', roles: %w{web}, active: true
dsl.server 'example1.com:5678', roles: %w{web}, active: true dsl.server 'example1.com:5678', roles: %w{web}, active: true
dsl.role :app, %w{deployer@example1.com:1234}
dsl.role :app, %w{example1.com:5678} dsl.role :app, %w{example1.com:5678}
end end
describe 'fetching all servers' do describe 'fetching all servers' do
subject { dsl.roles(:all).map { |server| "#{server.hostname}:#{server.port}" } } subject { dsl.roles(:all).map { |server| "#{server.user}@#{server.hostname}:#{server.port}" } }
it 'creates a server instance for each unique host:port combination' do it 'creates a server instance for each unique user@host:port combination' do
expect(subject).to eq %w{example1.com:1234 example1.com:5678} expect(subject).to eq %w{db@example1.com:1234 root@example1.com:1234 @example1.com:5678 deployer@example1.com:1234}
end end
end end
@ -220,7 +222,7 @@ describe Capistrano::DSL do
end end
it 'roles defined using the `role` syntax are included' do it 'roles defined using the `role` syntax are included' do
expect(dsl.roles(:app)).to have(1).items expect(dsl.roles(:app)).to have(2).items
end end
end end

View file

@ -3,7 +3,7 @@ require 'spec_helper'
module Capistrano module Capistrano
class Configuration class Configuration
describe Server do describe Server do
let(:server) { Server.new('hostname:1234') } let(:server) { Server.new('root@hostname:1234') }
describe 'adding a role' do describe 'adding a role' do
subject { server.add_role(:test) } subject { server.add_role(:test) }
@ -35,23 +35,23 @@ module Capistrano
describe 'comparing identity' do describe 'comparing identity' do
subject { server.matches? Server[hostname] } subject { server.matches? Server[hostname] }
context 'with the same hostname' do context 'with the same user, hostname and port' do
let(:hostname) { 'hostname:1234' } let(:hostname) { 'root@hostname:1234' }
it { should be_true } it { should be_true }
end end
context 'with the same hostname and a user' do context 'with a different user' do
let(:hostname) { 'user@hostname:1234' } let(:hostname) { 'deployer@hostname:1234' }
it { should be_true } it { should be_false }
end end
context 'with the same hostname but different port' do context 'with a different port' do
let(:hostname) { 'hostname:5678' } let(:hostname) { 'root@hostname:5678' }
it { should be_false } it { should be_false }
end end
context 'with a different hostname' do context 'with a different hostname' do
let(:hostname) { 'otherserver' } let(:hostname) { 'root@otherserver:1234' }
it { should be_false } it { should be_false }
end end
end end

View file

@ -106,10 +106,15 @@ module Capistrano
expect(servers.roles_for([:all]).first.properties.test).to eq :value expect(servers.roles_for([:all]).first.properties.test).to eq :value
end end
it 'can accept multiple servers with the same hostname but different ports' do it 'can accept multiple servers with the same hostname but different ports or users' do
servers.add_host('2', roles: [:app, 'web'], test: :value, port: 12) servers.add_host('1', roles: [:app, 'web'], test: :value, port: 12)
servers.add_host('2', roles: [:app, 'web'], test: :value, port: 34) servers.add_host('1', roles: [:app, 'web'], test: :value, port: 34)
expect(servers.count { |server| server.hostname == '2' }).to eq 2 servers.add_host('1', roles: [:app, 'web'], test: :value, user: 'root')
servers.add_host('1', roles: [:app, 'web'], test: :value, user: 'deployer')
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)
servers.should have(8).items
end end
end end