foreman/test/models/smart_proxy_test.rb @ 9d43fc71
06823dc7 | Ohad Levy | require 'test_helper'
|
|
class SmartProxyTest < ActiveSupport::TestCase
|
|||
90b5dff1 | ldjebran | should allow_values(*valid_name_list).for(:name)
|
|
should_not allow_values('', ' ', '\t', nil, 'invalid url').for(:url)
|
|||
test "should not create smart_proxy with invalid name" do
|
|||
invalid_name_list.each do |invalid_name|
|
|||
smart_proxy = FactoryBot.build(:smart_proxy, :name => invalid_name, :url => 'https://valid.url:4568')
|
|||
refute_valid smart_proxy
|
|||
assert_includes smart_proxy.errors.keys, :name
|
|||
end
|
|||
end
|
|||
98f6ca54 | Daniel Lobato | context 'url validations' do
|
|
setup do
|
|||
8c6bc83e | Marek Hulan | @proxy = FactoryBot.
|
|
98f6ca54 | Daniel Lobato | build_stubbed(:smart_proxy, :url => 'https://secure.proxy:4568')
|
|
end
|
|||
77bbe487 | Ohad Levy | ||
98f6ca54 | Daniel Lobato | test "should be valid" do
|
|
assert_valid @proxy
|
|||
end
|
|||
77bbe487 | Ohad Levy | ||
98f6ca54 | Daniel Lobato | test "should not be modified if has no leading slashes" do
|
|
assert_equal @proxy.url, "https://secure.proxy:4568"
|
|||
0fe147c7 | Joseph Mitchell Magen | end
|
|
77bbe487 | Ohad Levy | end
|
|
f5d4e70a | Jeremy Kitchen | ||
98f6ca54 | Daniel Lobato | test "should not include trailing slash" do
|
|
61802878 | Shlomi Zadok | ProxyAPI::Features.any_instance.stubs(:features => Feature.name_map.keys)
|
|
8c6bc83e | Marek Hulan | @proxy = FactoryBot.build(:smart_proxy)
|
|
98f6ca54 | Daniel Lobato | @proxy.url = 'http://some.proxy:4568/'
|
|
as_admin { assert @proxy.save }
|
|||
assert_equal @proxy.url, "http://some.proxy:4568"
|
|||
f5d4e70a | Jeremy Kitchen | end
|
|
98f6ca54 | Daniel Lobato | context 'legacy puppet hostname' do
|
|
setup do
|
|||
8c6bc83e | Marek Hulan | @proxy = FactoryBot.build_stubbed(:smart_proxy)
|
|
98f6ca54 | Daniel Lobato | @proxy.url = "http://puppet.example.com:4568"
|
|
end
|
|||
f5d4e70a | Jeremy Kitchen | ||
98f6ca54 | Daniel Lobato | test "when true returns puppet part of hostname" do
|
|
Setting.expects(:[]).with(:legacy_puppet_hostname).returns(true)
|
|||
assert_equal "puppet", @proxy.to_s
|
|||
end
|
|||
test "when false returns whole hostname" do
|
|||
Setting.expects(:[]).with(:legacy_puppet_hostname).returns(false)
|
|||
assert_equal "puppet.example.com", @proxy.to_s
|
|||
end
|
|||
f5d4e70a | Jeremy Kitchen | end
|
|
1fa008a4 | Joseph Magen | ||
8f657a84 | Stephen Benjamin | test "proxy should respond correctly to has_feature? method" do
|
|
8c6bc83e | Marek Hulan | proxy = FactoryBot.build_stubbed(:template_smart_proxy)
|
|
8f657a84 | Stephen Benjamin | assert proxy.has_feature?('Templates')
|
|
refute proxy.has_feature?('Puppet CA')
|
|||
end
|
|||
1fa008a4 | Joseph Magen | # test taxonomix methods
|
|
test "should get used location ids for host" do
|
|||
8c6bc83e | Marek Hulan | FactoryBot.create(:host, :with_environment, :puppet_proxy => smart_proxies(:puppetmaster),
|
|
e14b5758 | Greg Sutcliffe | :location => taxonomies(:location1))
|
|
1fa008a4 | Joseph Magen | assert_equal ["Puppet", "Puppet CA"], smart_proxies(:puppetmaster).features.pluck(:name).sort
|
|
assert_equal [taxonomies(:location1).id], smart_proxies(:puppetmaster).used_location_ids
|
|||
end
|
|||
test "should get used and selected location ids for host" do
|
|||
assert_equal [taxonomies(:location1).id], smart_proxies(:puppetmaster).used_or_selected_location_ids
|
|||
end
|
|||
9a54e63a | Shlomi Zadok | ||
2ac8d7ba | Tomer Brisker | test "should return environment stats" do
|
|
proxy = smart_proxies(:puppetmaster)
|
|||
ProxyAPI::Puppet.any_instance.expects(:environments).returns(['env1', 'env2'])
|
|||
ProxyAPI::Puppet.any_instance.expects(:class_count).with('env1').returns(1)
|
|||
ProxyAPI::Puppet.any_instance.expects(:class_count).with('env2').returns(2)
|
|||
assert_equal({'env1' => 1, 'env2' => 2}, proxy.statuses[:puppet].environment_stats)
|
|||
end
|
|||
acd64d03 | Tomer Brisker | test "can count connected hosts" do
|
|
8c6bc83e | Marek Hulan | proxy = FactoryBot.create(:puppet_smart_proxy)
|
|
FactoryBot.create(:host, :with_environment, :puppet_proxy => proxy)
|
|||
acd64d03 | Tomer Brisker | as_admin do
|
|
assert_equal 1, proxy.hosts_count
|
|||
end
|
|||
end
|
|||
61802878 | Shlomi Zadok | ||
test "should be saved if features exist" do
|
|||
8c6bc83e | Marek Hulan | proxy = FactoryBot.build(:smart_proxy)
|
|
9d43fc71 | Michael Moll | ProxyAPI::Features.any_instance.stubs(:features => ["tftp"])
|
|
61802878 | Shlomi Zadok | assert proxy.save
|
|
7b9aa630 | Dominic Cleal | assert_include(proxy.features, features(:tftp))
|
|
61802878 | Shlomi Zadok | end
|
|
test "should not be saved if features do not exist" do
|
|||
proxy = SmartProxy.new(:name => 'Proxy', :url => 'https://some.where.net:8443')
|
|||
error_message = 'Features "feature" in this proxy are not recognized by Foreman. '\
|
|||
'If these features come from a Smart Proxy plugin, make sure Foreman has the plugin installed too.'
|
|||
9d43fc71 | Michael Moll | ProxyAPI::Features.any_instance.stubs(:features => ["feature"])
|
|
61802878 | Shlomi Zadok | refute proxy.save
|
|
assert_equal(error_message, proxy.errors[:base].first)
|
|||
end
|
|||
test "should not be saved if features are not array" do
|
|||
proxy = SmartProxy.new(:name => 'Proxy', :url => 'https://some.where.net:8443')
|
|||
ProxyAPI::Features.any_instance.stubs(:features => {:fe => :at, :ur => :e})
|
|||
refute proxy.save
|
|||
assert_equal('An invalid response was received while requesting available features from this proxy', proxy.errors[:base].first)
|
|||
end
|
|||
06823dc7 | Ohad Levy | end
|