Add common functions to run mcp-qa tests
- Added RunTest package with function to run test
- Added EnvActions package with functions to manipulate env
- Added Common package with common functions
Change-Id: I921c75f990e182c1264aeecf29780b2390cfcc9c
diff --git a/src/com/mirantis/mcp-qa/Common.groovy b/src/com/mirantis/mcp-qa/Common.groovy
new file mode 100644
index 0000000..72842d2
--- /dev/null
+++ b/src/com/mirantis/mcp-qa/Common.groovy
@@ -0,0 +1,99 @@
+package com.mirantis.mcp-qa
+
+/**
+ * Get latest artifacts
+ * @param imageRepoName is the repo name where image is located
+ * @param imageTagName is the name of the image tag to be used
+ */
+
+def getLatestArtifacts(imageRepoName, imageTagName) {
+ def imageRepo = env.getAt(imageRepoName)
+ def imageTag = env.getAt(imageTagName)
+ if ( imageTag != null && (! imageTag || imageTag.equals('latest')) ) {
+ if ( imageRepo ) {
+ def registry = imageRepo.replaceAll(/\/.*/, '')
+ def image = imageRepo.minus(registry + '/')
+ def hyperkubeImageTag = latestImageTagLookup(registry, image)
+ return "${imageTagName}=${hyperkubeImageTag}"
+ } else {
+ echo "${imageRepoName} variable isn't set, can't inspect 'latest' image!"
+ return null
+ }
+ }
+}
+
+def jsonParse(def json) {
+ new groovy.json.JsonSlurperClassic().parseText(json)
+}
+
+/**
+ * Get digest metadata
+ * @param tag is the image tag to be used
+ * @param registry is the url of registry
+ * @param image is the image which info is looked for
+ */
+
+def get_digest(def tag, def registry, def image) {
+ def digest_link = sprintf('https://%1$s/v2/%2$s/manifests/%3$s', [registry, image, tag])
+ def digest_url = new URL(digest_link)
+ def connection = digest_url.openConnection()
+ connection.setRequestProperty('Accept', 'application/vnd.docker.distribution.manifest.v2+json')
+ def digest = connection.getHeaderField("Docker-Content-Digest")
+ return digest
+}
+
+/**
+ * Get latest tag metadata
+ * @param registry is the url of registry
+ * @param image is the image which tags are looked for
+ */
+
+def latestImageTagLookup(registry, image) {
+ def tags_link = sprintf('https://%1$s/v2/%2$s/tags/list', [registry, image])
+ def tags_url = new URL(tags_link)
+ def tags = jsonParse(tags_url.getText())['tags']
+ def latest_digest = get_digest('latest', registry, image)
+ def same_digest_tags = []
+
+ for (tag in tags) {
+ if (tag == 'latest') {
+ continue
+ }
+ if (get_digest(tag, registry, image) == latest_digest) {
+ same_digest_tags<< tag
+ }
+ }
+
+ return same_digest_tags[0] ?: 'latest'
+}
+
+
+/**
+ * Fetch custom refs
+ * @param gerritUrl is url of gerrit
+ * @param project is the name of project in gerrit
+ * @param targetDir is dir where to fetch changes
+ * @param refs is refs that need to be fetched
+ */
+
+def getCustomRefs(gerritUrl, project, targetDir, refs) {
+ def remote = "${gerritUrl}/${project}"
+ dir(targetDir) {
+ for(int i=0; i<refs.size(); i++) {
+ sh "git fetch ${remote} ${refs[i]} && git checkout FETCH_HEAD"
+ }
+ }
+}
+
+/**
+ * Set downstream k8s artifacts
+ * @param jobSetParameters are current job parameters that can be extended with kubernetes tag
+ */
+
+def set_downstream_k8s_artifacts(jobSetParameters) {
+ def k8sTag = getLatestArtifacts('HYPERKUBE_IMAGE_REPO', 'HYPERKUBE_IMAGE_TAG')
+ if (k8sTag) {
+ jobSetParameters.add(k8sTag)
+ }
+ return jobSetParameters
+}
diff --git a/src/com/mirantis/mcp-qa/EnvActions.groovy b/src/com/mirantis/mcp-qa/EnvActions.groovy
new file mode 100644
index 0000000..03a6a1b
--- /dev/null
+++ b/src/com/mirantis/mcp-qa/EnvActions.groovy
@@ -0,0 +1,34 @@
+package com.mirantis.mcp-qa
+
+/**
+ * Activate virtual environment and check k8s deployer is specified
+ */
+
+def prepareEnv() {
+ sh '''
+ if [ ! -r "${VENV_PATH}/bin/activate" ]; then
+ echo 'Python virtual environment not found! Set correct VENV_PATH!'
+ exit 1
+ fi
+ '''
+
+ sh '''
+ if [ ! -r "${WORKSPACE}/fuel-ccp-installer/${DEPLOY_SCRIPT_REL_PATH}" ]; then
+ echo "Deploy script \"${DEPLOY_SCRIPT_REL_PATH}\" not found in" \
+ "\"${WORKSPACE}/fuel-ccp-installer/\"!"
+ fi
+ '''
+}
+
+/**
+ * Destroy running environment
+ */
+
+def destroyEnv() {
+ if ( !(env.KEEP_BEFORE.equals('yes') || env.KEEP_BEFORE.equals('true')) ) {
+ sh '''
+ . ${VENV_PATH}/bin/activate
+ dos.py destroy ${ENV_NAME} || true
+ '''
+ }
+}
diff --git a/src/com/mirantis/mcp-qa/RunTest.groovy b/src/com/mirantis/mcp-qa/RunTest.groovy
new file mode 100644
index 0000000..a12d0ea
--- /dev/null
+++ b/src/com/mirantis/mcp-qa/RunTest.groovy
@@ -0,0 +1,34 @@
+package com.mirantis.mcp-qa
+
+/**
+ * Run mcp-qa test by specified group
+ * @param testGroup defines what tests to run, options are '-m test_mark', '-k test_expression'
+ * @param jobSetParameters is additional params needed to run mcp-qa test
+ */
+
+def runTest(testGroup, jobSetParameters) {
+ def testArgs = [ '-s', '-ra' ]
+ testArgs.add(testGroup)
+ jobSetParameters.add("TEST_ARGS=${testArgs.join(' ')}")
+ echo("The current tags, args, which were set by job: ${jobSetParameters.join(' ')}")
+ withEnv(jobSetParameters) {
+ sh '''\
+ . ${VENV_PATH}/bin/activate
+ exit_code=0
+ export IMAGE_PATH=$(readlink -f "${IMAGE_PATH}")
+ if ! py.test ${TEST_ARGS}; then
+ exit_code=1
+ fi
+ # erase environment if test passed and KEEP_AFTER isn't set to 'yes' or 'true'
+ if [ ${exit_code} -eq 0 ]; then
+ if ! [[ "${KEEP_AFTER}" == "yes" || "${KEEP_AFTER}" == "true" ]]; then
+ dos.py erase "${ENV_NAME}" || true
+ fi
+ fi
+ if [ ${exit_code} -gt 0 ]; then
+ echo "Tests failed!"
+ exit 1
+ fi
+ '''.stripIndent()
+ }
+}