chef 11.12.0.alpha.1 → 11.12.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/lib/chef/api_client/registration.rb +46 -9
  3. data/lib/chef/application.rb +1 -0
  4. data/lib/chef/application/client.rb +25 -24
  5. data/lib/chef/client.rb +34 -0
  6. data/lib/chef/config.rb +11 -0
  7. data/lib/chef/cookbook/chefignore.rb +10 -2
  8. data/lib/chef/cookbook/metadata.rb +31 -3
  9. data/lib/chef/cookbook/synchronizer.rb +2 -2
  10. data/lib/chef/cookbook/syntax_check.rb +4 -4
  11. data/lib/chef/encrypted_data_bag_item.rb +37 -1
  12. data/lib/chef/exceptions.rb +1 -0
  13. data/lib/chef/guard_interpreter/default_guard_interpreter.rb +42 -0
  14. data/lib/chef/guard_interpreter/resource_guard_interpreter.rb +122 -0
  15. data/lib/chef/http.rb +0 -1
  16. data/lib/chef/http/decompressor.rb +7 -4
  17. data/lib/chef/http/simple.rb +5 -0
  18. data/lib/chef/http/validate_content_length.rb +28 -12
  19. data/lib/chef/knife.rb +1 -0
  20. data/lib/chef/knife/client_bulk_delete.rb +48 -9
  21. data/lib/chef/knife/client_delete.rb +4 -4
  22. data/lib/chef/knife/cookbook_bulk_delete.rb +1 -1
  23. data/lib/chef/knife/cookbook_upload.rb +17 -7
  24. data/lib/chef/knife/core/bootstrap_context.rb +1 -1
  25. data/lib/chef/knife/core/ui.rb +42 -5
  26. data/lib/chef/knife/node_run_list_add.rb +31 -2
  27. data/lib/chef/knife/ssh.rb +44 -31
  28. data/lib/chef/knife/ssl_check.rb +213 -0
  29. data/lib/chef/knife/ssl_fetch.rb +145 -0
  30. data/lib/chef/mixin/deep_merge.rb +13 -5
  31. data/lib/chef/mixin/shell_out.rb +9 -3
  32. data/lib/chef/node.rb +23 -4
  33. data/lib/chef/node/immutable_collections.rb +32 -0
  34. data/lib/chef/platform/provider_mapping.rb +21 -18
  35. data/lib/chef/platform/query_helpers.rb +10 -2
  36. data/lib/chef/policy_builder/expand_node_object.rb +3 -6
  37. data/lib/chef/provider/cron.rb +25 -3
  38. data/lib/chef/provider/mount/mount.rb +1 -1
  39. data/lib/chef/provider/package/dpkg.rb +2 -1
  40. data/lib/chef/provider/package/windows.rb +80 -0
  41. data/lib/chef/provider/package/windows/msi.rb +69 -0
  42. data/lib/chef/provider/powershell_script.rb +19 -6
  43. data/lib/chef/provider/service/solaris.rb +11 -7
  44. data/lib/chef/resource.rb +18 -5
  45. data/lib/chef/resource/conditional.rb +20 -7
  46. data/lib/chef/resource/cron.rb +18 -2
  47. data/lib/chef/resource/execute.rb +0 -2
  48. data/lib/chef/resource/powershell_script.rb +23 -1
  49. data/lib/chef/resource/script.rb +25 -0
  50. data/lib/chef/resource/subversion.rb +4 -0
  51. data/lib/chef/resource/windows_package.rb +79 -0
  52. data/lib/chef/resource/windows_script.rb +0 -5
  53. data/lib/chef/resources.rb +1 -0
  54. data/lib/chef/rest.rb +6 -1
  55. data/lib/chef/run_context.rb +22 -2
  56. data/lib/chef/run_context/cookbook_compiler.rb +12 -0
  57. data/lib/chef/util/editor.rb +92 -0
  58. data/lib/chef/util/file_edit.rb +22 -54
  59. data/lib/chef/version.rb +2 -2
  60. data/lib/chef/win32/api/installer.rb +166 -0
  61. data/lib/chef/win32/version.rb +8 -0
  62. data/spec/data/standalone_cookbook/Gemfile +1 -0
  63. data/spec/data/standalone_cookbook/chefignore +9 -0
  64. data/spec/data/standalone_cookbook/recipes/default.rb +3 -0
  65. data/spec/data/standalone_cookbook/vendor/bundle/ruby/2.0.0/gems/multi_json-1.9.0/lib/multi_json.rb +1 -0
  66. data/spec/functional/resource/powershell_spec.rb +262 -1
  67. data/spec/functional/win32/versions_spec.rb +3 -3
  68. data/spec/integration/knife/chefignore_spec.rb +1 -2
  69. data/spec/integration/knife/raw_spec.rb +8 -13
  70. data/spec/integration/knife/redirection_spec.rb +6 -14
  71. data/spec/integration/solo/solo_spec.rb +19 -0
  72. data/spec/support/shared/functional/windows_script.rb +1 -1
  73. data/spec/support/shared/integration/app_server_support.rb +42 -0
  74. data/spec/support/shared/integration/integration_helper.rb +1 -0
  75. data/spec/support/shared/unit/script_resource.rb +38 -0
  76. data/spec/unit/api_client/registration_spec.rb +109 -38
  77. data/spec/unit/application/client_spec.rb +48 -1
  78. data/spec/unit/cookbook/chefignore_spec.rb +10 -0
  79. data/spec/unit/cookbook/metadata_spec.rb +45 -1
  80. data/spec/unit/cookbook/syntax_check_spec.rb +28 -0
  81. data/spec/unit/cookbook_spec.rb +0 -10
  82. data/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb +56 -0
  83. data/spec/unit/http/simple_spec.rb +32 -0
  84. data/spec/unit/http/validate_content_length_spec.rb +187 -0
  85. data/spec/unit/knife/bootstrap_spec.rb +13 -4
  86. data/spec/unit/knife/client_bulk_delete_spec.rb +123 -38
  87. data/spec/unit/knife/client_delete_spec.rb +4 -4
  88. data/spec/unit/knife/cookbook_upload_spec.rb +181 -88
  89. data/spec/unit/knife/core/bootstrap_context_spec.rb +11 -1
  90. data/spec/unit/knife/core/ui_spec.rb +109 -38
  91. data/spec/unit/knife/node_run_list_add_spec.rb +24 -1
  92. data/spec/unit/knife/ssh_spec.rb +17 -6
  93. data/spec/unit/knife/ssl_check_spec.rb +187 -0
  94. data/spec/unit/knife/ssl_fetch_spec.rb +151 -0
  95. data/spec/unit/mixin/deep_merge_spec.rb +17 -0
  96. data/spec/unit/node/immutable_collections_spec.rb +55 -0
  97. data/spec/unit/node_spec.rb +9 -0
  98. data/spec/unit/platform/query_helpers_spec.rb +32 -0
  99. data/spec/unit/platform_spec.rb +193 -175
  100. data/spec/unit/policy_builder/expand_node_object_spec.rb +1 -1
  101. data/spec/unit/provider/cron_spec.rb +175 -1
  102. data/spec/unit/provider/mount/mount_spec.rb +33 -3
  103. data/spec/unit/provider/package/dpkg_spec.rb +4 -0
  104. data/spec/unit/provider/package/windows/msi_spec.rb +60 -0
  105. data/spec/unit/provider/package/windows_spec.rb +80 -0
  106. data/spec/unit/provider/service/macosx_spec.rb +3 -3
  107. data/spec/unit/provider/service/solaris_smf_service_spec.rb +35 -10
  108. data/spec/unit/pure_application_spec.rb +32 -0
  109. data/spec/unit/recipe_spec.rb +4 -0
  110. data/spec/unit/resource/conditional_spec.rb +13 -12
  111. data/spec/unit/resource/cron_spec.rb +7 -2
  112. data/spec/unit/resource/powershell_spec.rb +85 -2
  113. data/spec/unit/resource/subversion_spec.rb +5 -0
  114. data/spec/unit/resource/windows_package_spec.rb +74 -0
  115. data/spec/unit/resource_spec.rb +23 -1
  116. data/spec/unit/rest_spec.rb +15 -0
  117. data/spec/unit/run_context/cookbook_compiler_spec.rb +12 -0
  118. data/spec/unit/run_context_spec.rb +7 -0
  119. data/spec/unit/util/editor_spec.rb +152 -0
  120. data/spec/unit/util/file_edit_spec.rb +37 -1
  121. metadata +41 -30
