Skip to content

Instantly share code, notes, and snippets.

@gsaslis
Last active May 8, 2019 13:23
Show Gist options
  • Save gsaslis/6226f7c1eef0f20d476a7da874ce0d7d to your computer and use it in GitHub Desktop.
Save gsaslis/6226f7c1eef0f20d476a7da874ce0d7d to your computer and use it in GitHub Desktop.
Invalid mocked method
import com.redhat.threescale.productization.DistGitUtils
import com.redhat.threescale.productization.ValidatingUtils
/**
*/
def call(Map <String, ?> config = [:]) {
validateConfig(config)
//echo config.get('token')
echo config.get('repository')
echo config.get('branch')
echo config.get('source')
echo config.get('create_repo')
echo config.get('stage')
// Work around Jenkins treatment of empty environment variables (not passed down)
if (config.get('source', "").equals("empty")) {
config.put('source', "")
}
def distGit = new DistGitUtils()
// Now we can start a job to do the work
def jobid = distGit.startJob(
config.get('token'),
config.get('repository'),
config.get('branch'),
config.get('source'),
config.get('create_repo'),
config.get('stage')
)
// Now wait till the job finishes
distGit.waitForJob(
config.get('token'),
jobid
)
}
private static void validateConfig(Map<String, ?> config) {
def validatingUtils = new ValidatingUtils()
validatingUtils.ensureNotEmpty(config, 'token')
validatingUtils.ensureNotEmpty(config, 'repository')
validatingUtils.ensureNotEmpty(config, 'branch')
validatingUtils.ensureYesNo(config, 'stage')
validatingUtils.ensureYesNo(config, 'create_repo')
}
package com.redhat.threescale.productization
import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
class DistGitCreateSpec extends JenkinsPipelineSpecification {
def distGit = null
Map <String, ?> config = null
def mockValidatingUtils = null
def mockDistGitUtils = null
def setup() {
distGit = loadPipelineScriptForTest("vars/distgitCreate.groovy")
distGit.getBinding().setVariable("env", [:])
config = [
repository: "containers/3scale-amp-backend",
token: "some_token",
branch: "3scale-amp-2.5-rhel-7",
source: "3scale-amp-2.4-rhel-7",
stage: "yes",
create_repo: "yes",
]
mockValidatingUtils = GroovyMock(ValidatingUtils.class, global: true)
new ValidatingUtils() >> mockValidatingUtils
mockDistGitUtils = GroovyMock(DistGitUtils.class, global: true)
new DistGitUtils() >> mockDistGitUtils
explicitlyMockPipelineVariable("out")
}
def "[validateConfig]: will check that branch is not empty"() {
setup:
config.branch = null
when:
distGit(config)
then:
1 * mockValidatingUtils.ensureNotEmpty(config, 'branch')
}
def "[validateConfig]: will check that token is not empty"() {
setup:
config.token = null
when:
distGit(config)
then:
1 * mockValidatingUtils.ensureNotEmpty(config, 'token')
}
def "[validateConfig]: will check that repository is not empty"() {
setup:
config.repository = null
when:
distGit(config)
then:
1 * mockValidatingUtils.ensureNotEmpty(config, 'repository')
}
def "will start and wait for ansible tower job"() {
setup:
mockDistGitUtils = GroovyMock(DistGitUtils.class, global: true)
new DistGitUtils() >> mockDistGitUtils
when:
distGit(config)
then:
1 * mockDistGitUtils.startJob(_, _, _, _, _, _)
1 * mockDistGitUtils.waitForJob(_, _)
}
}

Here is the command I ran:

mvn clean test -Dtest=DistGitCreateSpec

And here is the output

[ERROR] Tests run: 4, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 4.732 s <<< FAILURE! - in com.redhat.threescale.productization.DistGitCreateSpec
[ERROR] will start and wait for ansible tower job(com.redhat.threescale.productization.DistGitCreateSpec)  Time elapsed: 0.06 s  <<< FAILURE!
org.spockframework.mock.TooFewInvocationsError:
Too few invocations for:

1 * mockDistGitUtils.startJob(_, _, _, _, _, _)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * mockValidatingUtils.startJob('some_token', 'containers/3scale-amp-backend', '3scale-amp-2.5-rhel-7', '3scale-amp-2.4-rhel-7', 'yes', 'yes')
1 * mockValidatingUtils.waitForJob('some_token', null)
5 * getPipelineMock("echo").toString()
1 * getPipelineMock("echo").call('3scale-amp-2.5-rhel-7')
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'stage')
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'create_repo')
1 * getPipelineMock("echo").call('containers/3scale-amp-backend')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'repository')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'branch')
2 * getPipelineMock("echo").call('yes')
1 * getPipelineMock("echo").call('3scale-amp-2.4-rhel-7')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'token')

Too few invocations for:

1 * mockDistGitUtils.waitForJob(_, _)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * mockValidatingUtils.waitForJob('some_token', null)
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'stage')
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'create_repo')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'repository')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'branch')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'token')
5 * getPipelineMock("echo").toString()
1 * getPipelineMock("echo").call('3scale-amp-2.5-rhel-7')
1 * getPipelineMock("echo").call('containers/3scale-amp-backend')
2 * getPipelineMock("echo").call('yes')
1 * getPipelineMock("echo").call('3scale-amp-2.4-rhel-7')
1 * mockValidatingUtils.startJob('some_token', 'containers/3scale-amp-backend', '3scale-amp-2.5-rhel-7', '3scale-amp-2.4-rhel-7', 'yes', 'yes')


	at com.redhat.threescale.productization.DistGitCreateSpec.will start and wait for ansible tower job(DistGitCreateSpec.groovy:62)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR]   DistGitCreateSpec.will start and wait for ansible tower job:62 Too few invocations for:

