summaryrefslogtreecommitdiff
path: root/spec/unit/cookbook_version_spec.rb
blob: f20bc3766a2bb36992d7a8d13cac9cca43f67ca6 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#
# Author:: Daniel DeLeo (<dan@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'

describe Chef::CookbookVersion do
  describe "when first created" do
    before do
      @cookbook_version = Chef::CookbookVersion.new("tatft", '/tmp/blah')
    end

    it "has a name" do
      @cookbook_version.name.should == 'tatft'
    end

    it "has no attribute files" do
      @cookbook_version.attribute_filenames.should be_empty
    end

    it "has no resource definition files" do
      @cookbook_version.definition_filenames.should be_empty
    end

    it "has no cookbook files" do
      @cookbook_version.file_filenames.should be_empty
    end

    it "has no recipe files" do
      @cookbook_version.recipe_filenames.should be_empty
    end

    it "has no library files" do
      @cookbook_version.library_filenames.should be_empty
    end

    it "has no LWRP resource files" do
      @cookbook_version.resource_filenames.should be_empty
    end

    it "has no LWRP provider files" do
      @cookbook_version.provider_filenames.should be_empty
    end

    it "has no metadata files" do
      @cookbook_version.metadata_filenames.should be_empty
    end

    it "is not frozen" do
      @cookbook_version.should_not be_frozen_version
    end

    it "can be frozen" do
      @cookbook_version.freeze_version
      @cookbook_version.should be_frozen_version
    end

    it "is \"ready\"" do
      # WTF is this? what are the valid states? and why aren't they set with encapsulating methods?
      # [Dan 15-Jul-2010]
      @cookbook_version.status.should == :ready
    end

    it "has empty metadata" do
      @cookbook_version.metadata.should == Chef::Cookbook::Metadata.new
    end

    it "creates a manifest hash of its contents" do
      expected = {"recipes"=>[],
                  "definitions"=>[],
                  "libraries"=>[],
                  "attributes"=>[],
                  "files"=>[],
                  "templates"=>[],
                  "resources"=>[],
                  "providers"=>[],
                  "root_files"=>[],
                  "cookbook_name"=>"tatft",
                  "metadata"=>Chef::Cookbook::Metadata.new,
                  "version"=>"0.0.0",
                  "name"=>"tatft-0.0.0"}
      @cookbook_version.manifest.should == expected
    end
  end

  describe "with a cookbook directory named tatft" do
    MD5 = /[0-9a-f]{32}/

    before do
      @cookbook = Hash.new { |hash, key| hash[key] = [] }

      @cookbook_root = File.join(CHEF_SPEC_DATA, 'cb_version_cookbooks', 'tatft')

      # Dunno if the paths here are representitive of what is set by CookbookLoader...
      @cookbook[:attribute_filenames]   = Dir[File.join(@cookbook_root, 'attributes', '**', '*.rb')]
      @cookbook[:definition_filenames]  = Dir[File.join(@cookbook_root, 'definitions', '**', '*.rb')]
      @cookbook[:file_filenames]        = Dir[File.join(@cookbook_root, 'files', '**', '*.tgz')]
      @cookbook[:recipe_filenames]      = Dir[File.join(@cookbook_root, 'recipes', '**', '*.rb')]
      @cookbook[:template_filenames]    = Dir[File.join(@cookbook_root, 'templates', '**', '*.erb')]
      @cookbook[:library_filenames]     = Dir[File.join(@cookbook_root, 'libraries', '**', '*.rb')]
      @cookbook[:resource_filenames]    = Dir[File.join(@cookbook_root, 'resources', '**', '*.rb')]
      @cookbook[:provider_filenames]    = Dir[File.join(@cookbook_root, 'providers', '**', '*.rb')]
      @cookbook[:root_filenames]        = Array(File.join(@cookbook_root, 'README.rdoc'))
      @cookbook[:metadata_filenames]    = Array(File.join(@cookbook_root, 'metadata.json'))

    end

    describe "and a cookbook with the same name" do
      before do
        # Currently the cookbook loader finds all the files then tells CookbookVersion
        # where they are.
        @cookbook_version = Chef::CookbookVersion.new("tatft", @cookbook_root)

        @cookbook_version.attribute_filenames  = @cookbook[:attribute_filenames]
        @cookbook_version.definition_filenames = @cookbook[:definition_filenames]
        @cookbook_version.recipe_filenames     = @cookbook[:recipe_filenames]
        @cookbook_version.template_filenames   = @cookbook[:template_filenames]
        @cookbook_version.file_filenames       = @cookbook[:file_filenames]
        @cookbook_version.library_filenames    = @cookbook[:library_filenames]
        @cookbook_version.resource_filenames   = @cookbook[:resource_filenames]
        @cookbook_version.provider_filenames   = @cookbook[:provider_filenames]
        @cookbook_version.root_filenames       = @cookbook[:root_filenames]
        @cookbook_version.metadata_filenames   = @cookbook[:metadata_filenames]

        # Used to test file-specificity related file lookups
        @node = Chef::Node.new
        @node.set[:platform] = "ubuntu"
        @node.set[:platform_version] = "13.04"
        @node.name("testing")
      end

      it "generates a manifest containing the cookbook's files" do
        manifest = @cookbook_version.manifest

        manifest["metadata"].should == Chef::Cookbook::Metadata.new
        manifest["cookbook_name"].should == "tatft"

        manifest["recipes"].should have(1).recipe_file

        recipe = manifest["recipes"].first
        recipe["name"].should == "default.rb"
        recipe["path"].should == "recipes/default.rb"
        recipe["checksum"].should match(MD5)
        recipe["specificity"].should == "default"

        manifest["definitions"].should have(1).definition_file

        definition = manifest["definitions"].first
        definition["name"].should == "runit_service.rb"
        definition["path"].should == "definitions/runit_service.rb"
        definition["checksum"].should match(MD5)
        definition["specificity"].should == "default"

        manifest["libraries"].should have(1).library_file

        library = manifest["libraries"].first
        library["name"].should == "ownage.rb"
        library["path"].should == "libraries/ownage.rb"
        library["checksum"].should match(MD5)
        library["specificity"].should == "default"

        manifest["attributes"].should have(1).attribute_file

        attribute_file = manifest["attributes"].first
        attribute_file["name"].should == "default.rb"
        attribute_file["path"].should == "attributes/default.rb"
        attribute_file["checksum"].should match(MD5)
        attribute_file["specificity"].should == "default"

        manifest["files"].should have(1).cookbook_file

        cookbook_file = manifest["files"].first
        cookbook_file["name"].should == "giant_blob.tgz"
        cookbook_file["path"].should == "files/default/giant_blob.tgz"
        cookbook_file["checksum"].should match(MD5)
        cookbook_file["specificity"].should == "default"

        manifest["templates"].should have(1).template

        template = manifest["templates"].first
        template["name"].should == "configuration.erb"
        template["path"].should == "templates/default/configuration.erb"
        template["checksum"].should match(MD5)
        template["specificity"].should == "default"

        manifest["resources"].should have(1).lwr

        lwr = manifest["resources"].first
        lwr["name"].should == "lwr.rb"
        lwr["path"].should == "resources/lwr.rb"
        lwr["checksum"].should match(MD5)
        lwr["specificity"].should == "default"

        manifest["providers"].should have(1).lwp

        lwp = manifest["providers"].first
        lwp["name"].should == "lwp.rb"
        lwp["path"].should == "providers/lwp.rb"
        lwp["checksum"].should match(MD5)
        lwp["specificity"].should == "default"

        manifest["root_files"].should have(1).file_in_the_cookbook_root

        readme = manifest["root_files"].first
        readme["name"].should == "README.rdoc"
        readme["path"].should == "README.rdoc"
        readme["checksum"].should match(MD5)
        readme["specificity"].should == "default"
      end

      it "determines whether a template is available for a given node" do
        @cookbook_version.should have_template_for_node(@node, "configuration.erb")
        @cookbook_version.should_not have_template_for_node(@node, "missing.erb")
      end

      it "determines whether a cookbook_file is available for a given node" do
        @cookbook_version.should have_cookbook_file_for_node(@node, "giant_blob.tgz")
        @cookbook_version.should_not have_cookbook_file_for_node(@node, "missing.txt")
      end

      describe "raises an error when attempting to load a missing cookbook_file and" do
        before do
          node = Chef::Node.new.tap do |n|
            n.name("sample.node")
            n.automatic_attrs[:fqdn] = "sample.example.com"
            n.automatic_attrs[:platform] = "ubuntu"
            n.automatic_attrs[:platform_version] = "10.04"
          end
          @attempt_to_load_file = lambda { @cookbook_version.preferred_manifest_record(node, :files, "no-such-thing.txt") }
        end

        it "describes the cookbook and version" do
          useful_explanation = Regexp.new(Regexp.escape("Cookbook 'tatft' (0.0.0) does not contain"))
          @attempt_to_load_file.should raise_error(Chef::Exceptions::FileNotFound, useful_explanation)
        end

        it "lists suggested places to look" do
          useful_explanation = Regexp.new(Regexp.escape("files/default/no-such-thing.txt"))
          @attempt_to_load_file.should raise_error(Chef::Exceptions::FileNotFound, useful_explanation)
        end
      end
    end

    describe "and a cookbook_version with a different name" do
      before do
        # Currently the cookbook loader finds all the files then tells CookbookVersion
        # where they are.
        @cookbook_version = Chef::CookbookVersion.new("blarghle", @cookbook_root)
        @cookbook_version.attribute_filenames  = @cookbook[:attribute_filenames]
        @cookbook_version.definition_filenames = @cookbook[:definition_filenames]
        @cookbook_version.recipe_filenames     = @cookbook[:recipe_filenames]
        @cookbook_version.template_filenames   = @cookbook[:template_filenames]
        @cookbook_version.file_filenames       = @cookbook[:file_filenames]
        @cookbook_version.library_filenames    = @cookbook[:library_filenames]
        @cookbook_version.resource_filenames   = @cookbook[:resource_filenames]
        @cookbook_version.provider_filenames   = @cookbook[:provider_filenames]
        @cookbook_version.root_filenames       = @cookbook[:root_filenames]
        @cookbook_version.metadata_filenames   = @cookbook[:metadata_filenames]
      end

      it "generates a manifest containing the cookbook's files" do
        manifest = @cookbook_version.manifest

        manifest["metadata"].should == Chef::Cookbook::Metadata.new
        manifest["cookbook_name"].should == "blarghle"

        manifest["recipes"].should have(1).recipe_file

        recipe = manifest["recipes"].first
        recipe["name"].should == "default.rb"
        recipe["path"].should == "recipes/default.rb"
        recipe["checksum"].should match(MD5)
        recipe["specificity"].should == "default"

        manifest["definitions"].should have(1).definition_file

        definition = manifest["definitions"].first
        definition["name"].should == "runit_service.rb"
        definition["path"].should == "definitions/runit_service.rb"
        definition["checksum"].should match(MD5)
        definition["specificity"].should == "default"

        manifest["libraries"].should have(1).library_file

        library = manifest["libraries"].first
        library["name"].should == "ownage.rb"
        library["path"].should == "libraries/ownage.rb"
        library["checksum"].should match(MD5)
        library["specificity"].should == "default"

        manifest["attributes"].should have(1).attribute_file

        attribute_file = manifest["attributes"].first
        attribute_file["name"].should == "default.rb"
        attribute_file["path"].should == "attributes/default.rb"
        attribute_file["checksum"].should match(MD5)
        attribute_file["specificity"].should == "default"

        manifest["files"].should have(1).cookbook_file

        cookbook_file = manifest["files"].first
        cookbook_file["name"].should == "giant_blob.tgz"
        cookbook_file["path"].should == "files/default/giant_blob.tgz"
        cookbook_file["checksum"].should match(MD5)
        cookbook_file["specificity"].should == "default"

        manifest["templates"].should have(1).template

        template = manifest["templates"].first
        template["name"].should == "configuration.erb"
        template["path"].should == "templates/default/configuration.erb"
        template["checksum"].should match(MD5)
        template["specificity"].should == "default"

        manifest["resources"].should have(1).lwr

        lwr = manifest["resources"].first
        lwr["name"].should == "lwr.rb"
        lwr["path"].should == "resources/lwr.rb"
        lwr["checksum"].should match(MD5)
        lwr["specificity"].should == "default"

        manifest["providers"].should have(1).lwp

        lwp = manifest["providers"].first
        lwp["name"].should == "lwp.rb"
        lwp["path"].should == "providers/lwp.rb"
        lwp["checksum"].should match(MD5)
        lwp["specificity"].should == "default"

        manifest["root_files"].should have(1).file_in_the_cookbook_root

        readme = manifest["root_files"].first
        readme["name"].should == "README.rdoc"
        readme["path"].should == "README.rdoc"
        readme["checksum"].should match(MD5)
        readme["specificity"].should == "default"
      end
    end

  end


  describe "<=>" do

    it "should sort based on the version number" do
      examples = [
                  # smaller, larger
                  ["1.0", "2.0"],
                  ["1.2.3", "1.2.4"],
                  ["1.2.3", "1.3.0"],
                  ["1.2.3", "1.3"],
                  ["1.2.3", "2.1.1"],
                  ["1.2.3", "2.1"],
                  ["1.2", "1.2.4"],
                  ["1.2", "1.3.0"],
                  ["1.2", "1.3"],
                  ["1.2", "2.1.1"],
                  ["1.2", "2.1"]
                 ]
      examples.each do |smaller, larger|
        sm = Chef::CookbookVersion.new("foo", '/tmp/blah')
        lg = Chef::CookbookVersion.new("foo", '/tmp/blah')
        sm.version = smaller
        lg.version = larger
        sm.should be < lg
        lg.should be > sm
        sm.should_not == lg
      end
    end

    it "should equate versions 1.2 and 1.2.0" do
      a = Chef::CookbookVersion.new("foo", '/tmp/blah')
      b = Chef::CookbookVersion.new("foo", '/tmp/blah')
      a.version = "1.2"
      b.version = "1.2.0"
      a.should == b
    end


    it "should not allow you to sort cookbooks with different names" do
      apt = Chef::CookbookVersion.new "apt", '/tmp/blah'
      apt.version = "1.0"
      god = Chef::CookbookVersion.new "god", '/tmp/blah'
      god.version = "2.0"
      lambda {apt <=> god}.should raise_error(Chef::Exceptions::CookbookVersionNameMismatch)
    end
  end

  describe "when you set a version" do
    before do
      @cbv = Chef::CookbookVersion.new("version validation", '/tmp/blah')
    end
    it "should accept valid cookbook versions" do
      good_versions = %w(1.2 1.2.3 1000.80.50000 0.300.25)
      good_versions.each do |v|
        @cbv.version = v
      end
    end

    it "should raise InvalidVersion for bad cookbook versions" do
      bad_versions = ["1.2.3.4", "1.2.a4", "1", "a", "1.2 3", "1.2 a",
                      "1 2 3", "1-2-3", "1_2_3", "1.2_3", "1.2-3"]
      the_error = Chef::Exceptions::InvalidCookbookVersion
      bad_versions.each do |v|
        lambda {@cbv.version = v}.should raise_error(the_error)
      end
    end

  end

end