summaryrefslogtreecommitdiff
path: root/spec/unit/provider/zone/solaris_spec.rb
blob: 393df6b5158f95d60e65e4ff70e22595bc410111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#! /usr/bin/env ruby
require 'spec_helper'

describe Puppet::Type.type(:zone).provider(:solaris) do
  let(:resource) { Puppet::Type.type(:zone).new(:name => 'dummy', :path => '/', :provider => :solaris) }
  let(:provider) { described_class.new(resource) }

  context "#configure" do
    it "should add the create args to the create str" do
      resource.stubs(:properties).returns([])
      resource[:create_args] = "create_args"
      provider.expects(:setconfig).with("create -b create_args")
      provider.configure
    end
    it "should add the create args to the create str" do
      iptype = stub "property"
      iptype.stubs(:name).with().returns(:iptype)
      iptype.stubs(:safe_insync?).with(iptype).returns(false)
      provider.stubs(:properties).returns({:iptype => iptype})
      resource.stubs(:properties).with().returns([iptype])
      resource[:create_args] = "create_args"
      provider.expects(:setconfig).with("create -b create_args\nset ip-type=shared")
      provider.configure
    end
  end

  context "#install" do
    context "clone" do
      it "should call zoneadm" do
        provider.expects(:zoneadm).with(:install)
        provider.install
      end

      it "with the resource's clone attribute" do
        resource[:clone] = :clone_argument
        provider.expects(:zoneadm).with(:clone, :clone_argument)
        provider.install
      end
    end

    context "not clone" do
      it "should just install if there are no install args" do
        # there is a nil check in type.rb:[]= so we cannot directly set nil.
        resource.stubs(:[]).with(:clone).returns(nil)
        resource.stubs(:[]).with(:install_args).returns(nil)
        provider.expects(:zoneadm).with(:install)
        provider.install
      end

      it "should add the install args to the command if they exist" do
        # there is a nil check in type.rb:[]= so we cannot directly set nil.
        resource.stubs(:[]).with(:clone).returns(nil)
        resource.stubs(:[]).with(:install_args).returns('install args')
        provider.expects(:zoneadm).with(:install, ["install", "args"])
        provider.install
      end
    end
  end
  context "#instances" do
    it "should list the instances correctly" do
      described_class.expects(:adm).with(:list, "-cp").returns("0:dummy:running:/::native:shared")
      instances = described_class.instances.map { |p| {:name => p.get(:name), :ensure => p.get(:ensure)} }
      instances.size.should == 1
      instances[0].should == {
        :name=>"dummy",
        :ensure=>:running,
      }
    end
  end
  context "#setconfig" do
    it "should correctly set configuration" do
      provider.expects(:command).with(:cfg).returns('/usr/sbin/zonecfg')
      provider.expects(:exec_cmd).with(:input => "set zonepath=/\ncommit\nexit", :cmd => '/usr/sbin/zonecfg -z dummy -f -').returns({:out=>'', :exit => 0})
      provider.setconfig("set zonepath=\/")
      provider.flush
    end

    it "should correctly warn on 'not allowed'" do
      provider.expects(:command).with(:cfg).returns('/usr/sbin/zonecfg')
      provider.expects(:exec_cmd).with(:input => "set zonepath=/\ncommit\nexit", :cmd => '/usr/sbin/zonecfg -z dummy -f -').returns({:out=>"Zone z2 already installed; set zonepath not allowed.\n", :exit => 0})
      provider.setconfig("set zonepath=\/")
      expect {
        provider.flush
      }.to raise_error(ArgumentError, /Failed to apply configuration/)
    end
  end
  context "#getconfig" do
    zone_info =<<-EOF
zonename: dummy
zonepath: /dummy/z
brand: native
autoboot: true
bootargs:
pool:
limitpriv:
scheduling-class:
ip-type: shared
hostid:
net:
        address: 1.1.1.1
        physical: ex0001
        defrouter not specified
