2017-01-27 18:38:04 +00:00
|
|
|
require 'helper'
|
|
|
|
|
|
|
|
class TestEc2 < Minitest::Test
|
2017-07-05 20:31:27 +00:00
|
|
|
Id = 'instance-id'.freeze
|
2017-10-26 19:56:32 +00:00
|
|
|
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
|
2017-10-11 20:18:20 +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
|
|
|
|
mock_instance.expect :id, Id
|
|
|
|
@mock_resource.expect :instances, [mock_instance], [Hash]
|
2017-10-11 20:18:20 +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
|
|
|
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_resource.expect :instance, mock_instance, [Id]
|
2017-10-26 19:56:32 +00:00
|
|
|
assert_same(
|
|
|
|
mock_instance,
|
|
|
|
AwsEc2Instance.new(Id, @mock_conn).send(:instance),
|
|
|
|
)
|
2017-02-08 18:46:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_that_instance_returns_nil_when_instance_does_not_exist
|
2017-07-05 20:31:27 +00:00
|
|
|
@mock_resource.expect :instance, nil, [Id]
|
2017-10-11 20:18:20 +00:00
|
|
|
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
|
|
|
|
@mock_resource.expect :instance, mock_instance, [Id]
|
2017-10-11 20:18:20 +00:00
|
|
|
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
|
|
|
|
@mock_resource.expect :instance, mock_instance, [Id]
|
2017-10-11 20:18:20 +00:00
|
|
|
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
|
|
|
|
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
|
2017-01-27 18:38:04 +00:00
|
|
|
end
|