-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve usage of the 'hostname' executable, others
* Do not shell out directly with `%x()` to any executable. Instead, use the new Open3 based `NewRelic::Helper.run_command` method. * The new `NewRelic::Helper.run_command` should prevent any unwanted error messages pertaining to binary execution from appearing in any output or log unless the log level is set to 'debug'. * When executing an external binary, make sure it exists first * If the 'hostname' binary does not exist, have `NewRelic::Agent::Hostname.get_fqdn` fall back to `get`, which uses `Socket`. * When using 'uname' for JRuby to determine the platform, use 'unknown' if the 'uname' call fails * Introduce new `NewRelic::CommandExcutableNotFoundError` and `NewRelic::CommandRunFailedError` error classes to help with logic flow. * Bring in 'minitest-stub-const' as a development dependency for stubbing constants resolves #697 Thank you to @metaskills for submitting #697 and pointing out that 'hostname' will not be available to AWS Lambda Ruby functions. Thank you to @brcarp for suggesting the use of Open3 as an improvement over `%x()` to handle output. Thank you to both @metaskills and @brcarp for their continued input and maintainer collaboration on issue #697
- Loading branch information
Showing
8 changed files
with
188 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# encoding: utf-8 | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
|
||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) | ||
|
||
# tests NewRelic::Helper | ||
class HelperTest < Minitest::Test | ||
# | ||
# executable_in_path? | ||
# | ||
def test_executable_is_in_path | ||
executable = 'seagulls' | ||
fake_dir = '/usr/local/existent' | ||
ENV.stubs(:[]).with('PATH').returns(fake_dir) | ||
|
||
executable_path = File.join(fake_dir, executable) | ||
File.stubs(:exist?).with(executable_path).returns(true) | ||
File.stubs(:file?).with(executable_path).returns(true) | ||
File.stubs(:executable?).with(executable_path).returns(true) | ||
exists = NewRelic::Helper.executable_in_path?(executable) | ||
assert_truthy exists | ||
end | ||
|
||
def test_executable_is_not_in_path | ||
executable = 'seagulls' | ||
fake_dir = '/dev/null/nonexistent' | ||
ENV.stubs(:[]).with('PATH').returns(fake_dir) | ||
executable_path = File.join(fake_dir, executable) | ||
File.stubs(:exist?).with(executable_path).returns(false) | ||
exists = NewRelic::Helper.executable_in_path?(executable) | ||
assert_false exists | ||
end | ||
|
||
def test_path_does_not_exist | ||
ENV.stubs(:[]).with('PATH').returns(nil) | ||
exists = NewRelic::Helper.executable_in_path?('Whisper of the Heart') | ||
assert_false exists | ||
end | ||
|
||
# | ||
# run_command | ||
# | ||
def test_run_command_when_executable_does_not_exist | ||
NewRelic::Helper.stubs('executable_in_path?').returns(false) | ||
assert_raises(NewRelic::CommandExecutableNotFoundError) do | ||
NewRelic::Helper.run_command('mksh -v') | ||
end | ||
end | ||
|
||
def test_run_commmand_happy | ||
stubbed = 'Jinba ittai' | ||
NewRelic::Helper.stubs('executable_in_path?').returns(true) | ||
Open3.stubs('capture2e').returns([stubbed, OpenStruct.new(success?: true)]) | ||
result = NewRelic::Helper.run_command('figlet Zoom Zoom') | ||
assert_equal result, stubbed | ||
end | ||
|
||
def test_run_command_sad | ||
NewRelic::Helper.stubs('executable_in_path?').returns(true) | ||
Open3.stubs('capture2e').returns([nil, OpenStruct.new(success?: false)]) | ||
assert_raises(NewRelic::CommandRunFailedError) do | ||
NewRelic::Helper.run_command('find / -name tetris') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters