Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify what is missing if tag assignment failed #20197

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ def ns

def self.classify(obj, category_name, entry_name, is_request = true)
cat = Classification.lookup_by_name(category_name, obj.region_id)
unless cat.nil?
ent = cat.find_entry_by_name(entry_name, obj.region_id)
ent.assign_entry_to(obj, is_request) unless ent.nil? || obj.is_tagged_with?(ent.to_tag, :ns => "none")
end
raise "Tag category '#{category_name}' not found in region #{obj.region_id}" if cat.nil?

ent = cat.find_entry_by_name(entry_name, obj.region_id)
raise "Tag name '#{entry_name}' not found in region #{obj.region_id}" if ent.nil?

raise "Object already tagged with ':ns' set to 'none'" if obj.is_tagged_with?(ent.to_tag, :ns => "none")

ent.assign_entry_to(obj, is_request)
end

def self.unclassify(obj, category_name, entry_name, is_request = true)
Expand Down
33 changes: 33 additions & 0 deletions spec/models/classification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@
FactoryBot.create(:classification_tag, :name => "multi_entry_2", :parent => parent)
end

describe ".classify" do
let(:entry) { "test_entry" }
let(:category) { "test_category" }
before { @vm = FactoryBot.create(:vm) }

it "raise error if tag category not found" do
category = "Hello, World"
expect { Classification.classify(@vm, category, entry) }.to raise_error(
RuntimeError, "Tag category '#{category}' not found in region #{@vm.region_id}"
)
end

it "raise error if tag entry not found" do
entry = "Hello, World"
expect { Classification.classify(@vm, category, entry) }.to raise_error(
RuntimeError, "Tag name '#{entry}' not found in region #{@vm.region_id}"
)
end

it "raise error if object already tagged with tag namespace set to 'none'" do
allow(@vm).to receive(:is_tagged_with?)
allow(@vm).to receive(:is_tagged_with?).with("/managed/test_category/test_entry", :ns => "none").and_return(true)
expect { Classification.classify(@vm, category, entry) }.to raise_error(
RuntimeError, "Object already tagged with ':ns' set to 'none'"
)
end

it "assign tag entry to object if tag category and tag name exist" do
Classification.classify(@vm, category, entry)
expect(@vm.tags.first.name).to eq("/managed/test_category/test_entry")
end
end

context "#destroy" do
it "a category deletes all entries" do
cat = Classification.lookup_by_name("test_category")
Expand Down