bosh_common 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,12 @@
3
3
  module Bosh::Exec
4
4
  # Raised when there was an error executing the command
5
5
  class Error < StandardError
6
- def initialize(status, command)
6
+ attr_reader :output
7
+
8
+ def initialize(status, command, output=nil)
7
9
  @status = status
8
10
  @command = command
11
+ @output = output
9
12
  end
10
13
 
11
14
  def message
@@ -15,5 +18,9 @@ module Bosh::Exec
15
18
  "command not found: #{@command}"
16
19
  end
17
20
  end
21
+
22
+ def to_s
23
+ message
24
+ end
18
25
  end
19
- end
26
+ end
data/lib/common/exec.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # Copyright (c) 2012 VMware, Inc.
2
2
 
3
+ module Bosh; end
4
+
3
5
  require "common/exec/result"
4
6
  require "common/exec/error"
5
7
 
@@ -57,7 +59,7 @@ module Bosh
57
59
 
58
60
  if result.failed?
59
61
  unless opts[:on_error] == :return
60
- raise Error.new(result.exit_status, command)
62
+ raise Error.new(result.exit_status, command, output)
61
63
  end
62
64
  yield result if block_given? && opts[:yield] == :on_false
63
65
 
@@ -14,6 +14,9 @@ module Bosh::Common
14
14
  # @return [Hash] Template properties
15
15
  attr_reader :properties
16
16
 
17
+ # @return [Hash] Raw template properties (no openstruct)
18
+ attr_reader :raw_properties
19
+
17
20
  # @return [Hash] Template spec
18
21
  attr_reader :spec
19
22
 
@@ -43,16 +46,46 @@ module Bosh::Common
43
46
  end
44
47
 
45
48
  # Property lookup helper
46
- # @param [String] name Property name
47
- # @param [optional, Object] default Default value
48
- # @return [Object] Property value
49
- def p(name, default = nil)
50
- result = lookup_property(@raw_properties, name)
51
- if result.nil?
52
- return default if default
53
- raise UnknownProperty.new(name)
49
+ #
50
+ # @overload p(name, default_value)
51
+ # Returns property value or default value if property not set
52
+ # @param [String] name Property name
53
+ # @param [Object] default_value Default value
54
+ # @return [Object] Property value
55
+ #
56
+ # @overload p(names, default_value)
57
+ # Returns first property from the list that is set or default value if
58
+ # none of them are set
59
+ # @param [Array<String>] names Property names
60
+ # @param [Object] default_value Default value
61
+ # @return [Object] Property value
62
+ #
63
+ # @overload p(names)
64
+ # Looks up first property from the list that is set, raises an error
65
+ # if none of them are set.
66
+ # @param [Array<String>] names Property names
67
+ # @return [Object] Property value
68
+ # @raise [Bosh::Common::UnknownProperty]
69
+ #
70
+ # @overload p(name)
71
+ # Looks up property and raises an error if it's not set
72
+ # @param [String] name Property name
73
+ # @return [Object] Property value
74
+ # @raise [Bosh::Common::UnknownProperty]
75
+ def p(*args)
76
+ names = args[0]
77
+ default_given = args.size > 1
78
+ default = args[1]
79
+
80
+ names = Array(names) unless names.kind_of?(Array)
81
+
82
+ names.each do |name|
83
+ result = lookup_property(@raw_properties, name)
84
+ return result if result
54
85
  end
55
- result
86
+
87
+ return default if default_given
88
+ raise UnknownProperty.new(name)
56
89
  end
57
90
 
58
91
  # Run a block of code if all given properties are defined
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Bosh
4
4
  module Common
5
- VERSION = "0.5.1"
5
+ VERSION = "0.5.3"
6
6
  end
7
7
  end
@@ -6,9 +6,9 @@ require "common/exec"
6
6
  describe Bosh::Exec do
7
7
  let(:opts) { {} }
8
8
 
9
- describe "existing command" do
9
+ context "existing command" do
10
10
 
11
- describe "executes successfully" do
11
+ context "executes successfully" do
12
12
  it "should not fail" do
13
13
  Bosh::Exec.sh("ls /", opts).failed?.should be_false
14
14
  end
@@ -22,11 +22,14 @@ describe Bosh::Exec do
22
22
  end
23
23
  end
24
24
 
25
- describe "fails to execute" do
25
+ context "fails to execute" do
26
26
  it "should raise error by default" do
27
- lambda {
27
+ expect {
28
28
  Bosh::Exec.sh("ls /asdasd 2>&1", opts)
29
- }.should raise_error Bosh::Exec::Error
29
+ }.to raise_error { |error|
30
+ error.should be_a Bosh::Exec::Error
31
+ error.output.should match /No such file or directory/
32
+ }
30
33
  end
