Skip to content

Commit

Permalink
Systemd fixups (#868)
Browse files Browse the repository at this point in the history
Fix these expressions just a little and add a simple test for systemd adapter and it's launcher.
  • Loading branch information
johrstrom authored Feb 7, 2025
1 parent db13933 commit cdfce58
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
23 changes: 7 additions & 16 deletions lib/ood_core/job/adapters/systemd/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,20 @@ def call(cmd, *args, env: {}, stdin: "")
# @param cmd [Array<#to_s>] the command to be executed on the destination host
def ssh_cmd(destination_host, cmd)

sshcmd=[
sshcmd = [
'ssh', '-t',
'-p', OodCore::Job::Adapters::Helper.ssh_port,
'-o', 'Batchmode=yes',
]

if !strict_host_checking
sshcmd.concat([
'-o','StrictHostKeyChecking=no',
'-o','UserKnownHostsFile=/dev/null',
])
end

if (!ssh_keyfile.to_s.empty?)
sshcmd.concat([
'-i',ssh_keyfile.to_s,
])
end

sshcmd.concat([
"#{username}@#{destination_host}"
]).concat(cmd)
'-o','StrictHostKeyChecking=no',
'-o','UserKnownHostsFile=/dev/null',
]) unless strict_host_checking

sshcmd.concat(['-i',ssh_keyfile.to_s,]) unless ssh_keyfile.to_s.empty?

sshcmd.concat(["#{username}@#{destination_host}"]).concat(cmd)
end

def shell
Expand Down
51 changes: 51 additions & 0 deletions test/job/adapters/systemd_launcher_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'test_helper'
require 'ood_core/job/adapters/systemd'
require 'ood_core/job/adapters/systemd/launcher'

class SystemdLauncherTest < Minitest::Test
include TestHelper

def launcher_instance(config = {})
default = { ssh_hosts: ['foo'], submit_host: 'localhost' }
OodCore::Job::Adapters::LinuxSystemd::Launcher.new(**default.merge(config))
end

def setup
Etc.stubs(:getlogin).returns('testuser')
end

def test_instantiation
launcher = launcher_instance

refute_nil(launcher)
end

def test_ssh_cmd_default
launcher = launcher_instance
expected = [ 'ssh', '-t', '-p', '22', '-o',
'Batchmode=yes', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null',
'testuser@localhost', '/bin/bash'
]

assert_equal(expected, launcher.send(:ssh_cmd, 'localhost', ['/bin/bash']))
end

def test_ssh_cmd_with_host_checking
launcher = launcher_instance({ strict_host_checking: true })
expected = [ 'ssh', '-t', '-p', '22', '-o',
'Batchmode=yes', 'testuser@localhost', '/bin/bash'
]

assert_equal(expected, launcher.send(:ssh_cmd, 'localhost', ['/bin/bash']))
end

def test_ssh_cmd_with_keyfile
launcher = launcher_instance({ ssh_keyfile: "~/.ssh/my_key" })
expected = [ 'ssh', '-t', '-p', '22', '-o',
'Batchmode=yes', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null',
'-i', '~/.ssh/my_key', 'testuser@localhost', '/bin/bash'
]

assert_equal(expected, launcher.send(:ssh_cmd, 'localhost', ['/bin/bash']))
end
end
15 changes: 15 additions & 0 deletions test/job/adapters/systemd_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'test_helper'

class SystemdTest < Minitest::Test
include TestHelper

def systemd_instance(config = { })
OodCore::Job::Factory.build({ adapter: 'systemd' }.merge(config))
end

def test_instantiation
sysd = systemd_instance

refute_nil(sysd)
end
end

0 comments on commit cdfce58

Please sign in to comment.