summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThom May <thom@chef.io>2017-06-07 16:09:01 +0100
committerThom May <thom@chef.io>2017-07-05 12:20:35 +0100
commit35e24b6125089cd6bc71c7092e431562707ee8cd (patch)
treebf8384c3ae80a28f2f57012d67fd541ea92c1ffd
parente419d7f0eb0c494a0a2f5d5963eac6fb35153bac (diff)
downloadchef-tm/no_api_version.tar.gz
Have a sensible default for old chef serverstm/no_api_version
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 <thom@chef.io>
-rw-r--r--lib/chef/http/api_versions.rb2
-rw-r--r--lib/chef/server_api_versions.rb23
-rw-r--r--spec/unit/server_api_versions_spec.rb22
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)