diff --git a/app/models/service_sorter.rb b/app/models/service_sorter.rb index a6ad71d..2967d8f 100644 --- a/app/models/service_sorter.rb +++ b/app/models/service_sorter.rb @@ -71,10 +71,13 @@ def set_link_port_and_protocol(service, dependency) # Finds the explicitly exposed ports (:ports and :expose) on the dependency and # creates a hash of the port and protocol for each def ports_and_protocols_for(service) - return [] unless service.has_key?(:ports) + ports = [] - ports = service[:ports].map do |exposed_port| - { port: exposed_port[:hostPort], protocol: (exposed_port[:protocol] || 'tcp') } + if service[:ports] + mapped_ports = service[:ports].map do |exposed_port| + { port: exposed_port[:hostPort], protocol: (exposed_port[:protocol] || 'tcp') } + end + ports.push(mapped_ports).flatten! end if service[:expose] diff --git a/spec/models/service_sorter_spec.rb b/spec/models/service_sorter_spec.rb index 0131574..23fcc0f 100644 --- a/spec/models/service_sorter_spec.rb +++ b/spec/models/service_sorter_spec.rb @@ -33,6 +33,7 @@ it 'uses the lowest numbered exposed port as the link port' do expect(subject.last[:links].first[:port]).to eq 80 end + it "uses 'tcp' as the link protocol" do expect(subject.last[:links].first[:protocol]).to eq 'tcp' end @@ -92,4 +93,28 @@ expect(described_class.send(:get_service_names_for, links)).to contain_exactly("FOO", "BAZ") end end + + describe '.ports_and_protocols_for' do + it 'responds with mapped ports when there are only mapped ports' do + service = { ports: [{ hostPort: 80, protocol: 'tcp' }] } + expect(described_class.send(:ports_and_protocols_for, service)).to match_array([{ port: 80, protocol: 'tcp' }]) + end + + + it 'responds with exposed ports when there are only exposed ports' do + service = { expose: [80] } + expect(described_class.send(:ports_and_protocols_for, service)).to match_array([{ port: 80, protocol: 'tcp' }]) + end + + it 'combines mapped and exposed ports if there are both' do + service = { expose: [80], ports: [{ hostPort: 8080, protocol: 'tcp' }] } + expect(described_class.send(:ports_and_protocols_for, service)).to match_array([{ port: 80, protocol: 'tcp' }, + { port: 8080, protocol: 'tcp' }]) + end + + it 'returns an empty array if there no mapped or exposed ports' do + service = { expose: [], ports: [] } + expect(described_class.send(:ports_and_protocols_for, service)).to match_array([]) + end + end end