summaryrefslogtreecommitdiff
path: root/spec/unit/knife/cookbook_site_install_spec.rb
blob: 43afe09f5e8434d54c200400a65c3dbb07766d0a (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
#
# 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
  before(:each) do
    require 'chef/knife/core/cookbook_scm_repo'
    @knife = Chef::Knife::CookbookSiteInstall.new
    @knife.config = {}
    if Chef::Platform.windows?
      @install_path = 'C:/tmp/chef'
    else 
      @install_path = '/var/tmp/chef'
    end
    @knife.config[:cookbook_path] = [ @install_path ]

    @stdout = StringIO.new
    @stderr = StringIO.new
    @knife.stub!(:stderr).and_return(@stdout)
    @knife.stub!(:stdout).and_return(@stdout)

    #Assume all external commands would have succeed. :(
    File.stub!(:unlink)
    File.stub!(:rmtree)
    @knife.stub!(:shell_out!).and_return(true)

    #CookbookSiteDownload Stup
    @downloader = {}
    @knife.stub!(:download_cookbook_to).and_return(@downloader)
    @downloader.stub!(:version).and_return do
      if @knife.name_args.size == 2
        @knife.name_args[1]
      else
        "0.3.0"
      end
    end

    #Stubs for CookbookSCMRepo
    @repo = stub(:sanity_check => true, :reset_to_default_state => true,
                 :prepare_to_import => true, :finalize_updates_to => true,
                 :merge_updates_from => true)
    Chef::Knife::CookbookSCMRepo.stub!(:new).and_return(@repo)
  end


  describe "run" do
    it "should return an error if a cookbook name is not provided" do
      @knife.name_args = []
      @knife.ui.should_receive(:error).with("Please specify a cookbook to download and install.")
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it "should return an error if more than two arguments are given" do
      @knife.name_args = ["foo", "bar", "baz"]
      @knife.ui.should_receive(:error).with("Installing multiple cookbooks at once is not supported.")
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it "should return an error if the second argument is not a version" do
      @knife.name_args = ["getting-started", "1pass"]
      @knife.ui.should_receive(:error).with("Installing multiple cookbooks at once is not supported.")
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it "should return an error if the second argument is a four-digit version" do
      @knife.name_args = ["getting-started", "0.0.0.1"]
      @knife.ui.should_receive(:error).with("Installing multiple cookbooks at once is not supported.")
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it "should return an error if the second argument is a one-digit version" do
      @knife.name_args = ["getting-started", "1"]
      @knife.ui.should_receive(:error).with("Installing multiple cookbooks at once is not supported.")
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it "should install 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")
      @knife.should_receive(:download_cookbook_to).with(upstream_file)
      @knife.should_receive(:extract_cookbook).with(upstream_file, "0.1.0")
      @knife.should_receive(:clear_existing_files).with(File.join(@install_path, "getting-started"))
      @repo.should_receive(:merge_updates_from).with("getting-started", "0.1.0")
      @knife.run
    end

    it "should install 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")
      @knife.should_receive(:download_cookbook_to).with(upstream_file)
      @knife.should_receive(:extract_cookbook).with(upstream_file, "0.1")
      @knife.should_receive(:clear_existing_files).with(File.join(@install_path, "getting-started"))
      @repo.should_receive(:merge_updates_from).with("getting-started", "0.1")
      @knife.run
    end

    it "should install 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")
      @knife.should_receive(:download_cookbook_to).with(upstream_file)
      @knife.should_receive(:extract_cookbook).with(upstream_file, "0.3.0")
      @knife.should_receive(:clear_existing_files).with(File.join(@install_path, "getting-started"))
      @repo.should_receive(:merge_updates_from).with("getting-started", "0.3.0")
      @knife.run
    end

    it "should 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")
      @repo.should_not_receive(:prepare_to_import)
      @repo.should_not_receive(:reset_to_default_state)
      @knife.run
    end

    it "should 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")
      @knife.should_receive(:download_cookbook_to).with(upstream_file)
      @knife.should_receive(:extract_cookbook).with(upstream_file, "0.3.0")
      @knife.should_receive(:clear_existing_files).with(File.join(@install_path, "getting-started"))
      @repo.should_receive(:merge_updates_from).with("getting-started", "0.3.0")
      lambda { @knife.run }.should_not raise_error
    end
  end
end