summaryrefslogtreecommitdiff
path: root/spec/functional/knife/cookbook_delete_spec.rb
blob: 650db0ede580dffc8b50eb1502bc2532e3f210d2 (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
#
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright (c) 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"
require "tiny_server"

describe Chef::Knife::CookbookDelete do
  let(:server) { TinyServer::Manager.new }
  let(:api) { TinyServer::API.instance }
  let(:knife_stdout) { StringIO.new }
  let(:knife_stderr) { StringIO.new }
  let(:knife) do
    knife = Chef::Knife::CookbookDelete.new
    allow(knife.ui).to receive(:stdout).and_return(knife_stdout)
    allow(knife.ui).to receive(:stderr).and_return(knife_stderr)
    knife
  end

  before(:each) do
    server.start
    api.clear

    Chef::Config[:node_name] = nil
    Chef::Config[:client_key] = nil
    Chef::Config[:chef_server_url] = "http://localhost:9000"
  end

  after(:each) do
    server.stop
  end

  context "when the cookbook doesn't exist" do
    before do
      knife.name_args = %w{no-such-cookbook}
      api.get("/cookbooks/no-such-cookbook", 404, Chef::JSONCompat.to_json({ "error" => "dear Tim, no. -Sent from my iPad" }))
    end

    it "logs an error and exits" do
      expect { knife.run }.to raise_error(SystemExit)
      expect(knife_stderr.string).to match(/Cannot find a cookbook named no-such-cookbook to delete/)
    end

  end

  context "when there is only one version of a cookbook" do
    before do
      knife.name_args = %w{obsolete-cookbook}
      @cookbook_list = { "obsolete-cookbook" => { "versions" => ["version" => "1.0.0"] } }
      api.get("/cookbooks/obsolete-cookbook", 200, Chef::JSONCompat.to_json(@cookbook_list))
    end

    it "asks for confirmation, then deletes the cookbook" do
      stdin, stdout = StringIO.new("y\n"), StringIO.new
      allow(knife.ui).to receive(:stdin).and_return(stdin)
      allow(knife.ui).to receive(:stdout).and_return(stdout)

      cb100_deleted = false
      api.delete("/cookbooks/obsolete-cookbook/1.0.0", 200) { cb100_deleted = true; "[\"true\"]" }

      knife.run

      expect(stdout.string).to match(/#{Regexp.escape('Do you really want to delete obsolete-cookbook version 1.0.0? (Y/N)')}/)
      expect(cb100_deleted).to be_truthy
    end

    it "asks for confirmation before purging" do
      knife.config[:purge] = true

      stdin, stdout = StringIO.new("y\ny\n"), StringIO.new
      allow(knife.ui).to receive(:stdin).and_return(stdin)
      allow(knife.ui).to receive(:stdout).and_return(stdout)

      cb100_deleted = false
      api.delete("/cookbooks/obsolete-cookbook/1.0.0?purge=true", 200) { cb100_deleted = true; "[\"true\"]" }

      knife.run

      expect(stdout.string).to match(/#{Regexp.escape('Are you sure you want to purge files')}/)
      expect(stdout.string).to match(/#{Regexp.escape('Do you really want to delete obsolete-cookbook version 1.0.0? (Y/N)')}/)
      expect(cb100_deleted).to be_truthy

    end

  end

  context "when there are several versions of a cookbook" do
    before do
      knife.name_args = %w{obsolete-cookbook}
      versions = ["1.0.0", "1.1.0", "1.2.0"]
      with_version = lambda { |version| { "version" => version } }
      @cookbook_list = { "obsolete-cookbook" => { "versions" => versions.map(&with_version) } }
      api.get("/cookbooks/obsolete-cookbook", 200, Chef::JSONCompat.to_json(@cookbook_list))
    end

    it "deletes all versions of a cookbook when given the '-a' flag" do
      knife.config[:all] = true
      knife.config[:yes] = true
      cb100_deleted = cb110_deleted = cb120_deleted = nil
      api.delete("/cookbooks/obsolete-cookbook/1.0.0", 200) { cb100_deleted = true; "[\"true\"]" }
      api.delete("/cookbooks/obsolete-cookbook/1.1.0", 200) { cb110_deleted = true; "[\"true\"]" }
      api.delete("/cookbooks/obsolete-cookbook/1.2.0", 200) { cb120_deleted = true; "[\"true\"]" }
      knife.run

      expect(cb100_deleted).to be_truthy
      expect(cb110_deleted).to be_truthy
      expect(cb120_deleted).to be_truthy
    end

    it "asks which version to delete and deletes that when not given the -a flag" do
      cb100_deleted = cb110_deleted = cb120_deleted = nil
      api.delete("/cookbooks/obsolete-cookbook/1.0.0", 200) { cb100_deleted = true; "[\"true\"]" }
      stdin, stdout = StringIO.new, StringIO.new
      allow(knife.ui).to receive(:stdin).and_return(stdin)
      allow(knife.ui).to receive(:stdout).and_return(stdout)
      stdin << "1\n"
      stdin.rewind
      knife.run
      expect(cb100_deleted).to be_truthy
      expect(stdout.string).to match(/Which version\(s\) do you want to delete\?/)
    end

    it "deletes all versions of the cookbook when not given the -a flag and the user chooses to delete all" do
      cb100_deleted = cb110_deleted = cb120_deleted = nil
      api.delete("/cookbooks/obsolete-cookbook/1.0.0", 200) { cb100_deleted = true; "[\"true\"]" }
      api.delete("/cookbooks/obsolete-cookbook/1.1.0", 200) { cb110_deleted = true; "[\"true\"]" }
      api.delete("/cookbooks/obsolete-cookbook/1.2.0", 200) { cb120_deleted = true; "[\"true\"]" }

      stdin, stdout = StringIO.new("4\n"), StringIO.new
      allow(knife.ui).to receive(:stdin).and_return(stdin)
      allow(knife.ui).to receive(:stdout).and_return(stdout)

      knife.run

      expect(cb100_deleted).to be_truthy
      expect(cb110_deleted).to be_truthy
      expect(cb120_deleted).to be_truthy
    end

  end

end