N5ken's technical blog

Just another WordPress.com site

Monthly Archives: June 2011

Isolate your test environment in VM Desktop from the intranet of your company

Due to the increasing number of hosts and devises we are using now in our company, so it’s more easier to cause IP conflict than ever, to avoid affecting the test result, it’s better to separate our network from intranet.

Summary:
It’s a huge benefit for using the virtual network, on the one hand, it saving the precious IP address, on the other hand, the virtual network is more stable for the intranet, so this makes our test result more reliable! Especially in HA, federation, test automation and so on.

In the following tutorial, we will take a look at how to manage our NAT network:

1. After we finish installing the VM Workstation, we should see two more virtual network interface cards in “Network and Sharing Center”

2. The network structure:

3. So firstly, change the network connection setting of our virtual machine:

4. Secondly, restart network interface card or run “ipconfig /renew” to connect to VMnet08 (here is need to confirm that we get a C class IP address, e.g. 192.168.x.x)

5. Lastly, for testing the connectivity, we can ping each other by the host name, and our machine will send ping packages to the network 10.30.x.x and 192.168.x.x.

6. we need to be sure we are pinging the machine in VMnet 08.

7. If you are unable to connect to the internet, please check the gateway of your VM machine, it should be 192.168.133.2 normally.

[Cheat Sheet] Ruby’s Exception Hierarchy

Well, you could always use Ruby itself, too, that way you’ll always have an up-to-date list:

exceptions = []
tree = {}
ObjectSpace.each_object(Class) do |cls|
  next unless cls.ancestors.include? Exception
  next if exceptions.include? cls
  next if cls.superclass == SystemCallError # avoid dumping Errno's
  exceptions << cls
  cls.ancestors.delete_if {|e| [Object, Kernel].include? e }.reverse.inject(tree) {|memo,cls| memo[cls] ||= {}}
end

indent = 0
tree_printer = Proc.new do |t|
  t.keys.sort { |c1,c2| c1.name  c2.name }.each do |k|
    space = (' ' * indent); space ||= ''
    puts space + k.to_s
    indent += 2; tree_printer.call t[k]; indent -= 2
  end
end
tree_printer.call tree

The hierarchy:

Exception
 NoMemoryError
 ScriptError
   LoadError
   NotImplementedError
   SyntaxError
 SignalException
   Interrupt
 StandardError
   ArgumentError
   IOError
     EOFError
   IndexError
   LocalJumpError
   NameError
     NoMethodError
   RangeError
     FloatDomainError
   RegexpError
   RuntimeError
   SecurityError
   SystemCallError
   SystemStackError
   ThreadError
   TypeError
   ZeroDivisionError
 SystemExit
 fatal

Using Mustache in Rails 3

Mustache is …

So, for binding mustache in your Rails3 project, add the following gems to you Gemfile

gem 'mustache'
gem 'mustache_rails3'

You can enable the mustache template handler by running

rails g mustache:install

Then in RAILS_ROOT/config/initializers/mustache.rb

rails g mustache:install
# Be sure to install mustache gem and include mustache gem in project Gemfile.
# Template Handler
require 'mustache_rails'
# Generator
Rails.application.config.generators.template_engine :mustache
# Need to change dynamically when the theme changed
Mustache::Rails::Config.template_base_path = Rails.root.join('themes', 'n5ken', 'templates')
Mustache::Rails::Config.shared_path        = Rails.root.join('themes', 'n5ken', 'templates', 'shared')

# You can change these defaults in, say, a Rails initializer or
# environment.rb, e.g.:
#
# Mustache::Rails::Config.template_base_path = Rails.root.join('app', 'templates')
module Config
  def self.template_base_path
    @template_base_path ||= ::Rails.root.join('app', 'templates')
  end
  def self.template_base_path=(value)
    @template_base_path = value
  end
  def self.template_extension
    @template_extension ||= 'html.mustache'
  end
  def self.template_extension=(value)
    @template_extension = value
  end
  def self.shared_path
    @shared_path ||= ::Rails.root.join('app', 'templates', 'shared')
  end
  def self.shared_path=(value)
    @shared_path = value
  end