net:
        address: 1.1.1.2
        physical: ex0002
        defrouter not specified
    EOF
    it "should correctly parse zone info" do
      provider.expects(:zonecfg).with(:info).returns(zone_info)
      provider.getconfig.should == {
        :brand=>"native",
        :autoboot=>"true",
        :"ip-type"=>"shared",
        :zonename=>"dummy",
        "net"=>[{:physical=>"ex0001", :address=>"1.1.1.1"}, {:physical=>"ex0002", :address=>"1.1.1.2"}],
        :zonepath=>"/dummy/z"
      }
    end
  end
  context "#flush" do
    it "should correctly execute pending commands" do
      provider.expects(:command).with(:cfg).returns('/usr/sbin/zonecfg')
      provider.expects(:exec_cmd).with(:input => "set iptype=shared\ncommit\nexit", :cmd => '/usr/sbin/zonecfg -z dummy -f -').returns({:out=>'', :exit => 0})
      provider.setconfig("set iptype=shared")
      provider.flush
    end

    it "should correctly raise error on failure" do
      provider.expects(:command).with(:cfg).returns('/usr/sbin/zonecfg')
      provider.expects(:exec_cmd).with(:input => "set iptype=shared\ncommit\nexit", :cmd => '/usr/sbin/zonecfg -z dummy -f -').returns({:out=>'', :exit => 1})
      provider.setconfig("set iptype=shared")
      expect {
        provider.flush
      }.to raise_error(ArgumentError, /Failed to apply/)
    end
  end
  context "#start" do
    it "should not require path if sysidcfg is specified" do
      resource[:path] = '/mypath'
      resource[:sysidcfg] = 'dummy'
      Puppet::FileSystem.stubs(:exist?).with('/mypath/root/etc/sysidcfg').returns true
      File.stubs(:directory?).with('/mypath/root/etc').returns true
      provider.expects(:zoneadm).with(:boot)
      provider.start
    end

    it "should require path if sysidcfg is specified" do
      resource.stubs(:[]).with(:path).returns nil
      resource.stubs(:[]).with(:sysidcfg).returns 'dummy'
      expect {
        provider.start
      }.to raise_error(Puppet::Error, /Path is required/)
    end
  end
  context "#line2hash" do
    it "should parse lines correctly" do
      described_class.line2hash('0:dummy:running:/z::native:shared').should == {:ensure=>:running, :iptype=>"shared", :path=>"/z", :name=>"dummy", :id=>"0"}
    end
    it "should parse lines correctly(2)" do
      described_class.line2hash('0:dummy:running:/z:ipkg:native:shared').should == {:ensure=>:running, :iptype=>"shared", :path=>"/z", :name=>"dummy", :id=>"0"}
    end
    it "should parse lines correctly(3)" do
      described_class.line2hash('-:dummy:running:/z:ipkg:native:shared').should == {:ensure=>:running, :iptype=>"shared", :path=>"/z", :name=>"dummy"}
    end
    it "should parse lines correctly(3)" do
      described_class.line2hash('-:dummy:running:/z:ipkg:native:exclusive').should == {:ensure=>:running, :iptype=>"exclusive", :path=>"/z", :name=>"dummy"}
    end
  end
  context "#multi_conf" do
    it "should correctly add and remove properties" do
      provider.stubs(:properties).with().returns({:ip => ['1.1.1.1', '2.2.2.2']})
      should = ['1.1.1.1', '3.3.3.3']
      p = Proc.new do |a, str|
        case a
        when :add; 'add:' + str
        when :rm; 'rm:' + str
        end
      end
      provider.multi_conf(:ip, should, &p).should == "rm:2.2.2.2\nadd:3.3.3.3"
    end
  end
  context "single props" do
    {:iptype => /set ip-type/, :autoboot => /set autoboot/, :path => /set zonepath/, :pool => /set pool/, :shares => /add rctl/}.each do |p, v|
      it "#{p.to_s}: should correctly return conf string" do
        provider.send(p.to_s + '_conf', 'dummy').should =~ v
      end
      it "#{p.to_s}: should correctly set property string" do
        provider.expects((p.to_s + '_conf').intern).returns('dummy')
        provider.expects(:setconfig).with('dummy').returns('dummy2')
        provider.send(p.to_s + '=', 'dummy').should == 'dummy2'
      end

    end
  end
end