karma-runner/karma-chrome-launcher

Chromeheadless not captured inside a windows container

andyfisher100 opened this issue · 7 comments

So i have a windows docker container that works as a build agent for AzureDevOps, formally VSTS.

When running Karma Jasmine tests, either via ADO or manually via the command line, Karma launches chrome but never connects. I know the process has launched as using Get_Process from powershell shows the process running and also when Karma tries the second time the Process IDs all change

Tried running with the no sandbox flag and also fails. Any ideas?

Versions:
"karma": "1.7.0",
"karma-chrome-launcher": "2.1.1",
"karma-cli": "1.0.1",
"karma-coverage-istanbul-reporter": "1.2.1",
"karma-jasmine": "1.1.0",
"karma-jasmine-html-reporter": "0.2.2",
"karma-trx-reporter": "0.2.9",

Karma Conf

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-trx-reporter')
    ],
    files: [
      'test-mocks/mock.js',
      'test-mocks/mock.css'
    ],
    proxies: {
      "/test-mocks/": "/base/test-mocks/"
    },
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'), reports: ['html', 'cobertura'],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml', 'trx'],
    trxReporter: {
      outputFile: 'test-results.trx',
      shortTestName: false
    },
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: [
                '--no-sandbox'                
        ]
      }
    },
    browserDisconnectTolerance: 2,
    browserDisconnectTimeout: 180000,
    browserNoActivityTimeout: 180000,
    singleRun: true
  });
};

Debug Output
[plugin]: Loading inlined plugin (defining framework:jasmine).
[plugin]: Loading inlined plugin (defining launcher:Chrome, launcher:ChromeHeadless, launcher:ChromeCanary, launcher:ChromeCanaryHeadless, launcher:Chromium, launcher:Dartium, test).
[plugin]: Loading inlined plugin (defining reporter:kjhtml).
[plugin]: Loading inlined plugin (defining reporter:coverage-istanbul).
[plugin]: Loading inlined plugin (defining framework:@angular-devkit/build-angular, reporter:@angular-devkit/build-angular--sourcemap-reporter, reporter:@angular-devkit/build-angular--event-reporter, middleware:@angular-devkit/build-angular--blocker, middleware:@angular-devkit/build-angular--fallback).
[plugin]: Loading inlined plugin (defining reporter:trx).
[plugin]: Loading inlined plugin (defining launcher:ChromeHeadlessNoSandbox).
10% building modules 1/1 modules 0 active15 02 2019 02:01:31.210:DEBUG [web-server]: Instantiating middleware
[reporter]: Trying to load reporter: @angular-devkit/build-angular--sourcemap-reporter
[reporter]: Trying to load color-version of reporter: @angular-devkit/build-angular--sourcemap-reporter (@angular-devkit/build-angular--sourcemap-reporter_color)
[reporter]: Couldn't load color-version.
[reporter]: Trying to load reporter: @angular-devkit/build-angular--event-reporter
[reporter]: Trying to load color-version of reporter: @angular-devkit/build-angular--event-reporter (@angular-devkit/build-angular--event-reporter_color)
[reporter]: Couldn't load color-version.
[reporter]: Trying to load reporter: kjhtml
[reporter]: Trying to load color-version of reporter: kjhtml (kjhtml_color)
[reporter]: Couldn't load color-version.
[reporter]: Trying to load reporter: trx
[reporter]: Trying to load color-version of reporter: trx (trx_color)
[reporter]: Couldn't load color-version.
[karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
[launcher]: Launching browser ChromeHeadlessNoSandbox with unlimited concurrency
[launcher]: Starting browser ChromeHeadless
[temp-dir]: Creating temp dir at C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp\karma-82484357
[launcher]: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --user-data-dir=C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp\karma-82484357 --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-background-timer-throttling --disable-renderer-backgrounding --disable-device-discovery-notifications --no-sandbox http://localhost:9876/?id=82484357 --headless --disable-gpu --remote-debugging-port=9222
[web-server]: serving: C:\BuildAgent_work\1\s\node_modules\karma\static/client.html
[web-server]: serving: C:\BuildAgent_work\1\s\node_modules\karma\static/karma.js
[launcher]: ChromeHeadless have not captured in 60000 ms, killing.
[launcher]: Process ChromeHeadless exited with code 0
[temp-dir]: Cleaning temp dir C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp\karma-82484357
[launcher]: Trying to start ChromeHeadless again (1/2).

Same issue here = server 2019 host

Did you find a workaround?
I can see from your config that you've pretty much exhausted every other thread on the issue

My config is below.

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

process.env.CHROMIUM_BIN = "C:\\Chrome\\chrome.exe"

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
        clearContext: false, // leave Jasmine Spec Runner output visible in browser
        jasmine: {
            random: false
          }
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromiumHeadlessNoSandbox'],
    customLaunchers: {
        ChromiumHeadlessNoSandbox: {
          base: 'ChromiumHeadless',
            flags: [
                '--no-sandbox',
                '--disable-gpu',
                '--enable-logging',
                '--no-default-browser-check',
                '--no-first-run',
                '--disable-default-apps',
                '--disable-popup-blocking',
                '--disable-translate',
                '--disable-background-timer-throttling',
                '--disable-renderer-backgrounding',
                '--disable-device-discovery-notifications',
                '--remote-debugging-port=9222',
                '--disable-web-security'
            ]
        }
      },
    singleRun: false
  });
};

Same output in jenkins from:

def tests(projectName) {
  echo 'Performing Tests'
  bat  "ng test $projectName --watch=false --browsers=ChromiumHeadlessNoSandbox --progress=false --code-coverage"
  echo 'Ending Tests'
}

I've tried adding in process.env.NO_PROXY etc, tried everything I can find.
Tried using puppeteer, chrome and chromium.
Currently Chromium latest stable is in C:\Chrome

@prom3theu5 Yes i found a workaround for this. I had to add some fonts from my windows dev box to the windows container image. yes really Font files! I did this during docker build. I think this was to do with the default fonts for chrome not being available.

I added all of the variations of the following font files (bold, italics, etc.)

  • Arial
  • Consola
  • times new roman (saved as "times.ttf")

Once all of these fonts where added, i also added the --no-sandbox flag to the karma config for chromeheadless and karma and the tests worked as expected (as shown in the config i originally posted)

Awesome - I'll rebuild the container now with them - Thanks a lot
Edit
Confirmed - Working
Font Files.... Lol
Nice Find :)

@andyfisher100, @prom3theu5 were you guys using Server Core 2019, or a non-Core container? How did you end up installing the fonts into your containers?

Pretty sure I have the same issue - my container is built on servercore:1809, and Chrome Headless refuses to connect.

@Smenus Yes, I am using ServerCore 2019

I've used a powershell script to install the fonts required
Put it together Here for you as a simple dockerfile.

@prom3theu5 Thanks for the Dockerfile and fonts! I'll try that asap, but I'm hopeful this time...

So i have a folder that is local to my docker file called "tools/Fonts/" and in the Fonts folder i have the missing font files (i have 12 in total). i then have a simple copy in my dockerfile

# Add missing windows fonts, needed for any image that will utilise chrome
COPY ["tools/Fonts/", "C:/Windows/Fonts/"]

The repo i have isn't public so i cant provide a direct link sorry