31
34
 
32
35
  it "should yield block on false" do
@@ -46,32 +49,32 @@ describe Bosh::Exec do
46
49
 
47
50
  end
48
51
 
49
- describe "missing command" do
52
+ context "missing command" do
50
53
  it "should raise error by default" do
51
- lambda {
54
+ expect {
52
55
  Bosh::Exec.sh("/asdasd 2>&1", opts)
53
- }.should raise_error Bosh::Exec::Error
56
+ }.to raise_error Bosh::Exec::Error
54
57
  end
55
58
 
56
59
  it "should not raise error when requested" do
57
60
  opts[:on_error] = :return
58
- lambda {
61
+ expect {
59
62
  Bosh::Exec.sh("/asdasd 2>&1", opts)
60
- }.should_not raise_error Bosh::Exec::Error
63
+ }.to_not raise_error Bosh::Exec::Error
61
64
  end
62
65
 
63
66
  it "should execute block when requested" do
64
67
  opts[:yield] = :on_false
65
- lambda {
68
+ expect {
66
69
  Bosh::Exec.sh("/asdasd 2>&1", opts) do
67
70
  raise "foo"
68
71
  end
69
- }.should raise_error "foo"
72
+ }.to raise_error "foo"
70
73
  end
71
74
 
72
75
  end
73
76
 
74
- describe "mock" do
77
+ context "mock" do
75
78
  it "should be possible fake result" do
76
79
  cmd = "ls /"
77
80
  result = Bosh::Exec::Result.new(cmd, "output", 0)
@@ -81,13 +84,13 @@ describe Bosh::Exec do
81
84
  end
82
85
  end
83
86
 
84
- describe "module" do
87
+ context "module" do
85
88
  it "should be possible to invoke as a module" do
86
89
  Bosh::Exec.sh("ls /").success?.should be_true
87
90
  end
88
91
  end
89
92
 
90
- describe "include" do
93
+ context "include" do
91
94
  class IncludeTest
92
95
  include Bosh::Exec
93
96
  def run
@@ -32,6 +32,11 @@ describe Bosh::Common::TemplateEvaluationContext do
32
32
  eval_template("<%= properties.foo %>", @context).should == "bar"
33
33
  end
34
34
 
35
+ it "retains raw_properties" do
36
+ eval_template("<%= raw_properties['router']['token'] %>", @context).
37
+ should == "zbb"
38
+ end
39
+
35
40
  it "supports looking up template index" do
36
41
  eval_template("<%= spec.index %>", @context).should == "0"
37
42
  end
@@ -45,6 +50,32 @@ describe Bosh::Common::TemplateEvaluationContext do
45
50
  eval_template("<%= p('bar.baz', 22) %>", @context).should == "22"
46
51
  end
47
52
 
53
+ it "supports chaining property lookup via 'p' helper" do
54
+ eval_template(<<-TMPL, @context).strip.should == "zbb"
55
+ <%= p(%w(a b router.token c)) %>
56
+ TMPL
57
+
58
+ expect {
59
+ eval_template(<<-TMPL, @context)
60
+ <%= p(%w(a b c)) %>
61
+ TMPL
62
+ }.to raise_error(Bosh::Common::UnknownProperty)
63
+
64
+ eval_template(<<-TMPL, @context).strip.should == "22"
65
+ <%= p(%w(a b c), 22) %>
66
+ TMPL
67
+ end
68
+
69
+ it "allows 'false' and 'nil' defaults for 'p' helper" do
70
+ eval_template(<<-TMPL, @context).strip.should == "false"
71
+ <%= p(%w(a b c), false) %>
72
+ TMPL
73
+
74
+ eval_template(<<-TMPL, @context).strip.should == ""
75
+ <%= p(%w(a b c), nil) %>
76
+ TMPL
77
+ end
78
+
48
79
  it "supports 'if_p' helper" do
49
80
  template = <<-TMPL
50
81
  <% if_p("router.token") do |token| %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bosh_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-05 00:00:00.000000000 Z
12
+ date: 2012-12-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: BOSH common
15
15
  email: support@vmware.com
@@ -50,18 +50,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
50
  - - ! '>='
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
- segments:
54
- - 0
55
- hash: -411355085407901587
56
53
  required_rubygems_version: !ruby/object:Gem::Requirement
57
54
  none: false
58
55
  requirements:
59
56
  - - ! '>='
60
57
  - !ruby/object:Gem::Version
61
58
  version: '0'
62
- segments:
63
- - 0
64
- hash: -411355085407901587
65
59
  requirements: []
66
60
  rubyforge_project:
67
61
  rubygems_version: 1.8.24