summaryrefslogtreecommitdiff
path: root/spec/unit/knife/cookbook_site_download_spec.rb
blob: 1b0f9f53d344a96492c6f5ab6d24bf3e6ddd7a96 (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
#
# Author:: Thomas Bishop (<bishop.thomas@gmail.com>)
# Copyright:: Copyright (c) 2012 Thomas Bishop
# 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.dirname(__FILE__) + '/../../spec_helper')

describe Chef::Knife::CookbookSiteDownload do

  describe 'run' do
    before do
      @knife            = Chef::Knife::CookbookSiteDownload.new
      @knife.name_args  = ['apache2']
      @noauth_rest      = double('no auth rest')
      @stdout           = StringIO.new
      @stderr           = StringIO.new
      @cookbook_api_url = 'https://supermarket.getchef.com/api/v1/cookbooks'
      @version          = '1.0.2'
      @version_us       = @version.gsub '.', '_'
      @current_data     = { 'deprecated'       => false,
                            'latest_version'   => "#{@cookbook_api_url}/apache2/versions/#{@version_us}",
                            'replacement' => 'other_apache2' }

      @knife.ui.stub(:stdout).and_return(@stdout)
      @knife.stub(:noauth_rest).and_return(@noauth_rest)
      @noauth_rest.should_receive(:get_rest).
                   with("#{@cookbook_api_url}/apache2").
                   and_return(@current_data)
    end

    context 'when the cookbook is deprecated and not forced' do
      before do
        @current_data['deprecated'] = true
      end

      it 'should warn with info about the replacement' do
        @knife.ui.should_receive(:warn).
                  with(/.+deprecated.+replaced by other_apache2.+/i)
        @knife.ui.should_receive(:warn).
                  with(/use --force.+download.+/i)
        @knife.run
      end
    end

    context 'when' do
      before do
        @cookbook_data = { 'version' => @version,
                           'file'    => "http://example.com/apache2_#{@version_us}.tgz" }
        @temp_file     =  double( :path => "/tmp/apache2_#{@version_us}.tgz" )
        @file          = File.join(Dir.pwd, "apache2-#{@version}.tar.gz")

        @noauth_rest.should_receive(:sign_on_redirect=).with(false)
      end

      context 'downloading the latest version' do
        before do
          @noauth_rest.should_receive(:get_rest).
                       with(@current_data['latest_version']).
                       and_return(@cookbook_data)
          @noauth_rest.should_receive(:get_rest).
                       with(@cookbook_data['file'], true).
                       and_return(@temp_file)
        end

        context 'and it is deprecated and with --force' do
          before do
            @current_data['deprecated'] = true
            @knife.config[:force] = true
          end

          it 'should download the latest version' do
            @knife.ui.should_receive(:warn).
                      with(/.+deprecated.+replaced by other_apache2.+/i)
            FileUtils.should_receive(:cp).with(@temp_file.path, @file)
            @knife.run
            @stdout.string.should match /downloading apache2.+version.+#{Regexp.escape(@version)}/i
            @stdout.string.should match /cookbook save.+#{Regexp.escape(@file)}/i
          end

        end

        it 'should download the latest version' do
          FileUtils.should_receive(:cp).with(@temp_file.path, @file)
          @knife.run
          @stdout.string.should match /downloading apache2.+version.+#{Regexp.escape(@version)}/i
          @stdout.string.should match /cookbook save.+#{Regexp.escape(@file)}/i
        end

        context 'with -f or --file' do
          before do
            @file = '/opt/chef/cookbooks/apache2.tar.gz'
            @knife.config[:file] = @file
            FileUtils.should_receive(:cp).with(@temp_file.path, @file)
          end

          it 'should download the cookbook to the desired file' do
            @knife.run
            @stdout.string.should match /downloading apache2.+version.+#{Regexp.escape(@version)}/i
            @stdout.string.should match /cookbook save.+#{Regexp.escape(@file)}/i
          end
        end

        it 'should provide an accessor to the version' do
          FileUtils.stub(:cp).and_return(true)
          @knife.version.should == @version
          @knife.run
        end
      end

      context 'downloading a cookbook of a specific version' do
        before do
          @version         = '1.0.1'
          @version_us      = @version.gsub '.', '_'
          @cookbook_data   = { 'version' => @version,
                               'file'    => "http://example.com/apache2_#{@version_us}.tgz" }
          @temp_file       = double(:path => "/tmp/apache2_#{@version_us}.tgz")
          @file            = File.join(Dir.pwd, "apache2-#{@version}.tar.gz")
          @knife.name_args << @version
        end

        it 'should download the desired version' do
          @noauth_rest.should_receive(:get_rest).
                       with("#{@cookbook_api_url}/apache2/versions/#{@version_us}").
                       and_return(@cookbook_data)
          @noauth_rest.should_receive(:get_rest).
                       with(@cookbook_data['file'], true).
                       and_return(@temp_file)
          FileUtils.should_receive(:cp).with(@temp_file.path, @file)
          @knife.run
          @stdout.string.should match /downloading apache2.+version.+#{Regexp.escape(@version)}/i
          @stdout.string.should match /cookbook save.+#{Regexp.escape(@file)}/i
        end
      end

    end

  end

end