jproulx

Cucumber Vagrant steps.rb

Mar 17th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.94 KB | None | 0 0
  1. # note this requires vagrant ~>1.0
  2. # sahara as shipped requires vagrant ~>0.9
  3. # forcing sahara's vagrant dependency to >0.9 seems to make it go
  4. # though I don't know what it may break
  5. # evolving at https://github.com/jon-proulx/cucumber-vagrant-sahara
  6. require 'rubygems'                                                              
  7. require 'vagrant'                                                              
  8. require 'sahara'                                                                
  9.                                                                                
  10. class VagrantBox                                                                
  11.   attr_reader :env, :cwd                                                        
  12.   def initialize(basebox)                                                      
  13.     #brittle code...                                                            
  14.     @name=basebox                                                              
  15.     @cwd=File.join("/scratch/vagrant-boxen",@name)                              
  16.     @env=Vagrant::Environment.new(:cwd =>@cwd)                                  
  17.                                                                                
  18.     Dir.mkdir(@cwd) unless File::directory?(@cwd)                              
  19.     File.open(File.join(@cwd,"Vagrantfile"),"w+") do |conf_file|        
  20.       conf_file.write """Vagrant::Config.run do |config|                        
  21.  config.vm.box = \'#{@name}\'                                                  
  22. end
  23. """                                                                            
  24.     end                                                                        
  25.   end                                                                          
  26.                                                                                
  27.   def up                                                                        
  28.     # We might be created but suspended too, need to handle more cases          
  29.     if @env.primary_vm.state != :running                                        
  30.       print "Booting #{@name} patience..."                                      
  31.       @env.cli("up")                                                            
  32.       puts "done"                                                              
  33.     end                                                                        
  34.     raise "Failed to start vm #{@name} " if @env.primary_vm.state != :running  
  35.   end                                                                          
  36.                                                                                
  37.   def freeze                                                                    
  38.     @env.cli("sandbox","on")                                                    
  39.   end                                                                          
  40.                                                                                
  41.   def rollback                                                                  
  42.     @env.cli("sandbox","rollback")                                              
  43.   end                                                                          
  44. end                                                                            
  45.                                                                                
  46. Given /^a vagrant vm (\w+) (?:is running|is up|)$/ do |box|                    
  47.   @mybox=VagrantBox.new(box)                                                    
  48.   @mybox.up                                                                    
  49. end                                                                            
  50.                                                                                
  51. Given /^I freeze the vm$/ do                                                    
  52.   @mybox.env.cli("sandbox","on")                                                
  53. end
Advertisement
Add Comment
Please, Sign In to add comment