AOEpeople / gradle-jenkins-job-dsl-plugin

Plugin for easy management of Jenkins Job DSL scripts with Gradle
MIT License
25 stars 12 forks source link

Extending JobScriptsSpec? #5

Open TobiX opened 6 years ago

TobiX commented 6 years ago

I would like to switch from our internal fork of the job-dsl-gradle-example to this plugin, but we have some extensions to the JobScriptsSpec class, which make our project incompatible with the plugin.

Things we are currently doing different:

Any idea how to easily port those changes to the plugin? Maybe split JobScriptsSpec into a base class and a default implementation, which I can then replace in my own project?

carstenlenz commented 6 years ago

Sorry for answering that late. I will have a look at the code to see if I can come up with an Idea. Please so not expect this to be before new year ;)

carstenlenz commented 6 years ago

I just took a look at the current setup. Imho what would be needed:

Regarding the environment vars: That should work by injecting these into the jobDslTest task. These are then available while running the test.

How do you mock the HTTP class? What support is needed other than having the ability to provide your own test class?

TobiX commented 6 years ago

We are using ersatz to mock HTTP

    @AutoCleanup('stop')
    protected final ersatz = HttpMockAnswers.server()

(HttpMockAnswers is an extra class that collects all mocked answers)

The test method then looks like this:

    @Unroll
    void 'test script #file.name'(File file) {
        given: "a mock http client"
        GitLabUtil.mockGitLabUrl(ersatz.httpUrl)

        and: "a test jenkins context"
        JobManagement jm = new JenkinsJobManagement(System.out, [GITLAB_TOKEN: GITLAB_TOKEN, SOURCE_BRANCH: 'master'], new File('.'))

        when: "the JobDSL script is run"
        GeneratedItems items = new DslScriptLoader(jm).runScript(file.text)
        writeItems items

        then: "everything finishes successfully"
        noExceptionThrown()

        and: "there are corresponding jobs for master and a mocked branch"
        items.jobs.findAll { it.jobName.endsWith('/master') }.size() ==
                items.jobs.findAll { it.jobName.endsWith('mockedBranch') }.size()

        and: "old branches are suppressed"
        items.jobs.findAll { it.jobName.endsWith('veryOldBranch') }.isEmpty()

        and: "our mock server is happy"
        ersatz.verify()

        cleanup:
        GitLabUtil.mockGitLabUrl(null)

        where:
        file << jobFiles
    }

This is obviously just from JobScriptsSpec with some additional tests thrown in... Having a base class I can inherit (or maybe a helper class wrapping JenkinsJobManagement and DslScriptLoader) would be quite an improvement over my copy&paste-Job ;)

carstenlenz commented 6 years ago

I made quite some changes to the code. I have to add documentation on how to use it but would you agree on testing a yet-to-be-released release candidate?