2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "resources/aws/aws_ec2_instance"
|
2017-01-27 18:38:04 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
require "resource_support/aws"
|
|
|
|
require "resources/aws/aws_ec2_instance"
|
2019-05-21 00:19:38 +00:00
|
|
|
|
2017-01-27 18:38:04 +00:00
|
|
|
class TestEc2 < Minitest::Test
|
2019-05-31 21:59:06 +00:00
|
|
|
ID = "instance-id".freeze
|
|
|
|
INSTANCEPROFILE = "instance-role".freeze
|
|
|
|
ARN = "arn:aws:iam::123456789012:instance-profile/instance-role".freeze
|
2017-02-01 22:31:18 +00:00
|
|
|
|
2017-01-27 18:38:04 +00:00
|
|
|
def setup
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_conn = Minitest::Mock.new
|
|
|
|
@mock_client = Minitest::Mock.new
|
|
|
|
@mock_resource = Minitest::Mock.new
|
2017-10-26 19:56:32 +00:00
|
|
|
@mock_iam_resource = Minitest::Mock.new
|
2017-01-27 18:38:04 +00:00
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_conn.expect :ec2_client, @mock_client
|
|
|
|
@mock_conn.expect :ec2_resource, @mock_resource
|
2017-10-26 19:56:32 +00:00
|
|
|
@mock_conn.expect :iam_resource, @mock_iam_resource
|
2017-01-27 18:38:04 +00:00
|
|
|
end
|
|
|
|
|
2017-02-08 18:46:52 +00:00
|
|
|
def test_that_id_returns_id_directly_when_constructed_with_an_id
|
2019-05-31 21:59:06 +00:00
|
|
|
assert_equal ID, AwsEc2Instance.new(ID, @mock_conn).id
|
2017-01-27 18:38:04 +00:00
|
|
|
end
|
|
|
|
|
2017-02-08 18:46:52 +00:00
|
|
|
def test_that_id_returns_fetched_id_when_constructed_with_a_name
|
2017-07-05 20:31:27 +00:00
|
|
|
mock_instance = Minitest::Mock.new
|
|
|
|
mock_instance.expect :nil?, false
|
2019-05-31 21:59:06 +00:00
|
|
|
mock_instance.expect :id, ID
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_resource.expect :instances, [mock_instance], [Hash]
|
2019-05-31 21:59:06 +00:00
|
|
|
assert_equal ID, AwsEc2Instance.new({ name: "cut" }, @mock_conn).id
|
2017-02-08 18:46:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_that_instance_returns_instance_when_instance_exists
|
2017-07-05 20:31:27 +00:00
|
|
|
mock_instance = Object.new
|
2017-02-08 18:46:52 +00:00
|
|
|
|
2019-05-31 21:59:06 +00:00
|
|
|
@mock_resource.expect :instance, mock_instance, [ID]
|
2017-10-26 19:56:32 +00:00
|
|
|
assert_same(
|
|
|
|
mock_instance,
|
2019-05-31 21:59:06 +00:00
|
|
|
AwsEc2Instance.new(ID, @mock_conn).send(:instance)
|
2017-10-26 19:56:32 +00:00
|
|
|
)
|
2017-02-08 18:46:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_that_instance_returns_nil_when_instance_does_not_exist
|
2019-05-31 21:59:06 +00:00
|
|
|
@mock_resource.expect :instance, nil, [ID]
|
|
|
|
assert AwsEc2Instance.new(ID, @mock_conn).send(:instance).nil?
|
2017-02-01 22:31:18 +00:00
|
|
|
end
|
2017-02-08 18:46:52 +00:00
|
|
|
|
|
|
|
def test_that_exists_returns_true_when_instance_exists
|
2017-07-05 20:31:27 +00:00
|
|
|
mock_instance = Minitest::Mock.new
|
2017-10-11 20:18:20 +00:00
|
|
|
mock_instance.expect :nil?, false
|
2017-07-05 20:31:27 +00:00
|
|
|
mock_instance.expect :exists?, true
|
2019-05-31 21:59:06 +00:00
|
|
|
@mock_resource.expect :instance, mock_instance, [ID]
|
|
|
|
assert AwsEc2Instance.new(ID, @mock_conn).exists?
|
2017-02-08 18:46:52 +00:00
|
|
|
end
|
|
|
|
|
2017-02-16 19:33:21 +00:00
|
|
|
def test_that_exists_returns_false_when_instance_does_not_exist
|
2017-07-05 20:31:27 +00:00
|
|
|
mock_instance = Minitest::Mock.new
|
2017-10-11 20:18:20 +00:00
|
|
|
mock_instance.expect :nil?, false
|
2017-07-05 20:31:27 +00:00
|
|
|
mock_instance.expect :exists?, false
|
2019-05-31 21:59:06 +00:00
|
|
|
@mock_resource.expect :instance, mock_instance, [ID]
|
|
|
|
assert !AwsEc2Instance.new(ID, @mock_conn).exists?
|
2017-02-16 19:33:21 +00:00
|
|
|
end
|
2017-10-26 19:56:32 +00:00
|
|
|
|
|
|
|
def stub_iam_instance_profile
|
2019-05-31 21:59:06 +00:00
|
|
|
OpenStruct.new({ arn: ARN })
|
2017-10-26 19:56:32 +00:00
|
|
|
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
|
2019-05-31 21:59:06 +00:00
|
|
|
@mock_resource.expect :instance, mock_instance, [ID]
|
2017-10-26 19:56:32 +00:00
|
|
|
|
|
|
|
mock_roles = Minitest::Mock.new
|
|
|
|
mock_roles.expect :empty?, true
|
|
|
|
|
|
|
|
@mock_iam_resource.expect(
|
|
|
|
:instance_profile,
|
|
|
|
stub_instance_profile(mock_roles),
|
2019-05-31 21:59:06 +00:00
|
|
|
[INSTANCEPROFILE]
|
2017-10-26 19:56:32 +00:00
|
|
|
)
|
|
|
|
|
2019-05-31 21:59:06 +00:00
|
|
|
refute AwsEc2Instance.new(ID, @mock_conn).has_roles?
|
2017-10-26 19:56:32 +00:00
|
|
|
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
|
2019-05-31 21:59:06 +00:00
|
|
|
@mock_resource.expect :instance, mock_instance, [ID]
|
2017-10-26 19:56:32 +00:00
|
|
|
|
|
|
|
mock_roles = Minitest::Mock.new
|
|
|
|
mock_roles.expect :empty?, false
|
|
|
|
|
|
|
|
@mock_iam_resource.expect(
|
|
|
|
:instance_profile,
|
|
|
|
stub_instance_profile(mock_roles),
|
2019-05-31 21:59:06 +00:00
|
|
|
[INSTANCEPROFILE]
|
2017-10-26 19:56:32 +00:00
|
|
|
)
|
|
|
|
|
2019-05-31 21:59:06 +00:00
|
|
|
assert AwsEc2Instance.new(ID, @mock_conn).has_roles?
|
2017-10-26 19:56:32 +00:00
|
|
|
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
|
2019-05-31 21:59:06 +00:00
|
|
|
@mock_resource.expect :instance, mock_instance, [ID]
|
2017-10-26 19:56:32 +00:00
|
|
|
|
|
|
|
@mock_iam_resource.expect(
|
|
|
|
:instance_profile,
|
|
|
|
stub_instance_profile(nil),
|
2019-05-31 21:59:06 +00:00
|
|
|
[INSTANCEPROFILE]
|
2017-10-26 19:56:32 +00:00
|
|
|
)
|
|
|
|
|
2019-05-31 21:59:06 +00:00
|
|
|
refute AwsEc2Instance.new(ID, @mock_conn).has_roles?
|
2017-10-26 19:56:32 +00:00
|
|
|
end
|
2017-01-27 18:38:04 +00:00
|
|
|
end
|