diff --git a/test/lib/unit/simple_ansible_content_unit_test.rb b/test/lib/unit/simple_ansible_content_unit_test.rb new file mode 100644 index 00000000..c72db5c8 --- /dev/null +++ b/test/lib/unit/simple_ansible_content_unit_test.rb @@ -0,0 +1,98 @@ +require 'foreman_ansible_director_test_helper' + +module ForemanAnsibleDirectorTests + module Lib + module Unit + class SimpleAnsibleContentUnitTest < ForemanAnsibleDirectorTestCase + test 'splits collection name into namespace and unit name' do + unit = simple_unit(unit_name: 'theforeman.operations') + + assert_equal 'theforeman', unit.unit_namespace + assert_equal 'operations', unit.unit_name + end + + test 'builds collection file with explicit versions' do + unit = simple_unit(unit_versions: %w[1.0.0 1.1.0]) + + collection_file = YAML.safe_load(unit.collection_file, permitted_classes: [Symbol]) + + assert_equal( + [ + { + 'name' => 'theforeman.operations', + 'version' => '1.0.0', + 'source' => 'https://galaxy.example.com', + 'type' => :collection, + }, + { + 'name' => 'theforeman.operations', + 'version' => '1.1.0', + 'source' => 'https://galaxy.example.com', + 'type' => :collection, + }, + ], + collection_file.fetch('collections') + ) + end + + test 'builds collection file without versions' do + unit = simple_unit(unit_versions: []) + + collection_file = YAML.safe_load(unit.collection_file, permitted_classes: [Symbol]) + + assert_equal( + [ + { + 'name' => 'theforeman.operations', + 'source' => 'https://galaxy.example.com', + }, + ], + collection_file.fetch('collections') + ) + end + + test 'builds role url for role units' do + unit = simple_unit(unit_type: :role, unit_name: 'theforeman.foreman') + + assert_equal( + 'https://galaxy.example.comapi/v1/roles/?namespace=theforeman&name=foreman', + unit.role_url + ) + end + + test 'to_hash serializes action input fields' do + unit = simple_unit(unit_versions: ['1.0.0']) + + assert_equal( + { + unit_type: :collection, + name: 'theforeman.operations', + versions: ['1.0.0'], + source: 'https://galaxy.example.com', + type: :collection, + src: nil, + scm: nil, + }, + unit.to_hash + ) + end + + private + + def simple_unit(**overrides) + defaults = { + unit_type: :collection, + unit_name: 'theforeman.operations', + unit_source_type: :galaxy, + unit_source: 'https://galaxy.example.com', + unit_versions: ['1.0.0'], + } + + ::ForemanAnsibleDirector::AnsibleContent::SimpleAnsibleContentUnit.new( + **defaults.merge(overrides) + ) + end + end + end + end +end