summaryrefslogtreecommitdiff
path: root/spec/unit/knife/cookbook_list_spec.rb
blob: cc45970ead244871d2f89d061ac1e7bf4fcb4942 (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
#
# Author:: Thomas Bishop (<bishop.thomas@gmail.com>)
# Copyright:: Copyright 2011-2016, Thomas Bishop
# 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::CookbookList do
  before do
    @knife = Chef::Knife::CookbookList.new
    @rest_mock = double("rest")
    allow(@knife).to receive(:rest).and_return(@rest_mock)
    @cookbook_names = ["apache2", "mysql"]
    @base_url = "https://server.example.com/cookbooks"
    @cookbook_data = {}
    @cookbook_names.each do |item|
      @cookbook_data[item] = {"url" => "#{@base_url}/#{item}",
                              "versions" => [{"version" => "1.0.1",
                                              "url" => "#{@base_url}/#{item}/1.0.1"}]}
    end
    @stdout = StringIO.new
    allow(@knife.ui).to receive(:stdout).and_return(@stdout)
  end

  describe "run" do
    it "should display the latest version of the cookbooks" do
      expect(@rest_mock).to receive(:get).with("/cookbooks?num_versions=1").
                                           and_return(@cookbook_data)
      @knife.run
      @cookbook_names.each do |item|
        expect(@stdout.string).to match /#{item}\s+1\.0\.1/
      end
    end

    it "should query cookbooks for the configured environment" do
      @knife.config[:environment] = "production"
      expect(@rest_mock).to receive(:get).
                 with("/environments/production/cookbooks?num_versions=1").
                 and_return(@cookbook_data)
      @knife.run
    end

    describe "with -w or --with-uri" do
      it "should display the cookbook uris" do
        @knife.config[:with_uri] = true
        allow(@rest_mock).to receive(:get).and_return(@cookbook_data)
        @knife.run
        @cookbook_names.each do |item|
          pattern = /#{Regexp.escape(@cookbook_data[item]['versions'].first['url'])}/
          expect(@stdout.string).to match pattern
        end
      end
    end

    describe "with -a or --all" do
      before do
        @cookbook_names.each do |item|
          @cookbook_data[item]["versions"] << {"version" => "1.0.0",
                                               "url" => "#{@base_url}/#{item}/1.0.0"}
        end
      end

      it "should display all versions of the cookbooks" do
        @knife.config[:all_versions] = true
        expect(@rest_mock).to receive(:get).with("/cookbooks?num_versions=all").
                                             and_return(@cookbook_data)
        @knife.run
        @cookbook_names.each do |item|
          expect(@stdout.string).to match /#{item}\s+1\.0\.1\s+1\.0\.0/
        end
      end
    end

  end
end