Project

General

Profile

Download (8.32 KB) Statistics
| Branch: | Tag: | Revision:
cab0d8c6 Ohad Levy
require 'test_helper'

510d53cd Marek Hulan
class ProvisioningTemplateTest < ActiveSupport::TestCase
4a28771d Stephen Benjamin
test "should be valid when selecting a kind" do
510d53cd Marek Hulan
tmplt = ProvisioningTemplate.new
cab0d8c6 Ohad Levy
tmplt.name = "Default Kickstart"
tmplt.template = "Some kickstart goes here"
0fa5d146 Dominic Cleal
tmplt.template_kind = template_kinds(:ipxe)
cab0d8c6 Ohad Levy
assert tmplt.valid?
end

4a28771d Stephen Benjamin
test "should be valid as a snippet" do
510d53cd Marek Hulan
tmplt = ProvisioningTemplate.new
cab0d8c6 Ohad Levy
tmplt.name = "Default Kickstart"
tmplt.template = "Some kickstart goes here"
tmplt.snippet = true
assert tmplt.valid?
end

4a28771d Stephen Benjamin
test "should be invalid" do
510d53cd Marek Hulan
assert !ProvisioningTemplate.new.valid?
cab0d8c6 Ohad Levy
end

4a28771d Stephen Benjamin
test "should save assoications if not snippet" do
510d53cd Marek Hulan
tmplt = ProvisioningTemplate.new
a6fcf3b8 Sam Kottler
tmplt.name = "Some finish script"
tmplt.template = "echo $HOME"
tmplt.template_kind = template_kinds(:finish)
tmplt.snippet = false # this is the default, but it helps show the case
tmplt.hostgroups << hostgroups(:common)
tmplt.environments << environments(:production)
as_admin do
assert tmplt.save
end
assert_equal template_kinds(:finish), tmplt.template_kind
958d69cd Dmitri Dolguikh
assert_equal [hostgroups(:common)], tmplt.hostgroups
assert_equal [environments(:production)], tmplt.environments
a6fcf3b8 Sam Kottler
end

4a28771d Stephen Benjamin
test "should not save assoications if snippet" do
510d53cd Marek Hulan
tmplt = ProvisioningTemplate.new
cab0d8c6 Ohad Levy
tmplt.name = "Default Kickstart"
tmplt.template = "Some kickstart goes here"
tmplt.snippet = true
0fa5d146 Dominic Cleal
tmplt.template_kind = template_kinds(:ipxe)
cab0d8c6 Ohad Levy
tmplt.hostgroups << hostgroups(:common)
tmplt.environments << environments(:production)
ef5cbe9b Justin Sherrill
as_admin do
assert tmplt.save
end
cab0d8c6 Ohad Levy
assert_equal nil,tmplt.template_kind
assert_equal [],tmplt.hostgroups
assert_equal [],tmplt.environments
assert_equal [],tmplt.template_combinations
end
a6fcf3b8 Sam Kottler
# If the template is not a snippet is should require the specific declaration
0fa5d146 Dominic Cleal
# of a type (ipxe, finish, etc.)
4a28771d Stephen Benjamin
test "should require a template kind" do
510d53cd Marek Hulan
tmplt = ProvisioningTemplate.new
a6fcf3b8 Sam Kottler
tmplt.name = "Some finish script"
tmplt.template = "echo $HOME"

assert !tmplt.save
end
cab0d8c6 Ohad Levy
4a28771d Stephen Benjamin
test "should be able to clone" do
510d53cd Marek Hulan
tmplt = ProvisioningTemplate.new
4a28771d Stephen Benjamin
tmplt.name = "Finish It"
tmplt.template = "some content"
tmplt.snippet = false
tmplt.template_kind = template_kinds(:finish)
as_admin do
assert tmplt.save
end
clone = tmplt.clone

assert_equal clone.name, nil
assert_equal clone.operatingsystems, tmplt.operatingsystems
assert_equal clone.template_kind_id, tmplt.template_kind_id
assert_equal clone.template, tmplt.template
end

bf5f1801 Stephen Benjamin
test "can instantiate a locked template" do
assert FactoryGirl.create(:provisioning_template, :locked => true)
end

4a28771d Stephen Benjamin
test "should not edit a locked template" do
510d53cd Marek Hulan
tmplt = templates(:locked)
4a28771d Stephen Benjamin
tmplt.name = "something else"
refute_valid tmplt, :base, /is locked/
end

test "should clone a locked template as unlocked" do
510d53cd Marek Hulan
tmplt = templates(:locked)
4a28771d Stephen Benjamin
clone = tmplt.clone
assert_equal clone.name, nil
assert_equal clone.operatingsystems, tmplt.operatingsystems
assert_equal clone.template_kind_id, tmplt.template_kind_id
assert_equal clone.template, tmplt.template
ad2c9aa6 Stephen Benjamin
assert tmplt.locked
refute clone.locked
4a28771d Stephen Benjamin
end

test "should not remove a locked template" do
510d53cd Marek Hulan
tmplt = templates(:locked)
4a28771d Stephen Benjamin
refute_with_errors tmplt.destroy, tmplt, :base, /locked/
end

