Commits

Kaspar Schiess committed 04fc095

A spec for Shares#list

Comments (0)

Files changed (8)

 
 group :test do
   gem 'rspec'
+  gem 'flexmock'
 end
   specs:
     clamp (0.5.0)
     diff-lcs (1.1.3)
+    flexmock (1.2.0)
     mixlib-shellout (1.1.0)
     rspec (2.12.0)
       rspec-core (~> 2.12.0)
 
 DEPENDENCIES
   clamp
+  flexmock
   mixlib-shellout
   rspec
 
 require 'pp'
 require 'clamp'
-require 'mixlib/shellout'
 
 $:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
 require 'iscsi'
     def execute
       shares = ISCSI::Shares.new
       shares.list do |target|
-        pp target
+        puts target.name
+        puts target.groups.join('\n')
+        puts
       end
     end
   end

lib/iscsi/iscsi.rb

     end
 
     def list
-      stmf_targets.each do |node| 
-        yield Target.new(node)
+      stmf_targets.map do |node| 
+        t = Target.new(node)
+        
+        yield t if block_given?
+        
+        t
       end
     end
     def stmf_targets
     def initialize(node)
       @node = node
     end
+    
+    def name
+      @node.value
+    end
   end
 end
+require 'mixlib/shellout'
+
 class Runner
   class CommandFailed < StandardError
     def initialize(msg, command)

spec/fixture/target_list.txt

+Target: foo
+    Operational Status: Online
+    Provider Name     : iscsit
+    Alias             : -
+    Protocol          : iSCSI
+    Sessions          : 1
+        Initiator: iqn.1986-03.com.sun:01:80cabf310dff.506dfcc3
+            Alias: oos2
+            Logged in since: Fri Dec 14 16:40:04 2012
+Target: bar
+    Operational Status: Online
+    Provider Name     : iscsit
+    Alias             : -
+    Protocol          : iSCSI
+    Sessions          : 1
+        Initiator: iqn.1986-03.com.sun:01:40f581320dff.506ef076
+            Alias: oos1
+            Logged in since: Fri Dec 14 16:34:24 2012

spec/lib/iscsi/shares_spec.rb

+require 'spec_helper'
+
+require 'iscsi'
+
+class FakeRunner
+  def initialize(map={})
+    @map = map
+  end
+  
+  def shell cmd
+    @map.each do |re, out| 
+      if re.match(cmd)
+        return out
+      end
+    end
+    
+    fail "No such fixture (#{cmd.inspect})"
+  end
+end
+
+describe ISCSI::Shares do
+  slet(:shares) { described_class.new }
+  
+  let(:fake_runner) { FakeRunner.new(
+    /^stmfadm list-target/ => fixture('target_list.txt')) }
+  before(:each) { 
+    flexmock(shares, runner: fake_runner) }
+  
+  describe '#list' do
+    slet(:results) { shares.list }
+    describe 'results[0]' do
+      slet(:share) { results[0] }
+      
+      its(:name) { should == 'foo' }
+    end
+  end
+end

spec/spec_helper.rb

 def slet(name, &block)
   let name, &block
   subject { self.send(name) }
+end
+
+def fixture *args
+  File.read fixture_path(*args)
+end
+def fixture_path *args
+  File.expand_path(File.join(File.dirname(__FILE__) + "/fixture", *args))
+end
+
+RSpec.configure do |config| 
+  config.mock_with :flexmock
 end