summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThom May <thom@may.lt>2017-07-10 13:37:25 +0100
committerGitHub <noreply@github.com>2017-07-10 13:37:25 +0100
commit5199ae451c41d446873868cc021022fb0e7239f0 (patch)
treedf0362433829bb98f50b26fe0752b17b86013361
parented60cc2ec66a8547324605d77fbbed39bf773f4b (diff)
parent35e24b6125089cd6bc71c7092e431562707ee8cd (diff)
downloadchef-5199ae451c41d446873868cc021022fb0e7239f0.tar.gz
Merge pull request #6201 from chef/tm/no_api_version
Have a sensible default for old chef servers
-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)