3279c309 Stephen Benjamin
test "should not unlock a template if not allowed" do
510d53cd Marek Hulan
tmplt = ProvisioningTemplate.create :name => "Vendor Template", :template => "provision test",
4a28771d Stephen Benjamin
:template_kind => template_kinds(:provision), :default => true,
:vendor => "Katello"
tmplt.update_attribute(:locked, true)
3279c309 Stephen Benjamin
User.current = FactoryGirl.create(:user)
4a28771d Stephen Benjamin
tmplt.locked = false
3279c309 Stephen Benjamin
refute_valid tmplt, :base, /not authorized/
4a28771d Stephen Benjamin
end

ad2c9aa6 Stephen Benjamin
test "should change a locked template while in rake" do
Foreman.stubs(:in_rake?).returns(true)
510d53cd Marek Hulan
tmplt = templates(:locked)
ad2c9aa6 Stephen Benjamin
tmplt.template = "changing the template content"
tmplt.name = "giving it a new name too"
assert tmplt.locked
assert_valid tmplt
end

de9e7ada Marek Hulan
test '#preview_host_collection obeys view_hosts permission' do
provisioning_template = FactoryGirl.build(:provisioning_template)
Host.expects(:authorized).with(:view_hosts).returns(Host.scoped)
provisioning_template.preview_host_collection
end

1443ec13 Greg Sutcliffe
describe "Association cascading" do
setup do
@os1 = FactoryGirl.create(:operatingsystem)
@hg1 = FactoryGirl.create(:hostgroup)
@hg2 = FactoryGirl.create(:hostgroup)
@hg3 = FactoryGirl.create(:hostgroup)
@ev1 = FactoryGirl.create(:environment)
@ev2 = FactoryGirl.create(:environment)
@ev3 = FactoryGirl.create(:environment)

@tk = FactoryGirl.create(:template_kind)

# Most specific template association
510d53cd Marek Hulan
@ct1 = FactoryGirl.create(:provisioning_template, :template_kind => @tk, :operatingsystems => [@os1])
1443ec13 Greg Sutcliffe
@ct1.template_combinations.create(:hostgroup => @hg1, :environment => @ev1)

# HG only
# We add an association on HG2/EV2 to ensure that we're not just blindly
# selecting all template_combinations where environment_id => nil
510d53cd Marek Hulan
@ct2 = FactoryGirl.create(:provisioning_template, :template_kind => @tk, :operatingsystems => [@os1])
1443ec13 Greg Sutcliffe
@ct2.template_combinations.create(:hostgroup => @hg1, :environment => nil)
@ct2.template_combinations.create(:hostgroup => @hg2, :environment => @ev2)

# Env only
# We add an association on HG2/EV2 to ensure that we're not just blindly
# selecting all template_combinations where hostgroup_id => nil
510d53cd Marek Hulan
@ct3 = FactoryGirl.create(:provisioning_template, :template_kind => @tk, :operatingsystems => [@os1])
1443ec13 Greg Sutcliffe
@ct3.template_combinations.create(:hostgroup => nil, :environment => @ev1)
@ct3.template_combinations.create(:hostgroup => @hg2, :environment => @ev2)

# Default template for the OS
510d53cd Marek Hulan
@ctd = FactoryGirl.create(:provisioning_template, :template_kind => @tk, :operatingsystems => [@os1])
1443ec13 Greg Sutcliffe
@ctd.os_default_templates.create(:operatingsystem => @os1,
:template_kind_id => @ctd.template_kind_id)
end

test "find_template finds a matching template with hg and env" do
assert_equal @ct1.name,
510d53cd Marek Hulan
ProvisioningTemplate.find_template({:kind => @tk.name,
1443ec13 Greg Sutcliffe
:operatingsystem_id => @os1.id,
:hostgroup_id => @hg1.id,
:environment_id => @ev1.id}).name
end
test "find_template finds a matching template with hg only" do
assert_equal @ct2.name,
510d53cd Marek Hulan
ProvisioningTemplate.find_template({:kind => @tk.name,
1443ec13 Greg Sutcliffe
:operatingsystem_id => @os1.id,
:hostgroup_id => @hg1.id}).name
end
test "find_template finds a matching template with hg and mismatched env" do
assert_equal @ct2.name,
510d53cd Marek Hulan
ProvisioningTemplate.find_template({:kind => @tk.name,
1443ec13 Greg Sutcliffe
:operatingsystem_id => @os1.id,
:hostgroup_id => @hg1.id,
:environment_id => @ev3.id}).name
end
test "find_template finds a matching template with env only" do
assert_equal @ct3.name,
510d53cd Marek Hulan
ProvisioningTemplate.find_template({:kind => @tk.name,
1443ec13 Greg Sutcliffe
:operatingsystem_id => @os1.id,
:environment_id => @ev1.id}).name
end
test "find_template finds a matching template with env and mismatched hg" do
assert_equal @ct3.name,
510d53cd Marek Hulan
ProvisioningTemplate.find_template({:kind => @tk.name,
1443ec13 Greg Sutcliffe
:operatingsystem_id => @os1.id,
:hostgroup_id => @hg3.id,
:environment_id => @ev1.id}).name
end
test "find_template finds the default template when hg and env do not match" do
assert_equal @ctd.name,
510d53cd Marek Hulan
ProvisioningTemplate.find_template({:kind => @tk.name,
1443ec13 Greg Sutcliffe
:operatingsystem_id => @os1.id,
:hostgroup_id => @hg3.id,
:environment_id => @ev3.id}).name
end
end
cab0d8c6 Ohad Levy
end