Merge branch '19179-acct-activity' refs #19179
[arvados.git] / services / login-sync / test / stubs.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'etc'
6 require 'mocha/minitest'
7 require 'ostruct'
8
9 module Stubs
10   def stubpasswd
11     [{name: 'root', uid: 0}]
12   end
13
14   def stubgroup
15     [{name: 'root', gid: 0}]
16   end
17
18
19   def setup
20     super
21
22     # These Etc mocks help only when we run arvados-login-sync in-process.
23     ENV['ARVADOS_VIRTUAL_MACHINE_UUID'] = 'testvm2.shell'
24     Etc.stubs(:to_enum).with(:passwd).returns stubpasswd.map { |x| OpenStruct.new x }
25     Etc.stubs(:to_enum).with(:group).returns stubgroup.map { |x| OpenStruct.new x }
26
27     # These child-ENV tricks help only when we run arvados-login-sync as a subprocess.
28     @env_was = Hash[ENV]
29     @tmpdir = Dir.mktmpdir
30   end
31
32   def teardown
33     FileUtils.remove_dir(@tmpdir)
34     ENV.select! { |k| @env_was.has_key? k }
35     @env_was.each do |k,v| ENV[k]=v end
36     super
37   end
38
39   def stubenv opts={}
40     # Use UUID of testvm2.shell fixture, unless otherwise specified by test case.
41     Hash[ENV].merge('ARVADOS_VIRTUAL_MACHINE_UUID' => 'zzzzz-2x53u-382brsig8rp3065',
42                     'ARVADOS_LOGIN_SYNC_TMPDIR' => @tmpdir)
43   end
44
45   def invoke_sync opts={}
46     env = stubenv.merge(opts[:env] || {})
47     (opts[:binstubs] || []).each do |binstub|
48       env['PATH'] = File.absolute_path('../binstub_'+binstub, __FILE__) + ':' + env['PATH']
49     end
50     login_sync_path = File.absolute_path '../../bin/arvados-login-sync', __FILE__
51     system env, login_sync_path
52   end
53 end