Code Monkey home page Code Monkey logo

Comments (5)

kief avatar kief commented on June 18, 2024 6

OK, I've create a new issue #201 and pull request #202.

Respectfully, I disagree on wontfix for this issue. I agree, as you said in your comment on #166, that the spec must return a single instance. In my code example:

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

... clearly, if I have multiple instances with Name = 'My Server', this spec doesn't make any sense. But it also doesn't make sense for this to fail when there is a terminated instance with that Name. This issue breaks my workflow. And there are also many people like @ajlanghorn who don't make the assumption that the Name label is unique, even for running instances. It would be good if awspec could be used in these cases.

I'm not sure what the best solution would be - I'm not strong with rspec, and am new to the awspec codebase. So I'm hoping to learn something by exploring this issue.

  • In some cases, we would want the type to only return a running instances with the Name we've asked for. So we might want the ec2_finder to filter out terminated instances.

  • But in other cases, we might want to examine the instance even if it's terminated. Maybe we want to prove that an instance has been terminated.

  • In other cases, we might have different expectations about the state of the instance. Do we want to include instances that are still initializing? Ones that have failed to be created?

So what's the nice, rspec-like solution? Is it having multiple types? ec2, running_ec2, etc.? Or is it possible to pass options to the type, that can be used to filter what is selected, without looking ugly?

Should there be a type (or more than one type) that returns a list, like ec2_instances? This would help people who don't assume Name is unique, and also those who might want to filter through the returned instances in different states.

from awspec.

kief avatar kief commented on June 18, 2024 1

I'm having a similar issue. The solution of testing for should be_terminated doesn't help, because I'm not trying to test whether it's terminated.

For example, I have a spec like this:

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

My workflow is like this:

  1. Edit and apply terraform template
  2. Run tests
  3. Make a change to terraform that causes the server to be destroyed and a new instance to be created
  4. Run tests

Assuming my server was successfully recreated, I expect my tests to pass. Instead, I get:

  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)>'

This is because the terminated instance with the same name still exists for a little while.

The issue seems to be in helper/finder/ec2.rb:31:

  def find_ec2_status(id)
    res = ec2_client.describe_instance_status({
                                                instance_ids: [id]
                                              })
    res.instance_statuses.first if res.instance_statuses.count == 1
  end

If res.instance_status.count is not 1 (as it will be when you rebuild a server with a given Name), then this returns nil.

At the very least, it should throw Awspec::DuplicatedResourceTypeError. Better yet would be to return the running instance. But I can see this complicates things, as find_ec2 may be called in other situations where this behaviour isn't expected.

I'm happy to make a change and submit a pull request. But I'm not sure what the best solution is for the overall library.

from awspec.

tom-butler avatar tom-butler commented on June 18, 2024 1

For anyone else who needs a workaround to the terminated ec2 instance name issue, I created a little script to find the id of a running ec2 with the specified name.

ec2_helper.rb

So instead of:

require 'awspec'

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

You can do:

require 'awspec'
require 'ec2_helper'

describe ec2(EC2Helper.GetIdFromName('nametag')) do
  it { should exist }
end

make sure to add gem 'aws-sdk' to your Gemfile

from awspec.

k1LoW avatar k1LoW commented on June 18, 2024

Thank you for your comment.

awspec can test terminated instance.

describe ec2('my-ec2') do
  it { should be_terminated }
end

And awspec need "unique" instance name (if same name instances exist, it should use instance-id).

If you want to test same name tag, you can use Aws::EC2::Client#wait_until ( see http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#wait_until-instance_method )

Best regards

from awspec.

k1LoW avatar k1LoW commented on June 18, 2024

Hi @kief

At the very least, it should throw Awspec::DuplicatedResourceTypeError.

That's exactly right.


I think A and B are different issues.

A. Terminated and running instances having the same name will cause tests to fail => wontfix
B. find_ec2_status should throw Awspec::DuplicatedResourceTypeError. => pull request wanted

from awspec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.