require 'spec_helper' describe Mulder::Client do describe '.initialize' do let(:mocked_connection) { mock } it 'stores the app' do described_class.new(mocked_connection, 'foo', 'bar', 'worker').app.should == 'foo' end it 'stores the environment' do described_class.new(mocked_connection, 'foo', 'bar', 'worker').environment.should == 'bar' end end describe '#group' do it 'finds the correct group based on the given attributes' do mocked_connection = mock mocked_connection.expects(:group_by_id_regexp).with(/^foo-bar-(.*-)?WorkerGroup-.*$/i) client = described_class.new(mocked_connection, 'foo', 'bar', 'WorkerGroup') client.group end end describe '#instances' do let(:mocked_connection) { mock } let(:mocked_group) { mock } let(:client) { described_class.new(mocked_connection, 'foo', 'bar', 'worker') } let(:instances) { [] } before do mocked_connection.stubs(:instances_by_group).with(mocked_group).returns(instances) client.stubs(:group).returns(mocked_group) end context 'when there are no unhealthy instances' do let(:instances) {[mock('healthy instance', exists?: true), mock('healthy instance', exists?: true)]} it 'returns all instances' do client.instances.size.should == 2 end end context 'when there are unhealthy instances' do let(:instances) { [mock('healthy instance', exists?: true), mock('unhealthy instance', exists?: false)] } it 'returns only the healthy ones' do client.instances.size.should == 1 end end it 'finds the instances for the group' do mocked_connection = mock mocked_group = mock mocked_connection.expects(:instances_by_group).with(mocked_group).returns([]) client = described_class.new(mocked_connection, 'foo', 'bar', 'worker') client.expects(:group).returns(mocked_group) client.instances end end describe '#id_regexp' do it 'matches without an autogenerated id' do client = described_class.new(mock, 'foo', 'bar', 'worker') 'foo-bar-worker-3'.should match(client.id_regexp) end it 'matches with an autogenerated id' do client = described_class.new(mock, 'foo', 'bar', 'worker') 'foo-bar-abc123-worker-3'.should match(client.id_regexp) end context 'with prefixed id' do let(:client) { described_class.new(mock, 'widget-foo', 'bar', 'worker') } it 'matches the prefixed group' do 'widget-foo-bar-abd123-worker-8'.should match(client.id_regexp) end it 'does not match the non-prefixed group' do 'foo-bar-derp456-worker-99'.should_not match(client.id_regexp) end end context 'with non-prefixed id' do let(:client) { described_class.new(mock, 'foo', 'bar', 'worker') } it 'does not match the prefixed group' do 'widget-foo-bar-abd123-worker-8'.should_not match(client.id_regexp) end it 'matches the non-prefixed group' do 'foo-bar-derp456-worker-99'.should match(client.id_regexp) end end end end