diff options
author | Tim Smith <tsmith@chef.io> | 2021-07-03 11:39:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-03 11:39:30 -0700 |
commit | 82ce7634b12054891fb0c8496bf82cb22cfe8028 (patch) | |
tree | ce4a892d19536722562d5174f2d3e8e593ca586a | |
parent | 0c6e106b57e4d19036ae06bba5ebeaa6fbac3328 (diff) | |
parent | ffea38b6c84f96197fbb05b0768d01c2345e20a9 (diff) | |
download | ohai-82ce7634b12054891fb0c8496bf82cb22cfe8028.tar.gz |
Merge pull request #1672 from liu-song-6/livepatch
Add plugin for linux livepatch
-rw-r--r-- | lib/ohai/plugins/linux/livepatch.rb | 38 | ||||
-rw-r--r-- | spec/unit/plugins/linux/livepatch_spec.rb | 62 |
2 files changed, 100 insertions, 0 deletions
diff --git a/lib/ohai/plugins/linux/livepatch.rb b/lib/ohai/plugins/linux/livepatch.rb new file mode 100644 index 00000000..a8e33169 --- /dev/null +++ b/lib/ohai/plugins/linux/livepatch.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true +# +# Author:: Song Liu <song@kernel.org> +# Copyright:: Copyright (c) 2021 Facebook, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +Ohai.plugin(:Livepatch) do + provides "livepatch" + + collect_data(:linux) do + if file_exist?("/sys/kernel/livepatch") + patches = Mash.new + dir_glob("/sys/kernel/livepatch/*").each do |livepatch_dir| + dir = File.basename(livepatch_dir) + patches[dir] = Mash.new + %w{enabled transition}.each do |check| + if file_exist?("/sys/kernel/livepatch/#{dir}/#{check}") + file_open("/sys/kernel/livepatch/#{dir}/#{check}") { |f| patches[dir][check] = f.read_nonblock(1024).strip } + end + end + livepatch patches + end + end + end +end diff --git a/spec/unit/plugins/linux/livepatch_spec.rb b/spec/unit/plugins/linux/livepatch_spec.rb new file mode 100644 index 00000000..b02cba96 --- /dev/null +++ b/spec/unit/plugins/linux/livepatch_spec.rb @@ -0,0 +1,62 @@ +# +# Author:: Song Liu <song@kernel.org> +# Copyright:: Copyright (c) 2021 Facebook, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe Ohai::System, "Linux Livepatch Plugin" do + PATCHES = { + "hotfix1" => { + "enabled" => "1", + "transition" => "0", + }, + "hotfix2" => { + "enabled" => "0", + "transition" => "1", + }, + }.freeze + + def file_double(value) + tmp_double = double + expect(tmp_double).to receive(:read_nonblock).and_return(value) + tmp_double + end + + before do + @plugin = get_plugin("linux/livepatch") + allow(@plugin).to receive(:collect_os).and_return(:linux) + allow(File).to receive(:exist?).with("/sys/kernel/livepatch").and_return(true) + allow(@plugin).to receive(:dir_glob).with("/sys/kernel/livepatch/*") do + PATCHES.collect { |patch, _files| "/sys/kernel/livepatch/#{patch}" } + end + + PATCHES.each do |patch, checks| + allow(File).to receive(:exist?).with("/sys/kernel/livepatch/#{patch}").and_return(true) + checks.each do |check, value| + allow(File).to receive(:exist?).with("/sys/kernel/livepatch/#{patch}/#{check}").and_return(true) + allow(File).to receive(:open).with("/sys/kernel/livepatch/#{patch}/#{check}").and_yield(file_double(value)) + end + end + end + + it "collects all relevant data from livepatches" do + @plugin.run + PATCHES.each do |patch, checks| + expect(@plugin[:livepatch][patch.to_sym]).to include(checks) + end + end +end |