summaryrefslogtreecommitdiff
path: root/spec/unit/knife/bootstrap/client_builder_spec.rb
blob: 879fac71cac2b6090ba898d538b94367f4b9c2c1 (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
#
# Author:: Lamont Granquist <lamont@chef.io>)
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
# 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'


describe Chef::Knife::Bootstrap::ClientBuilder do

  let(:stdout) { StringIO.new }
  let(:stderr) { StringIO.new }
  let(:stdin) { StringIO.new }
  let(:ui) { Chef::Knife::UI.new(stdout, stderr, stdin, {}) }

  let(:knife_config) { {} }

  let(:chef_config) { {} }

  let(:node_name) { "bevell.wat" }

  let(:rest) { double("Chef::REST") }

  let(:client_builder) {
    client_builder = Chef::Knife::Bootstrap::ClientBuilder.new(knife_config: knife_config, chef_config: chef_config, ui: ui)
    allow(client_builder).to receive(:rest).and_return(rest)
    allow(client_builder).to receive(:node_name).and_return(node_name)
    client_builder
  }

  context "#sanity_check!" do
    let(:response_404) { OpenStruct.new(:code => '404') }
    let(:exception_404) { Net::HTTPServerException.new("404 not found", response_404) }

    context "in cases where the prompting fails" do
      before do
        # should fail early in #run
        expect(client_builder).to_not receive(:create_client!)
        expect(client_builder).to_not receive(:create_node!)
      end

      it "exits when the node exists and the user does not want to delete" do
        expect(rest).to receive(:get_rest).with("nodes/#{node_name}")
        expect(ui.stdin).to receive(:readline).and_return('n')
        expect { client_builder.run }.to raise_error(SystemExit)
      end

      it "exits when the client exists and the user does not want to delete" do
        expect(rest).to receive(:get_rest).with("nodes/#{node_name}").and_raise(exception_404)
        expect(rest).to receive(:get_rest).with("clients/#{node_name}")
        expect(ui.stdin).to receive(:readline).and_return('n')
        expect { client_builder.run }.to raise_error(SystemExit)
      end
    end

    context "in cases where the prompting succeeds" do
      before do
        # mock out the rest of #run
        expect(client_builder).to receive(:create_client!)
        expect(client_builder).to receive(:create_node!)
      end

      it "when both the client and node do not exist it succeeds" do
        expect(rest).to receive(:get_rest).with("nodes/#{node_name}").and_raise(exception_404)
        expect(rest).to receive(:get_rest).with("clients/#{node_name}").and_raise(exception_404)
        expect { client_builder.run }.not_to raise_error
      end

      it "when we are allowed to delete an old node" do
        expect(rest).to receive(:get_rest).with("nodes/#{node_name}")
        expect(ui.stdin).to receive(:readline).and_return('y')
        expect(rest).to receive(:get_rest).with("clients/#{node_name}").and_raise(exception_404)
        expect(rest).to receive(:delete).with("nodes/#{node_name}")
        expect { client_builder.run }.not_to raise_error
      end

      it "when we are allowed to delete an old client" do
        expect(rest).to receive(:get_rest).with("nodes/#{node_name}").and_raise(exception_404)
        expect(rest).to receive(:get_rest).with("clients/#{node_name}")
        expect(ui.stdin).to receive(:readline).and_return('y')
        expect(rest).to receive(:delete).with("clients/#{node_name}")
        expect { client_builder.run }.not_to raise_error
      end

      it "when we are are allowed to delete both an old client and node" do
        expect(rest).to receive(:get_rest).with("nodes/#{node_name}")
        expect(rest).to receive(:get_rest).with("clients/#{node_name}")
        expect(ui.stdin).to receive(:readline).twice.and_return('y')
        expect(rest).to receive(:delete).with("nodes/#{node_name}")
        expect(rest).to receive(:delete).with("clients/#{node_name}")
        expect { client_builder.run }.not_to raise_error
      end
    end
  end

  context "#create_client!" do
    before do
      # mock out the rest of #run
      expect(client_builder).to receive(:sanity_check)
      expect(client_builder).to receive(:create_node!)
    end

    it "delegates everything to Chef::ApiClient::Registration" do
      reg_double = double("Chef::ApiClient::Registration")
      expect(Chef::ApiClient::Registration).to receive(:new).with(node_name, client_builder.client_path, http_api: rest).and_return(reg_double)
      expect(reg_double).to receive(:run)
      client_builder.run
    end

  end

  context "#client_path" do
    it "has a public API for the temporary client.pem file" do
      expect(client_builder.client_path).to match(/#{node_name}.pem/)
    end
  end

  context "#create_node!" do
    before do
      # mock out the rest of #run
      expect(client_builder).to receive(:sanity_check)
      expect(client_builder).to receive(:create_client!)
      # mock out default node building steps
      expect(client_builder).to receive(:client_rest).and_return(client_rest)
      expect(Chef::Node).to receive(:new).with(chef_server_rest: client_rest).and_return(node)
      expect(node).to receive(:name).with(node_name)
      expect(node).to receive(:save)
    end

    let(:client_rest) { double("Chef::REST (client)") }

    let(:node) { double("Chef::Node") }

    it "builds a node with a default run_list of []" do
      expect(node).to receive(:run_list).with([])
      client_builder.run
    end

    it "builds a node when the run_list is a string" do
      knife_config[:run_list] = "role[base],role[app]"
      expect(node).to receive(:run_list).with(["role[base]", "role[app]"])
      client_builder.run
    end

    it "builds a node when the run_list is an Array" do
      knife_config[:run_list] = ["role[base]", "role[app]"]
      expect(node).to receive(:run_list).with(["role[base]", "role[app]"])
      client_builder.run
    end

    it "builds a node with first_boot_attributes if they're given" do
      knife_config[:first_boot_attributes] = {:baz => :quux}
      expect(node).to receive(:normal_attrs=).with({:baz=>:quux})
      expect(node).to receive(:run_list).with([])
      client_builder.run
    end

    shared_examples "first-boot environment" do
      let(:first_boot_attributes) {{ chef_environment: first_boot_environment }}

      let(:first_boot_environment) { "first_boot_environment" }

      before do
        knife_config[:first_boot_attributes] = first_boot_attributes
        allow(node).to receive(:run_list)
      end

      it "builds a node with the environment specified in the first_boot_attributes" do
        allow(node).to receive(:normal_attrs=)
        expect(node).to receive(:environment).with(first_boot_environment)
        client_builder.run
      end

      context "when environment is the only first-boot attribute" do
        it "does not save any first-boot attributes" do
          expect(node).to_not receive(:normal_attrs=)
          allow(node).to receive(:environment)
          client_builder.run
        end
      end

      context "when environment is not the only first-boot attribute" do
        let(:first_boot_attributes) {{ chef_environment: first_boot_environment,
                                       baz: :quux }}

        it "saves the first-boot attributes, but does not save environment" do
          expect(node).to receive(:normal_attrs=).with({ baz: :quux })
          allow(node).to receive(:environment)
          client_builder.run
        end
      end
    end

    shared_examples "cli environment" do
      let(:cli_environment) { "cli_environment" }

      before do
        knife_config[:environment] = cli_environment
        allow(node).to receive(:run_list)
      end

      it "builds a node with the environment specified from the command line" do
        expect(node).to receive(:environment).with(cli_environment)
        client_builder.run
      end
    end

    context "with an environment specified in the chef config" do
      let(:chef_config_environment) { "chef_config_environment" }

      before do
        chef_config[:environment] = chef_config_environment
        allow(node).to receive(:run_list)
      end

      it "builds a node with the environment specified in the chef config" do
        expect(node).to receive(:environment).with(chef_config_environment)
        client_builder.run
      end

      context "with an environment specified in first_boot_attributes" do
        include_examples "first-boot environment"

        context "with an environment specified as a cli option" do
          include_examples "cli environment"
        end

      end

      context "with an environment specified as a cli option" do
        include_examples "cli environment"
      end

    end

    context "with an environment specified in first_boot_attributes" do
      include_examples "first-boot environment"

      context "with an environment specified as a cli option" do
        include_examples "cli environment"
      end

    end

    context "with an environment specified as a cli option" do
      include_examples "cli environment"
    end
  end
end