summaryrefslogtreecommitdiff
path: root/spec/integration/knife/cookbook_upload_spec.rb
blob: 7253b4c350338aa9ef81165203d8f5626ecd5ddd (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
#
# Copyright:: Copyright 2013-2016, 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 "support/shared/integration/integration_helper"
require "support/shared/context/config"
require "chef/knife/cookbook_upload"

describe "knife cookbook upload", :workstation do
  include IntegrationSupport
  include KnifeSupport

  include_context "default config options"

  let (:cb_dir) { "#{@repository_dir}/cookbooks" }

  when_the_chef_server "is empty" do
    when_the_repository "has a cookbook" do
      before do
        file "cookbooks/x/metadata.rb", cb_metadata("x", "1.0.0")
      end

      it "knife cookbook upload uploads the cookbook" do
        knife("cookbook upload x -o #{cb_dir}").should_succeed stderr: <<~EOM
          Uploading x            [1.0.0]
          Uploaded 1 cookbook.
        EOM
      end

      it "knife cookbook upload --freeze uploads and freezes the cookbook" do
        knife("cookbook upload x -o #{cb_dir} --freeze").should_succeed stderr: <<~EOM
          Uploading x            [1.0.0]
          Uploaded 1 cookbook.
        EOM
        # Modify the file, attempt to reupload
        file "cookbooks/x/metadata.rb", 'name "x"; version "1.0.0"#different'
        knife("cookbook upload x -o #{cb_dir} --freeze").should_fail stderr: <<~EOM
          Uploading x              [1.0.0]
          ERROR: Version 1.0.0 of cookbook x is frozen. Use --force to override.
          WARNING: Not updating version constraints for x in the environment as the cookbook is frozen.
          ERROR: Failed to upload 1 cookbook.
        EOM
      end
    end

    when_the_repository "has a cookbook that depends on another cookbook" do
      before do
        file "cookbooks/x/metadata.rb", cb_metadata("x", "1.0.0", "\ndepends 'y'")
        file "cookbooks/y/metadata.rb", cb_metadata("y", "1.0.0")
      end

      it "knife cookbook upload --include-dependencies uploads both cookbooks" do
        knife("cookbook upload --include-dependencies x -o #{cb_dir}").should_succeed stderr: <<~EOM
          Uploading x            [1.0.0]
          Uploading y            [1.0.0]
          Uploaded 2 cookbooks.
        EOM
      end

      it "knife cookbook upload fails due to missing dependencies" do
        knife("cookbook upload x -o #{cb_dir}").should_fail stderr: <<~EOM
          Uploading x            [1.0.0]
          ERROR: Cookbook x depends on cookbooks which are not currently
          ERROR: being uploaded and cannot be found on the server.
          ERROR: The missing cookbook(s) are: 'y' version '>= 0.0.0'
        EOM
      end

      it "knife cookbook upload -a uploads both cookbooks" do
        knife("cookbook upload -a -o #{cb_dir}").should_succeed stderr: <<~EOM
          Uploading x            [1.0.0]
          Uploading y            [1.0.0]
          Uploaded all cookbooks.
        EOM
      end
    end

    when_the_repository "has cookbook metadata without name attribute in metadata file" do
      before do
        file "cookbooks/x/metadata.rb", cb_metadata(nil, "1.0.0")
      end

      it "knife cookbook upload x " do
        expect { knife("cookbook upload x -o #{cb_dir}") }.to raise_error(Chef::Exceptions::MetadataNotValid)
      end
    end
  end
end