summaryrefslogtreecommitdiff
path: root/spec/unit/knife/bootstrap_spec.rb
blob: de11a6ef15cc3fa94fcf7eb7172fceeaff7ed4c2 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
# Author:: Ian Meyer (<ianmmeyer@gmail.com>)
# Copyright:: Copyright (c) 2010 Ian Meyer
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

Chef::Knife::Bootstrap.load_deps
require 'net/ssh'

describe Chef::Knife::Bootstrap do
  before(:each) do
    Chef::Log.logger = Logger.new(StringIO.new)
    @knife = Chef::Knife::Bootstrap.new
    # Merge default settings in.
    @knife.merge_configs
    @knife.config[:template_file] = File.expand_path(File.join(CHEF_SPEC_DATA, "bootstrap", "test.erb"))
    @stdout = StringIO.new
    @knife.ui.stub!(:stdout).and_return(@stdout)
    @stderr = StringIO.new
    @knife.ui.stub!(:stderr).and_return(@stderr)
  end

  it "should return a name of default bootstrap template" do
    @knife.find_template.should be_a_kind_of(String)
  end

  it "should error if template can not be found" do
    @knife.config[:template_file] = false
    @knife.config[:distro] = 'penultimate'
    lambda { @knife.find_template }.should raise_error
  end

  it "should look for templates early in the run" do
    File.stub(:exists?).and_return(true)
    @knife.name_args = ['shatner']
    @knife.stub!(:read_template).and_return("")
    @knife.stub!(:knife_ssh).and_return(true)
    @knife_ssh = @knife.knife_ssh
    @knife.should_receive(:find_template).ordered
    @knife.should_receive(:knife_ssh).ordered
    @knife_ssh.should_receive(:run) # rspec appears to keep order per object
    @knife.run
  end

  it "should load the specified template" do
    @knife.config[:distro] = 'fedora13-gems'
    lambda { @knife.find_template }.should_not raise_error
  end

  it "should load the specified template from a Ruby gem" do
    @knife.config[:template_file] = false
    Gem.stub(:find_files).and_return(["/Users/schisamo/.rvm/gems/ruby-1.9.2-p180@chef-0.10/gems/knife-windows-0.5.4/lib/chef/knife/bootstrap/fake-bootstrap-template.erb"])
    File.stub(:exists?).and_return(true)
    IO.stub(:read).and_return('random content')
    @knife.config[:distro] = 'fake-bootstrap-template'
    lambda { @knife.find_template }.should_not raise_error
  end

  it "should return an empty run_list" do
    @knife.instance_variable_set("@template_file", @knife.config[:template_file])
    template_string = @knife.read_template
    @knife.render_template(template_string).should == '{"run_list":[]}'
  end

  it "should have role[base] in the run_list" do
    @knife.instance_variable_set("@template_file", @knife.config[:template_file])
    template_string = @knife.read_template
    @knife.parse_options(["-r","role[base]"])
    @knife.render_template(template_string).should == '{"run_list":["role[base]"]}'
  end

  it "should have role[base] and recipe[cupcakes] in the run_list" do
    @knife.instance_variable_set("@template_file", @knife.config[:template_file])
    template_string = @knife.read_template
    @knife.parse_options(["-r", "role[base],recipe[cupcakes]"])
    @knife.render_template(template_string).should == '{"run_list":["role[base]","recipe[cupcakes]"]}'
  end

  it "should have foo => {bar => baz} in the first_boot" do
    @knife.instance_variable_set("@template_file", @knife.config[:template_file])
    template_string = @knife.read_template
    @knife.parse_options(["-j", '{"foo":{"bar":"baz"}}'])
    expected_hash = Yajl::Parser.new.parse('{"foo":{"bar":"baz"},"run_list":[]}')
    actual_hash = Yajl::Parser.new.parse(@knife.render_template(template_string))
    actual_hash.should == expected_hash
  end

  it "should create a hint file when told to" do
    @knife.config[:template_file] = File.expand_path(File.join(CHEF_SPEC_DATA, "bootstrap", "test-hints.erb"))
    @knife.instance_variable_set("@template_file", @knife.config[:template_file])
    template_string = @knife.read_template
    @knife.parse_options(["--hint", "openstack"])
    @knife.render_template(template_string).should match /\/etc\/chef\/ohai\/hints\/openstack.json/
  end

  it "should populate a hint file with JSON when given a file to read" do
    @knife.stub(:find_template).and_return(true)
    @knife.config[:template_file] = File.expand_path(File.join(CHEF_SPEC_DATA, "bootstrap", "test-hints.erb"))
    ::File.stub!(:read).and_return('{ "foo" : "bar" }')
    @knife.instance_variable_set("@template_file", @knife.config[:template_file])
    template_string = @knife.read_template
    @knife.stub!(:read_template).and_return('{ "foo" : "bar" }')
    @knife.parse_options(["--hint", "openstack=hints/openstack.json"])
    @knife.render_template(template_string).should match /\{\"foo\":\"bar\"\}/
  end

  it "should take the node name from ARGV" do
    @knife.name_args = ['barf']
    @knife.name_args.first.should == "barf"
  end

  describe "specifying the encrypted data bag secret key" do
    subject(:knife) { described_class.new }
    let(:secret) { "supersekret" }
    let(:secret_file) { File.join(CHEF_SPEC_DATA, 'bootstrap', 'encrypted_data_bag_secret') }
    let(:options) { [] }
    let(:template_file) { File.expand_path(File.join(CHEF_SPEC_DATA, "bootstrap", "secret.erb")) }
    let(:rendered_template) do
      knife.instance_variable_set("@template_file", template_file)
      knife.parse_options(options)
      template_string = knife.read_template
      knife.render_template(template_string)
    end

    context "via --secret" do
      let(:options){ ["--secret", secret] }

      it "creates a secret file" do
        rendered_template.should match(%r{#{secret}})
      end

      it "renders the client.rb with an encrypted_data_bag_secret entry" do
        rendered_template.should match(%r{encrypted_data_bag_secret\s*"/etc/chef/encrypted_data_bag_secret"})
      end
    end

    context "via --secret-file" do
      let(:options) { ["--secret-file", secret_file] }
      let(:secret) { IO.read(secret_file) }

      it "creates a secret file" do
        rendered_template.should match(%r{#{secret}})
      end

      it "renders the client.rb with an encrypted_data_bag_secret entry" do
        rendered_template.should match(%r{encrypted_data_bag_secret\s*"/etc/chef/encrypted_data_bag_secret"})
      end
    end

    context "via Chef::Config[:encrypted_data_bag_secret]" do
      before(:each) { Chef::Config[:encrypted_data_bag_secret] = secret_file }
      let(:secret) { IO.read(secret_file) }

      it "creates a secret file" do
        rendered_template.should match(%r{#{secret}})
      end

      it "renders the client.rb with an encrypted_data_bag_secret entry" do
        rendered_template.should match(%r{encrypted_data_bag_secret\s*"/etc/chef/encrypted_data_bag_secret"})
      end
    end
  end

  describe "when configuring the underlying knife ssh command" do
    context "from the command line" do
      before do
        @knife.name_args = ["foo.example.com"]
        @knife.config[:ssh_user]      = "rooty"
        @knife.config[:ssh_port]      = "4001"
        @knife.config[:ssh_password]  = "open_sesame"
        Chef::Config[:knife][:ssh_user] = nil
        Chef::Config[:knife][:ssh_port] = nil
        @knife.config[:forward_agent] = true
        @knife.config[:identity_file] = "~/.ssh/me.rsa"
        @knife.stub!(:read_template).and_return("")
        @knife_ssh = @knife.knife_ssh
      end

      it "configures the hostname" do
        @knife_ssh.name_args.first.should == "foo.example.com"
      end

      it "configures the ssh user" do
        @knife_ssh.config[:ssh_user].should == 'rooty'
      end

      it "configures the ssh password" do
        @knife_ssh.config[:ssh_password].should == 'open_sesame'
      end

      it "configures the ssh port" do
        @knife_ssh.config[:ssh_port].should == '4001'
      end

      it "configures the ssh agent forwarding" do
        @knife_ssh.config[:forward_agent].should == true
      end

      it "configures the ssh identity file" do
        @knife_ssh.config[:identity_file].should == '~/.ssh/me.rsa'
      end
    end
    context "validating use_sudo_password" do
      before do
        @knife.config[:distro] = "ubuntu"
        @knife.config[:ssh_password] = "password"
        @knife.stub(:read_template).and_return(IO.read(@knife.find_template).chomp)
      end

      it "use_sudo_password contains description and long params for help" do
        @knife.options.should have_key(:use_sudo_password) \
          and @knife.options[:use_sudo_password][:description].to_s.should_not == ''\
          and @knife.options[:use_sudo_password][:long].to_s.should_not == ''
      end

      it "uses the password from --ssh-password for sudo when --use-sudo-password is set" do
        @knife.config[:use_sudo] = true
        @knife.config[:use_sudo_password] = true
        @knife.ssh_command.should include("echo #{@knife.config[:ssh_password]} | sudo -S")
      end

      it "should not honor --use-sudo-password when --use-sudo is not set" do
        @knife.config[:use_sudo] = false
        @knife.config[:use_sudo_password] = true
        @knife.ssh_command.should_not include("echo #{@knife.config[:ssh_password]} | sudo -S")
      end
    end
    context "from the knife config file" do
      before do
        @knife.name_args = ["config.example.com"]
        @knife.config[:ssh_user] = nil
        @knife.config[:ssh_port] = nil
        @knife.config[:ssh_gateway] = nil
        @knife.config[:forward_agent] = nil
        @knife.config[:identity_file] = nil
        @knife.config[:host_key_verify] = nil
        Chef::Config[:knife][:ssh_user] = "curiosity"
        Chef::Config[:knife][:ssh_port] = "2430"
        Chef::Config[:knife][:forward_agent] = true
        Chef::Config[:knife][:identity_file] = "~/.ssh/you.rsa"
        Chef::Config[:knife][:ssh_gateway] = "towel.blinkenlights.nl"
        Chef::Config[:knife][:host_key_verify] = true
        @knife.stub!(:read_template).and_return("")
        @knife_ssh = @knife.knife_ssh
      end

      it "configures the ssh user" do
        @knife_ssh.config[:ssh_user].should == 'curiosity'
      end

      it "configures the ssh port" do
        @knife_ssh.config[:ssh_port].should == '2430'
      end

      it "configures the ssh agent forwarding" do
        @knife_ssh.config[:forward_agent].should == true
      end

      it "configures the ssh identity file" do
        @knife_ssh.config[:identity_file].should == '~/.ssh/you.rsa'
      end

      it "configures the ssh gateway" do
        @knife_ssh.config[:ssh_gateway].should == 'towel.blinkenlights.nl'
      end

      it "configures the host key verify mode" do
        @knife_ssh.config[:host_key_verify].should == true
      end
    end

    describe "when falling back to password auth when host key auth fails" do
      before do
        @knife.name_args = ["foo.example.com"]
        @knife.config[:ssh_user]      = "rooty"
        @knife.config[:identity_file] = "~/.ssh/me.rsa"
        @knife.stub!(:read_template).and_return("")
        @knife_ssh = @knife.knife_ssh
      end

      it "prompts the user for a password " do
        @knife.stub!(:knife_ssh).and_return(@knife_ssh)
        @knife_ssh.stub!(:get_password).and_return('typed_in_password')
        alternate_knife_ssh = @knife.knife_ssh_with_password_auth
        alternate_knife_ssh.config[:ssh_password].should == 'typed_in_password'
      end

      it "configures knife not to use the identity file that didn't work previously" do
        @knife.stub!(:knife_ssh).and_return(@knife_ssh)
        @knife_ssh.stub!(:get_password).and_return('typed_in_password')
        alternate_knife_ssh = @knife.knife_ssh_with_password_auth
        alternate_knife_ssh.config[:identity_file].should be_nil
      end
    end
  end

  describe "when running the bootstrap" do
    before do
      @knife.name_args = ["foo.example.com"]
      @knife.config[:ssh_user]      = "rooty"
      @knife.config[:identity_file] = "~/.ssh/me.rsa"
      @knife.stub!(:read_template).and_return("")
      @knife_ssh = @knife.knife_ssh
      @knife.stub!(:knife_ssh).and_return(@knife_ssh)
    end

    it "verifies that a server to bootstrap was given as a command line arg" do
      @knife.name_args = nil
      lambda { @knife.run }.should raise_error(SystemExit)
      @stderr.string.should match /ERROR:.+FQDN or ip/
    end

    it "configures the underlying ssh command and then runs it" do
      @knife_ssh.should_receive(:run)
      @knife.run
    end

    it "falls back to password based auth when auth fails the first time" do
      @knife.stub!(:puts)

      @fallback_knife_ssh = @knife_ssh.dup
      @knife_ssh.should_receive(:run).and_raise(Net::SSH::AuthenticationFailed.new("no ssh for you"))
      @knife.stub!(:knife_ssh_with_password_auth).and_return(@fallback_knife_ssh)
      @fallback_knife_ssh.should_receive(:run)
      @knife.run
    end

    context "Chef::Config[:encrypted_data_bag_secret] is set" do
      let(:secret_file) { File.join(CHEF_SPEC_DATA, 'bootstrap', 'encrypted_data_bag_secret') }
      before { Chef::Config[:encrypted_data_bag_secret] = secret_file }

      it "warns the configuration option is deprecated" do
        @knife_ssh.should_receive(:run)
        @knife.ui.should_receive(:warn).at_least(3).times
        @knife.run
      end
    end

  end

end