summaryrefslogtreecommitdiff
path: root/spec/unit/knife/cookbook_site_share_spec.rb
blob: 77dce9b17534bd07faeafc1d089f317c2b1df212 (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
#
# 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'

require 'chef/cookbook_uploader'
require 'chef/cookbook_site_streaming_uploader'

describe Chef::Knife::CookbookSiteShare do

  before(:each) do
    @knife = Chef::Knife::CookbookSiteShare.new
    @knife.name_args = ['cookbook_name', 'AwesomeSausage']

    @cookbook = Chef::CookbookVersion.new('cookbook_name')

    @cookbook_loader = double('Chef::CookbookLoader')
    @cookbook_loader.stub(:cookbook_exists?).and_return(true)
    @cookbook_loader.stub(:[]).and_return(@cookbook)
    Chef::CookbookLoader.stub(:new).and_return(@cookbook_loader)

    @cookbook_uploader = Chef::CookbookUploader.new('herpderp', File.join(CHEF_SPEC_DATA, 'cookbooks'), :rest => "norest")
    Chef::CookbookUploader.stub(:new).and_return(@cookbook_uploader)
    @cookbook_uploader.stub(:validate_cookbooks).and_return(true)
    Chef::CookbookSiteStreamingUploader.stub(:create_build_dir).and_return(Dir.mktmpdir)

    @knife.stub(:shell_out!).and_return(true)
    @stdout = StringIO.new
    @knife.ui.stub(:stdout).and_return(@stdout)
  end

  describe 'run' do

    before(:each) do
      @knife.stub(:do_upload).and_return(true)
    end

    it 'should should print usage and exit when given no arguments' do
      @knife.name_args = []
      @knife.should_receive(:show_usage)
      @knife.ui.should_receive(:fatal)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should print usage and exit when given only 1 argument' do
      @knife.name_args = ['cookbook_name']
      @knife.should_receive(:show_usage)
      @knife.ui.should_receive(:fatal)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should check if the cookbook exists' do
      @cookbook_loader.should_receive(:cookbook_exists?)
      @knife.run
    end

    it "should exit and log to error if the cookbook doesn't exist" do
      @cookbook_loader.stub(:cookbook_exists?).and_return(false)
      @knife.ui.should_receive(:error)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should make a tarball of the cookbook' do
      @knife.should_receive(:shell_out!) do |args|
        args.to_s.should match /tar -czf/
      end
      @knife.run
    end

    it 'should exit and log to error when the tarball creation fails' do
      @knife.stub(:shell_out!).and_raise(Chef::Exceptions::Exec)
      @knife.ui.should_receive(:error)
      lambda { @knife.run }.should raise_error(SystemExit)
    end

    it 'should upload the cookbook and clean up the tarball' do
      @knife.should_receive(:do_upload)
      FileUtils.should_receive(:rm_rf)
      @knife.run
    end
  end

  describe 'do_upload' do

    before(:each) do
      @upload_response = double('Net::HTTPResponse')
      Chef::CookbookSiteStreamingUploader.stub(:post).and_return(@upload_response)

      @stdout = StringIO.new
      @stderr = StringIO.new
      @knife.ui.stub(:stdout).and_return(@stdout)
      @knife.ui.stub(:stderr).and_return(@stderr)
      File.stub(:open).and_return(true)
    end

    it 'should post the cookbook to "https://supermarket.getchef.com"' do
      response_text = {:uri => 'https://supermarket.getchef.com/cookbooks/cookbook_name'}.to_json
      @upload_response.stub(:body).and_return(response_text)
      @upload_response.stub(:code).and_return(201)
      Chef::CookbookSiteStreamingUploader.should_receive(:post).with(/supermarket\.getchef\.com/, anything(), anything(), anything())
      @knife.run
    end

    it 'should alert the user when a version already exists' do
      response_text = Chef::JSONCompat.to_json({:error_messages => ['Version already exists']})
      @upload_response.stub(:body).and_return(response_text)
      @upload_response.stub(:code).and_return(409)
      lambda { @knife.run }.should raise_error(SystemExit)
      @stderr.string.should match(/ERROR(.+)cookbook already exists/)
    end

    it 'should pass any errors on to the user' do
      response_text = Chef::JSONCompat.to_json({:error_messages => ["You're holding it wrong"]})
      @upload_response.stub(:body).and_return(response_text)
      @upload_response.stub(:code).and_return(403)
      lambda { @knife.run }.should raise_error(SystemExit)
      @stderr.string.should match("ERROR(.*)You're holding it wrong")
    end

    it 'should print the body if no errors are exposed on failure' do
      response_text = Chef::JSONCompat.to_json({:system_error => "Your call was dropped", :reason => "There's a map for that"})
      @upload_response.stub(:body).and_return(response_text)
      @upload_response.stub(:code).and_return(500)
      @knife.ui.should_receive(:error).with(/#{Regexp.escape(response_text)}/)#.ordered
      @knife.ui.should_receive(:error).with(/Unknown error/)#.ordered
      lambda { @knife.run }.should raise_error(SystemExit)
    end

  end

end