1
0

Переезд на градль-рельсы с тестами и объектами

This commit is contained in:
Nikita Gryzlov
2020-03-26 17:04:42 +03:00
parent 39acf5efb0
commit fa208d78ef
14 changed files with 334 additions and 15 deletions

View File

@@ -0,0 +1,25 @@
import com.mkobit.jenkins.pipelines.codegen.LocalLibraryRetriever
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration
import org.jenkinsci.plugins.workflow.libs.LibraryRetriever
import org.jvnet.hudson.test.JenkinsRule
final class RuleBootstrapper {
private RuleBootstrapper() {
}
/**
* This demonstrates how you can can configure the {@link JenkinsRule} to use the local source code
* as a {@link LibraryConfiguration}. In this example we are making it implicitly loaded.
*/
static void setup(JenkinsRule rule) {
rule.timeout = 30
final LibraryRetriever retriever = new LocalLibraryRetriever()
final LibraryConfiguration localLibrary =
new LibraryConfiguration('testLibrary', retriever)
localLibrary.implicit = true
localLibrary.defaultVersion = 'unused'
localLibrary.allowVersionOverride = false
GlobalLibraries.get().libraries = [localLibrary]
}
}

View File

@@ -0,0 +1,39 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.job.WorkflowRun
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.jvnet.hudson.test.JenkinsRule
class cmdTest {
@Rule
public JenkinsRule rule = new JenkinsRule()
@Before
void configureGlobalGitLibraries() {
RuleBootstrapper.setup(rule)
}
@Test
void "cmd should echo something"() {
def pipeline = '''
pipeline {
agent any
stages {
stage('test') {
steps {
cmd("echo helloWorld")
}
}
}
}
'''.stripIndent()
final CpsFlowDefinition flow = new CpsFlowDefinition(pipeline, true)
final WorkflowJob workflowJob = rule.createProject(WorkflowJob, 'project')
workflowJob.definition = flow
rule.assertLogContains('helloWorld', rule.buildAndAssertSuccess(workflowJob))
}
}

View File

@@ -0,0 +1,91 @@
package ru.pulsar.jenkins.library.steps;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import ru.pulsar.jenkins.library.IStepExecutor;
import ru.pulsar.jenkins.library.ioc.ContextRegistry;
import ru.pulsar.jenkins.library.ioc.IContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class CmdTest {
private IStepExecutor steps;
@BeforeEach
void setUp() {
IContext context = mock(IContext.class);
steps = mock(IStepExecutor.class);
when(context.getStepExecutor()).thenReturn(steps);
ContextRegistry.registerContext(context);
}
@Test
void runOk() {
// given
final String script = "echo hello";
Cmd cmd = new Cmd(script);
// when
int run = cmd.run();
// then
verify(steps).isUnix();
verify(steps).bat(contains(script), eq(false), anyString());
assertThat(run).isEqualTo(0);
}
@Test
void runFailNoReturn() {
// given
final String script = "false";
Cmd cmd = new Cmd(script);
when(steps.bat(anyString(), anyBoolean(), anyString())).thenReturn(1);
when(steps.sh(anyString(), anyBoolean(), anyString())).thenReturn(1);
// when
Throwable thrown = catchThrowable(cmd::run);
assertThat(thrown).hasMessageContaining("<1>");
// then
verify(steps).isUnix();
assertThat(steps).satisfiesAnyOf(
steps -> verify(steps).bat(contains(script), eq(false), anyString()),
steps -> verify(steps).sh(contains(script), eq(false), anyString())
);
}
@Test
void runPassAndReturn() {
// given
final String script = "false";
Cmd cmd = new Cmd(script, true);
when(steps.bat(anyString(), anyBoolean(), anyString())).thenReturn(1);
when(steps.sh(anyString(), anyBoolean(), anyString())).thenReturn(1);
// when
int run = cmd.run();
// then
verify(steps).isUnix();
assertThat(steps).satisfiesAnyOf(
steps -> verify(steps).bat(contains(script), eq(true), anyString()),
steps -> verify(steps).sh(contains(script), eq(true), anyString())
);
assertThat(run).isEqualTo(1);
}
}