blob: ef26ea5cf0c7bac6064401ce67b85db7f88fbadb [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001/**
2 * Test salt models pipeline
Jakub Josefcb4f1dc2018-02-19 15:27:29 +01003 * DEFAULT_GIT_URL default git url (will be used if pipeline run is not triggered by gerrit)
4 * DEFAULT_GIT_RED default git ref (branch,tag,...) (will be used if pipeline run is not triggered by gerrit)
5 * CREDENTIALS_ID Jenkins credetials id for git checkout
Jakub Josefcb4f1dc2018-02-19 15:27:29 +01006 * MAX_CPU_PER_JOB max cpu count for one docket test instance
7 * SYSTEM_GIT_URL reclass system git URL (optional)
8 * SYSTEM_GIT_REF reclass system git URL (optional)
9 * TEST_CLUSTER_NAMES list of comma separated cluster names to test (optional, default all cluster levels)
10 * LEGACY_TEST_MODE legacy test mode flag
11 * RECLASS_IGNORE_CLASS_NOTFOUND ignore missing class flag for reclass config
azvyagintsev1bcadc42018-10-05 20:13:15 +030012 * DISTRIB_REVISION of apt mirrror to be used (http://mirror.mirantis.com/DISTRIB_REVISION/ by default)
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010013 * APT_REPOSITORY extra apt repository url
14 * APT_REPOSITORY_GPG extra apt repository url GPG
Jakub Josef83379312017-03-29 18:12:34 +020015 */
16
Jakub Josefc5a223a2017-03-01 14:40:08 +010017def gerrit = new com.mirantis.mk.Gerrit()
azvyagintsev2856c3a2018-10-11 13:34:14 +030018common = new com.mirantis.mk.Common()
Filip Pytloun38005aa2017-03-06 10:26:38 +010019def ssh = new com.mirantis.mk.Ssh()
20def git = new com.mirantis.mk.Git()
Jakub Josefc5a223a2017-03-01 14:40:08 +010021
azvyagintsev1bcadc42018-10-05 20:13:15 +030022def config_node_name_pattern = env.CONFIG_NODE_NAME_PATTERN ?: 'cfg01'
azvyagintsev2856c3a2018-10-11 13:34:14 +030023def gerritRef = env.GERRIT_REFSPEC ?: null
azvyagintsev1bcadc42018-10-05 20:13:15 +030024def formulasSource = env.FORMULAS_SOURCE ?: 'pkg'
azvyagintsev2856c3a2018-10-11 13:34:14 +030025distribRevision = env.DISTRIB_REVISION ?: 'nightly'
chnyda64b73582017-07-19 12:01:32 +020026
azvyagintsev1bcadc42018-10-05 20:13:15 +030027def testClusterNames = env.TEST_CLUSTER_NAMES ?: ''
28def defaultGitRef = env.DEFAULT_GIT_REF ?: null
29def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
chnyda400babe2017-10-23 10:35:45 +020030
Jakub Josef83379312017-03-29 18:12:34 +020031def checkouted = false
chnyda400babe2017-10-23 10:35:45 +020032futureNodes = []
chnyda3dcf0bd2017-11-30 11:49:24 +010033failedNodes = false
azvyagintsev1bcadc42018-10-05 20:13:15 +030034
chnydafa674a02017-10-19 11:49:22 +020035
chnyda400babe2017-10-23 10:35:45 +020036def setupRunner() {
Denis Egorenkoeff76d32018-10-01 12:09:40 +040037 def branches = [:]
38 branches.failFast = true
39 for(int i = 0; i < futureNodes.size(); i++) {
40 def currentNode = futureNodes[i] ? futureNodes[i] : null
chnyda400babe2017-10-23 10:35:45 +020041 if (!currentNode) {
Denis Egorenkoeff76d32018-10-01 12:09:40 +040042 continue
chnyda400babe2017-10-23 10:35:45 +020043 }
Denis Egorenkoeff76d32018-10-01 12:09:40 +040044 branches["Runner ${i}"] = {
45 try {
46 triggerTestNodeJob(currentNode[0], currentNode[1], currentNode[2], currentNode[3], currentNode[4])
47 } catch (Exception e) {
48 common.warningMsg("Test of ${currentNode[2]} failed : ${e}")
49 throw e
50 }
chnyda400babe2017-10-23 10:35:45 +020051 }
chnyda400babe2017-10-23 10:35:45 +020052 }
chnyda3dcf0bd2017-11-30 11:49:24 +010053
Denis Egorenkoeff76d32018-10-01 12:09:40 +040054 if (branches) {
55 common.runParallel(branches, PARALLEL_NODE_GROUP_SIZE.toInteger())
56 }
chnyda400babe2017-10-23 10:35:45 +020057}
chnydafa674a02017-10-19 11:49:22 +020058
59def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
chnyda400babe2017-10-23 10:35:45 +020060 common.infoMsg("Test of ${clusterName} starts")
chnydafa674a02017-10-19 11:49:22 +020061 build job: "test-salt-model-node", parameters: [
62 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
63 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
64 [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName],
65 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
66 [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
chnydafa674a02017-10-19 11:49:22 +020067 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
68 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
azvyagintsev1bcadc42018-10-05 20:13:15 +030069 [$class: 'StringParameterValue', name: 'DISTRIB_REVISION', value: distribRevision],
chnydafa674a02017-10-19 11:49:22 +020070 [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
71 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
72 [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
Dmitry Ukovb2aa6db2017-10-23 12:30:31 +040073 [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()],
74 [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY],
75 [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG]
chnydafa674a02017-10-19 11:49:22 +020076 ]
77}
78
Jakub Josef3e77eb72018-01-15 14:21:53 +010079def _clusterTestEnabled(infraYMLConfig){
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010080 if (infraYMLConfig["parameters"].containsKey("_jenkins")) {
81 if (infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")) {
Jakub Josef3e77eb72018-01-15 14:21:53 +010082 return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"];
83 }
84 }
85 // ci tests are enabled by default
86 return true;
87}
88
Vladimir Khlyunevd77c1882022-01-31 15:57:35 +040089def nodeLabel = 'docker'
Vladimir Khlyunev81a3c902021-02-01 18:51:15 +040090
Jakub Josefa63f9862018-01-11 17:58:38 +010091timeout(time: 12, unit: 'HOURS') {
Vladimir Khlyunev81a3c902021-02-01 18:51:15 +040092 node(nodeLabel) {
Jakub Josefa63f9862018-01-11 17:58:38 +010093 try{
94 stage("checkout") {
chnyda32c6d9d2017-09-27 10:18:09 +020095 if (gerritRef) {
Jakub Josefa63f9862018-01-11 17:58:38 +010096 // job is triggered by Gerrit
97 // test if change aren't already merged
98 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
99 // test if gerrit change is already Verified
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100100 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100101 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
102 // test WIP contains in commit message
103 }else if (gerritChange.commitMessage.contains("WIP")) {
104 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
105 } else {
106 def merged = gerritChange.status == "MERGED"
107 if(!merged){
108 checkouted = gerrit.gerritPatchsetCheckout ([
109 credentialsId : CREDENTIALS_ID
110 ])
111 } else{
112 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
113 }
chnyda32c6d9d2017-09-27 10:18:09 +0200114 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100115 // defaultGitUrl is passed to the triggered job
116 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
117 defaultGitRef = GERRIT_REFSPEC
118 } else if(defaultGitRef && defaultGitUrl) {
119 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
120 } else {
121 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
chnyda3bf82d82017-10-19 14:01:53 +0200122 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100123 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100124
125 stage("Check YAML") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100126 common.infoMsg("Checking YAML syntax for changed files")
127 def syntaxCheckStatus = sh(script:"set +x;git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD | grep .yml | xargs -I {} python -c \"import yaml; yaml.load(open('{}', 'r'))\" \\;", returnStatus:true)
128 if(syntaxCheckStatus > 0){
129 common.errorMsg("YAML syntax check failed!")
130 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100131 }
132
133 stage("test-nodes") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100134 if (checkouted) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100135 def modifiedClusters = null
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100136 // testing modified cluster is used only if test was triggered by gerrit
137 if (gerritRef) {
138 def checkChange = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
139 if (checkChange == 1) {
140 modifiedClusters = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep classes/cluster/ | awk -F/ '{print \$3}' | uniq", returnStdout: true).tokenize()
Jakub Josef3e77eb72018-01-15 14:21:53 +0100141 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100142 }
143
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100144 def infraYMLs = []
145 // list of cluster names can be explicitly given
146 if (testClusterNames != null && testClusterNames != "") {
147 common.infoMsg("TEST_CLUSTER_NAMES param found, using explicitly defined cluster names: ${testClusterNames}")
Jakub Josefdbe40d32018-02-19 16:08:12 +0100148 def clusterNameRegex = testClusterNames.tokenize(",").collect{it.trim()}.join("|")
149 infraYMLs = sh(script:"set +x;find ./classes/ -regextype posix-egrep -regex '.*cluster/(${clusterNameRegex}){1}/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100150 } else {
151 common.infoMsg("TEST_CLUSTER_NAMES param not found, all clusters with enabled tests will be tested")
152 // else we want to test all cluster levels found
153 infraYMLs = sh(script: "set +x;find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
154 def clusterDirectories = sh(script: "set +x;ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
155
156 // create a list of cluster names present in cluster folder
157 def infraList = []
158 for (elt in infraYMLs) {
159 infraList << elt.tokenize('/')[3]
160 }
161
162 // verify we have all valid clusters loaded
163 def commonList = infraList.intersect(clusterDirectories)
164 def differenceList = infraList.plus(clusterDirectories)
165 differenceList.removeAll(commonList)
166
167 if (!differenceList.isEmpty()) {
168 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
169 }
170 if (modifiedClusters) {
171 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
172 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
173 }
174 }
175 common.infoMsg("Starting salt models test for these clusters " + infraYMLs.collect{ it.tokenize("/")[3] })
176 if (infraYMLs.size() > 0) {
177 for (int i = 0; i < infraYMLs.size(); i++) {
178 def infraYMLConfig = readYaml(file: infraYMLs[i])
179 if (_clusterTestEnabled(infraYMLConfig)) {
180 if(!infraYMLConfig["parameters"].containsKey("_param")){
181 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
182 continue
183 }
184 def infraParams = infraYMLConfig["parameters"]["_param"];
185 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
186 common.warningMsg("ERROR: Cannot find _param:infra_config_hostname or _param:cluster_name or _param:cluster_domain in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
187 continue
188 }
189 def clusterName = infraParams["cluster_name"]
190 def clusterDomain = infraParams["cluster_domain"]
191 def configHostname = infraParams["infra_config_hostname"]
192 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
193
194 futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
195 }
196 }
197 } else {
198 common.warningMsg("List of found salt model clusters is empty, no tests will be started!")
199 }
200
Jakub Josefa63f9862018-01-11 17:58:38 +0100201 setupRunner()
202
203 if (failedNodes) {
204 currentBuild.result = "FAILURE"
205 }
206 }
207 }
208 } catch (Throwable e) {
209 currentBuild.result = "FAILURE"
210 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
211 throw e
212 } finally {
213 common.sendNotification(currentBuild.result,"",["slack"])
chnyda51b03142017-05-10 17:15:27 +0200214 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100215 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200216}