Skip to content

Commit

Permalink
more unit tests. machinist now available for activerecord. fixed bug …
Browse files Browse the repository at this point in the history
…in hostgroups controller that broke checks
  • Loading branch information
Lennart Koopmann committed Dec 6, 2010
1 parent 0083fe5 commit fac0c3d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
9 changes: 8 additions & 1 deletion app/controllers/hostgrouphosts_controller.rb
@@ -1,7 +1,14 @@
class HostgrouphostsController < ApplicationController

def create
if params[:new_host][:ruletype] == HostgroupHost::TYPE_SIMPLE
# Check if hostgroup exists.
if Hostgroup.count(:conditions => { :id => params[:new_host][:hostgroup_id] }) == 0
flash[:error] = "Group does not exist."
redirect_to :controller => "hosts"
return
end

if params[:new_host][:ruletype].to_i == HostgroupHost::TYPE_SIMPLE
# Check if that host exists.
if Host.find_by_host(params[:new_host][:hostname]).blank?
flash[:error] = "Host does not exist!"
Expand Down
8 changes: 8 additions & 0 deletions test/blueprints-ar.rb
@@ -0,0 +1,8 @@
require 'machinist/active_record'
require 'sham'

Sham.sn { rand(500000) }

Hostgroup.blueprint do
name { "group-#{sn}" }
end
File renamed without changes.
84 changes: 81 additions & 3 deletions test/functional/hostgrouphosts_controller_test.rb
Expand Up @@ -2,8 +2,86 @@
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")

class HostgrouphostsControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true

test "add host with type simple" do
Host.make(:host => "foo").save

hostgroup_id = 100
Hostgroup.make(:id => hostgroup_id).save

assert_difference("HostgroupHost.count") do
post :create, :new_host => { :hostname => "foo", :hostgroup_id => hostgroup_id, :ruletype => HostgroupHost::TYPE_SIMPLE }
end

# Test that the host really has TYPE_SIMPLE (and hostname if we are already here...)
hosts = HostgroupHost.find_all_by_hostgroup_id(hostgroup_id)
assert_equal 1, hosts.count
assert_equal HostgroupHost::TYPE_SIMPLE, hosts[0].ruletype
assert_equal "foo", hosts[0].hostname

assert_redirected_to(:controller => "hostgroups", :action => "hosts", :id => hostgroup_id)
end

test "add host with type regex" do
hostgroup_id = 150
Hostgroup.make(:id => hostgroup_id).save

assert_difference("HostgroupHost.count") do
post :create, :new_host => { :hostname => "^foo\d", :hostgroup_id => hostgroup_id, :ruletype => HostgroupHost::TYPE_REGEX }
end

# Test that the host really has TYPE_REGEX (and hostname if we are already here...)
hosts = HostgroupHost.find_all_by_hostgroup_id(hostgroup_id)
assert_equal 1, hosts.count
assert_equal HostgroupHost::TYPE_REGEX, hosts[0].ruletype
assert_equal "^foo\d", hosts[0].hostname

assert_redirected_to(:controller => "hostgroups", :action => "hosts", :id => hostgroup_id)
end

test "host group must exist" do
Host.make(:host => "foo").save
post :create, :new_host => { :hostname => "foo", :hostgroup_id => 13371337, :ruletype => HostgroupHost::TYPE_SIMPLE }

assert_equal "Group does not exist.", flash[:error]

assert_redirected_to(:controller => "hosts", :action => "index")
end

test "simple host is not added if host does not exist" do
hostgroup_id = 500
Hostgroup.make(:id => hostgroup_id).save

post :create, :new_host => { :hostname => "i_dont_exist", :hostgroup_id => hostgroup_id, :ruletype => HostgroupHost::TYPE_SIMPLE }

assert_equal "Host does not exist!", flash[:error]

assert_redirected_to(:controller => "hostgroups", :action => "hosts", :id => hostgroup_id)
end

test "a host can only be added once to a host group" do
Host.make(:host => "foo").save

hostgroup_id = 50
Hostgroup.make(:id => hostgroup_id).save

# First insert should work.
assert_difference("HostgroupHost.count") do
post :create, :new_host => { :hostname => "foo", :hostgroup_id => hostgroup_id, :ruletype => HostgroupHost::TYPE_SIMPLE }
end

# First redirect
assert_redirected_to(:controller => "hostgroups", :action => "hosts", :id => hostgroup_id)

# Insert host a second time.
assert_no_difference("HostgroupHost.count") do
post :create, :new_host => { :hostname => "foo", :hostgroup_id => hostgroup_id, :ruletype => HostgroupHost::TYPE_SIMPLE }
end

assert_equal "Host already in group.", flash[:error]

# Second redirect
assert_redirected_to(:controller => "hostgroups", :action => "hosts", :id => hostgroup_id)
end

end
3 changes: 2 additions & 1 deletion test/test_helper.rb
Expand Up @@ -2,7 +2,8 @@
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

require File.expand_path(File.dirname(__FILE__) + "/blueprints")
require File.expand_path(File.dirname(__FILE__) + "/blueprints-mm.rb")
require File.expand_path(File.dirname(__FILE__) + "/blueprints-ar.rb")

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
Expand Down

0 comments on commit fac0c3d

Please sign in to comment.