require 'spec_helper' describe Stackster do describe "with a single running instance" do before do @instances = [ { 'instancesSet' => [ { 'tagSet' => { 'aws:cloudformation:stack-name' => 'my-stack' }, 'instanceState' => { 'name' => 'running' } } ] }, { 'instancesSet' => [ { 'tagSet' => { 'aws:cloudformation:stack-name' => 'my-stack' }, 'instanceState' => { 'name' => 'terminated' } } ] }, { 'instancesSet' => [ { 'tagSet' => { 'aws:cloudformation:stack-name' => 'dead-stack' }, 'instanceState' => { 'name' => 'terminated' } } ] } ] end it "should list running instance within the given stack" do @ec2_mock = mock 'simple ec2' config = Stackster::Config.new(:config => { 'access_key' => 'the-key', 'secret_key' => 'secret', 'region' => 'us-west-1' }) Stackster::AWS::EC2.should_receive(:new). with(:config => config). and_return @ec2_mock instance_reader = Stackster::InstanceReader.new :config => config @ec2_mock.should_receive(:describe_instances). and_return(@instances) instance_reader.list_stack_instances('my-stack').count. should == 1 end it "should not include instances which are in different stacks" do @ec2_mock = mock 'simple ec2' config = Stackster::Config.new(:config => { 'access_key' => 'the-key', 'secret_key' => 'secret', 'region' => 'us-west-1' }) Stackster::AWS::EC2.should_receive(:new). with(:config => config). and_return @ec2_mock instance_reader = Stackster::InstanceReader.new :config => config @ec2_mock.should_receive(:describe_instances). and_return(@instances) instance_reader.list_stack_instances('another-stack').count. should == 0 end it "should not include instances which are not running" do @ec2_mock = mock 'simple ec2' config = Stackster::Config.new(:config => { 'access_key' => 'the-key', 'secret_key' => 'secret', 'region' => 'us-west-1' }) Stackster::AWS::EC2.should_receive(:new). with(:config => config). and_return @ec2_mock instance_reader = Stackster::InstanceReader.new :config => config @ec2_mock.should_receive(:describe_instances). and_return(@instances) instance_reader.list_stack_instances('dead-stack').count. should == 0 end end end