@@ -127,7 +127,7 @@ describe Chef::Application::Client, "configure_chef" do
127
127
  end
128
128
 
129
129
  describe Chef::Application::Client, "run_application", :unix_only do
130
- before do
130
+ before(:each) do
131
131
  @pipe = IO.pipe
132
132
  @app = Chef::Application::Client.new
133
133
  @app.stub(:run_chef_client) do
@@ -147,4 +147,51 @@ describe Chef::Application::Client, "run_application", :unix_only do
147
147
  IO.select([@pipe[0]], nil, nil, 0).should_not be_nil
148
148
  @pipe[0].gets.should == "finished\n"
149
149
  end
150
+
151
+ describe "when splay is set" do
152
+ before do
153
+ Chef::Config[:splay] = 10
154
+ Chef::Config[:interval] = 10
155
+
156
+ run_count = 0
157
+
158
+ # uncomment to debug failures...
159
+ # Chef::Log.init($stderr)
160
+ # Chef::Log.level = :debug
161
+
162
+ @app.stub(:run_chef_client) do
163
+
164
+ run_count += 1
165
+ if run_count > 3
166
+ exit 0
167
+ end
168
+
169
+ # If everything is fine, sending USR1 to self should prevent
170
+ # app to go into splay sleep forever.
171
+ Process.kill("USR1", Process.pid)
172
+ end
173
+
174
+ number_of_sleep_calls = 0
175
+
176
+ # This is a very complicated way of writing
177
+ # @app.should_receive(:sleep).once.
178
+ # We have to do it this way because the main loop of
179
+ # Chef::Application::Client swallows most exceptions, and we need to be
180
+ # able to expose our expectation failures to the parent process in the test.
181
+ @app.stub(:sleep) do |arg|
182
+ number_of_sleep_calls += 1
183
+ if number_of_sleep_calls > 1
184
+ exit 127
185
+ end
186
+ end
187
+ end
188
+
189
+ it "shouldn't sleep when sent USR1" do
190
+ pid = fork do
191
+ @app.run_application
192
+ end
193
+ _pid, result = Process.waitpid2(pid)
194
+ result.exitstatus.should == 0
195
+ end
196
+ end
150
197
  end
