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