test/ec2/test_ec2.rb in aws-2.3.21 vs test/ec2/test_ec2.rb in aws-2.3.22

- old
+ new

@@ -56,12 +56,11 @@ assert @ec2.revoke_security_group_named_ingress(@group, TestCredentials.account_number, 'default') end def test_06_describe_images - images = @ec2.describe_images - assert images.size>0, 'Amazon must have at least some public images' + images = describe_images # unknown image assert_raise(Aws::AwsError) { @ec2.describe_images(['ami-ABCDEFGH']) } end def test_07_describe_instanses @@ -117,8 +116,90 @@ assert_equal 'a', ec2.params[:protocol] assert_equal 'b.c', ec2.params[:server] assert_equal '/d/', ec2.params[:service] assert_equal 0, ec2.params[:port] assert_nil ec2.params[:region] + end + + def test_13a_create_describe_delete_tag + images = describe_images + resource_id = images.first[:aws_id] + + assert @ec2.create_tag(resource_id, 'testkey', 'testvalue').inspect, "Could not add a tag to #{resource_id}" + assert_equal( + [{:aws_resource_id=>resource_id, :aws_resource_type=>"image", :aws_key=>"testkey", :aws_value=>"testvalue"}], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id) + ) + assert_equal( + [], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => '__blah__') + ) + + assert @ec2.delete_tag(resource_id, 'testkey').inspect, "Could not delete tag 'testkey' from #{resource_id}" + sleep 1 # :( + assert_equal( + [], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id) + ) + end + + def test_13b_create_describe_delete_tag_by_value + images = describe_images + resource_id = images.first[:aws_id] + + assert @ec2.create_tag(resource_id, 'testkey', 'testvalue').inspect, "Could not add a tag to #{resource_id}" + assert_equal( + [{:aws_resource_id=>resource_id, :aws_resource_type=>"image", :aws_key=>"testkey", :aws_value=>"testvalue"}], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id, 'Filter.2.Name' => 'key', 'Filter.2.Value.1' => 'testkey') + ) + assert_equal( + [], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id, 'Filter.2.Name' => 'key', 'Filter.2.Value.1' => '__blah__') + ) + + assert @ec2.delete_tag(resource_id, 'testkey', 'testvalue').inspect, "Could not delete tag 'testkey' from #{resource_id}" + sleep 1 # :( + assert_equal( + [], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id, 'Filter.2.Name' => 'key', 'Filter.2.Value.1' => 'testkey') + ) + end + + def test_13c_delete_tag_with_empty_or_nil_value + images = describe_images + resource_id = images.first[:aws_id] + + assert @ec2.create_tag(resource_id, 'testkey', 'testvalue').inspect, "Could not add a tag to #{resource_id}" + assert_equal( + [{:aws_resource_id=>resource_id, :aws_resource_type=>"image", :aws_key=>"testkey", :aws_value=>"testvalue"}], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id) + ) + + # Delete a tag with an empty string value... + assert @ec2.delete_tag(resource_id, 'testkey', '').inspect, "Could not delete tag 'testkey' from #{resource_id}" + sleep 1 # :( + # ... does nothing + assert_equal( + [{:aws_resource_id=>resource_id, :aws_resource_type=>"image", :aws_key=>"testkey", :aws_value=>"testvalue"}], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id) + ) + + # Delete a tag with value = nil... + assert @ec2.delete_tag(resource_id, 'testkey', nil).inspect, "Could not delete tag 'testkey' from #{resource_id}" + sleep 1 # :( + # ... deletes all tags with the given key + assert_equal( + [], + @ec2.describe_tags('Filter.1.Name' => 'resource-id', 'Filter.1.Value.1' => resource_id) + ) + end + + private + + # Memoize the images to speed up the tests + def describe_images + @@images ||= @ec2.describe_images + assert @@images.size>0, 'Amazon must have at least some public images' + @@images end end