blob: 3b88aeeb6532e8924f3ffb73d649007f62b61086 [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
Jakub Josefa63f9862018-01-11 17:58:38 +010089timeout(time: 12, unit: 'HOURS') {
90 node("python") {
91 try{
92 stage("checkout") {
chnyda32c6d9d2017-09-27 10:18:09 +020093 if (gerritRef) {
Jakub Josefa63f9862018-01-11 17:58:38 +010094 // job is triggered by Gerrit
95 // test if change aren't already merged
96 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
97 // test if gerrit change is already Verified
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010098 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) {
Jakub Josefa63f9862018-01-11 17:58:38 +010099 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
100 // test WIP contains in commit message
101 }else if (gerritChange.commitMessage.contains("WIP")) {
102 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
103 } else {
104 def merged = gerritChange.status == "MERGED"
105 if(!merged){
106 checkouted = gerrit.gerritPatchsetCheckout ([
107 credentialsId : CREDENTIALS_ID
108 ])
109 } else{
110 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
111 }
chnyda32c6d9d2017-09-27 10:18:09 +0200112 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100113 // defaultGitUrl is passed to the triggered job
114 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
115 defaultGitRef = GERRIT_REFSPEC
116 } else if(defaultGitRef && defaultGitUrl) {
117 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
118 } else {
119 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
chnyda3bf82d82017-10-19 14:01:53 +0200120 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100121 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100122
123 stage("Check YAML") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100124 common.infoMsg("Checking YAML syntax for changed files")
125 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)
126 if(syntaxCheckStatus > 0){
127 common.errorMsg("YAML syntax check failed!")
128 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100129 }
130
131 stage("test-nodes") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100132 if (checkouted) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100133 def modifiedClusters = null
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100134 // testing modified cluster is used only if test was triggered by gerrit
135 if (gerritRef) {
136 def checkChange = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
137 if (checkChange == 1) {
138 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 +0100139 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100140 }
141
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100142 def infraYMLs = []
143 // list of cluster names can be explicitly given
144 if (testClusterNames != null && testClusterNames != "") {
145 common.infoMsg("TEST_CLUSTER_NAMES param found, using explicitly defined cluster names: ${testClusterNames}")
Jakub Josefdbe40d32018-02-19 16:08:12 +0100146 def clusterNameRegex = testClusterNames.tokenize(",").collect{it.trim()}.join("|")
147 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 +0100148 } else {
149 common.infoMsg("TEST_CLUSTER_NAMES param not found, all clusters with enabled tests will be tested")
150 // else we want to test all cluster levels found
151 infraYMLs = sh(script: "set +x;find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
152 def clusterDirectories = sh(script: "set +x;ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
153
154 // create a list of cluster names present in cluster folder
155 def infraList = []
156 for (elt in infraYMLs) {
157 infraList << elt.tokenize('/')[3]
158 }
159
160 // verify we have all valid clusters loaded
161 def commonList = infraList.intersect(clusterDirectories)
162 def differenceList = infraList.plus(clusterDirectories)
163 differenceList.removeAll(commonList)
164
165 if (!differenceList.isEmpty()) {
166 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
167 }
168 if (modifiedClusters) {
169 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
170 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
171 }
172 }
173 common.infoMsg("Starting salt models test for these clusters " + infraYMLs.collect{ it.tokenize("/")[3] })
174 if (infraYMLs.size() > 0) {
175 for (int i = 0; i < infraYMLs.size(); i++) {
176 def infraYMLConfig = readYaml(file: infraYMLs[i])
177 if (_clusterTestEnabled(infraYMLConfig)) {
178 if(!infraYMLConfig["parameters"].containsKey("_param")){
179 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
180 continue
181 }
182 def infraParams = infraYMLConfig["parameters"]["_param"];
183 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
184 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.")
185 continue
186 }
187 def clusterName = infraParams["cluster_name"]
188 def clusterDomain = infraParams["cluster_domain"]
189 def configHostname = infraParams["infra_config_hostname"]
190 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
191
192 futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
193 }
194 }
195 } else {
196 common.warningMsg("List of found salt model clusters is empty, no tests will be started!")
197 }
198
Jakub Josefa63f9862018-01-11 17:58:38 +0100199 setupRunner()
200
201 if (failedNodes) {
202 currentBuild.result = "FAILURE"
203 }
204 }
205 }
206 } catch (Throwable e) {
207 currentBuild.result = "FAILURE"
208 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
209 throw e
210 } finally {
211 common.sendNotification(currentBuild.result,"",["slack"])
chnyda51b03142017-05-10 17:15:27 +0200212 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100213 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200214}