Skip to content

Commit

Permalink
Allow OrchestraionTemplate subclass to customize md5 calculation
Browse files Browse the repository at this point in the history
Currently, md5 checksum is forcibly calculated on entire orchestration
template text content:

```
OrchestrationTemplate.calc_md5(text)
```

But this is not always feasible. VMware vCloud, for example, yields
randomly ordered elements in XML even when describing the very same orchestration
template, hence the MD5 checksum differs. Therefore we need to be able to
customize the way the MD5 checksum is calculated.

With this commit we make sure that calc_md5 function from the subclass
is used:

```
ManageIQ::Providers::Vmware::CloudManager::OrchestrationTemplate.calc_md5(text)
```

Only this way, we're able to customize what part of the XML will actually
be used to calculate MD5 checksum.

Signed-off-by: Miha Pleško <miha.plesko@xlab.si>
  • Loading branch information
miha-plesko committed Mar 9, 2018
1 parent dd90df8 commit b448e6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/models/orchestration_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def self.find_or_create_by_contents(hashes)
create!(hash.except(:md5)) # always create a new template if it is a draft
true
else
md5s << calc_md5(with_universal_newline(hash[:content]))
klass = hash[:type].present? ? hash[:type].constantize : self
md5s << klass.calc_md5(with_universal_newline(hash[:content]))
false
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/models/orchestration_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
expect(@existing_record).not_to eq(OrchestrationTemplate.find_or_create_by_contents(@query_hash)[0])
expect(OrchestrationTemplate.count).to eq(2)
end

it "uses subclass if type is present" do
expect(ManageIQ::Providers::Vmware::CloudManager::OrchestrationTemplate).to receive(:calc_md5).at_least(:once)
expect(described_class).not_to receive(:calc_md5)

@query_hash[:draft] = false
@query_hash[:type] = ManageIQ::Providers::Vmware::CloudManager::OrchestrationTemplate.name
OrchestrationTemplate.find_or_create_by_contents(@query_hash)
end

it "uses parent if type is not present" do
expect(ManageIQ::Providers::Vmware::CloudManager::OrchestrationTemplate).not_to receive(:calc_md5)
expect(described_class).to receive(:calc_md5).at_least(:once)

@query_hash[:draft] = false
@query_hash[:type] = nil
OrchestrationTemplate.find_or_create_by_contents(@query_hash)
end
end
end

Expand Down

0 comments on commit b448e6c

Please sign in to comment.