k1LoW / awspec

RSpec tests for your AWS resources.
MIT License
1.17k stars 193 forks source link

find_ec2_status should throw Awspec::DuplicatedResourceTypeError #201

Open kief opened 7 years ago

kief commented 7 years ago

If there are two server instances with the same Name tag (even if one is terminated), the following code fails with a stack trace:

describe ec2('My Server') do
  it { should be_running }
end

The trace is:

1) private_instance ec2 'My Server' should be running
     On host `10.0.2.225'
     Failure/Error: it { should be_running }
     NoMethodError:
       undefined method `state' for nil:NilClass

     # /var/lib/gems/2.3.0/gems/awspec-0.61.0/lib/awspec/type/ec2.rb:29:in `block (2 levels) in <class:Ec2>'
     # /var/lib/gems/2.3.0/gems/serverspec-2.37.2/lib/serverspec/matcher/be_running.rb:7:in `block (2 levels) in <top (required)>'
     # ./spec/gocd_server/private_instance_spec.rb:9:in `block (3 levels) in <top (required)>'

Instead, Awspec::DuplicatedResourceTypeErro should be raised.

kief commented 7 years ago

I've submitted pull request #202.

As I mentioned in the pull request, it's not a complete solution - if the id isn't found (as an instance id or Name tag), then it returns a nil. This results in the same stacktrace.

If you have a preference for what to do instead, I can amend it. Ideally, the spec that calls it should simply fail. So this code:

describe ec2('My Server') do
    it { should exist }
end

The should exist check should simply return false. I'm not familiar enough with the codebase (or rspec code) to work out the best solution. Again, if you have pointers I can work on it some more.

k1LoW commented 7 years ago

Hi @kief . I re-check find_ec2_status code.

So I think find_ec2_status return only 1 response or not. (see https://github.com/k1LoW/awspec/blob/master/lib/awspec/helper/finder/ec2.rb#L31) Because find_ec2_status use instance id ( not name )

Best regards.