1

I'm running junit tests via gradle in a gitlab ci/cd pipeline.

The program needs a few random numbers.

The docker container the gitlab runners start however lack entropy. Therefore the java standard library Random, SecureRandom and Math.random() seem to block and the tests then timeout.

As far as i understand the problem comes from the lack of entropy for /dev/random. Since it's a gitlab runner starting the container i cant just mount the host urandom to the container random as suggested here

So i should instruct the jvm to use /dev/urandom in the container which doesn't have anymore entropy aswell but atleast it doesn't block. I can do this via the jvm option: -Djava.security.egd=file:/dev/urandom as suggested here and here

I tried specifiying this in the ci/cd yaml as follows:

test-server-component:
  stage: test
  needs: [build-server-component]
  image: gradle:alpine
  before_script:
  - GRADLE_USER_HOME="$(pwd)/applications/server_comp/.gradle"
  - export GRADLE_USER_HOME
  - cd applications/server_comp/
  - export GRADLE_OPTS="-Djava.security.egd=file:/dev/urandom"
  script: ./gradlew check -Djava.security.egd=file:/dev/urandom
  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: pull
    paths:
      - build
      - .gradle
  artifacts:
    name: "$CI_JOB_STAGE-$CI_COMMIT_REF_NAME"
    paths:
      - applications/server_comp/app/build/reports/tests/test/
    expire_in: 2 weeks
  rules:
    - changes:
       - applications/server_comp/**/*

which doesn't seem to work or get through to the jvm running the tests.

I then tried adding these lines to the build.gradle:

applicationDefaultJvmArgs = ["-Djava.security.egd=file:/dev/urandom"]

and


if(System.getenv('BUILD_NUMBER')){
    tasks.withType(Test) {systemProperty 'java.security.egd','file:/dev/urandom'}
}

as suggested here

but it's still probably using /random since all the Random classes and their functions are still blocking.

Is there any other way to get the jvm to use another source of randomness ? Or is there another option I'm not seeing to generate random numbers without entropy ? Or is there a way to get entropy for /random in this specific setup ?

The project uses java 17| gradle 7.4.2 | junit jupiter 5.8.1

hadron
  • 11
  • 1

0 Answers0