Non-random thoughts.

2009-02-21

Setting up rdiff-backup 1.2.5 on Ubuntu Hardy 8.04

I wanted to backup a server running Debian Lenny 5.0, to a slightly older box running Ubuntu Hardy 8.04 using rdiff-backup. As rdiff-backup complains if the version-numbers on both sides are not the same I had to upgrade the older box to version 1.2.5 of rdiff-backup.


aptitude install librsync-dev python-dev
tar xzvf tar xzvf rdiff-backup-1.2.6.tar.gz
cd rdiff-backup-1.2.6
python setup.py install --prefix=/usr/local


I have previously used standard rsync for efficiently mirror the contents of a server to the backup server. This meant that I also had to change my backupscript slightly, to accomodate rdiff-backup usage.


#!/bin/sh

EXCLUDE="/var/lib/mysql/ /tmp/ /proc/ /sys/ /dev/ /bin/ /sbin/ /lib/ /usr/"
INCLUDE="/usr/local/"

exclude=""
for i in $EXCLUDE; do
exclude="$exclude --exclude $i"
done

include=""
for i in $INCLUDE; do
include="$include --include $i"
done

# rsync --archive --delete --sparse $exclude $include --rsh=ssh / backupuser@backupserver:root
rdiff-backup $include $exclude / backupuser@backupserver::/home/backupuser/rdiff-backup/

2009-01-21

How to recycle an Application Pool on Windows Server 2008 from command line

For a client I wanted to implement automatic recycling of an Application Pool on Windows Server 2008, to be called from an event handler in nagios. I scorched the net for possible solutions, and found many suggestions, including iisreset.exe, iisapp.vbs and various wmi-based scripting techniques, none of which worked out.

After a while, nearly giving up, I stumbled upon this page which showed the correct technique:


c:\windows\system32\inetsrv\appcmd.exe recycle apppool "website.com"

2008-12-10

Difference between Mocks and Stubs

Someone new to unit-testing, as myself, might have a hard time judging when to use the various techniques available at your disposal. One of the choices to make is when to use a mock-object and when to use a stub.

After having spent an evening trying to iron out the issues and coming up with a clearer understanding, these are my thoughts.

The legendary Martin Fowler have written a good article about this. The article suggest the word double as a generic form of objects which are utilized in testing scenarios in place of the real implementation. Stubs and mocks are therefore specific versions of doubles. A stub is described as primarily "returning canned answers" and secondarily remembering what was called, while a mock are pre-set with expectations about which calls should be made.

This means that stubs don't really care whether they were called or not. The mock object, on the other hand, enforces which calls should be made, in what order and with what parameters. Mocks are concerned with the behaviour, in addition to the end-result, whereas stubs are only concerned with the end-result. Stubs lends itself to state-based testing, given A,B,C perform X, expect A',B',C'. Mocks on the other hand also enforces how the end-state was derived at.

One could argue that as mocks dictates the behaviour of a method, they break encapsulation. However this is a double edged sword, as it can be used to flesh out behaviour in an iterative manner, exemplified by Behaviour-Driven-Development (BDD).

In the context of Rhino Mocks, the waters are muddied. From this page we gather that you can set expectations on stubs, but they will never be enforced, and will therefore never make a test fail. On the other hand, you can use the
Repeat.Any()
method on a mocked call to basically make it behave like a stubbed method (if it was called it is ok, but if not that's ok too).

Further, in Rhino Mocks, when you create a stub of an object, all properties functions as you would normally expect them to (if you set a value X, you will get back X later on).

2007-08-11

NHibernate enum to char(1)

For a new project we are using NHibernate to implement database persistance. Today I ran into an issue with persisting enum's. By default, NHibernate uses type in the database and the enum's underlying type, in order to generate values. For example a simple enum like this:


public enum CoffeType
{
Sugar,
NoSugar
}

with this mapping file:

<property column="CoffeType" type="Business.Entities.CoffeType, Business.Core" name="CoffeType" null="true">

and a column in the database of type integer means that Sugar becomes 0 and NoSugar becomes 1. This is all fine, and works without problems at all. However, I wanted to use predetermined values for the enums, like this:


public enum CoffeType
{
Sugar = 'S',
NoSugar = 'N'
}


I changed the type in the database from integer to Char(1). This made NHibernate puke when inserting data. After digging into the generated sql-statements, it turned out that NHibernate choose to convert my enum-values into "Sugar" and "NoSugar" instead of 'S' and 'N'. This is probably due to recognizing any Char-type in the database as a String-datatype in .NET, and converting the enum's appropriately.

A few minutes of searching the net turned up the following post:

http://blog.bittercoder.com/CategoryView,category,NHibernate.aspx

.. which solved the exact same problem, with Castle's ActiveRecord instead of NHibernate. It turns out that the solution is called NHibernate User Types. This is what I ended up with in the mapping-file:

<property column="CoffeType" type="Business.Entities.CoffeTypeType, Business.Core" name="CoffeType" null="true" />

and this is the corresponding C#-class:


public class CoffeTypeType : NHibernate.Type.PersistentEnumType
{
public CoffeTypeType()
: base(typeof(CoffeType))
{
}

public override object GetValue(object code)
{
return Convert.ToChar(code);
}

public override object GetInstance(object code)
{
return (CoffeType) Enum.ToObject(typeof(CoffeType), (Byte) ((Char) GetValue(code)));
}

public override void Set(IDbCommand cmd, object value, int index)
{
IDataParameter par = (IDataParameter)cmd.Parameters[index];
par.DbType = DbType.StringFixedLength;

if (value == null) {
par.Value = DBNull.Value;
}
else {
par.Value = GetValue(value);
}
}

public new static SqlType GetUnderlyingSqlType(System.Type enumClass)
{
if (Enum.GetUnderlyingType(enumClass).FullName == "System.Char") {
return NHibernate.SqlTypes.SqlTypeFactory.GetString(1);
}
else {
return NHibernate.Type.PersistentEnumType.GetUnderlyingSqlType(enumClass);
}
}
}


This is the way to make NHibernate map an enum property to char(1) database field.

2007-04-06

First post!

Jaha, då har man skaffat sig en blogg då. Den här utrymmet på webben är tänkt att fungera som mitt andningshål ut mot omvärlden, helt enkelt ett sätt att försäkra sig om att samtida medmänniskor inte riskerar gå miste om min ofantliga intellektuella rikedom.

Namnet på bloggen? Radioskugga är/var en teve-serie med Figge Norling i huvudrollen, som jag och min mellanbror såg på med stort nöje någongång i mitten på 90-talet. Tyvärr verkar den inte komma i repris, men man kan ju alltid hoppas.

Vi får väl ser hurpass väluppdaterad den här bloggen egentligen blir, men jag ska verkligen försöka tänka på alla er hängivna fans där ute, om skrivarglöden skulle börja falna.