diff options
-rw-r--r-- | lib/chef/provider/package/snap.rb | 62 | ||||
-rw-r--r-- | spec/unit/provider/package/snap_spec.rb | 20 | ||||
-rw-r--r-- | spec/unit/resource/snap_package_spec.rb | 2 |
3 files changed, 42 insertions, 42 deletions
diff --git a/lib/chef/provider/package/snap.rb b/lib/chef/provider/package/snap.rb index e3c60dca12..324de2f486 100644 --- a/lib/chef/provider/package/snap.rb +++ b/lib/chef/provider/package/snap.rb @@ -19,8 +19,8 @@ require "chef/provider/package" require "chef/resource/snap_package" require "chef/mixin/shell_out" -require 'socket' -require 'json' +require "socket" +require "json" class Chef class Provider @@ -130,14 +130,14 @@ class Chef "Accept: application/json\r\n" + "Content-Type: application/json\r\n" if method == "POST" - request.concat("Content-Length: #{post_data.bytesize.to_s}\r\n\r\n#{post_data}") + request.concat("Content-Length: #{post_data.bytesize}\r\n\r\n#{post_data}") end request.concat("\r\n") # While it is expected to allow clients to connect using HTTPS over a TCP socket, # at this point only a UNIX socket is supported. The socket is /run/snapd.socket # Note - UNIXSocket is not defined on windows systems if defined?(::UNIXSocket) - UNIXSocket.open('/run/snapd.socket') do |socket| + UNIXSocket.open("/run/snapd.socket") do |socket| # Send request, read the response, split the response and parse the body socket.print(request) response = socket.read @@ -148,14 +148,14 @@ class Chef end def get_change_id(id) - call_snap_api('GET', "/v2/changes/#{id}") + call_snap_api("GET", "/v2/changes/#{id}") end def get_id_from_async_response(response) - if response['type'] == 'error' - raise "status: #{response['status']}, kind: #{response['result']['kind']}, message: #{response['result']['message']}" + if response["type"] == "error" + raise "status: #{response["status"]}, kind: #{response["result"]["kind"]}, message: #{response["result"]["message"]}" end - response['change'] + response["change"] end def wait_for_completion(id) @@ -163,8 +163,8 @@ class Chef waiting = true while waiting do result = get_change_id(id) - puts "STATUS: #{result['result']['status']}" - case result['result']['status'] + puts "STATUS: #{result["result"]["status"]}" + case result["result"]["status"] when "Do", "Doing", "Undoing", "Undo" # Continue when "Abort" @@ -189,7 +189,7 @@ class Chef def get_snap_version_from_source(path) body = { "context-id" => "get_snap_version_from_source_#{path}", - "args" => ["info", path] + "args" => ["info", path,] }.to_json #json = call_snap_api('POST', '/v2/snapctl', body) @@ -211,19 +211,19 @@ class Chef end def install_snaps(snap_names) - response = post_snaps(snap_names, 'install', new_resource.channel, new_resource.options) + response = post_snaps(snap_names, "install", new_resource.channel, new_resource.options) id = get_id_from_async_response(response) wait_for_completion(id) end def update_snaps(snap_names) - response = post_snaps(snap_names, 'refresh', new_resource.channel, new_resource.options) + response = post_snaps(snap_names, "refresh", new_resource.channel, new_resource.options) id = get_id_from_async_response(response) wait_for_completion(id) end def uninstall_snaps(snap_names) - response = post_snaps(snap_names, 'remove', new_resource.channel, new_resource.options) + response = post_snaps(snap_names, "remove", new_resource.channel, new_resource.options) id = get_id_from_async_response(response) wait_for_completion(id) end @@ -240,18 +240,18 @@ class Chef "action" => action, "snaps" => snap_names } - if ['install', 'refresh', 'switch'].include?(action) - request['channel'] = channel + if %w(install refresh switch).include?(action) + request["channel"] = channel end # No defensive handling of params # Snap will throw the proper exception if called improperly # And we can provide that exception to the end user - request['classic'] = true if options['classic'] - request['devmode'] = true if options['devmode'] - request['jailmode'] = true if options['jailmode'] - request['revision'] = revision unless revision.nil? - request['ignore_validation'] = true if options['ignore-validation'] + request["classic"] = true if options["classic"] + request["devmode"] = true if options["devmode"] + request["jailmode"] = true if options["jailmode"] + request["revision"] = revision unless revision.nil? + request["ignore_validation"] = true if options["ignore-validation"] request end @@ -264,26 +264,26 @@ class Chef # @param revision [String] A revision/version def post_snaps(snap_names, action, channel, options, revision = nil) json = generate_snap_json(snap_names, action, channel, options, revision = nil) - call_snap_api('POST', '/v2/snaps', json) + call_snap_api("POST", "/v2/snaps", json) end def get_latest_package_version(name, channel) - json = call_snap_api('GET', "/v2/find?name=#{name}") + json = call_snap_api("GET", "/v2/find?name=#{name}") if json["status-code"] != 200 raise Chef::Exceptions::Package, json["result"], caller end # Return the version matching the channel - json['result'][0]['channels']["latest/#{channel}"]['version'] + json["result"][0]["channels"]["latest/#{channel}"]["version"] end def get_installed_packages - json = call_snap_api('GET', '/v2/snaps') + json = call_snap_api("GET", "/v2/snaps") # We only allow 200 or 404s unless [200, 404].include? json["status-code"] raise Chef::Exceptions::Package, json["result"], caller end - json['result'] + json["result"] end def get_installed_package_version_by_name(name) @@ -297,21 +297,21 @@ class Chef end def get_installed_package_by_name(name) - json = call_snap_api('GET', "/v2/snaps/#{name}") + json = call_snap_api("GET", "/v2/snaps/#{name}") # We only allow 200 or 404s unless [200, 404].include? json["status-code"] raise Chef::Exceptions::Package, json["result"], caller end - json['result'] + json["result"] end def get_installed_package_conf(name) - json = call_snap_api('GET', "/v2/snaps/#{name}/conf") - json['result'] + json = call_snap_api("GET", "/v2/snaps/#{name}/conf") + json["result"] end def set_installed_package_conf(name, value) - response = call_snap_api('PUT', "/v2/snaps/#{name}/conf", value) + response = call_snap_api("PUT", "/v2/snaps/#{name}/conf", value) id = get_id_from_async_response(response) wait_for_completion(id) end diff --git a/spec/unit/provider/package/snap_spec.rb b/spec/unit/provider/package/snap_spec.rb index 9a1d1401f5..7638a17467 100644 --- a/spec/unit/provider/package/snap_spec.rb +++ b/spec/unit/provider/package/snap_spec.rb @@ -16,9 +16,9 @@ # require "spec_helper" -require 'chef/provider/package' -require 'chef/provider/package/snap' -require 'json' +require "chef/provider/package" +require "chef/provider/package/snap" +require "json" describe Chef::Provider::Package::Snap do let(:node) { Chef::Node.new } @@ -57,7 +57,7 @@ describe Chef::Provider::Package::Snap do "type" => "async", "status-code" => 202, "status" => "Accepted", - "change" => "401" + "change" => "401", }.to_json) result_fail = JSON.parse({ "type" => "error", @@ -71,7 +71,7 @@ describe Chef::Provider::Package::Snap do change_id_result = JSON.parse({"type" => "sync", "status-code" => 200, "status" => "OK", "result" => {"id" => "15", "kind" => "install-snap", "summary" => "Install snap \"hello\"", "status" => "Done", "tasks" => [{"id" => "165", "kind" => "prerequisites", "summary" => "Ensure prerequisites for \"hello\" are available", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.22104314Z", "ready-time" => "2018-09-22T20:25:25.231090966Z"}, {"id" => "166", "kind" => "download-snap", "summary" => "Download snap \"hello\" (20) from channel \"stable\"", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221070859Z", "ready-time" => "2018-09-22T20:25:25.24321909Z"}, {"id" => "167", "kind" => "validate-snap", "summary" => "Fetch and check assertions for snap \"hello\" (20)", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221080163Z", "ready-time" => "2018-09-22T20:25:25.55308904Z"}, {"id" => "168", "kind" => "mount-snap", "summary" => "Mount snap \"hello\" (20)", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221082984Z", "ready-time" => "2018-09-22T20:25:25.782452658Z"}, {"id" => "169", "kind" => "copy-snap-data", "summary" => "Copy snap \"hello\" data", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221085677Z", "ready-time" => "2018-09-22T20:25:25.790911883Z"}, {"id" => "170", "kind" => "setup-profiles", "summary" => "Setup snap \"hello\" (20) security profiles", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221088261Z", "ready-time" => "2018-09-22T20:25:25.972796111Z"}, {"id" => "171", "kind" => "link-snap", "summary" => "Make snap \"hello\" (20) available to the system", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221090669Z", "ready-time" => "2018-09-22T20:25:25.986931331Z"}, {"id" => "172", "kind" => "auto-connect", "summary" => "Automatically connect eligible plugs and slots of snap \"hello\"", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221093357Z", "ready-time" => "2018-09-22T20:25:25.996914144Z"}, {"id" => "173", "kind" => "set-auto-aliases", "summary" => "Set automatic aliases for snap \"hello\"", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221097651Z", "ready-time" => "2018-09-22T20:25:26.009155888Z"}, {"id" => "174", "kind" => "setup-aliases", "summary" => "Setup snap \"hello\" aliases", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221100379Z", "ready-time" => "2018-09-22T20:25:26.021062388Z"}, {"id" => "175", "kind" => "run-hook", "summary" => "Run install hook of \"hello\" snap if present", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221103116Z", "ready-time" => "2018-09-22T20:25:26.031383884Z"}, {"id" => "176", "kind" => "start-snap-services", "summary" => "Start snap \"hello\" (20) services", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221110251Z", "ready-time" => "2018-09-22T20:25:26.039564637Z"}, {"id" => "177", "kind" => "run-hook", "summary" => "Run configure hook of \"hello\" snap if present", "status" => "Done", "progress" => {"label" => "", "done" => 1, "total" => 1}, "spawn-time" => "2018-09-22T20:25:25.221115952Z", "ready-time" => "2018-09-22T20:25:26.05069451Z"}], "ready" => true, "spawn-time" => "2018-09-22T20:25:25.221130149Z", "ready-time" => "2018-09-22T20:25:26.050696298Z", "data" => {"snap-names" => ["hello"]}}}.to_json) - get_conf_success = JSON.parse({"type" => "sync", "status-code" => 200, "status" => "OK", "result" =>{"address" =>"0.0.0.0","allow-privileged" =>true,"anonymous-auth" => false}}.to_json) + get_conf_success = JSON.parse({"type" => "sync", "status-code" => 200, "status" => "OK", "result" => {"address" => "0.0.0.0", "allow-privileged" => true, "anonymous-auth" => false}}.to_json) describe "#define_resource_requirements" do @@ -91,7 +91,7 @@ describe Chef::Provider::Package::Snap do end describe "when using a local file source" do - let(:source) { '/tmp/hello_20.snap' } + let(:source) { "/tmp/hello_20.snap" } before do allow_any_instance_of(Chef::Provider::Package::Snap).to receive(:call_snap_api).with('GET', "/v2/snaps/#{package}").and_return(get_by_name_result_success) @@ -146,7 +146,7 @@ describe Chef::Provider::Package::Snap do it "throws an error if candidate version not found" do provider.load_current_resource - expect{provider.candidate_version}.to raise_error(Chef::Exceptions::Package) + expect { provider.candidate_version }.to raise_error(Chef::Exceptions::Package) end it "does not throw an error if installed version not found" do @@ -176,9 +176,9 @@ describe Chef::Provider::Package::Snap do describe Chef::Provider::Package::Snap do it "should post the correct json" do - snap_names = ['hello'] - action = 'install' - channel = 'stable' + snap_names = ["hello"] + action = "install" + channel = "stable" options = {} revision = nil actual = provider.send(:generate_snap_json, snap_names, action, channel, options, revision) diff --git a/spec/unit/resource/snap_package_spec.rb b/spec/unit/resource/snap_package_spec.rb index 5d30c2c0bf..2155645fcf 100644 --- a/spec/unit/resource/snap_package_spec.rb +++ b/spec/unit/resource/snap_package_spec.rb @@ -18,7 +18,7 @@ require "spec_helper" require "chef/resource/snap_package" -require 'chef/provider/package/snap' +require "chef/provider/package/snap" require "support/shared/unit/resource/static_provider_resolution" describe Chef::Resource::SnapPackage, "initialize" do |