@@ -36,4 +36,14 @@ describe Chef::Cookbook::Chefignore do
36
36
  @chefignore.ignored?('recipes/ignoreme.rb').should be_true
37
37
  @chefignore.ignored?('recipes/dontignoreme.rb').should be_false
38
38
  end
39
+
40
+ context "when using the single cookbook pattern" do
41
+ before do
42
+ @chefignore = Chef::Cookbook::Chefignore.new(File.join(CHEF_SPEC_DATA, 'standalone_cookbook'))
43
+ end
44
+
45
+ it "loads the globs in the chefignore file" do
46
+ @chefignore.ignores.should =~ %w[recipes/ignoreme.rb ignored vendor/bundle/*]
47
+ end
48
+ end
39
49
  end
@@ -402,7 +402,7 @@ describe Chef::Cookbook::Metadata do
402
402
  @meta.attributes["db/mysql/databases"][:recipes].should == []
403
403
  end
404
404
 
405
- it "should allow the default value to be a string, array, or hash" do
405
+ it "should allow the default value to be a string, array, hash, boolean or numeric" do
406
406
  lambda {
407
407
  @meta.attribute("db/mysql/databases", :default => [])
408
408
  }.should_not raise_error
@@ -412,11 +412,55 @@ describe Chef::Cookbook::Metadata do
412
412
  lambda {
413
413
  @meta.attribute("db/mysql/databases", :default => "alice in chains")
414
414
  }.should_not raise_error
415
+ lambda {
416
+ @meta.attribute("db/mysql/databases", :default => 1337)
417
+ }.should_not raise_error
418
+ lambda {
419
+ @meta.attribute("db/mysql/databases", :default => true)
420
+ }.should_not raise_error
415
421
  lambda {
416
422
  @meta.attribute("db/mysql/databases", :required => :not_gonna_do_it)
417
423
  }.should raise_error(ArgumentError)
418
424
  end
419
425
 
426
+ it "should limit the types allowed in the choice array" do
427
+ options = {
428
+ :type => "string",
429
+ :choice => [ "test1", "test2" ],
430
+ :default => "test1"
431
+ }
432
+ lambda {
433
+ @meta.attribute("test_cookbook/test", options)
434
+ }.should_not raise_error
435
+
436
+ options = {
437
+ :type => "boolean",
438
+ :choice => [ true, false ],
439
+ :default => true
440
+ }
441
+ lambda {
442
+ @meta.attribute("test_cookbook/test", options)
443
+ }.should_not raise_error
444
+
445
+ options = {
446
+ :type => "numeric",
447
+ :choice => [ 1337, 420 ],
448
+ :default => 1337
449
+ }
450
+ lambda {
451
+ @meta.attribute("test_cookbook/test", options)
452
+ }.should_not raise_error
453
+
454
+ options = {
455
+ :type => "numeric",
456
+ :choice => [ true, "false" ],
457
+ :default => false
458
+ }
459
+ lambda {
460
+ @meta.attribute("test_cookbook/test", options)
461
+ }.should raise_error
462
+ end
463
+
420
464
  it "should error if default used with calculated" do
421
465
  lambda {
422
466
  attrs = {
@@ -24,6 +24,17 @@ describe Chef::Cookbook::SyntaxCheck do
24
24
  let(:cookbook_path) { File.join(CHEF_SPEC_DATA, 'cookbooks', 'openldap') }
25
25
  let(:syntax_check) { Chef::Cookbook::SyntaxCheck.new(cookbook_path) }
26
26
 
27
+ let(:open_ldap_cookbook_files) {
28
+ %w{ attributes/default.rb
29
+ attributes/smokey.rb
30
+ definitions/client.rb
31
+ definitions/server.rb
32
+ metadata.rb
33
+ recipes/default.rb
34
+ recipes/gigantor.rb
35
+ recipes/one.rb }.map{ |f| File.join(cookbook_path, f) }
36
+ }
37
+
27
38
  before do
28
39
  Chef::Log.logger = Logger.new(StringIO.new)
29
40
  Chef::Log.level = :warn # suppress "Syntax OK" messages
@@ -47,6 +58,23 @@ describe Chef::Cookbook::SyntaxCheck do
47
58
  Chef::Config[:cookbook_path] = File.dirname(cookbook_path)
48
59
  syntax_check = Chef::Cookbook::SyntaxCheck.for_cookbook(:openldap)
49
60
  syntax_check.cookbook_path.should == cookbook_path
61
+ syntax_check.ruby_files.sort.should == open_ldap_cookbook_files.sort
62
+ end
63
+
64
+ it "creates a syntax checker given the cookbook name and cookbook_path" do
65
+ syntax_check = Chef::Cookbook::SyntaxCheck.for_cookbook(:openldap, File.join(CHEF_SPEC_DATA, 'cookbooks'))
66
+ syntax_check.cookbook_path.should == cookbook_path
67
+ syntax_check.ruby_files.sort.should == open_ldap_cookbook_files.sort
68
+ end
69
+
70
+ context "when using a standalone cookbook" do
71
+ let(:cookbook_path) { File.join(CHEF_SPEC_DATA, 'standalone_cookbook') }
72
+
73
+ it "creates a syntax checker given the cookbook name and cookbook_path for a standalone cookbook" do
74
+ syntax_check = Chef::Cookbook::SyntaxCheck.for_cookbook(:standalone_cookbook, CHEF_SPEC_DATA)
75
+ syntax_check.cookbook_path.should == cookbook_path
76
+ syntax_check.ruby_files.should == [File.join(cookbook_path, 'recipes/default.rb')]
77
+ end
50
78
  end
51
79
 
52
80
  describe "when first created" do
@@ -68,16 +68,6 @@ describe Chef::CookbookVersion do
68
68
  @cookbook.preferred_filename(@node, :files, 'a-filename', 'the-checksum').should be_nil
69
69
  end
70
70
 
71
- it "should allow you to include a fully-qualified recipe using the DSL" do
72
- # DSL method include_recipe allows multiple arguments, so extract the first
73
- @node.should_receive(:loaded_recipe).with(:openldap, "gigantor")
74
- recipe = @run_context.include_recipe("openldap::gigantor").first
75
-
76
- recipe.recipe_name.should == "gigantor"
77
- recipe.cookbook_name.should == :openldap
78
- @run_context.resource_collection[0].name.should == "blanket"
79
- end
80
-
81
71
  it "should raise an ArgumentException if you try to load a bad recipe name" do
82
72
  lambda { @cookbook.load_recipe("doesnt_exist", @node) }.should raise_error(ArgumentError)
83
73
  end
@@ -0,0 +1,56 @@
1
+ #
2
+ # Author:: Adam Edwards (<adamed@getchef.com>)
3
+ # Copyright:: Copyright (c) 2014 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Chef::GuardInterpreter::ResourceGuardInterpreter do
22
+ before(:each) do
23
+ node = Chef::Node.new
24
+
25
+ node.default["kernel"] = Hash.new
26
+ node.default["kernel"][:machine] = :x86_64.to_s
27
+
28
+ run_context = Chef::RunContext.new(node, nil, nil)
29
+
30
+ @resource = Chef::Resource.new("powershell_unit_test", run_context)
31
+ @resource.stub(:run_action)
32
+ @resource.stub(:updated).and_return(true)
33
+ end
34
+
35
+ describe "when evaluating a guard resource" do
36
+ let(:resource) { @resource }
37
+
38
+ it "should allow guard interpreter to be set to Chef::Resource::Script" do
39
+ resource.guard_interpreter(:script)
40
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
41
+ resource.only_if("echo hi")
42
+ end
43
+
44
+ it "should allow guard interpreter to be set to Chef::Resource::PowershellScript derived indirectly from Chef::Resource::Script" do
45
+ resource.guard_interpreter(:powershell_script)
46
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
47
+ resource.only_if("echo hi")
48
+ end
49
+
50
+ it "should raise an exception if guard_interpreter is set to a resource not derived from Chef::Resource::Script" do
51
+ resource.guard_interpreter(:file)
52
+ expect { resource.only_if("echo hi") }.to raise_error ArgumentError
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,32 @@
1
+ #
2
+ # Author:: Serdar Sutay (<serdar@opscode.com>)
3
+ # Copyright:: Copyright (c) 2014 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Chef::HTTP::Simple do
22
+ it "should have content length validation middleware after compressor middleware" do
23
+ client = Chef::HTTP::Simple.new("dummy.com")
24
+ middlewares = client.instance_variable_get(:@middlewares)
25
+ content_length = middlewares.find_index { |e| e.is_a? Chef::HTTP::ValidateContentLength }
26
+ decompressor = middlewares.find_index { |e| e.is_a? Chef::HTTP::Decompressor }
27
+
28
+ content_length.should_not be_nil
29
+ decompressor.should_not be_nil
30
+ (decompressor < content_length).should be_true
31
+ end
32
+ end
@@ -0,0 +1,187 @@
1
+ #
2
+ # Author:: Serdar Sutay (<serdar@opscode.com>)
3
+ # Copyright:: Copyright (c) 2014 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+ require 'stringio'
21
+
22
+ describe Chef::HTTP::ValidateContentLength do
23
+ class TestClient < Chef::HTTP
24
+ use Chef::HTTP::ValidateContentLength
25
+ end
26
+
27
+ let(:method) { "GET" }
28
+ let(:url) { "http://dummy.com" }
29
+ let(:headers) { {} }
30
+ let(:data) { false }
31
+
32
+ let(:request) { }
33
+ let(:return_value) { "200" }
34
+
35
+ # Test Variables
36
+ let(:request_type) { :streaming }
37
+ let(:content_length_value) { 23 }
38
+ let(:streaming_length) { 23 }
39
+ let(:response_body) { "Thanks for checking in." }
40
+ let(:response_headers) {
41
+ {
42
+ "content-length" => content_length_value
43
+ }
44
+ }
45
+
46
+ let(:response) {
47
+ m = double('HttpResponse', :body => response_body)
48
+ m.stub(:[]) do |key|
49
+ response_headers[key]
50
+ end
51
+
52
+ m
53
+ }
54
+
55
+ let(:middleware) {
56
+ client = TestClient.new(url)
57
+ client.middlewares[0]
58
+ }
59
+
60
+ def run_content_length_validation
61
+ stream_handler = middleware.stream_response_handler(response)
62
+ middleware.handle_request(method, url, headers, data)
63
+
64
+ case request_type
65
+ when :streaming
66
+ # First stream the data
67
+ data_length = streaming_length
68
+ while data_length > 0
69
+ chunk_size = data_length > 10 ? 10 : data_length
70
+ stream_handler.handle_chunk(double("Chunk", :bytesize => chunk_size))
71
+ data_length -= chunk_size
72
+ end
73
+
74
+ # Finally call stream complete
75
+ middleware.handle_stream_complete(response, request, return_value)
76
+ when :direct
77
+ middleware.handle_response(response, request, return_value)
78
+ else
79
+ raise "Unknown request_type: #{request_type}"
80
+ end
81
+ end
82
+
83
+ let(:debug_stream) { StringIO.new }
84
+ let(:debug_output) { debug_stream.string }
85
+
86
+ before(:each) {
87
+ Chef::Log.level = :debug
88
+ Chef::Log.stub(:debug) do |message|
89
+ debug_stream.puts message
90
+ end
91
+ }
92
+
93
+ describe "without response body" do
94
+ let(:request_type) { :direct }
95
+ let(:response_body) { "Thanks for checking in." }
96
+
97
+ it "shouldn't raise error" do
98
+ lambda { run_content_length_validation }.should_not raise_error
99
+ end
100
+ end
101
+
102
+ describe "without Content-Length header" do
103
+ let(:response_headers) { { } }
104
+
105
+ [ "direct", "streaming" ].each do |req_type|
106
+ describe "when running #{req_type} request" do
107
+ let(:request_type) { req_type.to_sym }
108
+
109
+ it "should skip validation and log for debug" do
110
+ run_content_length_validation
111
+ debug_output.should include("HTTP server did not include a Content-Length header in response")
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ describe "with correct Content-Length header" do
118
+ [ "direct", "streaming" ].each do |req_type|
119
+ describe "when running #{req_type} request" do
120
+ let(:request_type) { req_type.to_sym }
121
+
122
+ it "should validate correctly" do
123
+ run_content_length_validation
124
+ debug_output.should include("Content-Length validated correctly.")
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ describe "with wrong Content-Length header" do
131
+ let(:content_length_value) { 25 }
132
+ [ "direct", "streaming" ].each do |req_type|
133
+ describe "when running #{req_type} request" do
134
+ let(:request_type) { req_type.to_sym }
135
+
136
+ it "should raise ContentLengthMismatch error" do
137
+ lambda { run_content_length_validation }.should raise_error(Chef::Exceptions::ContentLengthMismatch)
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ describe "when download is interrupted" do
144
+ let(:streaming_length) { 12 }
145
+
146
+ it "should raise ContentLengthMismatch error" do
147
+ lambda { run_content_length_validation }.should raise_error(Chef::Exceptions::ContentLengthMismatch)
148
+ end
149
+ end
150
+
151
+ describe "when Transfer-Encoding & Content-Length is set" do
152
+ let(:response_headers) {
153
+ {
154
+ "content-length" => content_length_value,
155
+ "transfer-encoding" => "chunked"
156
+ }
157
+ }
158
+
159
+ [ "direct", "streaming" ].each do |req_type|
160
+ describe "when running #{req_type} request" do
161
+ let(:request_type) { req_type.to_sym }
162
+
163
+ it "should skip validation and log for debug" do
164
+ run_content_length_validation
165
+ debug_output.should include("Transfer-Encoding header is set, skipping Content-Length check.")
166
+ end
167
+ end
168
+ end
169
+ end
170
+
171
+ describe "when client is being reused" do
172
+ before do
173
+ run_content_length_validation
174
+ debug_output.should include("Content-Length validated correctly.")
175
+ end
176
+
177
+ it "should reset internal counter" do
178
+ middleware.instance_variable_get(:@content_length_counter).should be_nil
179
+ end
180
+
181
+ it "should validate correctly second time" do
182
+ run_content_length_validation
183
+ debug_output.should include("Content-Length validated correctly.")
184
+ end
185
+ end
186
+
187
+ end