1
0
mirror of https://github.com/firstBitMarksistskaya/jenkins-lib.git synced 2025-08-25 20:09:25 +02:00

refactoring

This commit is contained in:
Dima
2024-12-01 14:18:06 +03:00
parent a004277b2e
commit 20d0b05619
10 changed files with 138 additions and 94 deletions

View File

@@ -7,6 +7,9 @@ import jenkins.plugins.http_request.ResponseContentSupplier
import org.jenkinsci.plugins.pipeline.utility.steps.fs.FileWrapper import org.jenkinsci.plugins.pipeline.utility.steps.fs.FileWrapper
import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
import ru.pulsar.jenkins.library.steps.Coverable
interface IStepExecutor { interface IStepExecutor {
@@ -20,6 +23,7 @@ interface IStepExecutor {
FileWrapper[] findFiles(String glob) FileWrapper[] findFiles(String glob)
@SuppressWarnings('unused')
FileWrapper[] findFiles(String glob, String excludes) FileWrapper[] findFiles(String glob, String excludes)
String readFile(String file) String readFile(String file)
@@ -66,6 +70,8 @@ interface IStepExecutor {
def withEnv(List<String> strings, Closure body) def withEnv(List<String> strings, Closure body)
def withCoverage(JobConfiguration config, Coverable stage, StepCoverageOptions options, Closure body)
def lock(String resource, Closure<Object> body) def lock(String resource, Closure<Object> body)
def archiveArtifacts(String path) def archiveArtifacts(String path)
@@ -80,14 +86,17 @@ interface IStepExecutor {
def zip(String dir, String zipFile) def zip(String dir, String zipFile)
@SuppressWarnings('unused')
def zip(String dir, String zipFile, String glob) def zip(String dir, String zipFile, String glob)
def unzip(String dir, String zipFile) def unzip(String dir, String zipFile)
@SuppressWarnings('unused')
def unzip(String dir, String zipFile, quiet) def unzip(String dir, String zipFile, quiet)
def catchError(Closure body) def catchError(Closure body)
@SuppressWarnings('unused')
ResponseContentSupplier httpRequest(String url, String outputFile, String responseHandle, boolean wrapAsMultipart) ResponseContentSupplier httpRequest(String url, String outputFile, String responseHandle, boolean wrapAsMultipart)
ResponseContentSupplier httpRequest(String url, HttpMode httpMode, MimeType contentType, String requestBody, String validResponseCodes, boolean consoleLogResponseBody) ResponseContentSupplier httpRequest(String url, HttpMode httpMode, MimeType contentType, String requestBody, String validResponseCodes, boolean consoleLogResponseBody)

View File

@@ -7,6 +7,9 @@ import jenkins.plugins.http_request.ResponseContentSupplier
import org.jenkinsci.plugins.pipeline.utility.steps.fs.FileWrapper import org.jenkinsci.plugins.pipeline.utility.steps.fs.FileWrapper
import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
import ru.pulsar.jenkins.library.steps.Coverable
import ru.yandex.qatools.allure.jenkins.config.ResultsConfig import ru.yandex.qatools.allure.jenkins.config.ResultsConfig
class StepExecutor implements IStepExecutor { class StepExecutor implements IStepExecutor {
@@ -149,6 +152,11 @@ class StepExecutor implements IStepExecutor {
} }
} }
@Override
def withCoverage(JobConfiguration config, Coverable stage, StepCoverageOptions options, Closure body) {
steps.withCoverage(config, stage, options, body)
}
@Override @Override
def lock(String resource, Closure body) { def lock(String resource, Closure body) {
steps.lock(resource: resource) { steps.lock(resource: resource) {

View File

@@ -3,8 +3,6 @@ package ru.pulsar.jenkins.library.steps
import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.CoverageUtils
import ru.pulsar.jenkins.library.utils.FileUtils
import ru.pulsar.jenkins.library.utils.Logger import ru.pulsar.jenkins.library.utils.Logger
import ru.pulsar.jenkins.library.utils.VRunner import ru.pulsar.jenkins.library.utils.VRunner
@@ -32,9 +30,6 @@ class Bdd implements Serializable, Coverable {
} }
def options = config.bddOptions def options = config.bddOptions
def env = steps.env()
def srcDir = config.srcDir
def workspaceDir = FileUtils.getFilePath("$env.WORKSPACE")
List<String> logosConfig = ["LOGOS_CONFIG=$config.logosConfig"] List<String> logosConfig = ["LOGOS_CONFIG=$config.logosConfig"]
steps.withEnv(logosConfig) { steps.withEnv(logosConfig) {
@@ -42,13 +37,7 @@ class Bdd implements Serializable, Coverable {
steps.createDir('build/out') steps.createDir('build/out')
List<Integer> returnStatuses = [] List<Integer> returnStatuses = []
def coverageOpts = config.coverageOptions steps.withCoverage(config, this, options) {
def coverageContext = CoverageUtils.prepareContext(config, options)
steps.lock(coverageContext.lockableResource) {
if (options.coverage) {
CoverageContext.startCoverage(steps, coverageOpts, coverageContext, workspaceDir, srcDir, this)
}
config.bddOptions.vrunnerSteps.each { config.bddOptions.vrunnerSteps.each {
Logger.println("Шаг запуска сценариев командой ${it}") Logger.println("Шаг запуска сценариев командой ${it}")
@@ -64,18 +53,11 @@ class Bdd implements Serializable, Coverable {
} else { } else {
Logger.println("Тестирование сценариев завершилось успешно") Logger.println("Тестирование сценариев завершилось успешно")
} }
if (options.coverage) {
CoverageUtils.stopCoverage(steps, coverageOpts, coverageContext)
}
} }
} }
steps.stash(ALLURE_STASH, 'build/out/allure/**', true) steps.stash(ALLURE_STASH, 'build/out/allure/**', true)
steps.stash('bdd-cucumber', 'build/out/cucumber/**', true) steps.stash('bdd-cucumber', 'build/out/cucumber/**', true)
if (options.coverage) {
steps.stash(COVERAGE_STASH_NAME, COVERAGE_STASH_PATH, true)
}
} }
@@ -83,6 +65,11 @@ class Bdd implements Serializable, Coverable {
return COVERAGE_STASH_PATH return COVERAGE_STASH_PATH
} }
@Override
String getCoverageStashName() {
return COVERAGE_STASH_NAME
}
String getCoveragePidsPath() { String getCoveragePidsPath() {
return COVERAGE_PIDS_PATH return COVERAGE_PIDS_PATH
} }

View File

@@ -3,6 +3,7 @@ package ru.pulsar.jenkins.library.steps
interface Coverable { interface Coverable {
String getCoverageStashPath(); String getCoverageStashPath();
String getCoverageStashName();
String getCoveragePidsPath(); String getCoveragePidsPath();
} }

View File

@@ -5,14 +5,16 @@ import ru.pulsar.jenkins.library.configuration.GlobalCoverageOptions
class CoverageContext { class CoverageContext {
String lockableResource String lockableResource
String srcDir
GlobalCoverageOptions coverageOptions GlobalCoverageOptions coverageOptions
int port int port
ArrayList<String> pids ArrayList<String> pids
ArrayList<String> dbgsPids ArrayList<String> dbgsPids
ArrayList<String> coverage41CPids ArrayList<String> coverage41CPids
CoverageContext(String lockableResource, GlobalCoverageOptions coverageOptions, int port, ArrayList<String> dbgsPids, ArrayList<String> coverage41CPids) { CoverageContext(String lockableResource, String srcDir, GlobalCoverageOptions coverageOptions, int port, ArrayList<String> dbgsPids, ArrayList<String> coverage41CPids) {
this.lockableResource = lockableResource this.lockableResource = lockableResource
this.srcDir = srcDir
this.coverageOptions = coverageOptions this.coverageOptions = coverageOptions
this.port = port this.port = port
this.pids = dbgsPids + coverage41CPids this.pids = dbgsPids + coverage41CPids

View File

@@ -4,7 +4,6 @@ import hudson.FilePath
import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.CoverageUtils
import ru.pulsar.jenkins.library.utils.FileUtils import ru.pulsar.jenkins.library.utils.FileUtils
import ru.pulsar.jenkins.library.utils.Logger import ru.pulsar.jenkins.library.utils.Logger
import ru.pulsar.jenkins.library.utils.StringJoiner import ru.pulsar.jenkins.library.utils.StringJoiner
@@ -41,9 +40,6 @@ class SmokeTest implements Serializable, Coverable {
def options = config.smokeTestOptions def options = config.smokeTestOptions
def env = steps.env() def env = steps.env()
def srcDir = config.srcDir
def workspaceDir = FileUtils.getFilePath("$env.WORKSPACE")
String vrunnerPath = VRunner.getVRunnerPath() String vrunnerPath = VRunner.getVRunnerPath()
String command = "$vrunnerPath xunit --ibconnection \"/F./build/ib\"" String command = "$vrunnerPath xunit --ibconnection \"/F./build/ib\""
@@ -107,21 +103,10 @@ class SmokeTest implements Serializable, Coverable {
command += " $testsPath" command += " $testsPath"
} }
def coverageOpts = config.coverageOptions
def coverageContext = CoverageUtils.prepareContext(config, options)
steps.lock(coverageContext.lockableResource) {
if (options.coverage) {
CoverageContext.startCoverage(steps, coverageOpts, coverageContext, workspaceDir, srcDir, this)
}
steps.withEnv(logosConfig) { steps.withEnv(logosConfig) {
VRunner.exec(command, true)
}
if (options.coverage) { steps.withCoverage(config, this, options) {
CoverageUtils.stopCoverage(steps, coverageOpts, coverageContext) VRunner.exec(command, true)
}
} }
if (options.publishToAllureReport) { if (options.publishToAllureReport) {
@@ -133,17 +118,17 @@ class SmokeTest implements Serializable, Coverable {
steps.junit("$junitReportDir/*.xml", true) steps.junit("$junitReportDir/*.xml", true)
steps.archiveArtifacts("$junitReportDir/**") steps.archiveArtifacts("$junitReportDir/**")
} }
if (options.coverage) {
steps.stash(COVERAGE_STASH_NAME, COVERAGE_STASH_PATH, true)
} }
} }
String getCoverageStashPath() { String getCoverageStashPath() {
return COVERAGE_STASH_PATH return COVERAGE_STASH_PATH
} }
String getCoverageStashName() {
return COVERAGE_STASH_NAME
}
String getCoveragePidsPath() { String getCoveragePidsPath() {
return COVERAGE_PIDS_PATH return COVERAGE_PIDS_PATH
} }

View File

@@ -0,0 +1,47 @@
package ru.pulsar.jenkins.library.steps
import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.CoverageUtils
class WithCoverage implements Serializable {
private final JobConfiguration config
private final Coverable stage
private final StepCoverageOptions coverageOptions
private final Closure body
WithCoverage(JobConfiguration config, Coverable stage, StepCoverageOptions coverageOptions, Closure body) {
this.config = config
this.stage = stage
this.coverageOptions = coverageOptions
this.body = body
}
def run() {
def context = CoverageUtils.prepareContext(config, coverageOptions)
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
//noinspection GroovyMissingReturnStatement
steps.lock(context.lockableResource) {
if (coverageOptions.coverage) {
CoverageUtils.startCoverage(steps, config, context, stage)
}
body()
if (coverageOptions.coverage) {
CoverageUtils.stopCoverage(steps, config, context)
}
}
if (coverageOptions.coverage) {
steps.stash(stage.getCoverageStashName(), stage.getCoverageStashPath(), true)
}
}
}

View File

@@ -4,7 +4,6 @@ import hudson.FilePath
import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.CoverageUtils
import ru.pulsar.jenkins.library.utils.FileUtils import ru.pulsar.jenkins.library.utils.FileUtils
import ru.pulsar.jenkins.library.utils.Logger import ru.pulsar.jenkins.library.utils.Logger
import ru.pulsar.jenkins.library.utils.VRunner import ru.pulsar.jenkins.library.utils.VRunner
@@ -42,9 +41,6 @@ class Yaxunit implements Serializable, Coverable {
def options = config.yaxunitOptions def options = config.yaxunitOptions
def env = steps.env() def env = steps.env()
def srcDir = config.srcDir
def workspaceDir = FileUtils.getFilePath("$env.WORKSPACE")
String vrunnerPath = VRunner.getVRunnerPath() String vrunnerPath = VRunner.getVRunnerPath()
String ibConnection = ' --ibconnection "/F./build/ib"' String ibConnection = ' --ibconnection "/F./build/ib"'
@@ -69,22 +65,10 @@ class Yaxunit implements Serializable, Coverable {
} }
def coverageOpts = config.coverageOptions
def coverageContext = CoverageUtils.prepareContext(config, options)
steps.lock(coverageContext.lockableResource) {
if (options.coverage) {
CoverageContext.startCoverage(steps, coverageOpts, coverageContext, workspaceDir, srcDir, this)
}
// Выполняем команды
steps.withEnv(logosConfig) { steps.withEnv(logosConfig) {
VRunner.exec(runTestsCommand, true)
}
if (options.coverage) { steps.withCoverage(config, this, options) {
CoverageUtils.stopCoverage(steps, coverageOpts, coverageContext) VRunner.exec(runTestsCommand, true)
}
} }
// Сохраняем результаты // Сохраняем результаты
@@ -109,8 +93,6 @@ class Yaxunit implements Serializable, Coverable {
steps.archiveArtifacts("build/out/yaxunit/junit.xml") steps.archiveArtifacts("build/out/yaxunit/junit.xml")
if (options.coverage) {
steps.stash(COVERAGE_STASH_NAME, COVERAGE_STASH_PATH, true)
} }
} }
@@ -118,9 +100,12 @@ class Yaxunit implements Serializable, Coverable {
return COVERAGE_STASH_PATH return COVERAGE_STASH_PATH
} }
@Override
String getCoverageStashName() {
return COVERAGE_STASH_NAME
}
String getCoveragePidsPath() { String getCoveragePidsPath() {
return COVERAGE_PIDS_PATH return COVERAGE_PIDS_PATH
} }
} }

View File

@@ -1,9 +1,7 @@
package ru.pulsar.jenkins.library.utils package ru.pulsar.jenkins.library.utils
import hudson.FilePath
import org.apache.commons.lang3.RandomStringUtils import org.apache.commons.lang3.RandomStringUtils
import ru.pulsar.jenkins.library.IStepExecutor import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.GlobalCoverageOptions
import ru.pulsar.jenkins.library.configuration.JobConfiguration import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
import ru.pulsar.jenkins.library.ioc.ContextRegistry import ru.pulsar.jenkins.library.ioc.ContextRegistry
@@ -39,13 +37,20 @@ class CoverageUtils {
lockableResource = "${env.NODE_NAME}_$port" lockableResource = "${env.NODE_NAME}_$port"
} }
return new CoverageContext(lockableResource, coverageOpts, port, currentDbgsPids, currentCoverage41CPids) return new CoverageContext(lockableResource, config.srcDir, coverageOpts, port, currentDbgsPids, currentCoverage41CPids)
} }
static void startCoverage(IStepExecutor steps, GlobalCoverageOptions coverageOpts, CoverageContext coverageContext, FilePath workspaceDir, String srcDir, Coverable coverable) { static void startCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext, Coverable stage) {
def env = steps.env()
def srcDir = config.srcDir
def workspaceDir = FileUtils.getFilePath("$env.WORKSPACE")
def coverageOpts = config.coverageOptions
steps.start("${coverageOpts.dbgsPath} --addr=127.0.0.1 --port=$coverageContext.port") steps.start("${coverageOpts.dbgsPath} --addr=127.0.0.1 --port=$coverageContext.port")
steps.start("${coverageOpts.coverage41CPath} start -i DefAlias -u http://127.0.0.1:$coverageContext.port -P $workspaceDir -s $srcDir -o ${coverable.getCoverageStashPath()}") steps.start("${coverageOpts.coverage41CPath} start -i DefAlias -u http://127.0.0.1:$coverageContext.port -P $workspaceDir -s $srcDir -o ${stage.getCoverageStashPath()}")
steps.cmd("${coverageOpts.coverage41CPath} check -i DefAlias -u http://127.0.0.1:$coverageContext.port") steps.cmd("${coverageOpts.coverage41CPath} check -i DefAlias -u http://127.0.0.1:$coverageContext.port")
def newDbgsPids = getPIDs("dbgs") def newDbgsPids = getPIDs("dbgs")
@@ -57,12 +62,15 @@ class CoverageUtils {
newDbgsPids.addAll(newCoverage41CPids) newDbgsPids.addAll(newCoverage41CPids)
def pids = newDbgsPids.join(" ") def pids = newDbgsPids.join(" ")
steps.writeFile(coverable.getCoveragePidsPath(), pids, 'UTF-8') steps.writeFile(stage.getCoveragePidsPath(), pids, 'UTF-8')
Logger.println("Coverage PIDs for cleanup: $pids") Logger.println("Coverage PIDs for cleanup: $pids")
} }
static void stopCoverage(IStepExecutor steps, GlobalCoverageOptions coverageOpts, CoverageContext coverageContext) { static void stopCoverage(IStepExecutor steps, JobConfiguration config, CoverageContext coverageContext) {
def coverageOpts = config.coverageOptions
steps.cmd("${coverageOpts.coverage41CPath} stop -i DefAlias -u http://127.0.0.1:$coverageContext.port") steps.cmd("${coverageOpts.coverage41CPath} stop -i DefAlias -u http://127.0.0.1:$coverageContext.port")
} }

12
vars/withCoverage.groovy Normal file
View File

@@ -0,0 +1,12 @@
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.configuration.StepCoverageOptions
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.steps.Coverable
import ru.pulsar.jenkins.library.steps.WithCoverage
def call(JobConfiguration config, Coverable stage, StepCoverageOptions options, Closure body) {
ContextRegistry.registerDefaultContext(this)
WithCoverage withCoverage = new WithCoverage(config, stage, options, body)
return withCoverage.run()
}