From f71873ca6ec9969a90242a1c4d391770cabce1c2 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Fri, 14 Aug 2015 00:44:52 -0400 Subject: [PATCH] 6781: Add test cases and packaging support. --- .gitignore | 3 ++ services/login-sync/.gitignore | 2 + services/login-sync/Gemfile | 7 +++ services/login-sync/Rakefile | 8 +++ services/login-sync/bin/arvados-login-sync | 2 +- .../login-sync/test/binstub_new_user/useradd | 9 ++++ services/login-sync/test/stubs.rb | 52 +++++++++++++++++++ services/login-sync/test/test_add_user.rb | 32 ++++++++++++ 8 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 services/login-sync/.gitignore create mode 100644 services/login-sync/Gemfile create mode 100644 services/login-sync/Rakefile create mode 100755 services/login-sync/test/binstub_new_user/useradd create mode 100644 services/login-sync/test/stubs.rb create mode 100644 services/login-sync/test/test_add_user.rb diff --git a/.gitignore b/.gitignore index 0871c89cb9..6cbcccc895 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ .rvmrc *~ *.pyc +*.gem +*.rpm +*.deb docker/*/generated docker/config.yml doc/.site diff --git a/services/login-sync/.gitignore b/services/login-sync/.gitignore new file mode 100644 index 0000000000..cec3cb55a9 --- /dev/null +++ b/services/login-sync/.gitignore @@ -0,0 +1,2 @@ +*.gem +Gemfile.lock diff --git a/services/login-sync/Gemfile b/services/login-sync/Gemfile new file mode 100644 index 0000000000..ffeab22891 --- /dev/null +++ b/services/login-sync/Gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' +gemspec +group :test, :performance do + gem 'minitest', '>= 5.0.0' + gem 'mocha', require: false + gem 'rake' +end diff --git a/services/login-sync/Rakefile b/services/login-sync/Rakefile new file mode 100644 index 0000000000..cf4652f699 --- /dev/null +++ b/services/login-sync/Rakefile @@ -0,0 +1,8 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs << 'test' +end + +desc 'Run tests' +task default: :test diff --git a/services/login-sync/bin/arvados-login-sync b/services/login-sync/bin/arvados-login-sync index b9cecb6860..2e407d808f 100755 --- a/services/login-sync/bin/arvados-login-sync +++ b/services/login-sync/bin/arvados-login-sync @@ -81,7 +81,7 @@ begin groups << "fuse" groups.select! { |name| gids[name] } # Create new user - next unless system("/usr/sbin/useradd", "-m", + next unless system("useradd", "-m", "-c", l[:username], "-s", "/bin/bash", "-G", groups.join(","), diff --git a/services/login-sync/test/binstub_new_user/useradd b/services/login-sync/test/binstub_new_user/useradd new file mode 100755 index 0000000000..173bc1ddd2 --- /dev/null +++ b/services/login-sync/test/binstub_new_user/useradd @@ -0,0 +1,9 @@ +#!/bin/bash + +stub="${0##*/}" + +# Record what actually happened in the "spy" file +echo "$stub $*" >> "$ARVADOS_LOGIN_SYNC_TMPDIR/spy" + +# Exit 0 if this command was listed in the "succeed" file +exec fgrep -qx -- "$stub $*" "$ARVADOS_LOGIN_SYNC_TMPDIR/succeed" diff --git a/services/login-sync/test/stubs.rb b/services/login-sync/test/stubs.rb new file mode 100644 index 0000000000..62d952f236 --- /dev/null +++ b/services/login-sync/test/stubs.rb @@ -0,0 +1,52 @@ +require 'etc' +require 'mocha/mini_test' +require 'ostruct' + +module Stubs + # These Etc mocks help only when we run arvados-login-sync in-process. + + def setup + super + ENV['ARVADOS_VIRTUAL_MACHINE_UUID'] = 'testvm2.shell' + Etc.stubs(:to_enum).with(:passwd).returns stubpasswd.map { |x| OpenStruct.new x } + Etc.stubs(:to_enum).with(:group).returns stubgroup.map { |x| OpenStruct.new x } + end + + def stubpasswd + [{name: 'root', uid: 0}] + end + + def stubgroup + [{name: 'root', gid: 0}] + end + + # These child-ENV tricks help only when we run arvados-login-sync as a subprocess. + + def setup + super + @env_was = Hash[ENV] + @tmpdir = Dir.mktmpdir + end + + def teardown + FileUtils.remove_dir(@tmpdir) + ENV.select! { |k| @env_was.has_key? k } + @env_was.each do |k,v| ENV[k]=v end + super + end + + def stubenv opts={} + # Use UUID of testvm2.shell fixture, unless otherwise specified by test case. + Hash[ENV].merge('ARVADOS_VIRTUAL_MACHINE_UUID' => 'zzzzz-2x53u-382brsig8rp3065', + 'ARVADOS_LOGIN_SYNC_TMPDIR' => @tmpdir) + end + + def invoke_sync opts={} + env = stubenv.merge(opts[:env] || {}) + (opts[:binstubs] || []).each do |binstub| + env['PATH'] = File.absolute_path('../binstub_'+binstub, __FILE__) + ':' + env['PATH'] + end + login_sync_path = File.absolute_path '../../bin/arvados-login-sync', __FILE__ + system env, login_sync_path + end +end diff --git a/services/login-sync/test/test_add_user.rb b/services/login-sync/test/test_add_user.rb new file mode 100644 index 0000000000..c7d92b7efe --- /dev/null +++ b/services/login-sync/test/test_add_user.rb @@ -0,0 +1,32 @@ +require 'minitest/autorun' + +require 'stubs' + +class TestAddUser < Minitest::Test + include Stubs + + def test_useradd_error + # binstub_new_user/useradd will exit non-zero because its args + # won't match any line in this empty file: + File.open(@tmpdir+'/succeed', 'w') do |f| end + invoke_sync binstubs: ['new_user'] + spied = File.read(@tmpdir+'/spy') + assert_match %r{useradd -m -c active -s /bin/bash -G fuse active}, spied + assert_match %r{useradd -m -c adminroot -s /bin/bash -G docker,fuse adminroot}, spied + end + + def test_useradd_success + # binstub_new_user/useradd will exit non-zero because its args + # won't match any line in this empty file: + File.open(@tmpdir+'/succeed', 'w') do |f| + f.puts 'useradd -m -c active -s /bin/bash -G fuse active' + f.puts 'useradd -m -c adminroot -s /bin/bash -G docker,fuse adminroot' + end + $stderr.puts "*** Expect crash in dir_s_mkdir:" + invoke_sync binstubs: ['new_user'] + assert !$?.success? + spied = File.read(@tmpdir+'/spy') + # Expect a crash after adding one user, because Dir.mkdir({home}) fails. + assert_match %r{^useradd -m -c [^\n]+\n$}s, spied + end +end -- 2.30.2