Fix target host variable being overwritten

Also modify relevant test to cover this.

Resolves bug 1133204

Change-Id: I517b2dc19d9561723391150105b4943b56dba637