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

Help/advice with testing JTE library using openshift-client plugin #102

Open
max-allan-surevine opened this issue Jun 4, 2021 · 1 comment

Comments

@max-allan-surevine
Copy link

I'm trying to test a JTE library that is using the openshift-client plugin. (I have the dependency in my build.gradle already.)
My (cut down, minimal) template looks like this :

package aa;
void call(app_env) {
  stage("A and A") {
		def build_name_selector = openshift.selector( "bc", "foo" )
		if (build_name_selector.count() > 0)
		{
			echo "foo exists!"
			build_name_selector.delete()
			echo "foo deleted"
		}
	}
}

And one of my tests like this :

package aa;
class aaTest extends JTEPipelineSpecification {
    def aa = null
    def setup() {
        aa = loadPipelineScriptForTest("aa/aa.groovy")
    }
    def "testit" () {
        setup:
            def app_env = [:]
            def mockSelector = Mock(com.openshift.jenkins.plugins.OpenShiftDSL)
            getPipelineMock("openshift.selector")( _ )  >> mockSelector
            explicitlyMockPipelineStep('count')
            getPipelineMock("count")() >> 1
            explicitlyMockPipelineStep('delete')
        when:
            aa(app_env)
        then:
            2 * getPipelineMock("echo")(_)
            1 * getPipelineMock("delete")()
    }
 }

I have another test that sets count to 0 and expects 0 echoes and deletes. They both work as expected.
It has taken a lot of trial and error to get to that. But it feels wrong that I have mocked the entire count() function's result to 1. There must be a "better" way! The script under test could create a different selector later and expect a different result and I have no way of setting it.
NB It is not using the builtin groovy count() & delete() functions, but versions from the openshift-client library.

IF my test is totally wrong, then please throw out whichever bits are wrong and suggest improvements!

@deblaci
Copy link

deblaci commented Jun 4, 2021

Hi,

I think it is ok to stub the OpenShiftDSL because it is external dependency and usually we don't like to test other class.
As I experienced the Mock is a little buggy, you can use only one instance in one test.
I don't know the JTEPipelineSpecification so I created an example with original JenkinsSpock.
I used GroovyMock now, but I usually use GroovySpy too.

package aa

import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
import com.openshift.jenkins.plugins.OpenShiftDSL


class aaTest extends JenkinsPipelineSpecification {
  def aa = null

  def setup() {
    aa = loadPipelineScriptForTest("vars/a.groovy")
  }

  def "testit"() {
    setup:
    def app_env = [:]
    OpenShiftDSL mockSelector = GroovyMock(OpenShiftDSL) {
      // Assertion comes here
      1 * count() >> selectorCountResult
      expectedDeleteCalls * delete()
    }
    getPipelineMock("openshift.selector")(_) >> mockSelector

    when:
    aa(app_env)

    then:
    expectedEchoCalls * getPipelineMock("echo")(_)

    where:
    selectorCountResult || expectedDeleteCalls | expectedEchoCalls
    1                   || 1                   | 2
    0                   || 0                   | 0
  }
}

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

No branches or pull requests

2 participants