summaryrefslogtreecommitdiff
path: root/spec/unit/resource/deploy_spec.rb
blob: 0403a7ba6bbc3d1d98d3298d2101c1b89fad83ed (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Copyright:: Copyright (c) 2008 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 'support/shared/unit/resource/static_provider_resolution'

describe Chef::Resource::Deploy do

  static_provider_resolution(
    resource: Chef::Resource::Deploy,
    provider: Chef::Provider::Deploy::Timestamped,
    name: :deploy,
    action: :deploy,
  )


  class << self
    def resource_has_a_string_attribute(attr_name)
      it "has a String attribute for #{attr_name.to_s}" do
        @resource.send(attr_name, "this is a string")
        expect(@resource.send(attr_name)).to eql("this is a string")
        expect {@resource.send(attr_name, 8675309)}.to raise_error(ArgumentError)
      end
    end

    def resource_has_a_boolean_attribute(attr_name, opts={:defaults_to=>false})
      it "has a Boolean attribute for #{attr_name.to_s}" do
        expect(@resource.send(attr_name)).to eql(opts[:defaults_to])
        @resource.send(attr_name, !opts[:defaults_to])
        expect(@resource.send(attr_name)).to eql( !opts[:defaults_to] )
      end
    end

    def resource_has_a_callback_attribute(attr_name)
      it "has a Callback attribute #{attr_name}" do
        callback_block = lambda { :noop }
        expect {@resource.send(attr_name, &callback_block)}.not_to raise_error
        expect(@resource.send(attr_name)).to eq(callback_block)
        callback_file = "path/to/callback.rb"
        expect {@resource.send(attr_name, callback_file)}.not_to raise_error
        expect(@resource.send(attr_name)).to eq(callback_file)
        expect {@resource.send(attr_name, :this_is_fail)}.to raise_error(ArgumentError)
      end
    end
  end

  before do
    @resource = Chef::Resource::Deploy.new("/my/deploy/dir")
  end

  resource_has_a_string_attribute(:repo)
  resource_has_a_string_attribute(:deploy_to)
  resource_has_a_string_attribute(:role)
  resource_has_a_string_attribute(:restart_command)
  resource_has_a_string_attribute(:migration_command)
  resource_has_a_string_attribute(:user)
  resource_has_a_string_attribute(:group)
  resource_has_a_string_attribute(:repository_cache)
  resource_has_a_string_attribute(:copy_exclude)
  resource_has_a_string_attribute(:revision)
  resource_has_a_string_attribute(:remote)
  resource_has_a_string_attribute(:git_ssh_wrapper)
  resource_has_a_string_attribute(:svn_username)
  resource_has_a_string_attribute(:svn_password)
  resource_has_a_string_attribute(:svn_arguments)
  resource_has_a_string_attribute(:svn_info_args)

  resource_has_a_boolean_attribute(:migrate, :defaults_to=>false)
  resource_has_a_boolean_attribute(:enable_submodules, :defaults_to=>false)
  resource_has_a_boolean_attribute(:shallow_clone, :defaults_to=>false)

  it "uses the first argument as the deploy directory" do
    expect(@resource.deploy_to).to eql("/my/deploy/dir")
  end

  # For git, any revision, branch, tag, whatever is resolved to a SHA1 ref.
  # For svn, the branch is included in the repo URL.
  # Therefore, revision and branch ARE NOT SEPARATE THINGS
  it "aliases #revision as #branch" do
    @resource.branch "stable"
    expect(@resource.revision).to eql("stable")
  end

  it "takes the SCM resource to use as a constant, and defaults to git" do
    expect(@resource.scm_provider).to eql(Chef::Provider::Git)
    @resource.scm_provider Chef::Provider::Subversion
    expect(@resource.scm_provider).to eql(Chef::Provider::Subversion)
  end

  it "allows scm providers to be set via symbol" do
    expect(@resource.scm_provider).to eq(Chef::Provider::Git)
    @resource.scm_provider :subversion
    expect(@resource.scm_provider).to eq(Chef::Provider::Subversion)
  end

  it "allows scm providers to be set via string" do
    expect(@resource.scm_provider).to eq(Chef::Provider::Git)
    @resource.scm_provider "subversion"
    expect(@resource.scm_provider).to eq(Chef::Provider::Subversion)
  end

  it "has a boolean attribute for svn_force_export defaulting to false" do
    expect(@resource.svn_force_export).to be_falsey
    @resource.svn_force_export true
    expect(@resource.svn_force_export).to be_truthy
    expect {@resource.svn_force_export(10053)}.to raise_error(ArgumentError)
  end

  it "takes arbitrary environment variables in a hash" do
    @resource.environment "RAILS_ENV" => "production"
    expect(@resource.environment).to eq({"RAILS_ENV" => "production"})
  end

  it "takes string arguments to environment for backwards compat, setting RAILS_ENV, RACK_ENV, and MERB_ENV" do
    @resource.environment "production"
    expect(@resource.environment).to eq({"RAILS_ENV"=>"production", "RACK_ENV"=>"production","MERB_ENV"=>"production"})
  end

  it "sets destination to $deploy_to/shared/$repository_cache" do
    expect(@resource.destination).to eql("/my/deploy/dir/shared/cached-copy")
  end

  it "sets shared_path to $deploy_to/shared" do
    expect(@resource.shared_path).to eql("/my/deploy/dir/shared")
  end

  it "sets current_path to $deploy_to/current" do
    expect(@resource.current_path).to eql("/my/deploy/dir/current")
  end

  it "gets the current_path correct even if the shared_path is set (regression test)" do
    @resource.shared_path
    expect(@resource.current_path).to eql("/my/deploy/dir/current")
  end

  it "gives #depth as 5 if shallow clone is true, nil otherwise" do
    expect(@resource.depth).to be_nil
    @resource.shallow_clone true
    expect(@resource.depth).to eql("5")
  end

  it "aliases repo as repository" do
    @resource.repository "git@github.com/opcode/cookbooks.git"
    expect(@resource.repo).to eql("git@github.com/opcode/cookbooks.git")
  end

  it "aliases git_ssh_wrapper as ssh_wrapper" do
    @resource.ssh_wrapper "git_my_repo.sh"
    expect(@resource.git_ssh_wrapper).to eql("git_my_repo.sh")
  end

  it "has an Array attribute purge_before_symlink, default: log, tmp/pids, public/system" do
    expect(@resource.purge_before_symlink).to eq(%w{ log tmp/pids public/system })
    @resource.purge_before_symlink %w{foo bar baz}
    expect(@resource.purge_before_symlink).to eq(%w{foo bar baz})
  end

  it "has an Array attribute create_dirs_before_symlink, default: tmp, public, config" do
    expect(@resource.create_dirs_before_symlink).to eq(%w{tmp public config})
    @resource.create_dirs_before_symlink %w{foo bar baz}
    expect(@resource.create_dirs_before_symlink).to eq(%w{foo bar baz})
  end

  it 'has a Hash attribute symlinks, default: {"system" => "public/system", "pids" => "tmp/pids", "log" => "log"}' do
    default = { "system" => "public/system", "pids" => "tmp/pids", "log" => "log"}
    expect(@resource.symlinks).to eq(default)
    @resource.symlinks "foo" => "bar/baz"
    expect(@resource.symlinks).to eq({"foo" => "bar/baz"})
  end

  it 'has a Hash attribute symlink_before_migrate, default "config/database.yml" => "config/database.yml"' do
    expect(@resource.symlink_before_migrate).to eq({"config/database.yml" => "config/database.yml"})
    @resource.symlink_before_migrate "wtf?" => "wtf is going on"
    expect(@resource.symlink_before_migrate).to eq({"wtf?" => "wtf is going on"})
  end

  resource_has_a_callback_attribute :before_migrate
  resource_has_a_callback_attribute :before_symlink
  resource_has_a_callback_attribute :before_restart
  resource_has_a_callback_attribute :after_restart

  it "aliases restart_command as restart" do
    @resource.restart "foobaz"
    expect(@resource.restart_command).to eq("foobaz")
  end

  it "takes a block for the restart parameter" do
    restart_like_this = lambda {p :noop}
    @resource.restart(&restart_like_this)
    expect(@resource.restart).to eq(restart_like_this)
  end

  it "allows providers to be set with a full class name" do
    @resource.provider Chef::Provider::Deploy::Timestamped
    expect(@resource.provider).to eq(Chef::Provider::Deploy::Timestamped)
  end

  it "allows deploy providers to be set via symbol" do
    @resource.provider :revision
    expect(@resource.provider).to eq(Chef::Provider::Deploy::Revision)
  end

  it "allows deploy providers to be set via string" do
    @resource.provider "revision"
    expect(@resource.provider).to eq(Chef::Provider::Deploy::Revision)
  end

  it "defaults keep_releases to 5" do
    expect(@resource.keep_releases).to eq(5)
  end

  it "allows keep_releases to be set via integer" do
    @resource.keep_releases 10
    expect(@resource.keep_releases).to eq(10)
  end

  it "enforces a minimum keep_releases of 1" do
    @resource.keep_releases 0
    expect(@resource.keep_releases).to eq(1)
  end

  describe "when it has a timeout attribute" do
    let(:ten_seconds) { 10 }
    before { @resource.timeout(ten_seconds) }
    it "stores this timeout" do
      expect(@resource.timeout).to eq(ten_seconds)
    end
  end

  describe "when it has no timeout attribute" do
    it "should have no default timeout" do
      expect(@resource.timeout).to be_nil
    end
  end

  describe "when it has meta application root, revision, user, group,
            scm provider, repository cache, environment, simlinks and migrate" do
    before do
      @resource.repository("http://uri.org")
      @resource.deploy_to("/")
      @resource.revision("1.2.3")
      @resource.user("root")
      @resource.group("pokemon")
      @resource.scm_provider(Chef::Provider::Git)
      @resource.repository_cache("cached-copy")
      @resource.environment({"SUDO" => "TRUE"})
      @resource.symlinks({"system" => "public/system"})
      @resource.migrate(false)

    end

    it "describes its state" do
      state = @resource.state
      expect(state[:deploy_to]).to eq("/")
      expect(state[:revision]).to eq("1.2.3")
    end

    it "returns the repository URI as its identity" do
      expect(@resource.identity).to eq("http://uri.org")
    end
  end

end