Merge "Cosmetic changes inside opencontrail4.0-upgrade pipeline:  - rename variables to camelCase;  - remove undefined variable (oc_component_repo) from error message;  - delete commented code."
diff --git a/test-salt-formulas-env.groovy b/test-salt-formulas-env.groovy
index 1ea3de8..1759707 100644
--- a/test-salt-formulas-env.groovy
+++ b/test-salt-formulas-env.groovy
@@ -17,8 +17,14 @@
   defaultGitUrl = null
 }
 
+def openstack_credentials_id = ''
+if (env.OPENSTACK_API_CREDENTIALS) {
+  openstack_credentials_id = OPENSTACK_API_CREDENTIALS
+}
+
 def checkouted = false
 def openstackTest = false
+def cleanEnv = ''
 
 throttle(['test-formula']) {
   timeout(time: 1, unit: 'HOURS') {
@@ -55,21 +61,22 @@
                 def kitchenInit = kitchenConfigYML["install"]
                 def kitchenInstalled = false
                 if (kitchenInit && !kitchenInit.isEmpty()) {
-                  if (!openstackTest) {
-                    for (int i = 0; i < kitchenInit.size(); i++) {
-                      if (kitchenInit[i].trim().startsWith("test -e Gemfile")) { //found Gemfile config
-                        common.infoMsg("Custom Gemfile configuration found, using them")
-                        ruby.installKitchen(kitchenInit[i].trim())
-                        kitchenInstalled = true
+                  for (int i = 0; i < kitchenInit.size(); i++) {
+                    if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config
+                      common.infoMsg("Custom Gemfile configuration found, using them")
+                      if (openstackTest) {
+                        withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
+                        usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
+                          env.OS_USERNAME = OS_USERNAME
+                          env.OS_PASSWORD = OS_PASSWORD
+                          env.OS_AUTH_URL = OS_AUTH_URL
+                          env.OS_PROJECT_NAME = OS_PROJECT_NAME
+                          env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
+                          env.OS_AZ = OS_AZ
+                        }
                       }
-                    }
-                  } else {
-                    for (int i = 0; i < kitchenInit.size(); i++) {
-                      if (kitchenInit[i].trim().startsWith("git clone")) { //found Gemfile config TODO: Change keywords ??
-                        common.infoMsg("Custom Gemfile configuration found, using them")
-                        sh(kitchenInit[i].trim())
-                        kitchenInstalled = true
-                      }
+                      ruby.installKitchen(kitchenInit[i].trim())
+                      kitchenInstalled = true
                     }
                   }
                 }
@@ -82,10 +89,10 @@
               }
               common.infoMsg("Running part of kitchen test")
               if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
-                def cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
+                cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
                 if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv }
-                sh("find . -type f -exec sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {} \\;")
-                sh("find . -type f -exec sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {} \\;")
+                sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
+                sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
                 def suite = ruby.getSuiteName(KITCHEN_ENV)
                 if (suite && suite != "") {
                   common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
@@ -105,7 +112,8 @@
       } catch (Throwable e) {
         // If there was an error or exception thrown, the build failed
         currentBuild.result = "FAILURE"
-        ruby.runKitchenCommand("destroy")
+        sh(script: 'find .kitchen/logs/ iname "*.log" | xargs -I{} bash -c "echo {}; cat {}"')
+        ruby.runKitchenCommand("destroy", cleanEnv)
         throw e
       } finally {
         if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {