foreman/test/functional/environments_controller_test.rb @ 8387eb79
6e50fa1d | Ohad Levy | require 'test_helper'
|
|
class EnvironmentsControllerTest < ActionController::TestCase
|
|||
8d9dc8b0 | Lucas Tolchinsky | test "should get index" do
|
|
9c0e127b | Paul Kelly | get :index, {}, set_session_user
|
|
8d9dc8b0 | Lucas Tolchinsky | assert_response :success
|
|
5f6b2196 | Paul Kelly | assert_not_nil assigns(:environments)
|
|
8d9dc8b0 | Lucas Tolchinsky | end
|
|
9c0e127b | Paul Kelly | test "should get new" do
|
|
get :new, {}, set_session_user
|
|||
8d9dc8b0 | Lucas Tolchinsky | assert_response :success
|
|
end
|
|||
test "should create new environment" do
|
|||
assert_difference 'Environment.count' do
|
|||
5f6b2196 | Paul Kelly | post :create, { :commit => "Create", :environment => {:name => "some_environment"} }, set_session_user
|
|
8d9dc8b0 | Lucas Tolchinsky | end
|
|
assert_redirected_to environments_path
|
|||
end
|
|||
test "should get edit" do
|
|||
68234aea | Ivan Necas | setup_users
|
|
977384e0 | Lucas Tolchinsky | environment = Environment.new :name => "some_environment"
|
|
8d9dc8b0 | Lucas Tolchinsky | assert environment.save!
|
|
8387eb79 | Shlomi Zadok | get :edit, {:id => environment.to_param}, set_session_user
|
|
8d9dc8b0 | Lucas Tolchinsky | assert_response :success
|
|
end
|
|||
test "should update environment" do
|
|||
68234aea | Ivan Necas | setup_users
|
|
977384e0 | Lucas Tolchinsky | environment = Environment.new :name => "some_environment"
|
|
8d9dc8b0 | Lucas Tolchinsky | assert environment.save!
|
|
8387eb79 | Shlomi Zadok | put :update, { :commit => "Update", :id => environment.to_param, :environment => {:name => "other_environment"} }, set_session_user
|
|
0c96d0bf | Ohad Levy | env = Environment.find(environment)
|
|
assert env.name == "other_environment"
|
|||
8d9dc8b0 | Lucas Tolchinsky | ||
assert_redirected_to environments_path
|
|||
end
|
|||
test "should destroy environment" do
|
|||
68234aea | Ivan Necas | setup_users
|
|
977384e0 | Lucas Tolchinsky | environment = Environment.new :name => "some_environment"
|
|
8d9dc8b0 | Lucas Tolchinsky | assert environment.save!
|
|
assert_difference('Environment.count', -1) do
|
|||
8387eb79 | Shlomi Zadok | delete :destroy, {:id => environment.to_param}, set_session_user
|
|
8d9dc8b0 | Lucas Tolchinsky | end
|
|
assert_redirected_to environments_path
|
|||
6e50fa1d | Ohad Levy | end
|
|
adcd0374 | Paul Kelly | ||
bb9a1ad1 | Paul Kelly | def setup_import_classes
|
|
7d1d44cf | Joseph Mitchell Magen | as_admin do
|
|
Host::Managed.update_all(:environment_id => nil)
|
|||
Hostgroup.update_all(:environment_id => nil)
|
|||
Puppetclass.destroy_all
|
|||
Environment.destroy_all
|
|||
end
|
|||
adcd0374 | Paul Kelly | @request.env["HTTP_REFERER"] = environments_url
|
|
bb9a1ad1 | Paul Kelly | # This is the database status
|
|
# and should result in a db_tree of {"env1" => ["a", "b", "c"], "env2" => ["a", "b", "c"]}
|
|||
4e7b2b98 | Paul Kelly | as_admin do
|
|
bb9a1ad1 | Paul Kelly | ["a", "b", "c"].each {|name| Puppetclass.create :name => name}
|
|
for name in ["env1", "env2"] do
|
|||
017e1049 | Ohad Levy | e = Environment.create!(:name => name)
|
|
bb9a1ad1 | Paul Kelly | e.puppetclasses += [Puppetclass.find_by_name("a"), Puppetclass.find_by_name("b"), Puppetclass.find_by_name("c")]
|
|
end
|
|||
4e7b2b98 | Paul Kelly | end
|
|
bb9a1ad1 | Paul Kelly | # This is the on-disk status
|
|
# and should result in a disk_tree of {"env1" => ["a", "b", "c"],"env2" => ["a", "b", "c"]}
|
|||
a989a617 | Ohad Levy | envs = HashWithIndifferentAccess.new(:env1 => %w{a b c}, :env2 => %w{a b c})
|
|
f8d94608 | Amos Benari | pcs = [HashWithIndifferentAccess.new( "a" => { "name" => "a", "module" => "", "params"=> {}})]
|
|
classes = Hash[pcs.map { |k| [k.keys.first, Foreman::ImporterPuppetclass.new(k.values.first)] }]
|
|||
Environment.expects(:puppetEnvs).returns(envs).at_least(0)
|
|||
ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["env1", "env2"])
|
|||
ProxyAPI::Puppet.any_instance.stubs(:classes).returns(classes)
|
|||
adcd0374 | Paul Kelly | end
|
|
bb9a1ad1 | Paul Kelly | test "should handle disk environment containing additional classes" do
|
|
setup_import_classes
|
|||
Environment.find_by_name("env1").puppetclasses.delete(Puppetclass.find_by_name("a"))
|
|||
a989a617 | Ohad Levy | # db_tree of {"env1" => ["b", "c"], "env2" => ["a", "b", "c"]}
|
|
# disk_tree of {"env1" => ["a", "b", "c"],"env2" => ["a", "b", "c"]}
|
|||
f8d94608 | Amos Benari | get :import_environments, {:proxy => smart_proxies(:puppetmaster)}, set_session_user
|
|
445dd7ab | Daniel Lobato | assert_template "common/_puppetclasses_or_envs_changed"
|
|
f8d94608 | Amos Benari | assert_select 'input#changed_new_env1'
|
|
bb9a1ad1 | Paul Kelly | post :obsolete_and_new,
|
|
{"changed" =>
|
|||
{"new" =>
|
|||
f8d94608 | Amos Benari | {"env1" => '{"a":{"new":{}}}'}
|
|
bb9a1ad1 | Paul Kelly | }
|
|
}, set_session_user
|
|||
assert_redirected_to environments_url
|
|||
b4fbdf57 | Dominic Cleal | assert_equal "Successfully updated environments and Puppet classes from the on-disk Puppet installation", flash[:notice]
|
|
bb9a1ad1 | Paul Kelly | assert Environment.find_by_name("env1").puppetclasses.map(&:name).sort == ["a", "b", "c"]
|
|
end
|
|||
8387eb79 | Shlomi Zadok | ||
bb9a1ad1 | Paul Kelly | test "should handle disk environment containing less classes" do
|
|
setup_import_classes
|
|||
as_admin {Puppetclass.create(:name => "d")}
|
|||
Environment.find_by_name("env1").puppetclasses << Puppetclass.find_by_name("d")
|
|||
#db_tree of {"env1" => ["a", "b", "c", "d"], "env2" => ["a", "b", "c"]}
|
|||
#disk_tree of {"env1" => ["a", "b", "c"], "env2" => ["a", "b", "c"]}
|
|||
22a2bcb1 | Amos Benari | get :import_environments, {:proxy => smart_proxies(:puppetmaster)}, set_session_user
|
|
445dd7ab | Daniel Lobato | assert_template "common/_puppetclasses_or_envs_changed"
|
|
bb9a1ad1 | Paul Kelly | assert_select 'input#changed_obsolete_env1[value*="d"]'
|
|
post :obsolete_and_new,
|
|||
{"changed" =>
|
|||
{"obsolete" =>
|
|||
{"env1" => '["d"]'}
|
|||
}
|
|||
}, set_session_user
|
|||
assert_redirected_to environments_url
|
|||
b4fbdf57 | Dominic Cleal | assert_equal "Successfully updated environments and Puppet classes from the on-disk Puppet installation", flash[:notice]
|
|
bb9a1ad1 | Paul Kelly | envs = Environment.find_by_name("env1").puppetclasses.map(&:name).sort
|
|
assert envs == ["a", "b", "c"]
|
|||
end
|
|||
test "should handle disk environment containing less environments" do
|
|||
setup_import_classes
|
|||
as_admin {Environment.create(:name => "env3")}
|
|||
#db_tree of {"env1" => ["a", "b", "c"], "env2" => ["a", "b", "c"], "env3" => []}
|
|||
#disk_tree of {"env1" => ["a", "b", "c"], "env2" => ["a", "b", "c"]}
|
|||
316a4ccd | Ohad Levy | get :import_environments, {:proxy => smart_proxies(:puppetmaster).id}, set_session_user
|
|
445dd7ab | Daniel Lobato | assert_template "common/_puppetclasses_or_envs_changed"
|
|
bb9a1ad1 | Paul Kelly | assert_select 'input#changed_obsolete_env3'
|
|
post :obsolete_and_new,
|
|||
{"changed" =>
|
|||
{"obsolete" =>
|
|||
{"env3" => '[]'}
|
|||
}
|
|||
}, set_session_user
|
|||
assert_redirected_to environments_url
|
|||
b4fbdf57 | Dominic Cleal | assert_equal "Successfully updated environments and Puppet classes from the on-disk Puppet installation", flash[:notice]
|
|
bb9a1ad1 | Paul Kelly | assert Environment.find_by_name("env3").puppetclasses.map(&:name).sort == []
|
|
adcd0374 | Paul Kelly | end
|
|
test "should fail to remove active environments" do
|
|||
d076d573 | Joseph Mitchell Magen | disable_orchestration
|
|
bb9a1ad1 | Paul Kelly | setup_import_classes
|
|
4e7b2b98 | Paul Kelly | as_admin do
|
|
e14b5758 | Greg Sutcliffe | host = FactoryGirl.create(:host)
|
|
7d1d44cf | Joseph Mitchell Magen | Environment.find_by_name("env1").puppetclasses += [puppetclasses(:one)]
|
|
793072c5 | Joseph Mitchell Magen | host.environment_id = Environment.find_by_name("env1").id
|
|
assert host.save!
|
|||
bb9a1ad1 | Paul Kelly | assert host.errors.empty?
|
|
assert Environment.find_by_name("env1").hosts.count > 0
|
|||
4e7b2b98 | Paul Kelly | end
|
|
f8d94608 | Amos Benari | ||
bb9a1ad1 | Paul Kelly | # assert_template "puppetclasses_or_envs_changed". This assertion will fail. And it should fail. See above.
|
|
post :obsolete_and_new,
|
|||
{"changed"=>
|
|||
{"obsolete" =>
|
|||
{"env1" => '["a","b","c","_destroy_"]'}
|
|||
}
|
|||
}, set_session_user
|
|||
assert Environment.find_by_name("env1").hosts.count > 0
|
|||
f8d94608 | Amos Benari | #assert flash[:error] =~ /^Failed to update the environments and puppetclasses from the on-disk puppet installation/
|
|
bb9a1ad1 | Paul Kelly | assert Environment.find_by_name("env1")
|
|
adcd0374 | Paul Kelly | end
|
|
bb9a1ad1 | Paul Kelly | test "should obey config/ignored_environments.yml" do
|
|
adcd0374 | Paul Kelly | @request.env["HTTP_REFERER"] = environments_url
|
|
bb9a1ad1 | Paul Kelly | setup_import_classes
|
|
as_admin do
|
|||
Environment.create :name => "env3"
|
|||
7d1d44cf | Joseph Mitchell Magen | Environment.find_by_name("env2").destroy
|
|
bb9a1ad1 | Paul Kelly | end
|
|
f8d94608 | Amos Benari | #db_tree of {"env1" => ["a", "b", "c"], "env3" => []}
|
|
bb9a1ad1 | Paul Kelly | #disk_tree of {"env1" => ["a", "b", "c"], "env2" => ["a", "b", "c"]}
|
|
41d40698 | Paul Kelly | ||
f8d94608 | Amos Benari | PuppetClassImporter.any_instance.stubs(:ignored_environments).returns(["env1","env2","env3"])
|
|
22a2bcb1 | Amos Benari | get :import_environments, {:proxy => smart_proxies(:puppetmaster)}, set_session_user
|
|
41d40698 | Paul Kelly | ||
fc94df42 | Dominic Cleal | assert_equal "No changes to your environments detected", flash[:notice]
|
|
41d40698 | Paul Kelly | end
|
|
9fd7478e | Paul Kelly | ||
def setup_user
|
|||
@request.session[:user] = users(:one).id
|
|||
users(:one).roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
|
|||
end
|
|||
test 'user with viewer rights should fail to edit an environment' do
|
|||
setup_user
|
|||
d7bd2f22 | Ohad Levy | get :edit, {:id => environments(:production).name}, set_session_user.merge(:user => users(:one).id)
|
|
017e1049 | Ohad Levy | assert_equal @response.status, 403
|
|
9fd7478e | Paul Kelly | end
|
|
test 'user with viewer rights should succeed in viewing environments' do
|
|||
setup_user
|
|||
d7bd2f22 | Ohad Levy | get :index, {}, set_session_user
|
|
9fd7478e | Paul Kelly | assert_response :success
|
|
end
|
|||
8387eb79 | Shlomi Zadok | ||
test "should accept environment with name 'name'" do
|
|||
@request.env["HTTP_REFERER"] = environments_url
|
|||
ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["new"])
|
|||
get :import_environments, {:proxy => smart_proxies(:puppetmaster)}, set_session_user
|
|||
post :obsolete_and_new,
|
|||
{"changed" =>
|
|||
{"new" =>
|
|||
{"new" => '{"a":{"new":{}}}'}
|
|||
}
|
|||
}, set_session_user
|
|||
assert(Environment.all.map(&:name).include?('new'), 'Should include environment with name "new"')
|
|||
end
|
|||
6e50fa1d | Ohad Levy | end
|