Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to provide configuration file to JobDSL test? #103

Open
dinbtechit opened this issue Nov 28, 2018 · 8 comments
Open

How to provide configuration file to JobDSL test? #103

dinbtechit opened this issue Nov 28, 2018 · 8 comments

Comments

@dinbtechit
Copy link

dinbtechit commented Nov 28, 2018

I have Jenkins jobs that uses the Config File Provider plugin to get settings project name, etc. Unfortunately, the gradle unit test fails as it is not able to find the file or property.

Caused by: javaposse.jobdsl.dsl.DslScriptException: (script, line 1) No such property: CONFIG_FILE for class: script.

However, I'm able to do it on the GUI (see screenshots below). But like see my Unit tests work as well.

  1. Build Environments: Specify a config file

image

  1. Build : DSL Script - Is able to read the file from the workspace.

image

Any help is massively appreciated.

@sheehan
Copy link
Owner

sheehan commented Nov 28, 2018

@dinbtechit
Copy link
Author

Hi @sheehan, Thanks for the quick turn around. Okay I'll try it out and let you know.

On a side note - Ran into a similar issue with Credentials. Unit tests failing due to missing credentials. I want to pre-load a bunch of credentials for unit testing as seed jobs expects some credentials to be in place.

Would you be able to provide some guidance on how to create credentials (ID, Username and password) for JobsDSL Unit Tests?

Some sample code would be very helpful! :)

@dinbtechit
Copy link
Author

Hi @dinbtechit. You should be able to pass env vars in here for testing:
https://github.com/sheehan/job-dsl-gradle-example/blob/master/src/test/groovy/com/dslexample/JobScriptsSpec.groovy#L40

Hi @sheehan , I haven't had any luck :( Are you able to provide me an example that is more specific to my example?

Thanks really appreciate your help!

@sheehan
Copy link
Owner

sheehan commented Dec 14, 2018

Can you post your test code? Are you using JobScriptsSpec?

@dinbtechit
Copy link
Author

dinbtechit commented Dec 14, 2018

Yes, I'm using JobScriptsSpec.

Here is the my code.


  @Unroll
    void 'MySettings Config file #file.name'(File file) {
        given:
        JobManagement jm = new JenkinsJobManagement(System.out, [:], new File('.'))
        ConfigFileContext cfgProvider = jenkinsRule.getInstance().getExtensionList(MavenSettingsConfig.MavenSettingsConfigProvider.class).get(MavenSettingsConfig.MavenSettingsConfigProvider.class)
        
        Config siteCfg = cfgProvider.newConfig("test_settings.xml","MySettings", null, "<settings><name>mytest</name></settings>") /* Read a XML file from tests instead of passing a XML String */ 

        GlobalConfigFiles.get().save(siteCfg)
        
        //Need to set the path of this config file in Environment Variable - "CONFIG".

        when:
        GeneratedItems items = new DslScriptLoader(jm).runScript(file.text)
        writeItems(items, outputDir)

        then:
        noExceptionThrown()

        where:
        file << TestUtil.getJobFiles()
    }

@sheehan sheehan pinned this issue Dec 15, 2018
@sheehan sheehan unpinned this issue Dec 15, 2018
@sheehan
Copy link
Owner

sheehan commented Dec 15, 2018

I'm not 100% clear on your workflow, but does this work?

File settings = new File('test_settings.xml')
settings.text = '<settings><name>mytest</name></settings>'
JobManagement jm = new JenkinsJobManagement(System.out, [CONFIG_FILE: settings.name], new File('.'))

@dinbtechit
Copy link
Author

dinbtechit commented Dec 15, 2018

Let me try explaining this differently:

I have a DSL job in my jobs directory. Its reading a xml file from the workspace. The path of the file is passed in as a environment variable - ${CONFIG_FILE}.

jobs/MyDSLJob.groovy

def xml = new XmlSlurper().parseText(readFileFromWorkspace("${CONFIG_FILE}")) 

 pipelineJob("Project_${xml.name}") {  //I'm getting the name from the XML.
 . . .
}

Now, running the Gradle -> Test -> Fails (due to this following error)

Caused by: javaposse.jobdsl.dsl.DslScriptException: (script, line 1) No such property: CONFIG_FILE for class: script.

So I need to somehow create the xml file in workspace using JobScriptsSpec for the Unit Test to Pass?

Updated the JobScriptsSpec file to create the xml. (As shown in your above example)

test/groovy/com.dslexample/JobScriptsSpec

   @Unroll
    void 'Create My Config file in workspace #file.name'(File file) {
        given:
        File settings = new File('test_settings.xml')
        settings.text = '<settings><name>mytest</name></settings>'
        JobManagement jm = new JenkinsJobManagement(System.out, [CONFIG_FILE: settings.name], new File('.'))
        
         when:
         GeneratedItems items = new DslScriptLoader(jm).runScript(file.text)
         writeItems(items, outputDir)
 
         then:
         noExceptionThrown()
 
         where:
         file << TestUtil.getJobFiles()
    }

But when I ran the Gradle ->Verification -> test (getting the same error). It looks like it is not able to find the environment variable ${CONFIG_FILE}

Caused by: javaposse.jobdsl.dsl.DslScriptException: (script, line 1) No such property: CONFIG_FILE for class: script

Does this makes sense? :)

@Lysander086
Copy link

Hi @dinbtechit. You should be able to pass env vars in here for testing: https://github.com/sheehan/job-dsl-gradle-example/blob/master/src/test/groovy/com/dslexample/JobScriptsSpec.groovy#L40

This truly works when I want to provide string values for the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants