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
|
#
# Author:: Steven Danna (<steve@opscode.com>)
# Copyright:: Copyright (c) 2011 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
describe Chef::Knife::CookbookSiteInstall do
let(:knife) { Chef::Knife::CookbookSiteInstall.new }
let(:stdout) { StringIO.new }
let(:stderr) { StringIO.new }
let(:downloader) { Hash.new }
let(:repo) { double(:sanity_check => true, :reset_to_default_state => true,
:prepare_to_import => true, :finalize_updates_to => true,
:merge_updates_from => true) }
let(:install_path) { if Chef::Platform.windows?
'C:/tmp/chef'
else
'/var/tmp/chef'
end }
before(:each) do
require 'chef/knife/core/cookbook_scm_repo'
allow(knife.ui).to receive(:stdout).and_return(stdout)
knife.config = {}
knife.config[:cookbook_path] = [ install_path ]
allow(knife).to receive(:stderr).and_return(stderr)
allow(knife).to receive(:stdout).and_return(stdout)
# Assume all external commands would have succeed. :(
allow(File).to receive(:unlink)
allow(File).to receive(:rmtree)
allow(knife).to receive(:shell_out!).and_return(true)
# CookbookSiteDownload Stup
allow(knife).to receive(:download_cookbook_to).and_return(downloader)
allow(downloader).to receive(:version) do
if knife.name_args.size == 2
knife.name_args[1]
else
"0.3.0"
end
end
# Stubs for CookbookSCMRepo
allow(Chef::Knife::CookbookSCMRepo).to receive(:new).and_return(repo)
end
describe "run" do
it "raises an error if a cookbook name is not provided" do
knife.name_args = []
expect(knife.ui).to receive(:error).with("Please specify a cookbook to download and install.")
expect { knife.run }.to raise_error(SystemExit)
end
it "raises an error if more than two arguments are given" do
knife.name_args = ["foo", "bar", "baz"]
expect(knife.ui).to receive(:error).with("Installing multiple cookbooks at once is not supported.")
expect { knife.run }.to raise_error(SystemExit)
end
it "raises an error if the second argument is not a version" do
knife.name_args = ["getting-started", "1pass"]
expect(knife.ui).to receive(:error).with("Installing multiple cookbooks at once is not supported.")
expect { knife.run }.to raise_error(SystemExit)
end
it "raises an error if the second argument is a four-digit version" do
knife.name_args = ["getting-started", "0.0.0.1"]
expect(knife.ui).to receive(:error).with("Installing multiple cookbooks at once is not supported.")
expect { knife.run }.to raise_error(SystemExit)
end
it "raises an error if the second argument is a one-digit version" do
knife.name_args = ["getting-started", "1"]
expect(knife.ui).to receive(:error).with("Installing multiple cookbooks at once is not supported.")
expect { knife.run }.to raise_error(SystemExit)
end
it "installs the specified version if second argument is a three-digit version" do
knife.name_args = ["getting-started", "0.1.0"]
knife.config[:no_deps] = true
upstream_file = File.join(install_path, "getting-started.tar.gz")
expect(knife).to receive(:download_cookbook_to).with(upstream_file)
expect(knife).to receive(:extract_cookbook).with(upstream_file, "0.1.0")
expect(knife).to receive(:clear_existing_files).with(File.join(install_path, "getting-started"))
expect(repo).to receive(:merge_updates_from).with("getting-started", "0.1.0")
knife.run
end
it "installs the specified version if second argument is a two-digit version" do
knife.name_args = ["getting-started", "0.1"]
knife.config[:no_deps] = true
upstream_file = File.join(install_path, "getting-started.tar.gz")
expect(knife).to receive(:download_cookbook_to).with(upstream_file)
expect(knife).to receive(:extract_cookbook).with(upstream_file, "0.1")
expect(knife).to receive(:clear_existing_files).with(File.join(install_path, "getting-started"))
expect(repo).to receive(:merge_updates_from).with("getting-started", "0.1")
knife.run
end
it "installs the latest version if only a cookbook name is given" do
knife.name_args = ["getting-started"]
knife.config[:no_deps] = true
upstream_file = File.join(install_path, "getting-started.tar.gz")
expect(knife).to receive(:download_cookbook_to).with(upstream_file)
expect(knife).to receive(:extract_cookbook).with(upstream_file, "0.3.0")
expect(knife).to receive(:clear_existing_files).with(File.join(install_path, "getting-started"))
expect(repo).to receive(:merge_updates_from).with("getting-started", "0.3.0")
knife.run
end
it "does not create/reset git branches if use_current_branch is set" do
knife.name_args = ["getting-started"]
knife.config[:use_current_branch] = true
knife.config[:no_deps] = true
upstream_file = File.join(install_path, "getting-started.tar.gz")
expect(repo).not_to receive(:prepare_to_import)
expect(repo).not_to receive(:reset_to_default_state)
knife.run
end
it "does not raise an error if cookbook_path is a string" do
knife.config[:cookbook_path] = install_path
knife.config[:no_deps] = true
knife.name_args = ["getting-started"]
upstream_file = File.join(install_path, "getting-started.tar.gz")
expect(knife).to receive(:download_cookbook_to).with(upstream_file)
expect(knife).to receive(:extract_cookbook).with(upstream_file, "0.3.0")
expect(knife).to receive(:clear_existing_files).with(File.join(install_path, "getting-started"))
expect(repo).to receive(:merge_updates_from).with("getting-started", "0.3.0")
expect { knife.run }.not_to raise_error
end
end # end of run
end
|