awspecでEC2に割り当てられているIAM Roleをテストしたいときの話です。
ここを見ると、its(:iam_instance_profile)
というのがあります。例えばEC2-Roleというロールが割り当てられるていることをテストしたいとき、これを使って、
describe ec2('instance_name') do
its(:iam_instance_profile) { should match /EC2-Role$/ }
end
とやると、
1) ec2 'instance_name' iam_instance_profile should match /EC2-Role$/
Failure/Error: its(:iam_instance_profile) { should match /EC2-Role$/ }
expected #<struct Aws::EC2::Types::IamInstanceProfile arn="arn:aws:iam::xxxxxxxxxxx:instance-profile/EC2-Role", id="AIPAXXXXXXXXXXXXXX"> to match /EC2-Role$/
Diff:
@@ -1,2 +1,2 @@
-/EC2-Role$/
+"#<struct Aws::EC2::Types::IamInstanceProfile arn=\"arn:aws:iam::xxxxxxxxxxxxx:instance-profile/EC2-Role\", id=\"AIPAXXXXXXXXXXXXXX\">"
こんなエラーになり正しく動きません。
its(:iam_instance_profile)
はAws::EC2::Types::IamInstanceProfile
を返すみたいなので、そのまま文字列比較はできないようです。
正しい書き方は分かりませんが、ここを見ると、Aws::EC2::Instanceのリソースが使えるとのことなので、
describe ec2('instance_name') do
its('iam_instance_profile.arn') { should match /EC2-Role$/ }
end
と書けばテストできました。
ec2 'instance_name'
iam_instance_profile.arn
should match /EC2-Role$/