inspec/test/unit/resources/aws_ec2_instance_test.rb
Chris Redekop c8d4244ef4 Add has_roles to aws_ec2_instance (#90)
* Rename EC2-instance resources

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Add interim updates

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* testing for issue 82

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* completed integration for EC2 roles

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* adding in the beginning of the unit test for issue 82

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* Fix unit tests

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Add has_roles? examples

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Remove redundant gsub

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* corrected OpenStruct format

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* setting up variable for InstanceProfile

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* Updated the unit test so all variables are at the top

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* Fixed Rubocop issues that were detected

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* Updating README.md to include changes to aws_ec2

Signed-off-by: Simon Varlow <simon.varlow@d2l.com>

* Add failing IT for has_roles?

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Add negative IT and fix uncovered issue

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Fix Rubocop issue

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Fix integration test

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Fix Rubocop issues and unit tests

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>

* Pin AWS dependency to '~> 2'

Signed-off-by: Chris Redekop <chris.redekop@d2l.com>
2017-10-26 15:56:32 -04:00

118 lines
3.5 KiB
Ruby

require 'helper'
require 'aws_ec2_instance'
class TestEc2 < Minitest::Test
Id = 'instance-id'.freeze
InstanceProfile = 'instance-role'.freeze
Arn = 'arn:aws:iam::123456789012:instance-profile/instance-role'.freeze
def setup
@mock_conn = Minitest::Mock.new
@mock_client = Minitest::Mock.new
@mock_resource = Minitest::Mock.new
@mock_iam_resource = Minitest::Mock.new
@mock_conn.expect :ec2_client, @mock_client
@mock_conn.expect :ec2_resource, @mock_resource
@mock_conn.expect :iam_resource, @mock_iam_resource
end
def test_that_id_returns_id_directly_when_constructed_with_an_id
assert_equal Id, AwsEc2Instance.new(Id, @mock_conn).id
end
def test_that_id_returns_fetched_id_when_constructed_with_a_name
mock_instance = Minitest::Mock.new
mock_instance.expect :nil?, false
mock_instance.expect :id, Id
@mock_resource.expect :instances, [mock_instance], [Hash]
assert_equal Id, AwsEc2Instance.new({ name: 'cut' }, @mock_conn).id
end
def test_that_instance_returns_instance_when_instance_exists
mock_instance = Object.new
@mock_resource.expect :instance, mock_instance, [Id]
assert_same(
mock_instance,
AwsEc2Instance.new(Id, @mock_conn).send(:instance),
)
end
def test_that_instance_returns_nil_when_instance_does_not_exist
@mock_resource.expect :instance, nil, [Id]
assert AwsEc2Instance.new(Id, @mock_conn).send(:instance).nil?
end
def test_that_exists_returns_true_when_instance_exists
mock_instance = Minitest::Mock.new
mock_instance.expect :nil?, false
mock_instance.expect :exists?, true
@mock_resource.expect :instance, mock_instance, [Id]
assert AwsEc2Instance.new(Id, @mock_conn).exists?
end
def test_that_exists_returns_false_when_instance_does_not_exist
mock_instance = Minitest::Mock.new
mock_instance.expect :nil?, false
mock_instance.expect :exists?, false
@mock_resource.expect :instance, mock_instance, [Id]
assert !AwsEc2Instance.new(Id, @mock_conn).exists?
end
def stub_iam_instance_profile
OpenStruct.new({ arn: Arn })
end
def stub_instance_profile(roles)
OpenStruct.new({ roles: roles })
end
def test_that_has_roles_returns_false_when_roles_is_empty
mock_instance = Minitest::Mock.new
mock_instance.expect :iam_instance_profile, stub_iam_instance_profile
@mock_resource.expect :instance, mock_instance, [Id]
mock_roles = Minitest::Mock.new
mock_roles.expect :empty?, true
@mock_iam_resource.expect(
:instance_profile,
stub_instance_profile(mock_roles),
[InstanceProfile],
)
refute AwsEc2Instance.new(Id, @mock_conn).has_roles?
end
def test_that_has_roles_returns_true_when_roles_is_not_empty
mock_instance = Minitest::Mock.new
mock_instance.expect :iam_instance_profile, stub_iam_instance_profile
@mock_resource.expect :instance, mock_instance, [Id]
mock_roles = Minitest::Mock.new
mock_roles.expect :empty?, false
@mock_iam_resource.expect(
:instance_profile,
stub_instance_profile(mock_roles),
[InstanceProfile],
)
assert AwsEc2Instance.new(Id, @mock_conn).has_roles?
end
def test_that_has_roles_returns_false_when_roles_does_not_exist
mock_instance = Minitest::Mock.new
mock_instance.expect :iam_instance_profile, stub_iam_instance_profile
@mock_resource.expect :instance, mock_instance, [Id]
@mock_iam_resource.expect(
:instance_profile,
stub_instance_profile(nil),
[InstanceProfile],
)
refute AwsEc2Instance.new(Id, @mock_conn).has_roles?
end
end