end

If we run scaffold to create a product model, we will get the response messages as follow:

$:/var/projects/template# rails g scaffold Product title:string description:text
      invoke  active_record
      create    db/migrate/20110703090715_create_products.rb
      create    app/models/product.rb
      invoke    test_unit
      create      test/unit/product_test.rb
      create      test/fixtures/products.yml
       route  resources :products
      invoke  scaffold_controller
      create    app/controllers/products_controller.rb
      invoke    mustache
      create      app/views/products
      create      app/views/products/index.rb
      create      app/templates/products/index.html.mustache
      create      app/views/products/edit.rb
      create      app/templates/products/edit.html.mustache
      create      app/views/products/show.rb
      create      app/templates/products/show.html.mustache
      create      app/views/products/new.rb
      create      app/templates/products/new.html.mustache
      create      app/templates/products/_form.html.mustache
      invoke    test_unit
      create      test/functional/products_controller_test.rb
      invoke    helper
      create      app/helpers/products_helper.rb
      invoke      test_unit
      create        test/unit/helpers/products_helper_test.rb
      invoke  stylesheets
      create    public/stylesheets/scaffold.css

From the message, it will generate mainly three folders: controller, view, templates.

TCP and IP Datagram

For reviewing the concept and the structure of TCP, UDP, IP and HTTP, here are some diagram for reference, and will have further posts for discussion.

Getting specific revision via http/https with VisualSVN Server

For most of my test automation projects, I want to attach the link to the source scripts in the test report, so I need to query the specific revision via http or https, after a few minute searching on the web, I found a solution in stackoverflow.com, adding a URL parameter “?p=Rev” will solve the problem:

e.g.

https://zhu11174.prod.quest.corp/svn/workbot/?p=300

Will retrieve the revision 300 of the codes.

refer link: http://stackoverflow.com/questions/157603/getting-specific-revision-via-http-with-visualsvn-server

Installing REE by RVM in Ubuntu

Installing the REE by RVM in Ubuntu may have the following error, this because you missed some dependencies:

root@ubuntu:~# rvm install ree
Installing Ruby Enterprise Edition from source to: /usr/local/rvm/rubies/ree-1.8.7-2011.03
ree-1.8.7-2011.03 - #fetching (ruby-enterprise-1.8.7-2011.03)
curl: /usr/local/staf/lib/libcrypto.so.0.9.8: no version information available (required by /usr/lib/libcurl.so.4)
curl: /usr/local/staf/lib/libssl.so.0.9.8: no version information available (required by /usr/lib/libcurl.so.4)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 7713k  100 7713k    0     0  57223      0  0:02:18  0:02:18 --:--:-- 50726
ree-1.8.7-2011.03 - #extracting ruby-enterprise-1.8.7-2011.03 to /usr/local/rvm/src/ree-1.8.7-2011.03
ree-1.8.7-2011.03 - #installing 
ERROR: Error running './installer -a /usr/local/rvm/rubies/ree-1.8.7-2011.03  --dont-install-useful-gems ', please read /usr/local/rvm/log/ree-1.8.7-2011.03/install.log
ERROR: There has been an error while trying to run the ree installer. Halting the installation.

So, now you just need:

sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline6-dev

A Very Simplified Description of TCP 3-Way Handshake (SYN,SYN-ACK,ACK)

TCP 3-Way Handshake is used to establish TCP socket connections and tear down TCP socket connections over the network.

3 way handshake

From this image, we can see the TCP’s three-way handshaking technique is referred to as the 3-way handshake: SYN, SYN-ACK, ACK, SYNchronize and ACKnowledge messages are indicated by a bit inside the header of the TCP segment.

When the communication between two computers ends, another 3-way communication is performed to tear down the TCP socket connection. This setup and teardown of a TCP socket connection is part of what qualifies TCP a reliable protocol. TCP also acknowledges that data is successfully received and guarantees the data is reassembled in the correct order.

Note that UDP is connectionless. That means UDP doesn’t establish connections as TCP does, so UDP does not perform this 3-way handshake and for this reason, it is referred to as an unreliable protocol.

