spec/unit/health_check_spec.rb in omnibus-8.2.2 vs spec/unit/health_check_spec.rb in omnibus-8.3.2

- old
+ new

@@ -97,13 +97,51 @@ end context "on linux" do before { stub_ohai(platform: "ubuntu", version: "16.04") } + # file_list just needs to have one file which is inside of the install_dir + let(:file_list) do + double("Mixlib::Shellout", + error!: false, + stdout: <<~EOH + /opt/chefdk/shouldnt/matter + EOH + ) + end + + let(:empty_list) do + double("Mixlib::Shellout", + error!: false, + stdout: <<~EOH + EOH + ) + end + + let(:failed_list) do + failed_list = double("Mixlib::Shellout", + stdout: <<~EOH + /opt/chefdk/shouldnt/matter + EOH + ) + allow(failed_list).to receive(:error!).and_raise("Mixlib::Shellout::ShellCommandFailed") + failed_list + end + + let(:bad_list) do + double("Mixlib::Shellout", + error!: false, + stdout: <<~EOH + /somewhere/other/than/install/dir + EOH + ) + end + let(:bad_healthcheck) do double("Mixlib::Shellout", - stdout: <<-EOH.gsub(/^ {12}/, "") + error!: false, + stdout: <<~EOH /bin/ls: linux-vdso.so.1 => (0x00007fff583ff000) libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007fad8592a000) librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fad85722000) libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007fad85518000) @@ -120,11 +158,12 @@ ) end let(:good_healthcheck) do double("Mixlib::Shellout", - stdout: <<-EOH.gsub(/^ {12}/, "") + error!: false, + stdout: <<~EOH /bin/echo: linux-vdso.so.1 => (0x00007fff8a6ee000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f70f58c0000) /lib64/ld-linux-x86-64.so.2 (0x00007f70f5c52000) /bin/cat: @@ -133,29 +172,59 @@ /lib64/ld-linux-x86-64.so.2 (0x00007fe869252000) EOH ) end - let(:regexp) { ".*(\\.TXT|\\.[ch]|\\.[ch]pp|\\.[eh]rl|\\.app|\\.appup|\\.bat|\\.beam|\\.cc|\\.cmake|\\.conf|\\.css|\\.e*rb|\\.feature|\\.gemspec|\\.gif|\\.gitignore|\\.gitkeep|\\.h*h|\\.jar|\\.java|\\.jpg|\\.js|\\.jsm|\\.json|\\.lock|\\.log|\\.lua|\\.md|\\.mkd|\\.npmignore|\\.out|\\.packlist|\\.perl|\\.pl|\\.pm|\\.png|\\.pod|\\.properties|\\.py[oc]*|\\.r*html|\\.rake|\\.rdoc|\\.ri|\\.rst|\\.scss|\\.sh|\\.sql|\\.svg|\\.toml|\\.ttf|\\.txt|\\.xml|\\.yml|Gemfile|LICENSE|Makefile|README|Rakefile|VERSION|license)$|.*\\/build_info\\/.*|.*\\/licenses\\/.*|.*\\/LICENSES\\/.*|.*\\/man\\/.*|.*\\/share\\/doc\\/.*|.*\\/share\\/info\\/.*|.*\\/share\\/postgresql\\/.*|.*\\/share\\/terminfo\\/.*|.*\\/share\\/timezone\\/.*|.*\\/terminfo\\/.*" } - it "raises an exception when there are external dependencies" do allow(subject).to receive(:shellout) - .with("find #{project.install_dir}/ -type f -regextype posix-extended ! -regex '#{regexp}' | xargs ldd") + .with("find /opt/chefdk/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'") + .and_return(file_list) + + allow(subject).to receive(:shellout) + .with("xargs ldd", { input: "/opt/chefdk/shouldnt/matter\n" }) .and_return(bad_healthcheck) expect { subject.run! }.to raise_error(HealthCheckFailed) end it "does not raise an exception when the healthcheck passes" do allow(subject).to receive(:shellout) - .with("find #{project.install_dir}/ -type f -regextype posix-extended ! -regex '#{regexp}' | xargs ldd") + .with("find /opt/chefdk/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'") + .and_return(file_list) + + allow(subject).to receive(:shellout) + .with("xargs ldd", { input: "/opt/chefdk/shouldnt/matter\n" }) .and_return(good_healthcheck) expect { subject.run! }.to_not raise_error end it "will not perform dll base relocation checks" do expect(subject.relocation_checkable?).to be false + end + + it "raises an exception if there's nothing in the file list" do + allow(subject).to receive(:shellout) + .with("find /opt/chefdk/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'") + .and_return(empty_list) + + expect { subject.run! }.to raise_error(RuntimeError, "Internal Error: Health Check found no lines") + end + + it "raises an exception if the file list command raises" do + allow(subject).to receive(:shellout) + .with("find /opt/chefdk/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'") + .and_return(failed_list) + + expect { subject.run! }.to raise_error(RuntimeError, "Mixlib::Shellout::ShellCommandFailed") + end + + it "raises an exception if the file list command has no entries in the install_dir" do + allow(subject).to receive(:shellout) + .with("find /opt/chefdk/ -type f | xargs file | grep \"ELF\" | awk -F: '{print $1}' | sed -e 's/:$//'") + .and_return(bad_list) + + expect { subject.run! }.to raise_error(RuntimeError, "Internal Error: Health Check lines not matching the install_dir") end end end end