summaryrefslogtreecommitdiff
path: root/spec/unit/knife/index_rebuild_spec.rb
blob: d8a0dd72d70a896d17194e8e4f1ff5b22a22ab0e (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
#
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Copyright:: Copyright (c) 2009 Daniel DeLeo
# 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::IndexRebuild do

  let(:knife){Chef::Knife::IndexRebuild.new}
  let(:rest_client){double(Chef::ServerAPI)}

  let(:stub_rest!) do
    expect(knife).to receive(:rest).and_return(rest_client)
  end

  before :each do
    # This keeps the test output clean
    allow(knife.ui).to receive(:stdout).and_return(StringIO.new)
  end

  context "#grab_api_info" do
    let(:http_not_found_response) do
      e = Net::HTTPNotFound.new("1.1", 404, "blah")
      allow(e).to receive(:[]).with("x-ops-api-info").and_return(api_header_value)
      e
    end

    let(:http_server_exception) do
      Net::HTTPServerException.new("404: Not Found", http_not_found_response)
    end

    before(:each) do
      stub_rest!
      allow(rest_client).to receive(:get).and_raise(http_server_exception)
    end

    context "against a Chef 11 server" do
      let(:api_header_value){"flavor=osc;version=11.0.0;erchef=1.2.3"}
      it "retrieves API information" do
        expect(knife.grab_api_info).to eq({"flavor" => "osc", "version" => "11.0.0", "erchef" => "1.2.3"})
      end
    end # Chef 11

    context "against a Chef 10 server" do
      let(:api_header_value){nil}
      it "finds no API information" do
        expect(knife.grab_api_info).to eq({})
      end
    end # Chef 10
  end # grab_api_info

  context "#unsupported_version?" do
    context "with Chef 11 API metadata" do
      it "is unsupported" do
        expect(knife.unsupported_version?({"version" => "11.0.0", "flavor" => "osc", "erchef" => "1.2.3"})).to be_truthy
      end

      it "only truly relies on the version being non-nil" do
        expect(knife.unsupported_version?({"version" => "1", "flavor" => "osc", "erchef" => "1.2.3"})).to be_truthy
      end
    end

    context "with Chef 10 API metadata" do
      it "is supported" do
        # Chef 10 will have no metadata
        expect(knife.unsupported_version?({})).to be_falsey
      end
    end
  end # unsupported_version?

  context "Simulating a 'knife index rebuild' run" do

    before :each do
      expect(knife).to receive(:grab_api_info).and_return(api_info)
      server_specific_stubs!
    end

    context "against a Chef 11 server" do
      let(:api_info) do
        {"flavor" => "osc",
          "version" => "11.0.0",
          "erchef" => "1.2.3"
        }
      end
      let(:server_specific_stubs!) do
        expect(knife).to receive(:unsupported_server_message).with(api_info)
        expect(knife).to receive(:exit).with(1)
      end

      it "should not be allowed" do
        knife.run
      end
    end

    context "against a Chef 10 server" do
      let(:api_info){ {} }
      let(:server_specific_stubs!) do
        stub_rest!
        expect(rest_client).to receive(:post).with("/search/reindex", {}).and_return("representative output")
        expect(knife).not_to receive(:unsupported_server_message)
        expect(knife).to receive(:deprecated_server_message)
        expect(knife).to receive(:nag)
        expect(knife).to receive(:output).with("representative output")
      end
      it "should be allowed" do
        knife.run
      end
    end
  end

end