1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 require 'minitest/autorun'
9 class TestAddUser < Minitest::Test
12 def test_useradd_error
13 valid_groups = %w(docker admin fuse).select { |g| Etc.getgrnam(g) rescue false }
14 # binstub_new_user/useradd will exit non-zero because its args
15 # won't match any line in this empty file:
16 File.open(@tmpdir+'/succeed', 'w') do |f| end
17 invoke_sync binstubs: ['new_user']
18 spied = File.read(@tmpdir+'/spy')
19 assert_match %r{useradd -m -c active -s /bin/bash -G (fuse)? active}, spied
20 assert_match %r{useradd -m -c adminroot -s /bin/bash -G #{valid_groups.join(',')} adminroot}, spied
23 def test_useradd_success
24 # binstub_new_user/useradd will succeed.
25 File.open(@tmpdir+'/succeed', 'w') do |f|
26 f.puts 'useradd -m -c active -s /bin/bash -G fuse active'
27 f.puts 'useradd -m -c active -s /bin/bash -G active'
28 # Accept either form; see note about groups in test_useradd_error.
29 f.puts 'useradd -m -c adminroot -s /bin/bash -G docker,fuse adminroot'
30 f.puts 'useradd -m -c adminroot -s /bin/bash -G docker,admin,fuse adminroot'
31 f.puts 'useradd -m -c adminroot -s /bin/bash -G docker adminroot'
32 f.puts 'useradd -m -c adminroot -s /bin/bash -G docker,admin adminroot'
34 $stderr.puts "*** Expect crash after getpwnam() fails:"
35 invoke_sync binstubs: ['new_user']
37 spied = File.read(@tmpdir+'/spy')
38 # Expect a crash after adding one user, because Dir.mkdir({home}) fails.
39 assert_match %r{^useradd -m -c [^\n]+\n$}s, spied