2019-11-25 14:06:34 +08:00
|
|
|
#!/usr/bin/env groovy
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 - present Instructure, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of Canvas.
|
|
|
|
*
|
|
|
|
* Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
* Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-11-02 23:39:02 +08:00
|
|
|
library "canvas-builds-library@${env.CANVAS_BUILDS_REFSPEC}"
|
2023-03-17 02:46:17 +08:00
|
|
|
loadLocalLibrary('local-lib', 'build/new-jenkins/library')
|
2020-06-17 02:23:25 +08:00
|
|
|
|
2020-02-20 03:50:26 +08:00
|
|
|
@groovy.transform.Field
|
2020-09-23 03:30:29 +08:00
|
|
|
def partitions = []
|
2020-03-07 05:49:29 +08:00
|
|
|
@groovy.transform.Field
|
2021-05-19 01:03:16 +08:00
|
|
|
def changedTests = ''
|
2020-03-24 00:51:38 +08:00
|
|
|
@groovy.transform.Field
|
2021-05-19 01:03:16 +08:00
|
|
|
def fscStatus = null
|
2023-03-16 03:49:44 +08:00
|
|
|
final static FSC_TIMEOUT = commitMessageFlag('fsc-timeout') as Integer ?: 20
|
2020-02-20 03:50:26 +08:00
|
|
|
|
2020-06-10 01:49:59 +08:00
|
|
|
def isPlugin() {
|
2021-05-19 00:33:13 +08:00
|
|
|
return env.GERRIT_PROJECT == 'canvas-lms' ? '0' : '1'
|
2020-06-10 01:49:59 +08:00
|
|
|
}
|
|
|
|
|
2024-07-30 06:53:18 +08:00
|
|
|
def getNodeCount() {
|
|
|
|
return commitMessageFlag('fsc-node-count') as Integer ?: 0
|
|
|
|
}
|
|
|
|
|
2020-06-10 01:49:59 +08:00
|
|
|
def getDockerWorkDir() {
|
2022-01-19 09:58:11 +08:00
|
|
|
if (env.GERRIT_PROJECT == 'qti_migration_tool') {
|
|
|
|
return "/usr/src/app/vendor/${env.GERRIT_PROJECT}"
|
|
|
|
}
|
|
|
|
|
2021-05-19 00:33:13 +08:00
|
|
|
return env.GERRIT_PROJECT == 'canvas-lms' ? '/usr/src/app' : "/usr/src/app/gems/plugins/${env.GERRIT_PROJECT}"
|
2020-06-10 01:49:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def getLocalWorkDir() {
|
2022-01-19 09:58:11 +08:00
|
|
|
if (env.GERRIT_PROJECT == 'qti_migration_tool') {
|
|
|
|
return "vendor/${env.GERRIT_PROJECT}"
|
|
|
|
}
|
|
|
|
|
2021-05-19 00:33:13 +08:00
|
|
|
return env.GERRIT_PROJECT == 'canvas-lms' ? '.' : "gems/plugins/${env.GERRIT_PROJECT}"
|
2020-06-10 01:49:59 +08:00
|
|
|
}
|
|
|
|
|
2023-03-17 00:59:37 +08:00
|
|
|
def getMaxNodes() {
|
2024-07-30 06:53:18 +08:00
|
|
|
return commitMessageFlag('fsc-max-nodes') as Integer ?: getNodeCount() ?: 10
|
2023-03-17 00:59:37 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 03:42:51 +08:00
|
|
|
def setupNode() {
|
|
|
|
sh 'rm -vrf ./tmp'
|
2021-05-19 00:33:13 +08:00
|
|
|
def refspecToCheckout = env.GERRIT_PROJECT == 'canvas-lms' ? env.GERRIT_REFSPEC : env.CANVAS_LMS_REFSPEC
|
2020-11-02 23:35:53 +08:00
|
|
|
|
2023-03-14 02:16:40 +08:00
|
|
|
checkoutFromGit(gerritProjectUrl('canvas-lms'), refspec: refspecToCheckout, depth: 2)
|
2020-11-02 23:35:53 +08:00
|
|
|
|
2021-05-19 00:33:13 +08:00
|
|
|
if (env.IS_PLUGIN == '1') {
|
2020-06-10 01:49:59 +08:00
|
|
|
dir(env.LOCAL_WORKDIR) {
|
2023-03-14 02:16:40 +08:00
|
|
|
checkoutFromGit(gerritProjectUrl(), refspec: env.GERRIT_REFSPEC, depth: 2)
|
2020-06-10 01:49:59 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 09:51:36 +08:00
|
|
|
|
2021-06-24 05:39:36 +08:00
|
|
|
credentials.withStarlordCredentials { ->
|
2021-03-19 00:37:08 +08:00
|
|
|
sh(script: 'build/new-jenkins/docker-compose-pull.sh', label: 'Pull Images')
|
|
|
|
}
|
2021-05-12 02:56:43 +08:00
|
|
|
sh(script: 'build/new-jenkins/docker-compose-build-up.sh', label: 'Start Containers')
|
2020-09-24 03:42:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def computeTestCount() {
|
2020-02-20 03:50:26 +08:00
|
|
|
// oops, probably should have added an easier way to _count_ tests...
|
2020-06-10 01:49:59 +08:00
|
|
|
sh 'rm -vrf $LOCAL_WORKDIR/tmp'
|
|
|
|
sh 'mkdir -v $LOCAL_WORKDIR/tmp'
|
|
|
|
sh 'chmod -vv 777 $LOCAL_WORKDIR/tmp'
|
2020-09-24 23:40:09 +08:00
|
|
|
|
|
|
|
if (FORCE_FAILURE == '1') {
|
2021-05-19 01:03:16 +08:00
|
|
|
changedTests = 'spec/force_failure_spec.rb'
|
2020-09-24 23:40:09 +08:00
|
|
|
} else {
|
|
|
|
sh '''
|
|
|
|
docker run --volume $(pwd)/$LOCAL_WORKDIR/.git:$DOCKER_WORKDIR/.git \
|
|
|
|
--volume $(pwd)/$LOCAL_WORKDIR/tmp:$DOCKER_WORKDIR/tmp \
|
|
|
|
--env FSC_IGNORE_FILES \
|
|
|
|
-w=$DOCKER_WORKDIR \
|
|
|
|
$PATCHSET_TAG \
|
2022-08-18 22:19:36 +08:00
|
|
|
bash -c "git config --global --add safe.directory $DOCKER_WORKDIR; flakey_spec_catcher --use-parent --list-child-specs --dry-run-quiet > $DOCKER_WORKDIR/tmp/test_list"
|
2020-09-24 23:40:09 +08:00
|
|
|
'''
|
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
changedTests = readFile("$env.LOCAL_WORKDIR/tmp/test_list").trim()
|
2020-09-24 23:40:09 +08:00
|
|
|
}
|
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
echo "raw result from catcher: \n====\n$changedTests\n===="
|
2020-09-23 03:30:29 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
if (changedTests.length() == 0) {
|
2021-05-19 00:33:13 +08:00
|
|
|
echo 'no tests found to execute'
|
2020-09-25 01:39:02 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-16 01:50:34 +08:00
|
|
|
def changedTestsArr = changedTests.split('\n').collect { changedTest ->
|
|
|
|
"$DOCKER_WORKDIR/$changedTest"
|
|
|
|
}
|
2024-07-30 06:53:18 +08:00
|
|
|
if (FSC_NODE_COUNT.toInteger() > 0) {
|
|
|
|
def testCount = changedTestsArr.size().toInteger()
|
|
|
|
def leftoverTests = testCount % FSC_NODE_COUNT.toInteger()
|
|
|
|
def perNode = testCount.intdiv(FSC_NODE_COUNT.toInteger())
|
|
|
|
|
|
|
|
echo "Test Count: " + testCount + ", leftoverTests: " + leftoverTests + ", perNode: " + perNode
|
|
|
|
|
|
|
|
def sourceIndex = 0
|
|
|
|
def splits = Math.min(FSC_NODE_COUNT.toInteger(), testCount)
|
|
|
|
|
|
|
|
(0..( splits-1 )).each {
|
|
|
|
if (it < leftoverTests) {
|
|
|
|
sliceSize = perNode + 1
|
|
|
|
} else {
|
|
|
|
sliceSize = perNode
|
|
|
|
}
|
|
|
|
|
|
|
|
partitions += [changedTestsArr[sourceIndex .. (sourceIndex + sliceSize - 1)]]
|
|
|
|
sourceIndex += sliceSize
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "expected nodes to run on for ${changedTestsArr.size()} tests: ${partitions.size()}"
|
|
|
|
return
|
|
|
|
}
|
2021-10-16 01:50:34 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
def testCount = changedTestsArr.size()
|
2021-05-19 03:42:23 +08:00
|
|
|
def weightedValues = changedTestsArr.collect { changedTest ->
|
|
|
|
changedTest.contains('selenium') ? env.SELENIUM_RATIO.toInteger() : 1
|
2020-09-28 23:23:32 +08:00
|
|
|
}
|
2020-09-23 03:30:29 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
def distributedFactor = env.TESTS_PER_NODE.toInteger()
|
2020-02-20 03:50:26 +08:00
|
|
|
|
2020-09-28 23:23:32 +08:00
|
|
|
def i = 0
|
2021-05-19 01:03:16 +08:00
|
|
|
def curPartition = []
|
|
|
|
def curWeight = 0
|
2020-09-28 23:23:32 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
while (i < testCount) {
|
|
|
|
curPartition.add(changedTestsArr[i])
|
|
|
|
curWeight += weightedValues[i]
|
2020-09-23 03:30:29 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
if (curWeight >= distributedFactor) {
|
|
|
|
partitions.add(curPartition)
|
2020-09-23 03:30:29 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
curPartition = []
|
|
|
|
curWeight = 0
|
2020-09-28 23:23:32 +08:00
|
|
|
}
|
2020-09-23 03:30:29 +08:00
|
|
|
|
2020-09-28 23:23:32 +08:00
|
|
|
i++
|
2020-02-20 03:50:26 +08:00
|
|
|
}
|
2020-09-23 03:30:29 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
if (curPartition.size() > 0) {
|
|
|
|
partitions.add(curPartition)
|
2020-09-30 00:01:18 +08:00
|
|
|
}
|
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
echo "expected nodes to run on for $testCount tests: ${partitions.size()}"
|
2020-02-20 03:50:26 +08:00
|
|
|
}
|
|
|
|
|
2021-05-19 22:08:46 +08:00
|
|
|
def executeFlakeySpecCatcher() {
|
2020-02-20 03:50:26 +08:00
|
|
|
try {
|
2021-05-12 02:56:43 +08:00
|
|
|
sh 'build/new-jenkins/rspec-flakey-spec-catcher.sh'
|
2020-09-29 23:46:14 +08:00
|
|
|
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
|
|
|
|
if (e.causes[0] instanceof org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution.ExceededTimeout) {
|
2021-05-19 00:33:13 +08:00
|
|
|
echo 'Failing the build due to timeouts'
|
2021-05-19 01:03:16 +08:00
|
|
|
fscStatus = 'timeout'
|
2021-01-07 01:11:17 +08:00
|
|
|
throw e
|
2021-05-19 00:33:13 +08:00
|
|
|
} else if (e.causes[0] instanceof jenkins.model.CauseOfInterruption.UserInterruption) {
|
|
|
|
echo 'Build aborted'
|
2021-05-19 01:03:16 +08:00
|
|
|
fscStatus = 'aborted'
|
2020-09-29 23:46:14 +08:00
|
|
|
} else {
|
|
|
|
throw e
|
2020-03-24 00:51:38 +08:00
|
|
|
}
|
2020-09-29 23:46:14 +08:00
|
|
|
} finally {
|
2022-06-08 04:48:32 +08:00
|
|
|
sh """
|
|
|
|
rm -vrf tmp && mkdir -p tmp
|
|
|
|
docker cp flakey-spec-catcher_canvas_1:/usr/src/app/tmp/fsc.out tmp/fsc-${env.NODE_NUMBER}.out || true
|
|
|
|
"""
|
|
|
|
archiveArtifacts artifacts: "tmp/fsc-${env.NODE_NUMBER}.out", allowEmptyArchive: true
|
2020-02-20 03:50:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 05:49:29 +08:00
|
|
|
def sendSlack(success) {
|
2021-05-19 01:03:16 +08:00
|
|
|
def color = fscStatus == 'timeout' ? 'warning' : (success ? 'good' : 'danger')
|
2020-03-07 05:49:29 +08:00
|
|
|
def jobInfo = "<https://gerrit.instructure.com/$env.GERRIT_CHANGE_NUMBER|Gerrit> | <$env.BUILD_URL|Jenkins>"
|
2021-05-19 01:03:16 +08:00
|
|
|
def message = "$jobInfo\n$changedTests"
|
|
|
|
if (fscStatus == 'timeout') {
|
2020-03-24 00:51:38 +08:00
|
|
|
message = "Timeout Occurred!\n$message"
|
|
|
|
}
|
2020-03-07 05:49:29 +08:00
|
|
|
slackSend channel: '#flakey_spec_catcher_noisy', color: color, message: message
|
|
|
|
}
|
|
|
|
|
2020-07-27 21:22:42 +08:00
|
|
|
def cleanupFn(status) {
|
2021-05-19 01:03:16 +08:00
|
|
|
if (fscStatus != 'aborted') {
|
2021-04-19 23:12:48 +08:00
|
|
|
sendSlack(status == 'SUCCESS')
|
2020-07-27 21:22:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 14:06:34 +08:00
|
|
|
pipeline {
|
2020-07-27 21:22:42 +08:00
|
|
|
agent none
|
2020-05-09 02:23:07 +08:00
|
|
|
options {
|
|
|
|
ansiColor('xterm')
|
2021-01-07 01:11:17 +08:00
|
|
|
timeout(time: FSC_TIMEOUT)
|
2020-05-09 02:23:07 +08:00
|
|
|
timestamps()
|
|
|
|
}
|
2019-11-25 14:06:34 +08:00
|
|
|
|
|
|
|
environment {
|
2020-06-10 01:49:59 +08:00
|
|
|
GERRIT_PORT = '29418'
|
|
|
|
GERRIT_URL = "$GERRIT_HOST:$GERRIT_PORT"
|
2020-07-21 22:16:43 +08:00
|
|
|
BUILD_REGISTRY_FQDN = configuration.buildRegistryFQDN()
|
2019-11-25 14:06:34 +08:00
|
|
|
COMPOSE_FILE = 'docker-compose.new-jenkins.yml:docker-compose.new-jenkins-selenium.yml:docker-compose.new-jenkins-flakey-spec-catcher.yml'
|
2020-06-10 01:49:59 +08:00
|
|
|
IS_PLUGIN = isPlugin()
|
|
|
|
DOCKER_WORKDIR = getDockerWorkDir()
|
|
|
|
LOCAL_WORKDIR = getLocalWorkDir()
|
2023-03-17 02:00:28 +08:00
|
|
|
FORCE_FAILURE = commitMessageFlag('force-failure-fsc').asBooleanInteger()
|
2020-04-15 04:30:00 +08:00
|
|
|
// fsc errors when running specs from gems.
|
|
|
|
// until we figure out how to run them, we should ignore them
|
2023-03-29 04:54:42 +08:00
|
|
|
FSC_IGNORE_FILES = 'gems/.*/spec/,spec/contracts/,spec/selenium/performance/'
|
2020-04-30 07:18:24 +08:00
|
|
|
POSTGRES_PASSWORD = 'sekret'
|
2024-07-30 06:53:18 +08:00
|
|
|
FSC_NODE_COUNT = getNodeCount()
|
2020-09-28 23:23:32 +08:00
|
|
|
|
2021-05-12 02:56:43 +08:00
|
|
|
// Targeting 10 minutes / node, each node runs RSPEC_PROCESSES threads and
|
2020-09-28 23:23:32 +08:00
|
|
|
// repeats each test FSC_REPEAT_FACTOR times.
|
|
|
|
// Assumption: non-selenium tests take 500ms / test
|
|
|
|
// Assumption: selenium tests take 500ms * SELENIUM_RATIO / test
|
|
|
|
FSC_REPEAT_FACTOR = 10
|
2023-03-17 00:59:37 +08:00
|
|
|
MAX_NODES = getMaxNodes()
|
2021-05-12 02:56:43 +08:00
|
|
|
RSPEC_PROCESSES = 3
|
2020-09-28 23:23:32 +08:00
|
|
|
SELENIUM_RATIO = 20
|
|
|
|
TESTS_PER_NODE = 200
|
2019-11-25 14:06:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
stages {
|
2020-07-27 21:22:42 +08:00
|
|
|
stage('Environment') {
|
2019-11-25 14:06:34 +08:00
|
|
|
steps {
|
2020-02-20 03:50:26 +08:00
|
|
|
script {
|
2023-03-17 02:46:17 +08:00
|
|
|
extendedStage('Runner').nodeRequirements(label: nodeLabel()).obeysAllowStages(false).execute {
|
2021-04-14 04:59:49 +08:00
|
|
|
def postBuildHandler = [
|
2021-12-01 02:56:25 +08:00
|
|
|
onStageEnded: { stageName, stageConfig, result ->
|
2021-04-14 04:59:49 +08:00
|
|
|
cleanupFn(stageConfig.status())
|
|
|
|
}
|
|
|
|
]
|
2020-07-27 21:22:42 +08:00
|
|
|
|
2021-04-15 06:16:39 +08:00
|
|
|
extendedStage('Builder').hooks(postBuildHandler).obeysAllowStages(false).execute {
|
2021-04-14 04:59:49 +08:00
|
|
|
stage('Setup') {
|
|
|
|
setupNode()
|
|
|
|
}
|
2020-09-28 23:23:32 +08:00
|
|
|
|
2021-05-19 00:33:13 +08:00
|
|
|
stage('Compute Build Distribution') {
|
2021-04-14 04:59:49 +08:00
|
|
|
computeTestCount()
|
2020-09-25 04:12:44 +08:00
|
|
|
}
|
|
|
|
|
2021-04-14 04:59:49 +08:00
|
|
|
stage('Run Flakey Spec Catcher') {
|
2021-05-19 00:33:13 +08:00
|
|
|
if (partitions.size() == 0) {
|
2021-04-14 04:59:49 +08:00
|
|
|
return
|
2021-05-19 00:33:13 +08:00
|
|
|
} else if (partitions.size() > env.MAX_NODES.toInteger()) {
|
2021-04-14 04:59:49 +08:00
|
|
|
error "Refusing to use more than ${env.MAX_NODES} nodes to catch flakey specs. Consider breaking this change into smaller pieces."
|
2020-09-25 04:12:44 +08:00
|
|
|
|
2021-04-14 04:59:49 +08:00
|
|
|
return
|
2020-07-27 21:22:42 +08:00
|
|
|
}
|
2020-09-25 04:12:44 +08:00
|
|
|
|
2021-04-14 04:59:49 +08:00
|
|
|
def stages = [:]
|
|
|
|
|
2021-05-19 00:33:13 +08:00
|
|
|
stages['flakey set 00'] = {
|
2021-04-14 04:59:49 +08:00
|
|
|
withEnv([
|
|
|
|
"FSC_TESTS=${partitions[0].join(',')}",
|
2021-05-19 00:33:13 +08:00
|
|
|
'NODE_NUMBER=00'
|
2021-04-14 04:59:49 +08:00
|
|
|
]) {
|
|
|
|
executeFlakeySpecCatcher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 00:33:13 +08:00
|
|
|
for (int i = 1; i < partitions.size(); i++) {
|
2021-04-14 04:59:49 +08:00
|
|
|
// make sure to create a new index variable so this value gets
|
|
|
|
// captured by the lambda
|
|
|
|
def index = i
|
2021-05-19 01:03:16 +08:00
|
|
|
def nodeNumber = (index).toString().padLeft(2, '0')
|
2021-04-14 04:59:49 +08:00
|
|
|
|
2021-05-19 01:03:16 +08:00
|
|
|
stages["flakey set $nodeNumber"] = {
|
2023-03-17 02:46:17 +08:00
|
|
|
protectedNode(nodeLabel()) {
|
2021-04-14 04:59:49 +08:00
|
|
|
withEnv([
|
|
|
|
"FSC_TESTS=${partitions[index].join(',')}",
|
2021-05-19 01:03:16 +08:00
|
|
|
"NODE_NUMBER=$nodeNumber"
|
2021-04-14 04:59:49 +08:00
|
|
|
]) {
|
2021-04-19 23:12:48 +08:00
|
|
|
setupNode()
|
2021-05-19 22:08:46 +08:00
|
|
|
executeFlakeySpecCatcher()
|
2020-02-20 03:50:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 04:59:49 +08:00
|
|
|
parallel(stages)
|
2020-02-20 03:50:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 14:06:34 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 01:14:18 +08:00
|
|
|
}
|
2019-11-25 14:06:34 +08:00
|
|
|
}
|
|
|
|
}
|