Skip to content

Commit

Permalink
Merge pull request ManageIQ#16453 from cben/mapping_with_category_fac…
Browse files Browse the repository at this point in the history
…tory

Improve tag mapping tests
  • Loading branch information
agrare authored and cben committed Apr 14, 2018
1 parent 39ced9c commit 8a01f27
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
17 changes: 17 additions & 0 deletions spec/factories/container_label_tag_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@
labeled_resource_type 'ContainerNode'
end
end

# Mapping for <All> entities, as created in UI.
factory :tag_mapping_with_category, :parent => :container_label_tag_mapping do
transient do
category_name { "kubernetes::" + Classification.sanitize_name(label_name.tr("/", ":")) }
category_description { "Mapped #{label_name}" }
end

tag do
category = FactoryGirl.create(:classification,
:name => category_name,
:description => category_description,
:single_value => true,
:read_only => true)
category.tag
end
end
end
51 changes: 43 additions & 8 deletions spec/models/ems_refresh/save_inventory/save_tags_inventory_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
context "save_tags_inventory" do
# @return [Tag] a tag in a category linked to a mapping.
def mapped_tag(category_name, tag_name)
mapping = FactoryGirl.create(:tag_mapping_with_category,
:category_name => category_name,
:category_description => category_name)
category = mapping.tag.category
entry = category.add_entry(:name => tag_name, :description => tag_name)
category.tag
end

before(:each) do
@zone = FactoryGirl.create(:zone)
@ems = FactoryGirl.create(:ems_amazon, :zone => @zone)

@vm = FactoryGirl.create(:vm, :ext_management_system => @ems)
@node = FactoryGirl.create(:container_node, :ext_management_system => @ems)

# These might not pass the ContainerLabelTagMapping.controls_tag? criteria, doesn't matter if only adding.
@tag1 = FactoryGirl.create(:tag, :name => '/managed/amazon:vm:owner/alice')
@tag2 = FactoryGirl.create(:tag, :name => '/managed/kubernetes:container_node:stuff/jabberwocky')
@tag3 = FactoryGirl.create(:tag, :name => '/managed/kubernetes::foo/bar') # All
@tag1 = mapped_tag('amazon:vm:owner', 'alice')
@tag2 = mapped_tag('kubernetes:container_node:stuff', 'jabberwocky')
@tag3 = mapped_tag('kubernetes::foo', 'bar') # All entities
end

# This is what ContainerLabelTagMapping::Mapper.map_labels(cache, 'Type', labels) would
Expand All @@ -24,16 +33,42 @@
]
}
end
let(:data2) do
{
:tags => [
instance_double(ManagerRefresh::InventoryObject, :id => @tag2.id),
]
}
end
let(:data_empty) do
{
:tags => []
}
end
let(:data_empty_array) do
[]
end

# Note that in these tests we're explicitly passing the object, so that's
# why the object type may not match what you would expect from the tag
# name type above.

it "creates the expected number of taggings" do
it "creates/deletes the expected number of taggings" do
EmsRefresh.save_tags_inventory(@vm, data)
taggings = Tagging.all
expect(taggings.size).to eql(3)
expect(@vm.tags.size).to eql(3)
expect(Tagging.count).to eql(3)
expect(@vm.reload.tags.size).to eql(3)

EmsRefresh.save_tags_inventory(@vm, data_empty)
expect(Tagging.count).to eql(0)
expect(@vm.reload.tags.size).to eql(0)

EmsRefresh.save_tags_inventory(@vm, data2)
expect(Tagging.count).to eql(1)
expect(@vm.reload.tags.size).to eql(1)

EmsRefresh.save_tags_inventory(@vm, data_empty_array)
expect(Tagging.count).to eql(0)
expect(@vm.reload.tags.size).to eql(0)
end

it "creates the expected taggings for a VM" do
Expand Down

0 comments on commit 8a01f27

Please sign in to comment.