github.com/status-im/status-go@v1.1.0/_assets/ci/Jenkinsfile.tests (about)

     1  #!/usr/bin/env groovy
     2  library 'status-jenkins-lib@v1.9.6'
     3  
     4  pipeline {
     5    agent { label 'linux && x86_64 && nix-2.19' }
     6  
     7    parameters {
     8      string(
     9        name: 'BRANCH',
    10        defaultValue: 'develop',
    11        description: 'Name of branch to build.'
    12      )
    13      string(
    14        name: 'UNIT_TEST_COUNT',
    15        defaultValue: getDefaultUnitTestCount(),
    16        description: 'How many times to run tests?'
    17      )
    18      booleanParam(
    19        name: 'UNIT_TEST_FAILFAST',
    20        defaultValue: !isTestNightlyJob(),
    21        description: 'Should the job fail fast on first test failure?'
    22      )
    23      booleanParam(
    24        name: 'UNIT_TEST_RERUN_FAILS',
    25        defaultValue: !isTestNightlyJob(),
    26        description: 'Should the job rerun failed tests?'
    27      )
    28      booleanParam(
    29        name: 'UNIT_TEST_USE_DEVELOPMENT_LOGGER',
    30        defaultValue: !isTestNightlyJob(),
    31        description: 'Should the job use detailed logging for tests, potentially generating large logs?'
    32      )
    33      booleanParam(
    34        name: 'UNIT_TEST_REPORT_CODECLIMATE',
    35        defaultValue: true,
    36        description: 'Should the job report test coverage to CodeClimate?'
    37      )
    38      booleanParam(
    39        name: 'UNIT_TEST_REPORT_CODECOV',
    40        defaultValue: true,
    41        description: 'Should the job report test coverage to Codecov?'
    42      )
    43      booleanParam(
    44        name: 'UNIT_TEST_DRY_RUN',
    45        defaultValue: false,
    46        description: 'Should the job report ignore the actual test run and just print the test plan?'
    47      )
    48    }
    49  
    50    options {
    51      timestamps()
    52      ansiColor('xterm')
    53      /* Prevent Jenkins jobs from running forever */
    54      timeout(time: getDefaultTimeout(), unit: 'MINUTES')
    55      disableConcurrentBuilds()
    56      /* manage how many builds we keep */
    57      buildDiscarder(logRotator(
    58        numToKeepStr: isTestNightlyJob() ? '14' : '5',
    59        daysToKeepStr: '30',
    60        artifactNumToKeepStr: isTestNightlyJob() ? '14' : '1',
    61      ))
    62    }
    63  
    64    environment {
    65      PLATFORM    = 'tests'
    66      DB_CONT     = "status-go-test-db-${env.EXECUTOR_NUMBER.toInteger() + 1}"
    67      DB_PORT     = "${5432 + env.EXECUTOR_NUMBER.toInteger()}"
    68      TMPDIR      = "${WORKSPACE_TMP}"
    69      GOPATH      = "${WORKSPACE_TMP}/go"
    70      GOCACHE     = "${WORKSPACE_TMP}/gocache"
    71      PATH        = "${PATH}:${GOPATH}/bin"
    72      REPO_SRC    = "${GOPATH}/src/github.com/status-im/status-go"
    73      BASE_BRANCH = "${env.CHANGE_TARGET}"
    74  
    75      NWAKU_CONT  = "status-go-test-nwaku-${env.EXECUTOR_NUMBER.toInteger() + 1}"
    76      NWAKU_TCP_PORT  = "${60000 + env.EXECUTOR_NUMBER.toInteger()}"
    77      NWAKU_UDP_PORT  = "${9000 + env.EXECUTOR_NUMBER.toInteger()}"
    78      NWAKU_REST_PORT = "${9645 + env.EXECUTOR_NUMBER.toInteger()}"
    79  
    80      /* Hack-fix for params not being set in env on first job run. */
    81      UNIT_TEST_FAILFAST =               "${params.UNIT_TEST_FAILFAST}"
    82      UNIT_TEST_RERUN_FAILS =            "${params.UNIT_TEST_RERUN_FAILS}"
    83      UNIT_TEST_USE_DEVELOPMENT_LOGGER = "${params.UNIT_TEST_USE_DEVELOPMENT_LOGGER}"
    84      UNIT_TEST_REPORT_CODECLIMATE =     "${params.UNIT_TEST_REPORT_CODECLIMATE}"
    85      UNIT_TEST_REPORT_CODECOV =         "${params.UNIT_TEST_REPORT_CODECOV}"
    86      UNIT_TEST_DRY_RUN =                "${params.UNIT_TEST_DRY_RUN}"
    87    }
    88  
    89    stages {
    90      stage('Prep') {
    91        steps { /* Go needs to find status-go in GOPATH. */
    92          sh "mkdir -p \$(dirname ${REPO_SRC})"
    93          sh "ln -s ${WORKSPACE} ${REPO_SRC}"
    94        }
    95      }
    96  
    97      stage('Vendor Check') {
    98        steps { script {
    99          nix.shell('make vendor', pure: false)
   100          /* fail build if vendoring hasn't been done */
   101          nix.shell('git diff --exit-code --no-color --stat vendor/')
   102        } }
   103      }
   104  
   105      stage('Migration') {
   106        when { // https://github.com/status-im/status-go/issues/4993#issuecomment-2022685544
   107          expression { !isTestNightlyJob() }
   108        }
   109        steps { script {
   110          nix.shell('make migration-check', pure: false)
   111        } }
   112      }
   113  
   114      stage('Commit') {
   115        environment {
   116          BASE_BRANCH = "${env.BASE_BRANCH}"
   117        }
   118        when { // https://github.com/status-im/status-go/issues/4993#issuecomment-2022685544
   119          expression { !isTestNightlyJob() }
   120        }
   121        steps { script {
   122          nix.shell('make commit-check', pure: false)
   123        } }
   124      }
   125  
   126      stage('Lint') {
   127        steps { script {
   128          nix.shell('make lint', pure: true)
   129        } }
   130      }
   131  
   132      stage('Canary') {
   133        steps { script {
   134          nix.shell('make canary-test', pure: true)
   135        } }
   136      }
   137  
   138      stage('Unit Tests') {
   139        environment {
   140          TEST_POSTGRES_PORT = "${env.DB_PORT}"
   141          NWAKU_REST_PORT = "${env.NWAKU_REST_PORT}"
   142        }
   143        steps { script {
   144          def ipAddress = sh(script: "hostname -I | awk '{print \$1}'", returnStdout: true).trim()
   145          db = docker.image('postgres:9.6-alpine').withRun([
   146            "--name=${DB_CONT}",
   147            "--env=POSTGRES_HOST_AUTH_METHOD=trust",
   148            "--publish=${DB_PORT}:${DB_PORT}",
   149          ].join(' '), "-p ${DB_PORT}") { c ->
   150            nwaku = docker.image('harbor.status.im/wakuorg/nwaku:latest').withRun([
   151              "--name=${NWAKU_CONT}",
   152              "--publish=${NWAKU_TCP_PORT}:${NWAKU_TCP_PORT}/tcp",
   153              "--publish=${NWAKU_UDP_PORT}:${NWAKU_UDP_PORT}/udp",
   154              "--publish=${NWAKU_REST_PORT}:8645/tcp"
   155            ].join(' '), [
   156              "--tcp-port=${NWAKU_TCP_PORT}",
   157              "--discv5-discovery=true",
   158              "--cluster-id=16",
   159              "--pubsub-topic=/waku/2/rs/16/32",
   160              "--pubsub-topic=/waku/2/rs/16/64",
   161              "--nat=extip:${ipAddress}",
   162              "--discv5-discovery",
   163              "--discv5-udp-port=${NWAKU_UDP_PORT}",
   164              "--rest-address=0.0.0.0",
   165              "--store",
   166              "--filter",
   167              "--lightpush"
   168            ].join(' ')) { c2 ->
   169              nix.shell('make generate-handlers', pure: true)
   170              withCredentials([
   171                string(
   172                  credentialsId: 'codeclimate-test-reporter-id',
   173                  variable: 'CC_TEST_REPORTER_ID'
   174                ),
   175                string(
   176                  credentialsId: 'codecov-repository-upload-token',
   177                  variable: 'CODECOV_TOKEN'
   178                ),
   179              ]) {
   180                nix.shell('make test-unit V=1', pure: false)
   181              }
   182              sh "mv c.out test-coverage.out"
   183              archiveArtifacts('report_*.xml, test_*.log, test-coverage.out, coverage/codeclimate.json, test-coverage.html, coverage_merged.out')
   184            }
   185          }
   186        } }
   187        post { cleanup { /* Leftover DB containers. */
   188          sh "docker rm ${DB_CONT} || true"
   189          sh "docker rm ${NWAKU_CONT} || true"
   190        } }
   191      }
   192    } // stages
   193  
   194    post {
   195      always  {
   196        script {
   197          env.PKG_URL = "${currentBuild.absoluteUrl}/consoleText"
   198  
   199          if (isTestNightlyJob()) {
   200            archiveArtifacts('report_*.xml, test_*.log, test-coverage.html, test-coverage.out, coverage/codeclimate.json')
   201          }
   202          if (params.UNIT_TEST_RERUN_FAILS) {
   203            def rerunReports = findFiles(glob: 'report_rerun_fails_*.txt')
   204            if (rerunReports.length > 0) {
   205              archiveArtifacts('report_rerun_fails_*.txt')
   206            }
   207          }
   208          junit(
   209            testResults: 'report_*.xml',
   210            skipOldReports: true,
   211            skipPublishingChecks: true,
   212            skipMarkingBuildUnstable: true
   213          )
   214          publishHTML(target: [
   215            allowMissing:           true,
   216            alwaysLinkToLastBuild:  true,
   217            keepAll:                true,
   218            reportDir:    'reports',
   219            reportFiles:  'test_stats.txt',
   220            reportName:   'Reports',
   221            reportTitles: 'Test Stats'
   222          ])
   223        }
   224      }
   225      success { script { github.notifyPR(true) } }
   226      failure { 
   227        script { 
   228          github.notifyPR(false) 
   229          archiveArtifacts('**/test_*.log')
   230        }
   231      }
   232      cleanup {
   233        dir(env.TMPDIR) { deleteDir() }
   234        sh "make git-clean"
   235      }
   236    } // post
   237  } // pipeline
   238  
   239  def isTestNightlyJob() { env.JOB_BASE_NAME == 'tests-nightly' }
   240  
   241  def getDefaultUnitTestCount() { isTestNightlyJob() ? '20' : '1' }
   242  
   243  def getDefaultTimeout() { isTestNightlyJob() ? 5*60 : 50 }