reevoolabs : open source technology

October 16, 2009

Problems with Cookie testing in Rails 2.3

Filed under: Uncategorized — craigsmith @ 4:38 pm

Pre Rails 2.3, when you wanted to set the value of a cookie in a test you had to:

def test_should_set_user_name
  #set cookie value
  @request.cookies['visitor_name'] = CGI::Cookie.new('visitor_name', 'Dave')

  post :login, :name => 'Dan'
  assert_equal 'Dan', cookies['visitor_name'].value
end

But in 2.3 you can forget about all that CGI::Cookie crap and concentrate on the sweet code you actually want to test:

def test_should_set_user_name
  #set the cookie value
  @request.cookies['visitor_name'] = 'Dave'

  post :login, :name => 'Dan'
  assert_equal 'Dan', cookies['visitor_name'].value
end

Pretty!

The problem comes when you want to ensure that ‘Dave’ has been logged out. Consider this test:

def test_should_log_user_out
  @request.cookies['visitor_name'] = 'Dave'
  post :logout
  assert_nil cookies['visitor_name']
end

This test always passes! No matter whether the cookie is removed in the logout action or not.

The solution

‘cookies’ is defined in ActionController::TestProcess as:

def cookies
  @response.cookies
end

When you ask for cookies in your test, what you’re actually getting is @response.cookies which doesn’t include the values of the cookies set in the request. This means that cookies['visitor_name'], as far as the test is concerned, is never set, making it pass incorrectly.

I’ve written a simple patch which has recently been merged in to Rails. It merges the @response cookie with the @request cookie and returns that, rather than just returning the @response.cookie. So ActionController::TestProcess becomes:

def cookies
  @request.cookies.merge(@response.cookies)
end

If you’re interested you can find the patch here.

Happy testing!

July 9, 2009

Copying a VM between Xen hosts

Filed under: Uncategorized — louisg @ 2:06 pm

We recently moved a Xen virtual machine from one Xen host to another. The process involves simply copying across its disk partition (in this case an LVM2 partition using the device mapper) and copying across its config file:

1) Copy across the VM partition device.
On target host:


lvcreate -L10G -n vm1-disk xen
nc -l 12345 | dd of=/dev/mapper/xen-vm1--disk conv=nocreat bs=16065b

On source host:


xm shutdown vm1
dd if=/dev/mapper/vm1-disk bs=16065b | nc xen2 12345

2) Copy across the VM config file.


scp /etc/xen/vm1.cfg root@xen2:/etc/xen/

Some further explanation is probably needed for the first step. Creating the logical volume on the target host in advance ensures that the partition, once copied across, remains a block device rather than as an image file. Netcat (nc) provides a fast mechanism to transfer the partition over the ether (use SSH if you’re sensitive about your data). As for the flags to dd, the block size (bs) is set to 16065 bytes, the number of sectors in a cylinder so I’m told (it worked for me), and the nocreat flag tells dd not to overwrite the block device.

Note: remember to shutdown the VM first! This is an offline copy. If you want to minimise downtime I’m sure an LVM snapshot would work too.

July 3, 2009

Testing Apache and mod_rewrite using Test::Unit

Filed under: Uncategorized — matthouse @ 11:04 am

Here at Reevoo we (like many others) use Apache as our webserver of choice and with this comes the venerable mod_rewrite.

Mod_rewrite can be used for a lot more than just redirecting pages though, you can use it for forward and reverse proxying, redirection and url rewriting based on various factors such as the HTTP host or request uri.  However there are myriad ways in which to shoot yourself in the foot!

We love testing, so when an article about testing mod_rewrite rules using Test::Unit by the guys at Viget Labs popped up in my feed reader I quickly popped round to take a look.

Using the redirect tester you can easily define shoulda style tests by doing something like this


class ReevooRedirectTest < HTTPRedirectTest
  self.domain = 'www.reevoo.com'
  should_redirect '/decidewhattobuy/blog', :to => '/decidewhattobuy', :permanant => true
  should_redirect '/blog', :to => '/decidewhattobuy'
end

This is very cool and makes working with mod_rewrite much less painful than it can be!

The original code is a series of gists hosted on the vigetlabs github page and to make them easier to use and manage I packaged it up as a gem, which you can install as follows:

sudo gem sources -a http://gems.github.com && sudo gem install shadowaspect-http_redirect_test

and use it in your code like this:

require 'http_redirect_test'

have fun!

July 3, 2008

Welcome to Reevoo Labs

Filed under: Uncategorized — kylemcginn @ 6:03 pm

Welcome to the re-launch of labs.reevoo.com, a place where we (the team at Reevoo) try our best to contribute a little something back to the community.

To kick things off, we’ve brought across some of our existing projects from the previous labs site, but also added a few new things that we felt were ready for prime-time:

  1. Mocha – the Ruby mocking/stubbing library based on JMock syntax, used by Rails core
  2. NEW!Beanstalk Messaging plugin – a Rails plugin for managing, polling and communicating with the excellent Beanstalkd messaging queue
  3. NEW!Simple Config – a Rails plugin that makes it easy to set up application-wide configuration/settings for each of your development environments, separate to Rails’ own environment files, and provides an object-oriented way of accessing those settings throughout your application.
  4. NEW!CTO / Reevoo

Powered by WordPress