summaryrefslogtreecommitdiff
path: root/spec/unit/knife/environment_compare_spec.rb
blob: 5d4a71d3a42e9d63fee5c6df5ec48e1ff4be63e5 (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
#
# Author:: Sander Botman (<sbotman@schubergphilis.com>)
# Copyright:: Copyright 2013-2016, Sander Botman.
# 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::EnvironmentCompare do
  before(:each) do
    @knife = Chef::Knife::EnvironmentCompare.new
    
    @environments = {
      "cita" => "http://localhost:4000/environments/cita",
      "citm" => "http://localhost:4000/environments/citm",
    }

    allow(@knife).to receive(:environment_list).and_return(@environments)

    @constraints = {
      "cita" => { "foo" => "= 1.0.1", "bar" => "= 0.0.4" },
      "citm" => { "foo" => "= 1.0.1", "bar" => "= 0.0.2" },
    }
 
    allow(@knife).to receive(:constraint_list).and_return(@constraints)

    @cookbooks = { "foo"=>"= 1.0.1", "bar"=>"= 0.0.1" } 

    allow(@knife).to receive(:cookbook_list).and_return(@cookbooks)

    @rest_double = double("rest")
    allow(@knife).to receive(:rest).and_return(@rest_double)
    @cookbook_names = ["apache2", "mysql", "foo", "bar", "dummy", "chef_handler"]
    @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 

    allow(@rest_double).to receive(:get).with("/cookbooks?num_versions=1").and_return(@cookbook_data)

    @stdout = StringIO.new
    allow(@knife.ui).to receive(:stdout).and_return(@stdout)
  end

  describe "run" do
    it "should display only cookbooks with version constraints" do
      @knife.config[:format] = "summary"
      @knife.run
      @environments.each do |item, url|
        expect(@stdout.string).to match /#{item}/ and expect(@stdout.string.lines.count).to be 4
      end
    end
 
    it "should display 4 number of lines" do
      @knife.config[:format] = "summary"
      @knife.run
      expect(@stdout.string.lines.count).to be 4
    end
  end

  describe "with -m or --mismatch" do
    it "should display only cookbooks that have mismatching version constraints" do
      @knife.config[:format] = "summary"
      @knife.config[:mismatch] = true
      @knife.run
      @constraints.each do |item, ver|
        expect(@stdout.string).to match /#{ver[1]}/
      end
    end

    it "should display 3 number of lines" do
      @knife.config[:format] = "summary"
      @knife.config[:mismatch] = true
      @knife.run
      expect(@stdout.string.lines.count).to be 3
    end
  end
 
  describe "with -a or --all" do
    it "should display all cookbooks" do
      @knife.config[:format] = "summary"
      @knife.config[:all] = true
      @knife.run
      @constraints.each do |item, ver|
        expect(@stdout.string).to match /#{ver[1]}/
      end
    end

    it "should display 8 number of lines" do
      @knife.config[:format] = "summary"
      @knife.config[:all] = true
      @knife.run
      expect(@stdout.string.lines.count).to be 8
    end
  end

end