summaryrefslogtreecommitdiff
path: root/spec/unit/knife/bootstrap/client_builder_spec.rb
blob: f17a6af878aa6da9d672d39837b35063244c0d1e (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
#
# 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
    let(:client) { Chef::ApiClient.new }

    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 and sets client" 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).and_return(client)
      client_builder.run
      expect(client_builder.client).to eq(client)
    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 "does not add tags by default" do
      allow(node).to receive(:run_list).with([])
      expect(node).to_not receive(:tags)
      client_builder.run
    end

    it "adds tags to the node when given" do
      tag_receiver = []

      knife_config[:tags] = %w[foo bar]
      allow(node).to receive(:run_list).with([])
      allow(node).to receive(:tags).and_return(tag_receiver)
      client_builder.run
      expect(tag_receiver).to eq %w[foo bar]
    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

    it "builds a node with an environment if its given" do
      knife_config[:environment] = "production"
      expect(node).to receive(:environment).with("production")
      expect(node).to receive(:run_list).with([])
      client_builder.run
    end

    it "builds a node with policy_name and policy_group when given" do
      knife_config[:policy_name] = "my-app"
      knife_config[:policy_group] = "staging"

      expect(node).to receive(:run_list).with([])
      expect(node).to receive(:policy_name=).with("my-app")
      expect(node).to receive(:policy_group=).with("staging")

      client_builder.run
    end
  end
end