summaryrefslogtreecommitdiff
path: root/spec/unit/knife/cookbook_bulk_delete_spec.rb
blob: fb4b1d1484aca3de4e8a00766af9b98b6c3a8298 (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
#
# Author:: Stephen Delano (<stephen@opscode.com>)
# Copyright:: Copyright (c) 2010 Opscode, 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::CookbookBulkDelete do
  before(:each) do
    Chef::Log.logger = Logger.new(StringIO.new)

    Chef::Config[:node_name]  = "webmonkey.example.com"
    @knife = Chef::Knife::CookbookBulkDelete.new
    @knife.config = {:print_after => nil}
    @knife.name_args = ["."]
    @stdout = StringIO.new
    @stderr = StringIO.new
    @knife.ui.stub(:stdout).and_return(@stdout)
    @knife.ui.stub(:stderr).and_return(@stderr)
    @knife.ui.stub(:confirm).and_return(true)
    @cookbooks = Hash.new
    %w{cheezburger pizza lasagna}.each do |cookbook_name|
      cookbook = Chef::CookbookVersion.new(cookbook_name)
      @cookbooks[cookbook_name] = cookbook
    end
    @rest = double("Chef::REST")
    @rest.stub(:get_rest).and_return(@cookbooks)
    @rest.stub(:delete_rest).and_return(true)
    @knife.stub(:rest).and_return(@rest)
    Chef::CookbookVersion.stub(:list).and_return(@cookbooks)

  end



  describe "when there are several cookbooks on the server" do
    before do
      @cheezburger = {'cheezburger' => {"url" => "file:///dev/null", "versions" => [{"url" => "file:///dev/null-cheez", "version" => "1.0.0"}]}}
      @rest.stub(:get_rest).with('cookbooks/cheezburger').and_return(@cheezburger)
      @pizza = {'pizza' => {"url" => "file:///dev/null", "versions" => [{"url" => "file:///dev/null-pizza", "version" => "2.0.0"}]}}
      @rest.stub(:get_rest).with('cookbooks/pizza').and_return(@pizza)
      @lasagna = {'lasagna' => {"url" => "file:///dev/null", "versions" => [{"url" => "file:///dev/null-lasagna", "version" => "3.0.0"}]}}
      @rest.stub(:get_rest).with('cookbooks/lasagna').and_return(@lasagna)
    end

    it "should print the cookbooks you are about to delete" do
      expected = @knife.ui.list(@cookbooks.keys.sort, :columns_down)
      @knife.run
      @stdout.string.should match(/#{expected}/)
    end

    it "should confirm you really want to delete them" do
      @knife.ui.should_receive(:confirm)
      @knife.run
    end

    it "should delete each cookbook" do
      {"cheezburger" => "1.0.0", "pizza" => "2.0.0", "lasagna" => '3.0.0'}.each do |cookbook_name, version|
        @rest.should_receive(:delete_rest).with("cookbooks/#{cookbook_name}/#{version}")
      end
      @knife.run
    end

    it "should only delete cookbooks that match the regex" do
      @knife.name_args = ["cheezburger"]
      @rest.should_receive(:delete_rest).with('cookbooks/cheezburger/1.0.0')
      @knife.run
    end
  end

  it "should exit if the regex is not provided" do
    @knife.name_args = []
    lambda { @knife.run }.should raise_error(SystemExit)
  end

end