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