Fix test failure, refs #16689
[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     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 active}, spied
20     assert_match %r{useradd -m -c adminroot -s /bin/bash adminroot}, spied
21   end
22
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 active'
27       f.puts 'useradd -m -c adminroot -s /bin/bash adminroot'
28     end
29     $stderr.puts "*** Expect crash after getpwnam() fails:"
30     invoke_sync binstubs: ['new_user']
31     assert !$?.success?
32     spied = File.read(@tmpdir+'/spy')
33     # Expect a crash after adding one user, because Dir.mkdir({home}) fails.
34     assert_match %r{^useradd -m -c [^\n]+\n$}s, spied
35   end
36 end