Merge branch '8784-dir-listings'
[arvados.git] / services / login-sync / test / test_add_user.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'minitest/autorun'
6
7 require 'stubs'
8
9 class TestAddUser < Minitest::Test
10   include Stubs
11
12   def test_useradd_error
13     # binstub_new_user/useradd will exit non-zero because its args
14     # won't match any line in this empty file:
15     File.open(@tmpdir+'/succeed', 'w') do |f| end
16     invoke_sync binstubs: ['new_user']
17     spied = File.read(@tmpdir+'/spy')
18     assert_match %r{useradd -m -c active -s /bin/bash -G fuse active}, spied
19     # BUG(TC): This assertion succeeds only if docker and fuse groups
20     # exist on the host, but is insensitive to the admin group (groups
21     # are quietly ignored by login-sync if they don't exist on the
22     # current host).
23     assert_match %r{useradd -m -c adminroot -s /bin/bash -G docker(,admin)?,fuse adminroot}, spied
24   end
25
26   def test_useradd_success
27     # binstub_new_user/useradd will succeed.
28     File.open(@tmpdir+'/succeed', 'w') do |f|
29       f.puts 'useradd -m -c active -s /bin/bash -G fuse active'
30       # Accept either form; see note about groups in test_useradd_error.
31       f.puts 'useradd -m -c adminroot -s /bin/bash -G docker,fuse adminroot'
32       f.puts 'useradd -m -c adminroot -s /bin/bash -G docker,admin,fuse adminroot'
33     end
34     $stderr.puts "*** Expect crash after getpwnam() fails:"
35     invoke_sync binstubs: ['new_user']
36     assert !$?.success?
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
40   end
41 end