From 35e24b6125089cd6bc71c7092e431562707ee8cd Mon Sep 17 00:00:00 2001 From: Thom May Date: Wed, 7 Jun 2017 16:09:01 +0100 Subject: Have a sensible default for old chef servers If we don't see a version header, we should default to only supporting API version 0, and if we can't support that, then we should die as usual Signed-off-by: Thom May --- lib/chef/http/api_versions.rb | 2 ++ lib/chef/server_api_versions.rb | 23 +++++++++++++++++++++-- spec/unit/server_api_versions_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/chef/http/api_versions.rb b/lib/chef/http/api_versions.rb index 674d8f85a7..6c5ede40aa 100644 --- a/lib/chef/http/api_versions.rb +++ b/lib/chef/http/api_versions.rb @@ -37,6 +37,8 @@ class Chef end if http_response.key?("x-ops-server-api-version") ServerAPIVersions.instance.set_versions(JSONCompat.parse(http_response["x-ops-server-api-version"])) + else + ServerAPIVersions.instance.unversioned! end [http_response, rest_request, return_value] end diff --git a/lib/chef/server_api_versions.rb b/lib/chef/server_api_versions.rb index 2a4d0e6a5b..40fb6385e1 100644 --- a/lib/chef/server_api_versions.rb +++ b/lib/chef/server_api_versions.rb @@ -26,15 +26,34 @@ class Chef end def min_server_version - !@versions.nil? ? Integer(@versions["min_version"]) : nil + # If we're working with a pre-api-versioning server, always claim to be zero + if @versions.nil? + unversioned? ? 0 : nil + else + Integer(@versions["min_version"]) + end end def max_server_version - !@versions.nil? ? Integer(@versions["max_version"]) : nil + # If we're working with a pre-api-versioning server, always claim to be zero + if @versions.nil? + unversioned? ? 0 : nil + else + Integer(@versions["max_version"]) + end + end + + def unversioned! + @unversioned = true + end + + def unversioned? + @unversioned end def reset! @versions = nil + @unversioned = false end end end diff --git a/spec/unit/server_api_versions_spec.rb b/spec/unit/server_api_versions_spec.rb index 43445eb825..1dab0548cb 100644 --- a/spec/unit/server_api_versions_spec.rb +++ b/spec/unit/server_api_versions_spec.rb @@ -22,10 +22,28 @@ describe Chef::ServerAPIVersions do Chef::ServerAPIVersions.instance.reset! end + describe "#reset!" do + it "resets the version information" do + Chef::ServerAPIVersions.instance.set_versions({ "min_version" => 0, "max_version" => 2 }) + Chef::ServerAPIVersions.instance.reset! + expect(Chef::ServerAPIVersions.instance.min_server_version).to be_nil + end + + it "resets the unversioned flag" do + Chef::ServerAPIVersions.instance.unversioned! + Chef::ServerAPIVersions.instance.reset! + expect(Chef::ServerAPIVersions.instance.unversioned?).to be false + end + end + describe "#min_server_version" do it "returns nil if no versions have been recorded" do expect(Chef::ServerAPIVersions.instance.min_server_version).to be_nil end + it "returns 0 if unversioned" do + Chef::ServerAPIVersions.instance.unversioned! + expect(Chef::ServerAPIVersions.instance.min_server_version).to eq(0) + end it "returns the correct value" do Chef::ServerAPIVersions.instance.set_versions({ "min_version" => 0, "max_version" => 2 }) expect(Chef::ServerAPIVersions.instance.min_server_version).to eq(0) @@ -36,6 +54,10 @@ describe Chef::ServerAPIVersions do it "returns nil if no versions have been recorded" do expect(Chef::ServerAPIVersions.instance.max_server_version).to be_nil end + it "returns 0 if unversioned" do + Chef::ServerAPIVersions.instance.unversioned! + expect(Chef::ServerAPIVersions.instance.min_server_version).to eq(0) + end it "returns the correct value" do Chef::ServerAPIVersions.instance.set_versions({ "min_version" => 0, "max_version" => 2 }) expect(Chef::ServerAPIVersions.instance.max_server_version).to eq(2) -- cgit v1.2.1