File indexing completed on 2023-10-03 08:45:10

0001 #!/usr/bin/env ruby
0002 # frozen_string_literal: true
0003 
0004 # SPDX-License-Identifier: AGPL-3.0-or-later
0005 # SPDX-FileCopyrightText: 2022-2023 Harald Sitter <sitter@kde.org>
0006 
0007 require 'fileutils'
0008 require 'logger'
0009 require 'shellwords'
0010 require 'tmpdir'
0011 
0012 def at_bus_exists?
0013   IO.popen(['dbus-send', '--print-reply=literal', '--dest=org.freedesktop.DBus', '/org/freedesktop/DBus', 'org.freedesktop.DBus.ListNames'], 'r') do |io|
0014     io.read.include?('org.a11y.Bus')
0015   end
0016 end
0017 
0018 def at_bus_address
0019   IO.popen(['dbus-send', '--print-reply=literal', '--dest=org.a11y.Bus', '/org/a11y/bus', 'org.a11y.Bus.GetAddress'], 'r') do |io|
0020     io.read.strip
0021   end
0022 end
0023 
0024 def terminate_pgids(pgids)
0025   (pgids || []).reverse.each do |pgid|
0026     Process.kill('-TERM', pgid)
0027     Process.waitpid(pgid)
0028   rescue Errno::ECHILD => e
0029     warn "Process group not found #{e}"
0030   end
0031 end
0032 
0033 def terminate_pids(pids)
0034   (pids || []).reverse.each do |pid|
0035     Process.kill('TERM', pid)
0036     Process.waitpid(pid)
0037   end
0038 end
0039 
0040 class ATSPIBus
0041   def initialize(logger:)
0042     @logger = logger
0043   end
0044 
0045   def with(&block)
0046     return block.yield if at_bus_exists?
0047 
0048     bus_existed = at_bus_exists?
0049 
0050     launcher_path = find_program('at-spi-bus-launcher')
0051     registry_path = find_program('at-spi2-registryd')
0052     @logger.warn "Testing with #{launcher_path} and #{registry_path}"
0053 
0054     pids = []
0055     pids << spawn(launcher_path, '--launch-immediately')
0056     pids << spawn(registry_path)
0057     block.yield
0058   ensure
0059     # NB: do not signal KILL the launcher, it only shutsdown the a11y dbus-daemon when terminated!
0060     terminate_pids(pids)
0061     # Restart the regular bus or the user may be left with malfunctioning accerciser
0062     # (intentionally ignoring the return value! it never passes in the CI & freebsd in absence of systemd)
0063     system('systemctl', 'restart', '--user', 'at-spi-dbus-bus.service') if !pids&.empty? && bus_existed
0064   end
0065 
0066   private
0067 
0068   def find_program(name)
0069     @atspi_paths ||= [
0070       '/usr/lib/at-spi2-core/', # debians
0071       '/usr/libexec/', # newer debians
0072       '/usr/lib/at-spi2/', # suses
0073       '/usr/libexec/at-spi2/', # newer suses
0074       '/usr/lib/' # arch
0075     ]
0076 
0077     @atspi_paths.each do |x|
0078       path = "#{x}/#{name}"
0079       return path if File.exist?(path)
0080     end
0081     raise "Could not resolve absolute path for #{name}; searched in #{@atspi_paths.join(', ')}"
0082   end
0083 end
0084 
0085 def kwin_reexec!
0086   # KWin redirection is a bit tricky. We want to run this script itself under kwin so both the flask server and
0087   # the actual test script can inherit environment variables from that nested kwin. Most notably this is required
0088   # to have the correct DISPLAY set to access the xwayland instance.
0089   # As such this function has two behavior modes. If kwin redirection should run (that is: it's not yet inside kwin)
0090   # it will fork and exec into kwin. If redirection is not required it yields out.
0091 
0092   return if ENV.include?('KWIN_PID') # already inside a kwin parent
0093   return if ENV['TEST_WITH_KWIN_WAYLAND'] == '0'
0094 
0095   kwin_pid = fork do |pid|
0096     ENV['QT_QPA_PLATFORM'] = 'wayland'
0097     ENV['KWIN_SCREENSHOT_NO_PERMISSION_CHECKS'] = '1'
0098     ENV['KWIN_WAYLAND_NO_PERMISSION_CHECKS'] = '1'
0099     ENV['KWIN_PID'] = pid.to_s
0100     extra_args = []
0101     extra_args << '--virtual' if ENV['LIBGL_ALWAYS_SOFTWARE']
0102     extra_args << '--xwayland' if ENV.fetch('TEST_WITH_XWAYLAND', '0').to_i.positive?
0103     # A bit awkward because of how argument parsing works on the kwin side: we must rely on shell word merging for
0104     # the __FILE__ ARGV bit, separate ARGVs to kwin_wayland would be distinct subprocesses to start but we want
0105     # one processes with a bunch of arguments.
0106     exec('kwin_wayland', '--no-lockscreen', '--no-global-shortcuts', *extra_args,
0107          '--exit-with-session', "#{__FILE__} #{ARGV.shelljoin}")
0108   end
0109   _pid, status = Process.waitpid2(kwin_pid)
0110   status.success? ? exit : abort
0111 end
0112 
0113 def dbus_reexec!(logger:)
0114   return if ENV.include?('CUSTOM_BUS') # already inside a nested bus
0115 
0116   if ENV.fetch('USE_CUSTOM_BUS', '0').to_i.zero? && # not explicitly enabled
0117      at_bus_exists? # already have an a11y bus, use it
0118 
0119     logger.info('using existing dbus session')
0120     return
0121   end
0122 
0123   logger.info('starting dbus session')
0124   ENV['CUSTOM_BUS'] = '1'
0125   # Using spawn() rather than exec() so we can print useful debug information after the run
0126   # (useful to debug problems with shutdown of started processes)
0127   pid = spawn('dbus-run-session', '--', __FILE__, *ARGV, pgroup: true)
0128   pgid = Process.getpgid(pid)
0129   _pid, status = Process.waitpid2(pid)
0130   terminate_pgids([pgid])
0131   logger.info('dbus session ended')
0132   system('ps fja')
0133   status.success? ? exit : abort
0134 end
0135 
0136 # Video recording wrapper
0137 class Recorder
0138   def self.with(&block)
0139     return block.yield unless ENV['RECORD_VIDEO_NAME']
0140 
0141     abort 'RECORD_VIDEO requires that a nested kwin wayland be used! (TEST_WITH_KWIN_WAYLAND)' unless ENV['KWIN_PID']
0142 
0143     # Make sure kwin is up. This can be removed once the code was changed to re-exec as part of a kwin
0144     # subprocess, then the wayland server is ready by the time we get re-executed.
0145     sleep(5)
0146     FileUtils.rm_f(ENV['RECORD_VIDEO_NAME'])
0147     pids = []
0148     pids << spawn('pipewire')
0149     pids << spawn('wireplumber')
0150     pids << spawn(find_program('xdg-desktop-portal-kde'))
0151     pids << spawn('selenium-webdriver-at-spi-recorder', '--output', ENV.fetch('RECORD_VIDEO_NAME'))
0152     5.times do
0153       break if File.exist?(ENV['RECORD_VIDEO_NAME'])
0154 
0155       sleep(1)
0156     end
0157     block.yield
0158   ensure
0159     terminate_pids(pids)
0160     if ENV['RECORD_VIDEO_NAME'] &&
0161        (!File.exist?(ENV['RECORD_VIDEO_NAME']) || File.size(ENV['RECORD_VIDEO_NAME']) < 256_000)
0162       warn "recording apparently didn't work properly"
0163     end
0164   end
0165 
0166   def self.find_program(name)
0167     @paths ||= ENV.fetch('LD_LIBRARY_PATH', '').split(':').map { |x| "#{x}/libexec" } +
0168                [
0169                  '/usr/lib/*/libexec/', # debian
0170                  '/usr/libexec/', # suse
0171                  '/usr/lib/' # arch
0172                ]
0173 
0174     @paths.each do |x|
0175       path = "#{x}/#{name}"
0176       return path if Dir.glob(path)&.first
0177     end
0178     raise "Could not resolve absolute path for #{name}; searched in #{@paths.join(', ')}"
0179   end
0180 end
0181 
0182 class Driver
0183   def self.with(datadir, &block)
0184     pids = []
0185     env = { 'FLASK_ENV' => 'production', 'FLASK_APP' => 'selenium-webdriver-at-spi.py' }
0186     env['GDK_BACKEND'] = 'wayland' if ENV['KWIN_PID']
0187     pids << spawn(env,
0188                   'flask', 'run', '--port', PORT, '--no-reload',
0189                   chdir: datadir)
0190     block.yield
0191   ensure
0192     terminate_pids(pids)
0193   end
0194 end
0195 
0196 PORT = '4723'
0197 $stdout.sync = true # force immediate flushing without internal caching
0198 logger = Logger.new($stdout)
0199 
0200 # Tweak the CIs logging rules. They are way too verbose for our purposes
0201 if ENV['KDECI_BUILD'] == 'TRUE'
0202   ENV['QT_LOGGING_RULES'] = <<-RULES.gsub(/\s/, '')
0203     default=true;*.debug=true;kf.globalaccel.kglobalacceld=false;kf.wayland.client=false;
0204     qt.scenegraph.*=false;qt.qml.diskcache=false;
0205     qt.qml.*=false;qt.qpa.wayland.*=false;qt.quick.dirty=false;qt.accessibility.cache=false;qt.v4.asm=false;
0206     qt.opengl.diskcache=false;qt.qpa.fonts=false;kf.kio.workers.http=false;
0207     qt.quick.*=false;qt.text.*=false;qt.qpa.input.methods=false;
0208     qt.qpa.backingstore=false;qt.gui.*=false;qt.core.plugin.loader=false;
0209   RULES
0210   ENV['QT_LOGGING_RULES'] = ENV['QT_LOGGING_RULES_OVERRIDE'] if ENV.include?('QT_LOGGING_RULES_OVERRIDE')
0211 end
0212 
0213 logger.info 'Installing dependencies'
0214 datadir = File.absolute_path("#{__dir__}/../share/selenium-webdriver-at-spi/")
0215 requirements_installed_marker = "#{Dir.tmpdir}/selenium-requirements-installed"
0216 if !File.exist?(requirements_installed_marker) && File.exist?("#{datadir}/requirements.txt")
0217   raise 'pip3 not found in PATH!' unless system('which', 'pip3')
0218   unless system('pip3', 'install', '-r', 'requirements.txt', chdir: datadir)
0219     unless system('pip3', 'install', '--break-system-packages', '-r', 'requirements.txt', chdir: datadir)
0220       raise 'Failed to run pip3 install!'
0221     end
0222   end
0223 
0224   if ENV['KDECI_BUILD'] == 'TRUE'
0225     File.open(requirements_installed_marker, "w") do |file|
0226       # create an empty file so tests in the same CI container can skip the process
0227     end
0228   end
0229 end
0230 
0231 ENV['PATH'] = "#{Dir.home}/.local/bin:#{ENV.fetch('PATH')}"
0232 
0233 ret = false
0234 
0235 # create a throw-away XDG home, so the test starts with a clean slate
0236 # with every run, and doesn't mess with your local installation
0237 Dir.mktmpdir('selenium') do |xdg_home|
0238   %w[CACHE CONFIG DATA STATE].each do |d|
0239     Dir.mkdir("#{xdg_home}/#{d}")
0240     ENV["XDG_#{d}_HOME"] = "#{xdg_home}/#{d}"
0241   end
0242 
0243   dbus_reexec!(logger: logger)
0244   kwin_reexec!
0245   if ENV['KDECI_BUILD'] == 'TRUE'
0246     raise 'Failed to set dbus env' unless system('dbus-update-activation-environment', '--all')
0247   end
0248 
0249   ATSPIBus.new(logger: logger).with do
0250     # Prevent a race condition in Qt when it tries to figure out the bus address,
0251     # instead just tell it the address explicitly.
0252     # https://codereview.qt-project.org/c/qt/qtbase/+/493700/2
0253     ENV['AT_SPI_BUS_ADDRESS'] = at_bus_address
0254     Recorder.with do
0255       Driver.with(datadir) do
0256         i = 0
0257         begin
0258           require 'net/http'
0259           Net::HTTP.get(URI("http://localhost:#{PORT}/status"))
0260         rescue => e
0261           i += 1
0262           if i < 30
0263             logger.info 'not up yet'
0264             sleep 0.5
0265             retry
0266           end
0267           raise e
0268         end
0269 
0270         logger.info "starting test #{ARGV}"
0271         IO.popen(ARGV, 'r', &:readlines)
0272         ret = $?.success?
0273         logger.info 'tests done'
0274       end
0275     end
0276   end
0277 end
0278 
0279 logger.info "run.rb exiting #{ret}"
0280 ret ? exit : abort