Useful commands in RVM

RVM is a good tool for managing Ruby and Gems, so the following codes shows you how to use:

Ruby Management

Use a particular Ruby

$ rvm use 1.8.6

Equivalently (because of RVM’s defaults):

$ rvm 1.8.6

List Ruby interpreters available for installation

$ rvm list known
  # MRI Rubies
  [ruby-]1.8.6[-p399]
  [ruby-]1.8.6-head
  [ruby-]1.8.7[-p330]
  [ruby-]1.8.7-head
  ...bla bla bla

List Ruby interpreters you’ve already installed

$ rvm list

   ruby-1.8.6-p383:  ruby 1.8.6 (2009-08-04 patchlevel 383) [i686-darwin10.0.0]
   ruby-1.9.1-head:  ruby 1.9.2dev (2009-10-30 trunk 25559) [x86_64-darwin10.0.0]
   ...bla bla bla

Ruby information for the current shell

$ rvm info

rvm 0.0.49 (2009.09.24) [http://rvm.beginrescueend.com/]

ruby:
interpreter:  "ruby"
version:      "1.8.6"
...bla bla bla

Install a specific Ruby version, e.g.

$  rvm install jruby

or

$  rvm install 1.9.2

Switch to gems directory for current ruby

$ rvm gemdir

/Users/wayne/.gem/ruby/1.9.1

Switch to the system gems directory

$ rvm gemdir system

/Library/Ruby/Gems/1.8

Use the user set default ruby

$  rvm default

Use the system ruby (as if no rvm)

$  rvm system

Reset to pre-RVM state.

$  rvm reset

Uninstall RVM installed 1.8.7 version

$  rvm uninstall 1.8.7

Named Gem Sets
Examples of creating and changing gem sets

$ rvm 1.9.2-head
$ gem install rails -v 2.3.3

$ rvm gemset create rails222 rails126
Gemset 'rails222' created.
Gemset 'rails126' created.

$ rvm 1.9.2-head@rails222
$ gem install rails -v 2.2.2

$ rvm 1.9.2-head@rails126
$ gem install rails -v 1.2.6

$ rvm 1.8.7
$ gem install rails -v 1.2.3

If you are deploying to a server or you do not want to wait around for rdoc and ri to install for each gem then you can disable these for gem installs and updates. Place these two lines in your ~/.gemrc or /etc/gemrc:

install: --no-rdoc --no-ri
update: --no-rdoc --no-ri

First play of RVM (Ruby Version Manager)

Due to the development environment is becoming much more complex than ever, the gems now may have conflict between some projects, so RVM is what exactly you need!

So RVM is not Ruby Virtual Machine, it’s Ruby Version Manager which is used to separate your various Ruby environment, by Using the RVM, you are able to install, uninstall difference Ruby, e.g. Ruby 1.8.x, 1.9.x, REE, IronRuby, and managing gems..

RVM is cool, right? so now, i think you can’t wait to have a try :)

Installing RVM is quite easy, firstly, you need to confirm the following dependencies, my OS is Ubuntu 10.10:

cURL (apt-get install curl)

If you are ready, you just need to execute this command to finish the installation:

$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

and add the line below to your bash profile, for example in Ubuntu is “/etc/profile”

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.

Back to the command line, run “rvm -v”, and you should see the version information:

$ source /etc/profile
$ rvm -v
rvm 1.6.20 by Wayne E. Seguin (wayneeseguin@gmail.com) [https://rvm.beginrescueend.com/]

So, by now, you have finished the installation of RVM, congratulation!!!

Some useful HTTP return codes

This article is from: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

10 Status Code DefinitionsEach Status-Code is described below, including a description of which method(s) it can follow and any metainformation required in the response.

Here are some codes we may see usually:

200 OK

304 Not Modified

400 Bad Request

403 Forbidden

404 Not Found

500 Internal Server Error

502 Bad Gateway

503 Service Unavailable

504 Gateway Timeout
Follow

Get every new post delivered to your Inbox.