1 * mockDistGitUtils.startJob(_, _, _, _, _, _)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * mockValidatingUtils.startJob('some_token', 'containers/3scale-amp-backend', '3scale-amp-2.5-rhel-7', '3scale-amp-2.4-rhel-7', 'yes', 'yes')
1 * mockValidatingUtils.waitForJob('some_token', null)
5 * getPipelineMock("echo").toString()
1 * getPipelineMock("echo").call('3scale-amp-2.5-rhel-7')
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'stage')
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'create_repo')
1 * getPipelineMock("echo").call('containers/3scale-amp-backend')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'repository')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'branch')
2 * getPipelineMock("echo").call('yes')
1 * getPipelineMock("echo").call('3scale-amp-2.4-rhel-7')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'token')

Too few invocations for:

1 * mockDistGitUtils.waitForJob(_, _)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * mockValidatingUtils.waitForJob('some_token', null)
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'stage')
1 * mockValidatingUtils.ensureYesNo(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'create_repo')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'repository')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'branch')
1 * mockValidatingUtils.ensureNotEmpty(['repository':'containers/3scale-amp-backend', 'token':'some_token', 'branch':'3scale-amp-2.5-rhel-7', 'source':'3scale-amp-2.4-rhel-7', 'stage':'yes', 'create_repo':'yes'], 'token')
5 * getPipelineMock("echo").toString()
1 * getPipelineMock("echo").call('3scale-amp-2.5-rhel-7')
1 * getPipelineMock("echo").call('containers/3scale-amp-backend')
2 * getPipelineMock("echo").call('yes')
1 * getPipelineMock("echo").call('3scale-amp-2.4-rhel-7')
1 * mockValidatingUtils.startJob('some_token', 'containers/3scale-amp-backend', '3scale-amp-2.5-rhel-7', '3scale-amp-2.4-rhel-7', 'yes', 'yes')


[INFO]
[ERROR] Tests run: 4, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.080 s
[INFO] Finished at: 2019-05-08T12:21:41+03:00
[INFO] Final Memory: 49M/647M
[INFO] ------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.redhat</groupId>
<artifactId>3scale-productization-library</artifactId>
<packaging>jar</packaging>
<version>0.0.0-SNAPSHOT</version> <!-- unused -->
<description>Jenkins Shared Library for 3scale Continuous Productization pipelines</description>
<repositories>
<repository>
<id>jenkins-releases</id>
<name>Jenkins Releases</name>
<url>http://repo.jenkins-ci.org/releases</url>
</repository>
</repositories>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<groovy.core.version>2.4.11</groovy.core.version>
<groovy.spock.version>1.1-groovy-2.4</groovy.spock.version>
<groovy.gmaven.pluginVersion>1.6.1</groovy.gmaven.pluginVersion>
<jenkins.version>2.102</jenkins.version>
<jenkins.servlet.version>3.1.0</jenkins.servlet.version>
<jenkins.symbol.version>1.10</jenkins.symbol.version>
<jenkins.workflow.cps.version>2.36</jenkins.workflow.cps.version>
<jenkins.workflow.step.version>2.10</jenkins.workflow.step.version>
<jenkins-spock.version>2.0.0</jenkins-spock.version>
<junit.version>4.12</junit.version>
<logback.configration>logback-test.xml</logback.configration>
<logdir>${project.build.directory}/log</logdir>
<test.loglevel>ERROR</test.loglevel>
<log.logback.version>1.2.3</log.logback.version>
<log.slf4j.version>1.7.25</log.slf4j.version>
<surefire.pluginVersion>2.22.0</surefire.pluginVersion>
</properties>
<dependencyManagement>
<dependencies>
<!-- Transitive Dependencies that need Managing -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.homeaway.devtools.jenkins</groupId>
<artifactId>jenkins-spock</artifactId>
<version>${jenkins-spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${log.logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${log.logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${jenkins.servlet.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-core</artifactId>
<version>${jenkins.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${jenkins.workflow.step.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${jenkins.workflow.cps.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>symbol-annotation</artifactId>
<version>${jenkins.symbol.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.core.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${log.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${log.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${log.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${groovy.spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides the sh() pipeline step -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.21</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides the slackSend() pipeline step -->
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>slack</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides stage() step -->
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-stage-step</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides sshagent() step -->
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-agent</artifactId>
<version>1.16</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides libraryResource() step -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.10</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides ansiColor() step -->
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ansicolor</artifactId>
<version>0.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides echo() step -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.10</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- provides credentialsBinding() step -->
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials-binding</artifactId>
<version>1.16</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.pluginVersion}</version>
<executions>
<execution>
<id>default-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<forkCount>1</forkCount>
<testSourceDirectory>test/src</testSourceDirectory>
<includes>
<include>**/*Spec</include>
</includes>
<useManifestOnlyJar>false</useManifestOnlyJar>
<systemPropertyVariables>
<root.loglevel>${test.loglevel}</root.loglevel>
<root.appender>Stdout</root.appender>
<test.loglevel>${test.loglevel}</test.loglevel>
<logdir>${logdir}</logdir>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${groovy.gmaven.pluginVersion}</version>
<executions>
<execution>
<id>groovy</id>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>generateTestStubs</goal>
<goal>compile</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
<configuration>
<sources>
<source>
<directory>src</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
</sources>
<testSources>
<testSource>
<directory>test</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</testSource>
</testSources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
<testResources>
<testResource>
<includes>
<include>vars/**/*.groovy</include>
</includes>
<directory>${project.basedir}</directory>
</testResource>
<testResource>
<directory>test/resources</directory>
</testResource>
<testResource>
<directory>resources</directory>
</testResource>
</testResources>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment